Dubbo作为一款优秀的RPC框架,在国内有着众多的使用者,自从2018年2月,Dubbo被阿里捐献给Apache基金会以后,Dubbo似乎以全新的名称 Apache Dubbo焕发了新的生命力。虽然有着同样优秀和优势的Spring Clould框架,但是Dubbo能焕发新生也是一件好事,期待着Dubbo能够完善机制,更新迭代出的更好。
所有代码我已上传到github,需要的可以自取测试,欢迎start,传送门
https://github.com/AggerChen/spring-boot-dubbo
在Dubbo新的官网上查看资料,虽然也有很多demo发现要不还是旧包名com.alibaba.dubbo,要不就是集成的SpringBoot1.x,根本没法满足新的需要嘛,既然都已经是Apache Dubbo了,当然要使用最新的org.apache.dubbo和最新的SpringBoot2.x版本了~
此篇文章就是以一下版本来展示示例:
SpringBoot 2.2.2
org.apache.dubbo 2.7.5
zookeeper 3.4.14
既然是Dubbo项目,那么我们的使用场景就是分布式微服务,多个service和一个consumer,还需要一个公共项目common来编写公用的类与接口。
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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.2.RELEASEversion>
<relativePath/>
parent>
<groupId>com.aggergroupId>
<artifactId>spring-boot-dubboartifactId>
<version>1.0.0-SNAPSHOTversion>
<packaging>pompackaging>
<name>${project.artifactId}name>
<description>dubbo根项目description>
<properties>
<skip_maven_deploy>trueskip_maven_deploy>
<dubbo.version>2.7.5dubbo.version>
<zookeeper.version>3.4.14zookeeper.version>
<spring.version>5.2.2.RELEASEspring.version>
<dubbo.common>0.0.1-SNAPSHOTdubbo.common>
properties>
<modules>
<module>dubbo-commonmodule>
<module>dubbo-consumermodule>
<module>dubbo-providermodule>
modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.springframeworkgroupId>
<artifactId>springartifactId>
exclusion>
<exclusion>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper.version}version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12artifactId>
<groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>4.2.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>4.2.0version>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>com.aggergroupId>
<artifactId>dubbo-commonartifactId>
<version>${dubbo.common}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
此项目是一个公共项目,不需要其他多余的依赖,只是编写接口和公共的类使用。
编写一个接口名为HelloService:
HelloService.java
package com.agger.dubbocommon.service;
/**
* @classname: HelloService
* @description: Hello服务接口
* @author chenhx
* @date 2020-01-14 13:55:38
*/
public interface HelloService {
String sayHello(String name);
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.aggergroupId>
<artifactId>spring-boot-dubboartifactId>
<version>1.0.0-SNAPSHOTversion>
parent>
<groupId>com.aggergroupId>
<artifactId>dubbo-commonartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubbo-commonname>
<packaging>jarpackaging>
<description>dubbo公共项目description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
此项目是一个服务类项目,也就是将接口服务注册到zookeeper注册中心供消费端调取使用。
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.aggergroupId>
<artifactId>spring-boot-dubboartifactId>
<version>1.0.0-SNAPSHOTversion>
parent>
<groupId>com.aggergroupId>
<artifactId>dubbo-providerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubbo-providername>
<packaging>jarpackaging>
<description>dubbo项目服务端description>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper.version}version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12artifactId>
<groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>4.2.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>4.2.0version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
HelloServiceImpl.java
package com.agger.dubboprovider.impl;
import com.agger.dubbocommon.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
/**
* @classname: HelloServiceImpl
* @description: HelloService实现
* @author chenhx
* @date 2020-01-14 14:11:39
*/
@Service
@Component
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
System.out.println("dubbo服务调用:" + name);
return "早上好啊~" + name;
}
}
DubboProviderApplication.java
package com.agger.dubboprovider;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubboConfig
@DubboComponentScan("com.agger.dubboprovider.impl")
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
application.yml
server:
port: 8087 # 服务端口
dubbo:
application:
id: dubbo-provider
name: dubbo-provider #应用名称
owner: aggerChen #应用所属者
organization: agger #应用所属组织
registry:
id: zookeeper-registry #注册中心id
protocol: zookeeper #注册中心协议
address: zookeeper://127.0.0.1:2181 #注册中心地址
metadata-report:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo #协议名称
port: 20880 #协议端口
accesslog: dubbo-access.log #协议访问log
provider:
retries: 0 #重试次数
timeout: 3000 #超时时间
monitor:
protocol: registry # 注册监控中心
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.aggergroupId>
<artifactId>spring-boot-dubboartifactId>
<version>1.0.0-SNAPSHOTversion>
parent>
<groupId>com.aggergroupId>
<artifactId>dubbo-consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubbo-consumername>
<packaging>jarpackaging>
<description>dubbo项目客户端description>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper.version}version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12artifactId>
<groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>4.2.0version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>4.2.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
plugins>
build>
project>
HelloController.java
package com.agger.dubboconsumer.controller;
import com.agger.dubbocommon.service.HelloService;
import com.agger.dubboconsumer.vo.ResultVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @classname: HelloController
* @description: Hello控制器
* @author chenhx
* @date 2020-01-14 13:57:48
*/
@RestController
@RequestMapping("/hello")
public class HelloController {
@Reference
HelloService helloService;
@GetMapping("/morning/{name}")
public ResultVO morning(@PathVariable("name") String name){
System.out.println(name);
String hello = helloService.sayHello(name);
System.out.println(hello);
return ResultVO.builder().flag(true).msg("调用成功").data(hello).build();
}
}
当然你可以将调用接口的方法再封装一层service,方便更改service的版本号等等。
3. 在项目启动类上添加配置
在项目的启动类DubboConsumerApplication中添加Dubbo启动注解和扫描注解。
DubboConsumerApplication.java
package com.agger.dubboconsumer;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubboConfig
@DubboComponentScan("com.agger.dubboconsumer.controller")
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
application.yml
server:
port: 8088
dubbo:
application:
name: dubbo-consumer # 应用名称
owner: aggerChen # 应用所属者
organization: agger # 应用所属组织
registry:
id: zookeeper-registry #注册中心id
protocol: zookeeper #注册中心协议
address: zookeeper://127.0.0.1:2181 #注册中心地址
monitor:
protocol: registry # 注册监控中心
到此为止编码部分就完成了,现在开始启动测试。
启动zookeeper
启动项目之前需要先启动zookeeper客户端,这个可以从官网上去下载传送门
选择稳定版本并下载。
下载完成后,记得将conf文件夹下面的文件zoo_sample.cfg改名为zoo.cfg,然后启动bin/zkServer.cmd就可以了。
启动dubbo-provider
如果你在内网有多台机器或者虚拟机,可以打包dubbo-provider项目在不同的机器上多启动几台。并使用java -jar dubbo-provider命令启动运行。我在IDEA中运行查看控制台信息:已经成功将接口HelloService注册到zookeeper上了
测试访问
在浏览器访问地址:http://localhost:8088/hello/morning/tom
或者可以用IDEA的测试工具HTTP Client非常好用
看到测试结果如下,OK,测试成功!
git clone https://github.com/apache/dubbo-admin.git
cd dubbo-admin mvn clean package
cd dubbo-admin-distribution/target java -jar dubbo-admin-0.1.jar