• Nacos源码深度解析2-服务注册(客户端)


    一.服务注册
    naming.registerInstance("nacos.test.3", "10.0.4.221", 8081, "TEST1");
     
    二.参数解析
    @Override
    public void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName)
            throws NacosException {
    
        Instance instance = new Instance();
        instance.setIp(ip);
        instance.setPort(port);
        instance.setWeight(1.0);
        instance.setClusterName(clusterName);
    
        registerInstance(serviceName, groupName, instance);
    }
     
    三.注册实例
    @Override
    public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
        String groupedServiceName = NamingUtils.getGroupedName(serviceName, groupName);
        //是否是临时实例
        if (instance.isEphemeral()) {
            //构建心跳实例beatinfo,Period = 5秒
            BeatInfo beatInfo = beatReactor.buildBeatInfo(groupedServiceName, instance);
            beatReactor.addBeatInfo(groupedServiceName, beatInfo);
        }
        //向服务端注册请求
        serverProxy.registerService(groupedServiceName, groupName, instance);
    }
     
    四.添加心跳信息
     
    /**
     * Add beat information.
     *
     * @param serviceName service name
     * @param beatInfo    beat information
     */
    public void addBeatInfo(String serviceName, BeatInfo beatInfo) {
        NAMING_LOGGER.info("[BEAT] adding beat: {} to beat map.", beatInfo);
        String key = buildKey(serviceName, beatInfo.getIp(), beatInfo.getPort());
        BeatInfo existBeat = null;
        //fix #1733
        if ((existBeat = dom2Beat.remove(key)) != null) {
            existBeat.setStopped(true);
        }
        dom2Beat.put(key, beatInfo);
        //构建心跳任务,放入线程池中,5秒
        executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS);
        MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());
    }
    五.执行心跳任务

    class BeatTask implements Runnable {
    
        BeatInfo beatInfo;
    
        public BeatTask(BeatInfo beatInfo) {
            this.beatInfo = beatInfo;
        }
    
        @Override
        public void run() {
            if (beatInfo.isStopped()) {
                return;
            }
            //默认5秒
            long nextTime = beatInfo.getPeriod();
            try {
                //发送心跳
                JsonNode result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
                long interval = result.get("clientBeatInterval").asLong();
                boolean lightBeatEnabled = false;
                if (result.has(CommonParams.LIGHT_BEAT_ENABLED)) {
                    lightBeatEnabled = result.get(CommonParams.LIGHT_BEAT_ENABLED).asBoolean();
                }
                BeatReactor.this.lightBeatEnabled = lightBeatEnabled;
                if (interval > 0) {
                    nextTime = interval;
                }
                int code = NamingResponseCode.OK;
                if (result.has(CommonParams.CODE)) {
                    code = result.get(CommonParams.CODE).asInt();
                }
                if (code == NamingResponseCode.RESOURCE_NOT_FOUND) {
                    Instance instance = new Instance();
                    instance.setPort(beatInfo.getPort());
                    instance.setIp(beatInfo.getIp());
                    instance.setWeight(beatInfo.getWeight());
                    instance.setMetadata(beatInfo.getMetadata());
                    instance.setClusterName(beatInfo.getCluster());
                    instance.setServiceName(beatInfo.getServiceName());
                    instance.setInstanceId(instance.getInstanceId());
                    instance.setEphemeral(true);
                    try {
                        //资源不存在,重新注册服务
                        serverProxy.registerService(beatInfo.getServiceName(),
                                NamingUtils.getGroupName(beatInfo.getServiceName()), instance);
                    } catch (Exception ignore) {
                    }
                }
            } catch (NacosException ex) {
                NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}",
                        JacksonUtils.toJson(beatInfo), ex.getErrCode(), ex.getErrMsg());
    
            }
            //nextTime后再次执行
            executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);
        }
    }

  • 相关阅读:
    python 文件批量改名重命名 rename
    AN INTEGER FORMULA FOR FIBONACCI NUMBERS
    python opencv cv2 imshow threading 多线程
    adb shell 查看当前与用户交互的 activity
    sublime text build system automatic ctrl/cmd+B自动选择 python2 或 python3
    pip 源的问题
    Mac 安装 Android commandlinetools 各种报错的问题
    GIMP 一键均匀添加多条参考线 一键均匀切分图片
    Shell 脚本如何输出帮助信息?
    raspberry pi 配置
  • 原文地址:https://www.cnblogs.com/xwzp/p/14519235.html
Copyright © 2020-2023  润新知