Zookeeper服务注册与发现
什么是Zookeeper?
Zookeeper是一个分布式协调工具,可以实现注册中心功能
(官方解释)ZooKeeper是用于维护配置信息,命名,提供分布式同步以及提供组服务的集中式服务。所有这些类型的服务都以某种形式被分布式应用程序使用。每次实施它们时,都会进行很多工作来修复不可避免的错误和竞争条件。由于难以实现这类服务,因此应用程序最初通常会跳过它们,这会使它们在发生更改时变得脆弱并且难以管理。即使部署正确,这些服务的不同实现也会导致管理复杂。
安装Zookeeper(linux)
https://blog.csdn.net/she_lock/article/details/80435176
使用Zookeeper实现注册中心功能的步骤
-
新建
cloud-provider-payment8004
-
pom
<dependencies> <!-- 引入公共模块--> <dependency> <groupId>org.example</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--SpringBoot整合Zookeeper客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <exclusions> <!--先排除自带的zookeeper3.5.3--> <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.14</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
-
yaml
server: port: 8004 spring: application: name: cloud-provider-payment #微服务名称 #zookeeper的配置 cloud: zookeeper: connect-string: 116.62.102.168:2181
-
主启动类
@SpringBootApplication @EnableDiscoveryClient public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class,args); } }
-
controller
@RestController @Slf4j public class PaymentController { @Value("${server.port}") private String serverPort; @RequestMapping(value = "payment/zk") public String paymentZk() { return "SpringCloud with zookeeper:" + serverPort + "\t" + UUID.randomUUID().toString(); } }
-
启动测试注册,注意如果你的依赖是
spring-cloud-starter-zookeeper-discovery
那么这个依赖是自带最新版的zookeeper
所以可能会造成依赖不兼容的问题并且会爆出解决方案就是排除他原本带的版本,更换成我们自己版本匹配的依赖,具体操作如下:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> <exclusions> <!--先排除自带的zookeeper3.5.3--> <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.14</version> </dependency>
-
改完之后在测试,去zookeeper查看是否注册成功
Zookeeper节点问题
Zookeeper是临时节点(每次注册的都是不同的流水号)