zookeeper用做注册中心(服务的注册与发现)

源于蚂蚁课堂的学习,点击这里查看(老余很给力)

1.注册

package live.yanxiaohui.server;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @Description 服务注册
 * @Author: yanxh<br>
 * @Date 2020-05-06 16:15<br>
 * @Version 1.0<br>
 */
@Component
public class OrderServer implements ApplicationRunner {

    @Value("${name}")
    private String name;

    @Value("${zk.url}")
    private String url;

    @Value("${server.port}")
    private String port;

    // 超时时间
    private int TIME_OUT = 50000;


    /**
     * 启动springboot时做的事情
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        String path = "/my_servers";
        ZooKeeper zooKeeper = new ZooKeeper(url, TIME_OUT, a -> System.out.println(a.getState().name()));
        Stat exists = zooKeeper.exists(path, a -> System.out.println(a.getState().name()));
        String data = "http://127.0.0.1:" + port + "/" + name;
        if (exists == null) {
            // 不存在父节点
            zooKeeper.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zooKeeper.create(path  + "/" + port, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        } else {
            exists = zooKeeper.exists(path  + "/" + port, a -> System.out.println(a.getState().name()));
            if (exists == null) {
                zooKeeper.create(path  + "/" + port, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            }else{
                System.out.println("节点已存在");
            }
        }
    }
}

2.发现 

package live.yanxiaohui.server;

import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Value;

import java.util.concurrent.CountDownLatch;

/**
 * @Description 服务发现
 * @Author: yanxh<br>
 * @Date 2020-05-06 17:26<br>
 * @Version 1.0<br>
 */
public class FindServer {

    private static final String url = "127.0.0.1:2181";

    private static final CountDownLatch countDownLatch = new CountDownLatch(1);


    // 超时时间
    private static final int TIME_OUT = 50000;

    public static void main(String[] args) throws Exception {
        String path = "/my_servers/";
        ZooKeeper zooKeeper = new ZooKeeper(url, TIME_OUT, a -> {
            Watcher.Event.KeeperState state = a.getState();
            // 如果当前连接成功,则开始放心
            if (state == Watcher.Event.KeeperState.SyncConnected) {
                System.out.println("zk连接成功~~");
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();

        for (String children : zooKeeper.getChildren(path, null, new Stat())) {
            String childPath = path + children;
            byte[] datas = zooKeeper.getData(childPath, null, new Stat());
            System.out.println("服务名称:" + new String(datas));
        }
    }
}

3.启动

配置文件application.yml
server:
  port: 8080
name: order_service
zk:
  url: 127.0.0.1:2181


启动后,变更配置再启动
server:
  port: 8081
name: prod_service
zk:
  url: 127.0.0.1:2181

启动后,变更配置再启动
server:
  port: 8082
name: user_service
zk:
  url: 127.0.0.1:2181

 通过客户端插件可以看出有三个服务已注册至zookeeper中

 

 

此客户端连接工具可在我上传的资源中仅进行下载 

启动服务发现的程序,可以遍历ZK中所有的服务

欢迎大家和帝都的雁积极互动,头脑交流会比个人埋头苦学更有效!共勉!

公众号:帝都的雁

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值