Nacos1.3.2 Spring Cloud k8s灰度下线

/**
     * nacos服务主动下线

     *   

  * 主动下线方法中,1调nacos实例下线(no run),2.调nacos的超时(仅在springcloud服务中能*    和),
*     最后选择用2

     */
    public boolean nacosDown(Map map) {
        log.info("nacosDown deregister from nacos, serviceName:{}, hostUrl:{}, namespace:{} ", finalServiceName, hostUrl, namespace);

//        String finalServiceName = "DEFAULT_GROUP@@" + serviceName;
//        final String finalServiceName = serviceName;
        new Thread(
                () -> {
                    try {
                        //由于它会比terminationGracePeriodSeconds先执行,所以如果立刻下线的话新的pods还没开好的话,
                        //应用会卡10秒左右, 所以预留60秒的可用时间
                        Thread.sleep(waitTime * 1000);

                        Properties properties = new Properties();
                        properties.setProperty("serverAddr", hostUrl);
//                        properties.setProperty("namespace", namespace);
//
                        NamingService naming = NamingFactory.createNamingService(properties);

//        naming.registerInstance("nacos.test.3", "11.11.11.11", 8888, "TEST1");

//                        naming.deregisterInstance(finalServiceName, ip, port);
//                        List<Instance> instances = naming.selectInstances(finalServiceName, true);
                        //有bug,已提交gitbug issure #4819
                        //https://github.com/alibaba/nacos/issues/4819
//                        naming.deregisterInstance(finalServiceName, ip, port, clusterName);

                        //只能修改心跳周期了,改成1秒, 尽量延迟最低
                        Instance instance = new Instance();
                        instance.setIp(ip);
                        instance.setPort(port);
                        Map<String, String> metadata = new HashMap<String, String>();
                        // 设置心跳的周期,单位为秒,这里将心跳间隔设置为3秒:
                        metadata.put(PreservedMetadataKeys.HEART_BEAT_INTERVAL, "1000");
                        // 设置心跳超时时间,单位为秒,这里将心跳超时时间设为500毫秒
                        // 即服务端6秒收不到客户端心跳,会将该客户端注册的实例设为不健康:
                        metadata.put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, "500");
                        // 设置实例删除的超时时间,单位为秒,这里将实例删除超时时间设为500毫秒,
                        // 即服务端9秒收不到客户端心跳,会将该客户端注册的实例删除:
                        metadata.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT, "500");
                        instance.setMetadata(metadata);
                        naming.registerInstance(finalServiceName, instance);


                        log.info("sucess deregister from nacos, serviceName:{}, clusterName:{}, ip:{}, port:{}", finalServiceName, clusterName, ip, port);
                        log.info("sucess deregister instants size : {} ", naming.getAllInstances(finalServiceName).size());
                        log.info("sucess deregister instants size : {} ", naming.selectInstances(finalServiceName, false).size());
//                        naming.shutDown();
                    } catch (NacosException e) {
                        log.info("error deregister from nacos, serviceName:{}, clusterName:{}, ip:{}, port:{}", finalServiceName, clusterName, ip, port);
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        log.info("error thread deregister from nacos, serviceName:{}, clusterName:{}, ip:{}, port:{}", finalServiceName, clusterName, ip, port);
                        e.printStackTrace();
                    }
                }
        ).start();

        log.info("deregister from nacos, serviceName:{}, clusterName:{}, ip:{}, port:{}", finalServiceName, clusterName, ip, port);

        return true;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

折腾数据折腾代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值