【手写RPC框架(五)】整合Zookeeper

一、Zookeeper

Zookeeper是一种分布式程序协调服务,主要功能包括:配置管理名字服务分布式锁集群管理。RPC框架中使用Zookeeper可以更加灵活,不仅可以存储<K,V>结构的数据,也可以存储树状结构的数据,同时Zookeeper相较于Redis支持分布式,当一台节点挂掉时,也可以用其他机器提供服务。

1.1 Zookeeper下载

官网:https://zookeeper.apache.org/releases.html

1.2 Zookeeper安装

ZooKeeper下载安装(Windows版本)

二、使用zookeeper实现注册中心

在使用zookeeper实现注册中心之前,我们采用开源Zookeeper客户端ZKClient,它对ZookeeperAPI进行了封装,实现了超时重连,Watcher反复注册等功能。

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.11</version>
        </dependency>

注意: zkclient中自带的日志的jar包可能会和maven项目自己引入的日志冲突。
解决方法: 排除依赖冲突包。

		<dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.11</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2.1 utils工具类

在【手写RPC框架(四)】utils工具类的基础上,可以增加Zookeeper管理工具,即基于zookeeper实现注册中心。

2.1.1 Zookeeper管理工具类(注册中心)
  1. zookeeper读写数据
public class ZooKeeperUtil {
    private static final Logger logger = LoggerFactory.getLogger(ZooKeeperUtil.class);
    private static final String ZK_ADDRESS = "127.0.0.1:2181";

    private static ZkClient client;

    static {
        client = new ZkClient(ZK_ADDRESS);
    }

    public static void writeData(String path, String data) {
        if (!client.exists(path)) {
            // 创建持久化节点 ,初始化数据
            client.createPersistent(path, true);
        }
        // 修改节点数据,并返回该节点的状态
        client.writeData(path, data, -1);

    }

    public static <T> T readData(String path) {
        // 获取节点数据
        return client.readData(path);
    }
}
  1. RegisterEntity(服务注册中心数据处理)
public class RegisterCenter {
    private static final String ROOT = "/cn/dhu";
    private static final String PROVIDER_PATH = "%s/%s/server";
    private static final String CONSUMER_PATH = "%s/%s/client";

    public static void registerProvider(RpcRegisterEntity entity) {
        ZooKeeperUtil.writeData(String.format(PROVIDER_PATH, ROOT, entity.getInterfaceClassFullName()), JSON.toJSONString(entity));
    }

    public static void registerConsumer(RpcRegisterEntity entity) {
        ZooKeeperUtil.writeData(String.format(CONSUMER_PATH, ROOT, entity.getInterfaceClassFullName()), JSON.toJSONString(entity));
    }

    public static <T> T getProviderData(String path) {
        return ZooKeeperUtil.readData(String.format(PROVIDER_PATH, ROOT, path));
    }

    public static <T> T getConsumerData(String path) {
        return ZooKeeperUtil.readData(String.format(CONSUMER_PATH, ROOT, path));
    }
}

2.2 服务端

服务端启动的时候,扫描service模块,将提供的服务进行服务注册。

RegisterCenter.registerProvider(rpcRegisterEntity);

2.3 客户端

客户端启动,扫描请求服务信息,在代理类的invoke方法中与服务端建立socket连接。因此需要从zookeeper中获取host、port、接口等服务信息。

String serviceObject= RegisterCenter.getProviderData(interfaceName);

注:其它逻辑与前一篇文章基本一致,不再赘述
参考:手写RPC框架(五)整合Zookeeper

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring中整合Zookeeper,你可以按照以下步骤进行操作: 1. 添加Zookeeper依赖:在你的Spring项目的pom.xml文件中添加Zookeeper依赖。例如,使用Apache Curator作为Zookeeper客户端,可以添加以下依赖: ```xml <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> </dependency> ``` 2. 创建Zookeeper客户端:在你的Spring配置文件中,创建一个Zookeeper客户端的Bean,以连接到Zookeeper服务器。你可以配置Zookeeper连接的相关属性,例如Zookeeper服务器的地址、会话超时时间等。 ```java @Bean public CuratorFramework curatorFramework() { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy); curatorFramework.start(); return curatorFramework; } ``` 3. 使用Zookeeper:现在你可以在你的Spring应用程序中使用Zookeeper了。例如,你可以创建一个Zookeeper节点、读取节点数据或监视节点的变化。 ```java @Autowired private CuratorFramework curatorFramework; public void createNode(String path, byte[] data) throws Exception { curatorFramework.create().creatingParentsIfNeeded().forPath(path, data); } public byte[] getNodeData(String path) throws Exception { return curatorFramework.getData().forPath(path); } public void watchNode(String path) throws Exception { NodeCache nodeCache = new NodeCache(curatorFramework, path); nodeCache.start(); nodeCache.getListenable().addListener(() -> { ChildData childData = nodeCache.getCurrentData(); if (childData != null) { System.out.println("Node data changed: " + new String(childData.getData())); } }); } ``` 这样,你就可以在Spring中实现与Zookeeper的集成。通过Curator Framework提供的API,可以方便地操作Zookeeper节点,并响应节点数据的变化。记得根据你项目的实际需要进行相应的配置和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值