自定义服务的服务注册- 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);
    }
}
相关推荐
一条大祥脚11 小时前
【CS336】lecture7 多卡|通信架构|集合通信|NCCL|通信原语|torch.distributed
分布式·架构·nccl·多机多卡·集合通信
程序员梅雨13 小时前
ZooKeeper面经总结
java·大数据·linux·分布式·后端·zookeeper·中间件
张洛闻Eren13 小时前
Ansible【第一课】:Ansible介绍
运维·数据库·云原生·kubernetes·github·ansible
我是人✓13 小时前
Git入门:从零开始掌握分布式版本控制
分布式·git
杰克尼14 小时前
Kubernetes
云原生·容器·kubernetes
码上上班15 小时前
基于 K3s 的微服务容器化与 GitOps 持续交付平台
微服务·云原生·架构
张洛闻Eren15 小时前
Ansible【第二课】:实施 Ansible Playbook
linux·运维·服务器·云原生·kubernetes·k8s·ansible
做运维的阿瑞16 小时前
Terraform 入门实战:安装与实施
云原生·云计算·terraform
guo_wen_qiang17 小时前
redis持久化
数据库·redis·分布式
空空dream17 小时前
06 | Prometheus打通FastAPI云原生数据采集
云原生·prometheus·fastapi