dubbo 实战之三:使用 Zookeeper 注册中心
本篇概览
本文是《dubbo 实战》系列的第三篇,主要内容是使用 Zookeeper 作为注册中心的实战;
前面两篇的实战都是使用了广播模式,整个环境由服务提供者和服务消费者组成,这种简单形态只是为了方便学习,实际上多数情况都会部署注册中心,服务提供者在上面注册,服务消费者再从注册中心得到具体的服务信息;
下面是个典型的提供者、消费者、注册中心的典型架构图:
如下图所示,dubbo 官方推荐的注册中心是 Zookeeper:
关于 Zookeeper
本次使用了单节点 Zookeeper,版本 3.4.13,IP192.168.50.43,端口 2181
关于 Zookeeper 的部署,我这边是通过 docker 快速部署的,参考文章《群晖DS218+部署kafka》
如果您部署好了 Zookeeper,接下来开始编写服务提供者和消费者的代码;
源码下载
如果您不想编码,可以在 GitHub 下载所有源码,地址和链接信息如下表所示:
| 名称 | 链接 | 备注 |
| :-- | :-- | :-- |
| 项目主页 | https://github.com/zq2599/blog_demos | 该项目在 GitHub 上的主页 |
| git 仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https 协议 |
| git 仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh 协议 |
这个 git 项目中有多个文件夹,本章的应用在 dubbopractice 文件夹下,如下图红框所示:
dubbopractice 是父子结构的工程,本篇的代码在 springbootzkprovider 和 springbootzkconsumer 这两个子工程中,如下图:
编码(服务提供方)
先创建提供服务的工程 springbootmulticastprovider,一共要创建 4 个文件,创建顺序和功能如下表:
| 创建顺序 | 文件名 | 作用 |
| --- | --- | --- |
| 1 | pom.xml | 工程的 pom 文件 |
| 2 | src/main/resources/application.yml | 配置文件 |
| 3 | DemoServiceImpl.java | 提供具体的服务 |
| 4 | SpringBootZKProviderApplication.java | 启动类 |
完整的文件位置如下图:
接下来逐个创建上述内容;
创建名为 springbootmulticastprovider 的子工程,pom.xml 内容如下,要重点关注的是新增依赖 dubbo-spring-boot-starter,这就是 dubbo 在 SpringBoot 环境的 starter 依赖,还有 Zookeeper 的依赖 dubbo-dependencies-zookeeper:
<?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>dubbopractice</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.bolingcavalry</groupId>
<artifactId>springbootzkprovider</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springbootzkprovider</name>
<description>Demo project for dubbo service provider from Spring Boot</description>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>practiceinterface</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
</plugin>
</plugins>
</build>
</project>
配置文件 application.yml,要注意的是 registry.address 的值 zookeeper://192.168.50.43:2181,这就是 Zookeeper 注册中心的配置:
dubbo:
application:
#application-name 本模块名字
name: springboot-zk-provider
registry:
address: zookeeper://192.168.50.43:2181
protocol:
name: dubbo
port: 20880
编写服务实现类 DemoServiceImpl.java,注意 @Service 注解将当前类的实例作为远程服务对外暴露:
package com.bolingcavalry.springbootzkprovider;
import com.bolingcavalry.dubbopractice.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
@Slf4j
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
log.info("I'm springboot-zk-provider, Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "I'm springboot-zk-provider, Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
编写 SpringBoot 启动类 SpringBootZKProviderApplication.java,注意要添加 @EnableDubbo 注解:
package com.bolingcavalry.springbootzkprovider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class SpringBootZKProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootZKProviderApplication.class, args);
}
}
至此服务提供方编码完成,直接在 IDEA 上运行 SpringBootZKProviderApplication 类即可启动服务,启动成功后的日志输出如下图,如红框所示,已连上了 Zookeeper:
编码(服务消费方)
现在网络上已经有了服务,咱们再来编写服用消费方的代码,一共要创建 6 个文件,创建顺序和功能如下表:
| 创建顺序 | 文件名 | 作用 |
| --- | --- | --- |
| 1 | pom.xml | 工程的 pom 文件 |
| 2 | src/main/resources/application.yml | 配置文件 |
| 3 | RemoteInvokeServiceImpl.java | service 层,在这里远程调用服务提供方的服务 |
| 4 | DemoController.java | web 接口类,对外提供 web
服务 |
| 5 | SwaggerConfig.java | swagger 配置类,便于通过页面测试接口 |
| 6 | SpringBootZKConsumerApplication.java | 启动类 |
完整的文件位置如下图:
接下来逐个创建上述文件;
创建名为 springbootzkconsumer 的子工程,pom.xml 内容如下,同样需要依赖 dubbo-spring-boot-starter 和 dubbo-dependencies-zookeeper:
<?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>dubbopractice</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.bolingcavalry</groupId>
<artifactId>springbootzkconsumer</artifactId>
<version>1.0-SNAPSHOT</version>
<name>springbootzkconsumer</name>
<description>Demo project for dubbo service consumer from Spring Boot</description>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>com.bolingcavalry</groupId>
<artifactId>practiceinterface</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${springboot.version}</version>
</plugin>
</plugins>
</build>
</project>
编写配置文件 application.yml,注意 dubbo.registry.address 的值:
dubbo:
application:
#application-name 本模块名字
name: springboot-zk-consumer
registry:
address: zookeeper://192.168.50.43:2181
server:
port: 8081
编写调用远程服务的代码,如下,可见如果想调用远程服务,只要对接口做 @Reference 注释即可,另外还通过 timeout 属性增加了超时配置:
package com.bolingcavalry.springbootzkconsumer.service;
import com.bolingcavalry.dubbopractice.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service
public class RemoteInvokeServiceImpl {
@Reference(timeout = 2000)
private DemoService demoService;
public String sayHello(String name) {
return "from dubbo remote (zk registry center mode) : " + demoService.sayHello(name);
}
}
再编写对外提供 web 服务的 Controller 类:
package com.bolingcavalry.springbootzkconsumer.controller;
import com.bolingcavalry.springbootzkconsumer.service.RemoteInvokeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
@Api(tags = {"DemoController"})
public class DemoController {
@Autowired
private RemoteInvokeServiceImpl remoteInvokeService;
@ApiOperation(value = "获取 dubbo service provider 的响应", notes=""获取 dubbo service provider 的响应")
@ApiImplicitParam(name = "name", value = "昵称", paramType = "path", required = true, dataType = "String")
@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String sayHello(@PathVariable String name){
return remoteInvokeService.sayHello(name);
}
}
还要添加 swagger 配置类:
评论