专栏首页码匠的流水账聊聊NacosNamingService的deregisterInstance
原创

聊聊NacosNamingService的deregisterInstance

本文主要研究一下NacosNamingService的deregisterInstance

NacosNamingService

nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java

public class NacosNamingService implements NamingService {
    private static final String DEFAULT_PORT = "8080";
    private static final long DEFAULT_HEART_BEAT_INTERVAL = TimeUnit.SECONDS.toMillis(5);
​
    /**
     * Each Naming instance should have different namespace.
     */
    private String namespace;
​
    private String endpoint;
​
    private String serverList;
​
    private String cacheDir;
​
    private String logName;
​
    private HostReactor hostReactor;
​
    private BeatReactor beatReactor;
​
    private EventDispatcher eventDispatcher;
​
    private NamingProxy serverProxy;
​
    //......
​
    @Override
    public void deregisterInstance(String serviceName, String ip, int port) throws NacosException {
        deregisterInstance(serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME);
    }
​
    @Override
    public void deregisterInstance(String serviceName, String groupName, String ip, int port) throws NacosException {
        deregisterInstance(serviceName, groupName, ip, port, Constants.DEFAULT_CLUSTER_NAME);
    }
​
    @Override
    public void deregisterInstance(String serviceName, String ip, int port, String clusterName) throws NacosException {
        deregisterInstance(serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName);
    }
​
    @Override
    public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException {
        Instance instance = new Instance();
        instance.setIp(ip);
        instance.setPort(port);
        instance.setClusterName(clusterName);
​
        deregisterInstance(serviceName, groupName, instance);
    }
​
    @Override
    public void deregisterInstance(String serviceName, Instance instance) throws NacosException {
        deregisterInstance(serviceName, Constants.DEFAULT_GROUP, instance);
    }
​
    @Override
    public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException {
        if (instance.isEphemeral()) {
            beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort());
        }
        serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), instance);
    }
​
    //......
}
  • deregisterInstance方法对于ephemeral的instance(默认是ephemeral)会将其BeatInfo从beatReactor中移除,然后通过serverProxy.deregisterService进行服务注销

BeatReactor

nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java

public class BeatReactor {
​
    private ScheduledExecutorService executorService;
​
    private NamingProxy serverProxy;
​
    public final Map<String, BeatInfo> dom2Beat = new ConcurrentHashMap<String, BeatInfo>();
​
    //......
​
    public void removeBeatInfo(String serviceName, String ip, int port) {
        NAMING_LOGGER.info("[BEAT] removing beat: {}:{}:{} from beat map.", serviceName, ip, port);
        BeatInfo beatInfo = dom2Beat.remove(buildKey(serviceName, ip, port));
        if (beatInfo == null) {
            return;
        }
        beatInfo.setStopped(true);
        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;
            }
            long result = serverProxy.sendBeat(beatInfo);
            long nextTime = result > 0 ? result : beatInfo.getPeriod();
            executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);
        }
    }
​
    //......
}
  • removeBeatInfo方法则将指定key从dom2Beat移除,标记其stopped为true;BeatTask则会先判断是否为stopped,否的话则通过serverProxy.sendBeat发送心跳信息,计算下一次调度的时间,往executorService添加调度任务

NamingProxy

nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java

public class NamingProxy {
​
    private static final int DEFAULT_SERVER_PORT = 8848;
​
    private int serverPort = DEFAULT_SERVER_PORT;
​
    private String namespaceId;
​
    private String endpoint;
​
    private String nacosDomain;
​
    private List<String> serverList;
​
    private List<String> serversFromEndpoint = new ArrayList<String>();
​
    private long lastSrvRefTime = 0L;
​
    private long vipSrvRefInterMillis = TimeUnit.SECONDS.toMillis(30);
​
    private Properties properties;
​
    //......
​
    public void deregisterService(String serviceName, Instance instance) throws NacosException {
​
        NAMING_LOGGER.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}",
            namespaceId, serviceName, instance);
​
        final Map<String, String> params = new HashMap<String, String>(8);
        params.put(CommonParams.NAMESPACE_ID, namespaceId);
        params.put(CommonParams.SERVICE_NAME, serviceName);
        params.put(CommonParams.CLUSTER_NAME, instance.getClusterName());
        params.put("ip", instance.getIp());
        params.put("port", String.valueOf(instance.getPort()));
        params.put("ephemeral", String.valueOf(instance.isEphemeral()));
​
        reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE);
    }
​
    //......
}
  • NamingProxy的deregisterService方法会往/instance接口发送DELETE请求进行服务注销

小结

NacosNamingService的deregisterInstance方法对于ephemeral的instance(默认是ephemeral)会将其BeatInfo从beatReactor中移除,然后通过serverProxy.deregisterService进行服务注销

doc

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊NacosNamingService的deregisterInstance

    本文主要研究一下NacosNamingService的deregisterInstance

    codecraft
  • springboot2上报metrics到statsd

    micrometer-registry-statsd-1.0.1-sources.jar!/io/micrometer/statsd/StatsdFlavor....

    codecraft
  • 聊聊rocketmq的AclClientRPCHook

    rocketmq-remoting-4.5.2-sources.jar!/org/apache/rocketmq/remoting/RPCHook.java

    codecraft
  • 聊聊NacosNamingService的deregisterInstance

    本文主要研究一下NacosNamingService的deregisterInstance

    codecraft
  • Java基础笔记之String相关知识

    我的理解:比如说 String myName = "Cherish";这个Cherish字符串一旦被创建,就会存到内存上某个地址如 0x58 处,然后这个字...

    CherishTheYouth
  • RestAssured| 3.快速搭建验证Web Server

    测试邦
  • JAVA实现的微信扫描二维码支付

    吐槽一下 支付项目采用springMvc+Dubbo架构实现,只对外提供接口。 话说,为什么微信支付比支付宝来的晚了那么一点,一句话,那一阵挺忙的,然后就没有时...

    小柒2012
  • 聊聊rocketmq的AclClientRPCHook

    rocketmq-remoting-4.5.2-sources.jar!/org/apache/rocketmq/remoting/RPCHook.java

    codecraft
  • 来了来了!Chrome 高级玩法,秒变摸鱼神器

    Chrome 作为地球上最受欢迎的浏览器,将它的“本职工作”做到了极致,但是却因自带功能太少而被大家诟病。

    用户1516716
  • 面试不是无情物,参数注解知多少?

    前两天面试的,面试官问我用在参数上的注解有哪些?我想了一下说有RequestParam,每个参数都需要RequestParam修饰,可以设置required 等...

    程序员爱酸奶

扫码关注云+社区

领取腾讯云代金券