zoukankan      html  css  js  c++  java
  • nacos 重启服务

    服务的临时实例保存在内存中,一旦重启,服务元数据全部丢失。

    server 正常运行

    naming 客户端发送心跳给 server

        // com.alibaba.nacos.client.naming.beat.BeatReactor#addBeatInfo
        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);
            executorService.schedule(new BeatTask(beatInfo), beatInfo.getPeriod(), TimeUnit.MILLISECONDS);
            MetricsMonitor.getDom2BeatSizeMonitor().set(dom2Beat.size());
        }

    server 重启,数据全部丢失,集群模式下,当前 server 启动时从其他 server 同步数据

        // DistroConsistencyServiceImpl#init
        @PostConstruct
        public void init() {
            GlobalExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        load();
                    } catch (Exception e) {
                        Loggers.DISTRO.error("load data failed.", e);
                    }
                }
            });
    
            executor.submit(notifier);
        }
        
        public boolean syncAllDataFromRemote(Server server) {
    
            try {
                byte[] data = NamingProxy.getAllData(server.getKey());
                processData(data);
                return true;
            } catch (Exception e) {
                Loggers.DISTRO.error("sync full data from " + server + " failed!", e);
                return false;
            }
        }

    naming 客户端发送心跳给 server,发现实例不存在,重新注册实例,server 增加服务数据

        public void run() {
            if (beatInfo.isStopped()) {
                return;
            }
            long nextTime = beatInfo.getPeriod();
            try {
                JSONObject result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
                long interval = result.getIntValue("clientBeatInterval");
                boolean lightBeatEnabled = false;
                if (result.containsKey(CommonParams.LIGHT_BEAT_ENABLED)) {
                    lightBeatEnabled = result.getBooleanValue(CommonParams.LIGHT_BEAT_ENABLED);
                }
                BeatReactor.this.lightBeatEnabled = lightBeatEnabled;
                if (interval > 0) {
                    nextTime = interval;
                }
                int code = NamingResponseCode.OK;
                if (result.containsKey(CommonParams.CODE)) {
                    code = result.getIntValue(CommonParams.CODE);
                }
                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 ne) {
                NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}",
                    JSON.toJSONString(beatInfo), ne.getErrCode(), ne.getErrMsg());
    
            }
            executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);
        }

    nacos server 为什么不在本地持久化一份数据呢?

  • 相关阅读:
    mysql 数据库 II(数据类型)
    mysql 数据库 I
    网络协议
    Python 类IV(类成员,异常处理等)
    Python 类III(三大特性,约束,super)
    Python 类II
    类加载机制
    Java新篇章之集合
    Java 类类型之 String 类型
    java 多态
  • 原文地址:https://www.cnblogs.com/allenwas3/p/12187843.html
Copyright © 2011-2022 走看看