自定义服务的服务注册- zookeeper

自定义服务的服务注册- zookeeper

服务注册使用 zookeeper 的大致过程如下

  1. 链接 zookeeper 集群或者单机
  2. 注册自己的 ip + 端口到 zookeeper 的 node(这个 node 要是临时节点的,因为可以保证健康检查以及服务发现的高可用

编码实现

java 复制代码
package com.rpc.custom_rpc.registery;

import com.rpc.custom_rpc.config.CustomRpcConfig;
import com.rpc.pre.zookeeper.curator.ZkCuratorFrameworkServerOps;
import org.apache.zookeeper.CreateMode;

import java.nio.charset.StandardCharsets;

/**
 * @author xl-9527
 * @since 2024/12/14
 **/
public class ServerRegistryImpl implements ServerRegistry {
    private final String ip;
    private final int port;
    private final ZkCuratorFrameworkServerOps zkCuratorFrameworkServerOps;

    public ServerRegistryImpl(final String ip, final int port) {
        this.ip = ip;
        this.port = port;
        this.zkCuratorFrameworkServerOps = new ZkCuratorFrameworkServerOps();
    }

    @Override
    public void registry(final String serviceName) {
        // 基础的路径注册为永久节点
        final String path = CustomRpcConfig.BASE_PATH + serviceName;
        if (!zkCuratorFrameworkServerOps.exists(path)) {
            zkCuratorFrameworkServerOps.createNode(path, null, true);
        }

        // 创建临时节点,这其实就是我们的服务的元数据信息
        zkCuratorFrameworkServerOps.createNode(path + "/" + ip + ":" + port, (ip + ":" + port).getBytes(StandardCharsets.UTF_8), CreateMode.EPHEMERAL);
    }
}
相关推荐
Chan162 小时前
微服务 - Higress网关
java·spring boot·微服务·云原生·面试·架构·intellij-idea
Java 码农3 小时前
RabbitMQ集群部署方案及配置指南09
分布式·rabbitmq
u0104058363 小时前
基于 Kafka Exactly-Once 语义保障微信群发消息不重复不丢失
分布式·kafka·linq
没有bug.的程序员3 小时前
Serverless 架构深度解析:FaaS/BaaS、冷启动困境与场景适配指南
云原生·架构·serverless·架构设计·冷启动·baas·faas
超级种码3 小时前
Kafka四部曲之二:核心架构与设计深度解析
分布式·架构·kafka
optimistic_chen3 小时前
【Redis 系列】持久化特性
linux·数据库·redis·分布式·中间件·持久化
论迹3 小时前
RabbitMQ
分布式·rabbitmq
Java 码农3 小时前
RabbitMQ集群部署方案及配置指南08--电商业务延迟队列定制化方案
大数据·分布式·rabbitmq
CodeAmaz4 小时前
分布式 ID 方案(详细版)
分布式·分布式id
悟空码字4 小时前
SpringBoot整合Zookeeper,实现分布式集群部署
java·zookeeper·springboot·编程技术·后端开发