博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Zookeeper | 实现服务注册与发现
阅读量:4104 次
发布时间:2019-05-25

本文共 3232 字,大约阅读时间需要 10 分钟。

一、简介

Zookeeper 可作为注册中心,实现服务注册与发现,当服务启动后,就会注册到 Zookeeper,然后保存该服务的地址以以及一些基本的信息,其他服务可从注册中心获取到该服务的地址,进行 RPC 远程调用,和 Eureka 作为注册中心同理,Eureka 和 Zookeeper 的区别在于 Eureka 保证 AP,也就是保证高可用和分区容错,Zookeeper 保证 CP,也就是保证一致性和分区容错。

二、实例

1、创建 SpringBoot 项目添加依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
com.101tec
zkclient
0.8
2、编写服务注册代码

编写一个类实现 ApplicationRunner 接口,然后注册到 Spring 容器中,当我们启动 Spring 项目后,就会执行以下代码把该服务注册到 Zookeeper,我们可以修改 yml 配置文件中的端口号,分别注册多个实例。

@Componentpublic class MyApplicationRunner implements ApplicationRunner {
@Value("${server.port}") private String port; private static CountDownLatch countDownLatch = new CountDownLatch(1); @Override public void run(ApplicationArguments args) throws Exception {
ZooKeeper zooKeeper = new ZooKeeper("10.13.1.171:2181", 5000, new Watcher() {
@Override public void process(WatchedEvent watchedEvent) {
//获取连接状态 Event.KeeperState state = watchedEvent.getState(); if (state == Event.KeeperState.SyncConnected) {
System.out.println("zk连接成功"); countDownLatch.countDown(); } } }); countDownLatch.await(); String parentPath = "/user-service"; Stat exits = zooKeeper.exists(parentPath, null); if (exits == null) {
zooKeeper.create(parentPath,"user".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } String path = "http://127.0.0.1"+port; zooKeeper.create(parentPath+"/"+port,path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); System.out.println("服务注册成功!"); }}

在这里插入图片描述

在这里插入图片描述

3、编写服务发现代码
public class ZkTest {
//计数器 private static CountDownLatch countDownLatch = new CountDownLatch(1); public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
/** * 1、连接地址 * 2、超时时间 * 3、事件通知 */ ZooKeeper zooKeeper = new ZooKeeper("10.13.1.171:2181", 5000, new Watcher() {
@Override public void process(WatchedEvent watchedEvent) {
//获取连接状态 Event.KeeperState state = watchedEvent.getState(); if (state == Event.KeeperState.SyncConnected) {
System.out.println("zk连接成功"); countDownLatch.countDown(); } } }); countDownLatch.await(); String path = "/user-service"; List
children = zooKeeper.getChildren(path, null, new Stat()); for (int i = 0; i < children.size(); i++) {
String pathChildren = path + "/" + children.get(i); byte[] data = zooKeeper.getData(pathChildren, null, new Stat()); System.out.println("服务地址:" + new String(data)); } }}

在这里插入图片描述

四、代码下载

https://github.com/huangliangyun/Spring-Boot-2.X/tree/master/spring-boot-zookeeper

ABOUT

公众号:【星尘Pro】

github:

推荐阅读

转载地址:http://dyfsi.baihongyu.com/

你可能感兴趣的文章
JavaSE_day12 集合
查看>>
JavaSE_day14 集合中的Map集合_键值映射关系
查看>>
Day_15JavaSE 异常
查看>>
异常 Java学习Day_15
查看>>
JavaSE_day_03 方法
查看>>
day-03JavaSE_循环
查看>>
Mysql初始化的命令
查看>>
day_21_0817_Mysql
查看>>
day-22 mysql_SQL 结构化查询语言
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
HTML&CSS进阶
查看>>
Servlet进阶和JSP基础
查看>>
servlet&jsp 的使用以及jsp的历史遗留用法
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
MySQL-分布式架构-MyCAT
查看>>