1、 首先创建springBoot项目,springBoot是一堆组件的集合,在pom文件中对需要的组件进行配置。生成如下目录结构
创建test项目,同步在test创建dubbo-api,dubbo-consume,dubbo-provider三个Model
dubbo-provider 为服务的提供者,服务的实现层
dubbo-api 为服务提供者dubbo-provider的接口层 (此处dubbo-provider,dubbo-api相当于一个微服务,dubbo-api为对外接口,由消费者dubbo-consume调用)
dubbo-consume 为服务的消费者,消费由dubbo-api提供的服务
2、配置pom.xml
test中的pom.xml是其他三个Module的父文件,所以公共的部分在test中配置即可。
test 中 pom.xml部分配置
1 <modules> 2 <module>dubbo-provider</module> 3 <module>dubbo-consume</module> 4 <module>dubbo-api</module> 5 </modules> 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>2.0.3.RELEASE</version> 10 <relativePath/> <!-- lookup parent from repository --> 11 </parent> 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 15 <java.version>1.8</java.version> 16 <com.alibaba.dubbo.version>2.5.3</com.alibaba.dubbo.version> 17 <org.apache.zookeeper.version>3.4.12</org.apache.zookeeper.version> 18 <com.github.sgroschupf.zkclient.version>0.1</com.github.sgroschupf.zkclient.version> 19 </properties> 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-test</artifactId> 29 <scope>test</scope> 30 </dependency> 31 </dependencies> 32 33 <build> 34 <plugins> 35 <plugin> 36 <groupId>org.springframework.boot</groupId> 37 <artifactId>spring-boot-maven-plugin</artifactId> 38 </plugin> 39 </plugins> 40 </build>
pom.xml
dubbo-api中pom.xml
1 <parent> 2 <artifactId>test</artifactId> 3 <groupId>com.example</groupId> 4 <version>0.0.1-SNAPSHOT</version> 5 </parent> 6 7 <artifactId>dubbo-api</artifactId> 8 <packaging>jar</packaging> 9 <name>dubbo-api</name> 10 <dependencies> 11 <dependency> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-web</artifactId> 14 </dependency> 15 </dependencies> 16 <build> 17 <plugins> 18 <plugin> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-maven-plugin</artifactId> 21 </plugin> 22 </plugins> 23 </build>
pom.xml
dubbo-provider中pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <artifactId>test</artifactId> 8 <groupId>com.example</groupId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 12 <artifactId>dubbo-provider</artifactId> 13 <packaging>jar</packaging> 14 <name>dubbo-provide</name> 15 <dependencies> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>com.example</groupId> 22 <artifactId>dubbo-api</artifactId> 23 <version>0.0.1-SNAPSHOT</version> 24 </dependency> 25 <!-- dubbo --> 26 <dependency> 27 <groupId>com.alibaba</groupId> 28 <artifactId>dubbo</artifactId> 29 <exclusions> 30 <exclusion> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring</artifactId> 33 </exclusion> 34 </exclusions> 35 <version>${com.alibaba.dubbo.version}</version> 36 </dependency> 37 <dependency> 38 <groupId>org.apache.zookeeper</groupId> 39 <artifactId>zookeeper</artifactId> 40 <version>${org.apache.zookeeper.version}</version> 41 </dependency> 42 <dependency> 43 <groupId>com.github.sgroschupf</groupId> 44 <artifactId>zkclient</artifactId> 45 <version>${com.github.sgroschupf.zkclient.version}</version> 46 </dependency> 47 </dependencies> 48 <build> 49 <plugins> 50 <plugin> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-maven-plugin</artifactId> 53 </plugin> 54 </plugins> 55 </build> 56 </project>
pom.xml
dubbo-consume中pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <artifactId>test</artifactId> 8 <groupId>com.example</groupId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 12 <artifactId>dubbo-consume</artifactId> 13 <name>dubbo-consume</name> 14 <packaging>jar</packaging> 15 <dependencies> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-web</artifactId> 19 </dependency> 20 <dependency> 21 <groupId>com.example</groupId> 22 <artifactId>dubbo-api</artifactId> 23 <version>0.0.1-SNAPSHOT</version> 24 </dependency> 25 <!-- dubbo --> 26 <dependency> 27 <groupId>com.alibaba</groupId> 28 <artifactId>dubbo</artifactId> 29 <exclusions> 30 <exclusion> 31 <groupId>org.springframework</groupId> 32 <artifactId>spring</artifactId> 33 </exclusion> 34 </exclusions> 35 <version>${com.alibaba.dubbo.version}</version> 36 </dependency> 37 <dependency> 38 <groupId>org.apache.zookeeper</groupId> 39 <artifactId>zookeeper</artifactId> 40 <version>${org.apache.zookeeper.version}</version> 41 </dependency> 42 <dependency> 43 <groupId>com.github.sgroschupf</groupId> 44 <artifactId>zkclient</artifactId> 45 <version>${com.github.sgroschupf.zkclient.version}</version> 46 </dependency> 47 </dependencies> 48 <build> 49 <plugins> 50 <plugin> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-maven-plugin</artifactId> 53 </plugin> 54 </plugins> 55 </build> 56 </project>
pom.xml
3、dubbo-api
定义接口IDubboDemoService
public interface IDubboDemoService { public String getString(); }
4、dubbo-provider
首先创建启动类,和dubbo配置文件启动类,mainConfig项目启动类就不多说了
@Configuration @PropertySource("classpath:dubbo-provider.properties") @ImportResource({ "classpath:dubbo-provider.xml" }) public class DubboConfig { }
DubboConfig.java
dubbo读取的两个配置文件
首先对dubbo进行配置
#应用名称 dubbo.application.name=dubbo-provider #注册中心类型 dubbo.registry.protocol=zookeeper #注册中心地址 dubbo.registry.address=127.0.0.1:2181 #暴露服务方式 dubbo.protocol.name=dubbo #暴露服务端口 dubbo.protocol.port=20880
之后对dubbo中的接口进行配置
<?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:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="${dubbo.application.name}" /> <!-- 注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> --> <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" /> <!-- 暴露服务 --> <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" /> <!-- 暴露的接口 --> <dubbo:service interface="com.example.service.IDubboDemoService" ref="dubboDemoServiceImpl" retries="0" timeout="6000" /> </beans>
注意:mainConfig.java和DubboConfg.java 两个文件要放在接口的上一层,否则加载时会找不到接口
接口实现:
@Service public class DubboDemoServiceImpl implements IDubboDemoService { @Override public String getString() { return "成功"; } }
5、dubbo-consume
只来看下配置文件
dubbo.consume.properties:消费者只需要知道注册中心的类型和地址
#应用名称 dubbo.application.name=dubbo-consume #注册中心类型 dubbo.registry.protocol=zookeeper #注册中心地址 dubbo.registry.address=127.0.0.1:2181
dubbo-consume.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:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="${dubbo.application.name}" /> <!-- 注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> --> <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" /> <dubbo:reference id="consumeService" interface="com.example.service.IDubboDemoService" /> </beans>
测试代码
1 @RestController 2 @RequestMapping("/dubbo") 3 public class DubboDemoController { 4 5 @Resource 6 public IDubboDemoService service; 7 8 @RequestMapping(value = "/getString", method = RequestMethod.GET) 9 public String getString(){ 10 return service.getString(); 11 } 12 13 }
DubboDemoController.java
到此简单的配置已经ok了。
6、整个流程为 服务的提供者方面 dubbo将对外的服务注册到zooKeeper上,同时在zooKeeper生成相应的node,对其监控。消费方从zooKeeper中去拿服务,所以服务的接口必须到时提供者和消费者约定好的。这样就利用dubbo+zooKeeper实现的远程RPC调用
原文地址:https://www.cnblogs.com/volare/p/9248836.html