Zookeeper+Dubbo
一. Zookeeper
zookeeper最早是由雅虎推出的一个服务治理的框架,其充当的角色类似于
nacos
.
1.1 下载
下载地址为:https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.5.9/
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pDBK49mm-1626415850527)(D:\武汉-Java-2101(笔记)]\项目周\zookeeper+dubbo\images\1.png)
1.2 配置使用
- 将
ZOOKEEPER_HOME/conf
目录下的zoo_sample.cfg
拷贝一份,命名为zoo.cfg
。然后修改其数据保存的位置,如下所示:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 修改数据保存的位置
dataDir=D:/zookeeper/apache-zookeeper-3.5.8-bin/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
- 双击
ZOOKEEPER_HOME/bin
目录下的zkServer.cmd
启动zookeeper
- 双击
ZOOKEEPER_HOME/bin
目录下的zkCli.cmd
打开zookeeper的客户端
1.3 zookeeper的基本命令
如果不知道 zookeeper 有哪些命令,可以在客户端随便输入任何字母,然后回给出所有的 zookeeper 的命令。
# 递归查看根节点下所有的节点
ls -R /
# 查看 /zookeeper 节点下的所有直接子节点
ls /zookeeper
# 创建 /app 节点
create /app
# 创建 /app/node1 节点
create /app/node1
# 给 /app/node1 节点设置数据,数据内容为 welcome
set /app/node1 welcome
# 查看 /app/node1 节点的数据,返回 welcome
get /app/node1
# 创建 /app/node2 节点,并指定节点的数据为 helloworld
create /app/node2 helloworld
# 监听 /app 节点,如果 /app 节点下出现任何的节点变化,都会出发监听
ls -w /app
1.4 zookeeper 注册中心的原理
zookeeper也可以作为任何 RPC 框架的注册中心使用,其原理就是,服务的提供方和消费方协定好数据保存的节点位置,服务的提供方在zookeeper上注册服务的时候往对应的节点写入服务的信息,然后服务消费方到对应的节点获取对应的信息即可。
1.5 zookeeper分布式锁实现
在之前的课程中我们介绍过使用 Redis 来实现分布式锁,为防止集群中其中一个节点在获取到锁之后宕机的发生,需要设置过期时间,然而为了防止过期时间过短,又需要设置看门狗程序来给锁续命,导致了程序的设计较为复杂。
zookeeper实现分布式锁的实现原理就是利用临时节点,因为临时节点是和连接绑定的,如果服务宕机了连接自动断掉,那么就不需要设置过期时间,也就不需要担心时间的问题。
# 创建临时节点
create -e /lock/el
二. Dubbo
Dubbo是阿里巴巴推出的一个RPC(Remote Procedure Call, 远程过程调用)框架,遗憾的是中途停更过一段的时间,导致Spring Cloud迅速的崛起。dubbo推荐使用zookeeper来作为其默认的注册中心,当然也可以使用 nacos 作为注册中心。
2.1 项目的结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4fRwoKOg-1626415850530)(D:\武汉-Java-2101(笔记)]\项目周\zookeeper+dubbo\images\2.png)
对照上图来说明一下:
dubbo-service-api
中定义接口,dubbo-consumer
和dubbo-provider
都需要依赖该模块dubbo-provider
是服务的提供方,需要实现dubbo-service-api
中定义的接口dubbo-consumer
中可以像使用本地服务一样调用到dubbo-provider
中对接口的实现
2.2 添加依赖
在
dubbo-consumer
和dubbo-provider
中都要实现添加该依赖,我们没有提取出来
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>dubbo-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.7</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.13.0</version>
</dependency>
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.protostuff/protostuff-runtime -->
<dependency>
<groupId>io.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
<dependency>
<groupId>com.diffplug.guava</groupId>
<artifactId>guava-cache</artifactId>
<version>19.0.0</version>
</dependency>
</dependencies>
2.3 配置
在
dubbo-consumer
和dubbo-provider
的主配置文件中都需要书写如下的配置:
server:
port: 8082
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
# 最小空闲的连接的数量
minimum-idle: 2
# 最大的数量, mysql的最大连接数量 1000 左右
maximum-pool-size: 100
dubbo:
# 注册中心的地址
registry:
address: zookeeper://localhost:2181
# 配置协议
protocol:
# hessian2 是数据的序列化方式
serialization: hessian2
# 协议的名称 就是 dubbo
name: dubbo
# 每个项目启动之后,其实是有两个服务器运行的, 一个tomcat, 另外一个用于两个服务间通信的 netty 服务器
# 这个端口是个 netty 服务器用的
port: 7421
application:
# 服务名,就是注册到zookeeper的服务名
name: dubbo-provider
2.4 启动dubbo配置
在所有项目的启动类上加上
@EnableDubbo
注解
2.5 服务提供方
服务提供方需要实现
dubbo-service-api
中的接口,如下所示
@Transactional
@DubboService
public class PersonService implements IPersonService {
private PersonMapper personMapper;
public PersonService(PersonMapper personMapper) {
this.personMapper = personMapper;
}
public void save() {
Person person = new Person();
person.setName("一个人");
personMapper.insert(person);
}
}
2.6 服务的消费方
@Service
@Transactional
public class UserService {
private UserMapper userMapper;
@DubboReference
private IPersonService personService;
public UserService(UserMapper userMapper) {
this.userMapper = userMapper;
}
public void save() {
User user = new User();
user.setName("用户");
personService.save();
userMapper.insert(user);
}
}