Nacos2.0.3 Dubbo k8s灰度下线

Nacos2.0.3 Dubbo k8s灰度下线,第1张

Nacos2.0.3 Dubbo k8s灰度下线
private int waitTime = 8;                      //等待几秒 todo
//    private String nacosmetaUrl = "http://10.18.225.115:8848/nacos/v1/ns/instance/metadata/batch
    private String nacosmetaProt = "http://";
    private String nacosmetaHttpProt = "PUT";
    private String nacosmetaUrl = "/nacos/v1/ns/instance/metadata/batch";

    @Autowired
    NacosDiscoveryProperties nacosDiscoveryProperties;

    
    public boolean nacosDown(Map map) {

        String serviceName = nacosDiscoveryProperties.getService();
        String groupName = nacosDiscoveryProperties.getGroup();
        String clusterName = nacosDiscoveryProperties.getClusterName();
        String server = nacosDiscoveryProperties.getServerAddr();
        String ip = nacosDiscoveryProperties.getIp();
        int port = nacosDiscoveryProperties.getPort();
        log.info("nacosDown deregister from nacos, serviceName:{}, groupName:{}, clusterName:{}, server:{}, ip:{}, port:{} ", serviceName, groupName, clusterName, server, ip, port);

        try {
            //由于它会比terminationGracePeriodSeconds先执行,所以如果立刻下线的话新的pods还没开好的话,

            //a. 先让nacos服务主动下线,
            DubboShutdownHook.destroyAll();

            //b. 然后等待一段时间再关闭容器(尽量是5秒以上),
            Thread.sleep(waitTime * 1000);

            //c. 设置nacos的心跳超时(可选)
            nacosSetTimeout(nacosDiscoveryProperties);

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

        } catch (Exception e) {
            e.printStackTrace();
        }


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

        return true;
    }


    
    private void nacosSetTimeout(NacosDiscoveryProperties nacosDiscoveryProperties) {
        String serviceName = nacosDiscoveryProperties.getService();
        String clusterName = nacosDiscoveryProperties.getClusterName();
        String server = nacosDiscoveryProperties.getServerAddr();
        String ip = nacosDiscoveryProperties.getIp();
        int port = nacosDiscoveryProperties.getPort();
        String nameSpace = nacosDiscoveryProperties.getNamespace();

        String[] split = server.split(",");
        JSonObject variables = new JSonObject();
        variables.put("namespaceId", nameSpace);
        variables.put("serviceName", serviceName);

        JSonArray instantsList = new JSonArray();   //实例ip
        JSonObject instantsOne = new JSonObject();
        instantsOne.put("ip", ip);
        instantsOne.put("port", port);
        instantsOne.put("clusterName", clusterName);
        instantsList.add(instantsOne);
        variables.put("instances", instantsList.toJSonString());

        JSonObject metaDataOne = new JSonObject();
        metaDataOne.put(PreservedmetadataKeys.HEART_BEAT_INTERVAL, "1000");
        // 设置心跳超时时间,单位为秒,这里将心跳超时时间设为500毫秒
        // 即服务端6秒收不到客户端心跳,会将该客户端注册的实例设为不健康:
        metaDataOne.put(PreservedmetadataKeys.HEART_BEAT_TIMEOUT, "500");
        // 设置实例删除的超时时间,单位为秒,这里将实例删除超时时间设为500毫秒,
        // 即服务端9秒收不到客户端心跳,会将该客户端注册的实例删除:
        metaDataOne.put(PreservedmetadataKeys.IP_DELETE_TIMEOUT, "500");
        variables.put("metadata", metaDataOne.toJSonString());

        nacosmetaUrl = nacosmetaProt + split[0] + nacosmetaUrl;
//        if (variables != null) {
//            nacosmetaUrl = nacosmetaUrl + "?" + asUrlVariables(variables);
//        }

        log.info("nacosDown rest , nacosmetaUrl:{} , variables: {}", nacosmetaUrl, asUrlVariables(variables));
//        JSonObject result = RestUtil.put(nacosmetaUrl, variables);
        String result = RestUtil(nacosmetaUrl, variables, nacosmetaHttpProt);
        log.info("nacosDown rest ok , nacosmetaUrl:{}, result:{} ", nacosmetaUrl, result);
    }

    
    public static String RestUtil(String url, JSonObject variables, String METHOD) {

//        String parm = ""[\"default\",\"ASDSFGSDGRRGSSSSS\"]"";
        String[] cmds = {"curl", "-X", METHOD, url, "-d", " " + asUrlVariables(variables)};
        log.info("nacosDown rest , rest : {}, ", cmds.toString());
        ProcessBuilder process = new ProcessBuilder(cmds);
        StringBuilder builder;
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

    public static String asUrlVariables(JSonObject variables) {

        Map source = variables.getInnerMap();
        Iterator it = source.keySet().iterator();

        StringBuilder urlVariables;
        String key;
        String value;
        for(urlVariables = new StringBuilder(); it.hasNext(); urlVariables.append("&").append(key).append("=").append(value)) {
            key = (String)it.next();
            value = "";
            Object object = source.get(key);
            if (object != null && !StrUtil.isEmpty(object.toString())) {
                value = object.toString();
            }
        }

        return urlVariables.substring(1);
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5696026.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存