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-providermodule> 3 <module>dubbo-consumemodule> 4 <module>dubbo-apimodule> 5 modules> 6 <parent> 7 <groupId>org.springframework.bootgroupId> 8 <artifactId>spring-boot-starter-parentartifactId> 9 <version>2.0.3.RELEASEversion> 10 <relativePath/> 11 parent> 12 <properties> 13 <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> 14 <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> 15 <java.version>1.8java.version> 16 <com.alibaba.dubbo.version>2.5.3com.alibaba.dubbo.version> 17 <org.apache.zookeeper.version>3.4.12org.apache.zookeeper.version> 18 <com.github.sgroschupf.zkclient.version>0.1com.github.sgroschupf.zkclient.version> 19 properties> 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.bootgroupId> 23 <artifactId>spring-boot-starter-webartifactId> 24 dependency> 25 26 <dependency> 27 <groupId>org.springframework.bootgroupId> 28 <artifactId>spring-boot-starter-testartifactId> 29 <scope>testscope> 30 dependency> 31 dependencies> 32 33 <build> 34 <plugins> 35 <plugin> 36 <groupId>org.springframework.bootgroupId> 37 <artifactId>spring-boot-maven-pluginartifactId> 38 plugin> 39 plugins> 40 build>
dubbo-api中pom.xml
1 <parent> 2 <artifactId>testartifactId> 3 <groupId>com.examplegroupId> 4 <version>0.0.1-SNAPSHOTversion> 5 parent> 6 7 <artifactId>dubbo-apiartifactId> 8 <packaging>jarpackaging> 9 <name>dubbo-apiname> 10 <dependencies> 11 <dependency> 12 <groupId>org.springframework.bootgroupId> 13 <artifactId>spring-boot-starter-webartifactId> 14 dependency> 15 dependencies> 16 <build> 17 <plugins> 18 <plugin> 19 <groupId>org.springframework.bootgroupId> 20 <artifactId>spring-boot-maven-pluginartifactId> 21 plugin> 22 plugins> 23 build>
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.0modelVersion> 6 <parent> 7 <artifactId>testartifactId> 8 <groupId>com.examplegroupId> 9 <version>0.0.1-SNAPSHOTversion> 10 parent> 11 12 <artifactId>dubbo-providerartifactId> 13 <packaging>jarpackaging> 14 <name>dubbo-providename> 15 <dependencies> 16 <dependency> 17 <groupId>org.springframework.bootgroupId> 18 <artifactId>spring-boot-starterartifactId> 19 dependency> 20 <dependency> 21 <groupId>com.examplegroupId> 22 <artifactId>dubbo-apiartifactId> 23 <version>0.0.1-SNAPSHOTversion> 24 dependency> 25 26 <dependency> 27 <groupId>com.alibabagroupId> 28 <artifactId>dubboartifactId> 29 <exclusions> 30 <exclusion> 31 <groupId>org.springframeworkgroupId> 32 <artifactId>springartifactId> 33 exclusion> 34 exclusions> 35 <version>${com.alibaba.dubbo.version}version> 36 dependency> 37 <dependency> 38 <groupId>org.apache.zookeepergroupId> 39 <artifactId>zookeeperartifactId> 40 <version>${org.apache.zookeeper.version}version> 41 dependency> 42 <dependency> 43 <groupId>com.github.sgroschupfgroupId> 44 <artifactId>zkclientartifactId> 45 <version>${com.github.sgroschupf.zkclient.version}version> 46 dependency> 47 dependencies> 48 <build> 49 <plugins> 50 <plugin> 51 <groupId>org.springframework.bootgroupId> 52 <artifactId>spring-boot-maven-pluginartifactId> 53 plugin> 54 plugins> 55 build> 56 project>
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.0modelVersion> 6 <parent> 7 <artifactId>testartifactId> 8 <groupId>com.examplegroupId> 9 <version>0.0.1-SNAPSHOTversion> 10 parent> 11 12 <artifactId>dubbo-consumeartifactId> 13 <name>dubbo-consumename> 14 <packaging>jarpackaging> 15 <dependencies> 16 <dependency> 17 <groupId>org.springframework.bootgroupId> 18 <artifactId>spring-boot-starter-webartifactId> 19 dependency> 20 <dependency> 21 <groupId>com.examplegroupId> 22 <artifactId>dubbo-apiartifactId> 23 <version>0.0.1-SNAPSHOTversion> 24 dependency> 25 26 <dependency> 27 <groupId>com.alibabagroupId> 28 <artifactId>dubboartifactId> 29 <exclusions> 30 <exclusion> 31 <groupId>org.springframeworkgroupId> 32 <artifactId>springartifactId> 33 exclusion> 34 exclusions> 35 <version>${com.alibaba.dubbo.version}version> 36 dependency> 37 <dependency> 38 <groupId>org.apache.zookeepergroupId> 39 <artifactId>zookeeperartifactId> 40 <version>${org.apache.zookeeper.version}version> 41 dependency> 42 <dependency> 43 <groupId>com.github.sgroschupfgroupId> 44 <artifactId>zkclientartifactId> 45 <version>${com.github.sgroschupf.zkclient.version}version> 46 dependency> 47 dependencies> 48 <build> 49 <plugins> 50 <plugin> 51 <groupId>org.springframework.bootgroupId> 52 <artifactId>spring-boot-maven-pluginartifactId> 53 plugin> 54 plugins> 55 build> 56 project>
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 { }
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 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 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 }
到此简单的配置已经ok了。
6、整个流程为 服务的提供者方面 dubbo将对外的服务注册到zooKeeper上,同时在zooKeeper生成相应的node,对其监控。消费方从zooKeeper中去拿服务,所以服务的接口必须到时提供者和消费者约定好的。这样就利用dubbo+zooKeeper实现的远程RPC调用