创建一个Packaging模式为pom的Maven Project:youyijiu-starter。pom.xml内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>youyijiu</groupId>
<artifactId>youyijiu-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>youyijiu-starter-common</module>
<module>youyijiu-starter-dubbo</module>
</modules>
<!-- 基于spring boot 2.6.4 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
</parent>
<!-- 配置私有仓库发布地址 -->
<distributionManagement>
<repository>
<id>releases</id>
<url>http://192.168.0.123:7676/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.0.123:7676/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<!-- 各种项目通用属性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
<fastjson.version>1.2.3</fastjson.version>
<dubbo.version>2.5.3</dubbo.version>
</properties>
<!-- Package as an executable jar -->
<build>
<plugins>
<!-- Compiler 插件, 设定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!--配置生成源码包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
创建一个Packaging模式为jar的Maven Module:youyijiu-starter-dubbo,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>youyijiu</groupId>
<artifactId>youyijiu-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>youyijiu-starter-dubbo</groupId>
<artifactId>youyijiu-starter-dubbo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>youyijiu-starter-common</groupId>
<artifactId>youyijiu-starter-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
<dependency>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
</project>
在youyijiu-starter-dubbo模块中创建自动配置类DubboAutoConfiguration,内容如下:
/**
* @Title: DubboAutoConfiguration.java
* @Package com.youyijiu.starter.dubbo
* @author Jun.Yang 24696026@qq.com
* @date 2022年8月13日 上午8:18:00
*/
package com.youyijiu.starter.dubbo.autoconfigure;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.ImportResource;
/**
* @ClassName: DubboAutoConfiguration
* @Description: 加载Dubbo元数据配置,当属性配置中有 dubbo.zookeeper.address 和 dubbo.name 才会加载,
* 当依赖该starter的项目配置有dubbo.zookeeper.address 和 dubbo.name 时,会扫描classpath
* 下的dubbo-interfaces.xml文件加载dubbo的配置。
* @author Jun.Yang 24696026@qq.com
* @date 2022年8月13日 上午8:18:00
*
*/
@ConditionalOnProperty(name = {"dubbo.zookeeper.address","dubbo.name"})
@ImportResource({ "classpath:dubbo-interfaces.xml" })
public class DubboAutoConfiguration {
}
在youyijiu-starter-dubbo模块中的src/main/resources下创建META-INF目录,里面的spring.factories对于一个starter来说有着关键的作用,这里spring.factories的内容为:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.youyijiu.starter.dubbo.autoconfigure.DubboAutoConfiguration
这样我们的dubbo就可以集成到springboot中了。使用的时候只需要引入如下依赖:
<dependency>
<groupId>youyijiu-starter-dubbo</groupId>
<artifactId>youyijiu-starter-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
接下来我们创建一个提供dubbo服务的模块来说明下,怎么使用这个youyijiu-starter-dubbo。创建一个Maven Module:youyijiu-test-dubbo-server,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.youyijiu</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>youyijiu-test-dubbo-server</artifactId>
<!-- 基于spring boot 2.6.4 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>youyijiu-starter-dubbo</groupId>
<artifactId>youyijiu-starter-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.youyijiu</groupId>
<artifactId>youyijiu-test-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<!-- Compiler 插件, 设定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!--配置生成源码包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
youyijiu-test-dubbo-server的application.properties中的内容为:
##web端口
server.port=8080
## dubbo配置
dubbo.name=youyijiu-test-dubbo-server
dubbo.zookeeper.address=127.0.0.1:2181
dubbo.group=test
dubbo.version=0.0.1
dubbo.timeout=20000
youyijiu-test-dubbo-server的dubbo-interfaces.xml中的内容为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="${dubbo.name}"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="${dubbo.zookeeper.address}" check="false" protocol="zookeeper"></dubbo:registry>
<!-- 使用${}引用配置项 , 这里的dubboServer是在META-INF下的dubbo里的com.alibaba.dubbo.rpc.Filter中定义的-->
<dubbo:provider port="-1" filter="dubboServer" retries="0" />
<!-- 要暴露的服务接口 -->
<dubbo:service group="${dubbo.group}" version="${dubbo.version}" interface="com.youyijiu.dubbo.IDubboService" ref="dubboService"/>
<!-- 消费方调用过程缺省拦截器,将拦截所有reference,这里的dubboClient是在META-INF下的dubbo里的com.alibaba.dubbo.rpc.Filter中定义的 -->
<dubbo:consumer filter="dubboClient"/>
</beans>
这样,启动youyijiu-test-dubbo-server的时候,这个dubbo-interfaces.xml就已经把com.youyijiu.dubbo.IDubboService所提供的功能注册到zookeeper中去了。
接下来我们创建一个使用dubbo服务的模块。创建一个Maven Module:youyijiu-test-dubbo-client,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.youyijiu</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>youyijiu-test-dubbo-client</artifactId>
<!-- 基于spring boot 2.6.4 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>youyijiu-starter-dubbo</groupId>
<artifactId>youyijiu-starter-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.youyijiu</groupId>
<artifactId>youyijiu-test-dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<!-- Compiler 插件, 设定JDK版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<!--配置生成源码包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
youyijiu-test-dubbo-client的application.properties如下:
##web端口
##web端口
server.port=8090
## dubbo配置
dubbo.name=youyijiu-test-dubbo-client
dubbo.zookeeper.address=127.0.0.1:2181
dubbo.group=test
dubbo.version=0.0.1
dubbo.timeout=20000
youyijiu-test-dubbo-client的dubbo-interfaces.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="${dubbo.name}"></dubbo:application>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="${dubbo.zookeeper.address}" check="false" protocol="zookeeper"></dubbo:registry>
<!-- 使用${}引用配置项 这里的dubboServer是在META-INF下的dubbo里的com.alibaba.dubbo.rpc.Filter中定义的-->
<dubbo:provider port="-1" filter="dubboServer" retries="0" />
<dubbo:reference id="IDubboService" interface="com.youyijiu.dubbo.IDubboService" group="${dubbo.group}" retries="0" version="${dubbo.version}" check="false" timeout="${dubbo.timeout}"/>
<!-- 消费方调用过程缺省拦截器,将拦截所有reference , 这里的dubboClient是在META-INF下的dubbo里的com.alibaba.dubbo.rpc.Filter中定义的-->
<dubbo:consumer filter="dubboClient"/>
</beans>
这样一个youyijiu-test-dubbo-client消费者就配置好了。IDubboService接口如下:
package com.youyijiu.dubbo;
/**
*
* @ClassName: IDubboService
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Jun.Yang 24696026@qq.com
* @date 2022年8月13日 上午9:57:06
*
* @param <R>
* @param <P>
*/
public interface IDubboService<R, P> {
/**
* 根据参数访问后返回结果
* @param param
* @return
*/
R query(P param);
}
IDubboService的实现类DubboService如下:
/**
* @Title: DubboService.java
* @Package com.youyijiu.dubbo.service
* @author Sean.Yang 908682620@qq.com
* @date 2022年8月9日 下午2:57:53
*/
package com.youyijiu.dubbo.service;
import org.springframework.stereotype.Service;
import com.youyijiu.dubbo.IDubboService;
/**
* @ClassName: DubboService
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Sean.Yang 908682620@qq.com
* @date 2022年8月9日 下午2:57:53
*
*/
@Service("dubboService")
public class DubboService implements IDubboService<String, String>{
@Override
public String query(String param) {
StringBuilder sb = new StringBuilder(param);
return sb.append(" ").append(param).toString();
}
}
启动类:
package com.youyijiu.dubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import com.youyijiu.starter.common.util.SpringUtil;
/**
*
* @ClassName: Application
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Jun.Yang 24696026@qq.com
* @date 2022年8月13日 上午9:57:31
*
*/
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,
PersistenceExceptionTranslationAutoConfiguration.class })
@ComponentScan(basePackages = "com.youyijiu.dubbo")
public class Application {
public static void main(String[] args) {
SpringUtil.setApplicationContext(SpringApplication.run(
Application.class, args));
}
}
单元测试:
/**
* @Title: DubboTest.java
* @Package com.youyijiu.dubbo
* @author Sean.Yang 908682620@qq.com
* @date 2022年8月9日 下午4:17:55
*/
package com.youyijiu.dubbo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @ClassName: DubboTest
* @Description: TODO(这里用一句话描述这个类的作用)
* @author Sean.Yang 908682620@qq.com
* @date 2022年8月9日 下午4:17:55
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class DubboTest {
@Autowired
IDubboService<String, String> service;
@Test
public void dubboTest(){
System.out.println(service.query("今天测试一下西"));
}
}