2.7.X的Dubbo,包名不再是com.alibaba,而是org.apache
先看工程目录结构
一个公共api模块,一个SpringBoot项目充当Dubbo服务,一个SpringBoot项目充当服务消费者。
代码
父项目pom
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId> <artifactId>dubbo-2.7.3</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-api</module> <module>dubbo-service</module> <module>dubbo-consumer</module> </modules> <properties> <java.version>1.8</java.version> <dubbo.version>2.7.3</dubbo.version> <!--如果curator版本与zookeeper不对应,会报错:KeeperErrorCode = Unimplemented for /dubbo/com.demo.common.HelloService--> <curator.version>2.13.0</curator.version> </properties> <dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>${curator.version}</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>${curator.version}</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> </project>
api模块
只是提供了一个公共的服务接口
package com.demo.common; public interface HelloService { String hello(); }
pom
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo-2.7.3</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-api</artifactId> </project>
Dubbo服务模块
主要提供服务实现
package com.demo.dubboservice.service.impl; import com.demo.common.HelloService; import org.apache.dubbo.config.annotation.Service; /** * 建议由服务提供方设置超时,在 Provider 上尽量多配置 Consumer 端属性 * timeout 方法调用超时 * retries 失败重试次数,缺省是 2 [2] * loadbalance 负载均衡算法 [3],缺省是随机 random。还可以有轮询 roundrobin、最不活跃优先 [4] leastactive 等 * actives 消费者端,最大并发调用限制,即当 Consumer 对一个服务的并发调用到上限后,新调用会阻塞直到超时 */ @Service(registry = "dubboRegistry", timeout = 3000, version = "1.0", retries = 3, loadbalance = "random", actives = 5) public class HelloServiceImpl implements HelloService { @Override public String hello() { return "动物园里有大西几! 小凶许! 小脑斧! 梅发怒!"; } }
application.properties
spring.application.name=service-provider server.port=9999 spring.main.allow-bean-definition-overriding=true # Dubbo Application # The default value of dubbo.application.name is ${spring.application.name} # dubbo.application.name=${spring.application.name} # 扫描dubbo服务(@Service.. @Reference..) dubbo.scan.basePackages=com.demo.dubboservice.service.impl # 通信协议 dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 20880 # 注册中心 dubbo.registry.id = dubboRegistry dubbo.registry.timeout=5000 dubbo.registry.address=zookeeper://127.0.0.1:2181 # Dubbo 支持 zkclient 和 curator 两种 Zookeeper 客户端实现:在2.7.x的版本中已经移除了zkclient的实现,如果要使用zkclient客户端,需要自行拓展 dubbo.registry.client=curator # 元数据地址 dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
pom
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo-2.7.3</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-service</artifactId> <name>dubbo-service</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.demo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
服务消费者模块
调用dubbo服务,提供前端接口
package com.demo.dubboconsumer.controller; import com.demo.common.HelloService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Reference(version = "1.0") private HelloService helloService; @RequestMapping("/hello") public String hello(){ return helloService.hello(); } }
application.properties
spring.application.name=dubbo-consumer server.port=9990 spring.main.allow-bean-definition-overriding=true # Dubbo Application # The default value of dubbo.application.name is ${spring.application.name} # dubbo.application.name=${spring.application.name} # 扫描dubbo服务(@Service.. @Reference..) dubbo.scan.basePackages=com.demo.dubboconsumer.controller # 通信协议 dubbo.protocol.id=dubbo dubbo.protocol.name=dubbo dubbo.protocol.port=20880 # 注册中心 dubbo.registry.id=dubboRegistry dubbo.registry.timeout=5000 dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.registry.client=curator # 元数据地址 dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
pom
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo-2.7.3</artifactId> <groupId>com.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubbo-consumer</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.demo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
代码搞定,又出了个新问题
为什么呢,因为中央仓库里还没有2.7.3版本的依赖,等有了就不会出现这个问题了(这里就要吐槽一下,Dubbo-SpringBoot-Starter项目与Dubbo项目的版本不同步有点尴尬)
临时解决,去GitHub上下载2.7.3的源码,然后本地install。
解压缩,然后进入根目录,执行 mvn install -Dmaven.test.skip=true
启动
先在本地启动一个3.4.X的zookeeper(去官网下载,然后复制conf里的zoo_sample.cfg,重命名zoo.cfg,即可启动),然后先后启动Dubbo服务以及消费者项目
访问
如果你启动Dubbo管控台(注:最好是最新版),可以看见Dubbo服务
点击查看【详情】,在最后有个【元数据】信息,这与前面配置的【元数据地址】是相关联的。