微服务注册中心zookeeper

微服务注册中心zookeeper

Posted by John Doe on 2021-05-17
Words 340 and Reading Time 1 Minutes
Viewed Times

Zookeeper服务注册与发现

demo地址:

项目地址:https://github.com/raineddown/spring-cloud-demo

涉及服务: cloud-consumerzk-order80、cloud-provider-payment8004

配置流程

服务提供方配置

1
2
3
4
5
6
7
8
9
10
11
server:
port: 8004


#服务别名----注册zookeeper到注册中心名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 192.168.30.87:2181

服务消费方配置

1
2
3
4
5
6
7
8
9
server:
port: 80

spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.30.87:2181

启动类添加注解

1
@EnableDiscoveryClient

注意事项

依赖冲突问题:

@EnableDiscoveryClient 注解属于org.springframework.cloud.client.discovery包,该包自带org.apache.zookeeper包依赖可能和服务器上zookeeper版本不统一请修改pom.xml配置文件为以下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper3.5.3-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
区别于Eureka的自我保护机制AP,我们在zk上注册的node是临时节点,当我们的服务一定时间内没有发送心跳那么zk就会将这个服务的node删除了

This is copyright.

...

...

00:00
00:00