Dubbo+ZooKeeper 微服务开发
1. 准备
- 相关框架的版本,版本号推荐按下表配置,否则会出现dubbo和zk通信协议报错等各种异常情况
name | version | 类型 |
---|---|---|
Zookeeper | 3.9.2 | 软件 |
Dubbo-Admin | 20240824 | 软件 |
spring-boot | 3.3.2 | 依赖库 |
spring-cloud | 2023.0.3 | 依赖库 |
spring-cloud-alibaba | 2023.0.1.2 | 依赖库 |
dubbo-spring-boot-starter | 3.3.0-beta.2 | 依赖库 |
cubator-framework | 5.7.0 | 依赖库 |
cubator-recipes | 5.7.0 | 依赖库 |
curator-x-discovery-server | 5.7.0 | 依赖库 |
1.1 Zookeeper搭建
本案例中Zookeeper仅作为服务发现组件使用,此处只部署单机zk
- 基于Docker部署ZK服务
docker run -itd --name zk_server1 --network zk_net -p 2181:2181 -p 2888:2888 -p 3888:3888 zookeeper
1.2 Dubbo-Admin搭建
Dubbo-Admin是一个基于Zookeeper的服务监控项目,主要用于实时查看微服务的注册状态
-
在Github上下载dubbo-admin项目
-
启动dubbo-admin server服务
- CMD下进入dubbo-admin根目录,执行mvn clean package生成各模块的jar包
- 打包成功后进入dubbo-admin-distribution/target目录,运行dubbo-admin-x.x.x-SNAPSHOT.jar
-
启动dubbo-admin-ui服务
- 执行npm install
- 执行npm run serve
- 成功后访问 npm run serve打印的访问地址信息
-
至此,dubbo-admin启动成功,如下图
1.3 基于Dubbo开发可通信的Order/User微服务
1.3.1 common-interface 公共接口模块开发
Dubbo推荐将RPC调用的接口统一放在一个interface模块以供服务提供者和服务消费者使用
- common-interface模块 pom配置
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.skymaple</groupId>
<artifactId>dsd-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- RPC接口定义
public interface UserService {
//该接口用于获取用户的发货地址信息,Address是实现了序列化接口的数据实体类
public List<Address> getUserAddress();
}
1.3.2 User微服务开发(服务提供者)
该服务主要用于被Order服务调用
- user-service模块 pom配置
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.skymaple</groupId>
<artifactId>dsd-interface</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!-- spring starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- registry dependency -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-x-discovery-server -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery-server</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
</plugin>
</plugins>
</build>
- 服务配置:application.yml
dubbo:
application:
name: dubbo-springboot-user-provider
logger: slf4j
protocol:
port: 50055
# 此处的协议使用dubbo,也可使用triple协议-tri
name: dubbo
registry:
address: zookeeper://127.0.0.1:2181
# address: nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
server:
port: 10000
- user服务提供者实现
//DubboService注解能将该service注册到application.yml指定的服务注册中心registry中,以便消费者查询服务注册中心并调用
@DubboService(group = "grp1",version = "1.0")
public class UserServiceImpl implements UserService {
@Override
public List<Address> getUserAddress() {
Address zs = new Address(1,"zhhangsan","test");
Address ls = new Address(2,"lisi","test");
List<Address> list = Arrays.asList(zs, ls);
System.out.println("call user service getUserAddress success:"+list.toString());
return list;
}
}
//在User服务的程序入口添加@EnableDubbo启动Dubbo功能
@EnableDubbo
@SpringBootApplication
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
1.3.3 Order微服务开发(服务消费者)
该服务主要用于查询服务注册中心中的服务提供者并调用@DubboReference标记的接口
- order-service模块 pom配置
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.skymaple</groupId>
<artifactId>dsd-interface</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery-server</artifactId>
<version>5.7.0</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>
- 服务配置:application.yml
server:
port: 10001
dubbo:
application:
name: dubbo-springboot-order-consumer
logger: slf4j
qos-port: 33333
registry:
address: zookeeper://127.0.0.1:2181
# address: nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos
- order服务消费者实现
@RestController
public class ComsumerController {
//DubboReference注解能从服务注册中心registry中查询到相应的服务提供者接口并注入本地bean
@DubboReference(group = "grp1",version = "1.0",check = false)
private UserService userServiceV1;
@GetMapping("/useraddr")
@ResponseBody
public String getUserAddress(){
return userServiceV1.getUserAddress().toString();
}
}
//在Order服务的程序入口添加@EnableDubbo启动Dubbo功能
@EnableDubbo
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
----- 至此,基于Dubbo的微服务基础开发工作已经完成-----
FAQ
- dubbo 消费者启动报错:no class found xxx/protobuf
- 解决:protobuf缺失—pom中加上protobuf的依赖
- Failed to deserialize triple response, service=org.apache.dubbo.remoting.transport.netty4.NettyConnectionClient ,This may be caused by , go to https://dubbo.apache.org/faq/4/14 to find instructions
- 解决:根据dubbo官方推荐的ZK依赖版本、ZK服务版本进行开发和部署