nacos address中postCluster的原理及作用是什么

发布时间:2021-06-24 10:07:01 作者:chen
来源:亿速云 阅读:190

本篇内容介绍了“nacos address中postCluster的原理及作用是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

本文主要研究一下nacos address的postCluster

AddressServerClusterController

nacos-1.1.3/address/src/main/java/com/alibaba/nacos/address/controller/AddressServerClusterController.java

@RestController
@RequestMapping({AddressServerConstants.ADDRESS_SERVER_REQUEST_URL + "/nodes"})
public class AddressServerClusterController {

    @Autowired
    private ServiceManager serviceManager;

    @Autowired
    private AddressServerManager addressServerManager;

    @Autowired
    private AddressServerGeneratorManager addressServerGeneratorManager;

    /**
     * @param product Ip list of products to be associated
     * @param cluster Ip list of product cluster to be associated
     * @param ips     will post ip list.
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity postCluster(@RequestParam(required = false) String product,
                                      @RequestParam(required = false) String cluster,
                                      @RequestParam(name = "ips") String ips) {

        //1. prepare the storage name for product and cluster
        String productName = addressServerGeneratorManager.generateProductName(product);
        String clusterName = addressServerManager.getDefaultClusterNameIfEmpty(cluster);

        //2. prepare the response name for product and cluster to client
        String rawProductName = addressServerManager.getRawProductName(product);
        String rawClusterName = addressServerManager.getRawClusterName(cluster);
        Loggers.addressLogger.info("put cluster node,the cluster name is " + cluster + "; the product name=" + product + "; the ip list=" + ips);
        ResponseEntity responseEntity;
        try {
            String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);

            Cluster clusterObj = new Cluster();
            clusterObj.setName(clusterName);
            clusterObj.setHealthChecker(new AbstractHealthChecker.None());
            serviceManager.createServiceIfAbsent(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, clusterObj);
            String[] ipArray = addressServerManager.splitIps(ips);
            String checkResult = AddressServerParamCheckUtil.checkIps(ipArray);
            if (AddressServerParamCheckUtil.CHECK_OK.equals(checkResult)) {
                List<Instance> instanceList = addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
                for (Instance instance : instanceList) {
                    serviceManager.registerInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, instance);
                }
                responseEntity = ResponseEntity.ok("product=" + rawProductName + ",cluster=" + rawClusterName + "; put success with size=" + instanceList.size());
            } else {
                responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
            }
        } catch (Exception e) {
            responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }

        return responseEntity;
    }

    //......
}

AddressServerGeneratorManager

nacos-1.1.3/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java

@Component
public class AddressServerGeneratorManager {

    public String generateProductName(String name) {

        if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) {

            return AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME;
        }

        return String.format(AddressServerConstants.ALIWARE_NACOS_PRODUCT_DOM_TEMPLATE, name);
    }

    /**
     * @param rawServiceName the raw service name will not contains the {@Constans.DEFAULT_GROUP}
     * @return the nacos service name
     */
    public String generateNacosServiceName(String rawServiceName) {

        if (rawServiceName.indexOf(Constants.DEFAULT_GROUP) != -1) {
            return rawServiceName;
        }

        return Constants.DEFAULT_GROUP + AddressServerConstants.GROUP_SERVICE_NAME_SEP + rawServiceName;
    }

    public List<Instance> generateInstancesByIps(String serviceName, String rawProductName, String clusterName, String[] ipArray) {
        if (StringUtils.isEmpty(serviceName)
            || StringUtils.isEmpty(clusterName)
            || ipArray == null || ipArray.length == 0) {
            return Collections.emptyList();
        }

        List<Instance> instanceList = new ArrayList<>(ipArray.length);
        for (String ip : ipArray) {
            String[] ipAndPort = generateIpAndPort(ip);
            Instance instance = new Instance();
            instance.setIp(ipAndPort[0]);
            instance.setPort(Integer.valueOf(ipAndPort[1]));
            instance.setClusterName(clusterName);
            instance.setServiceName(serviceName);
            instance.setTenant(Constants.DEFAULT_NAMESPACE_ID);
            instance.setApp(rawProductName);
            instance.setEphemeral(false);
            instanceList.add(instance);
        }

        return instanceList;
    }

    //......
}

AddressServerManager

nacos-1.1.3/address/src/main/java/com/alibaba/nacos/address/component/AddressServerManager.java

@Component
public class AddressServerManager {

    public String getRawProductName(String name) {

        if (StringUtils.isBlank(name) || AddressServerConstants.DEFAULT_PRODUCT.equals(name)) {

            return AddressServerConstants.DEFAULT_PRODUCT;
        }

        return name;
    }

    public String getRawClusterName(String name) {

        return getDefaultClusterNameIfEmpty(name);
    }

    public String getDefaultClusterNameIfEmpty(String name) {

        if (StringUtils.isEmpty(name) || AddressServerConstants.DEFAULT_GET_CLUSTER.equals(name)) {
            return AddressServerConstants.DEFAULT_GET_CLUSTER;
        }

        return name;
    }

    public String[] splitIps(String ips) {

        if (StringUtils.isBlank(ips)) {

            return new String[0];
        }

        return ips.split(AddressServerConstants.MULTI_IPS_SEPARATOR);
    }
       
    //......
}

小结

“nacos address中postCluster的原理及作用是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. 如何使用Nacos中的Optional
  2. Nacos的作用是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nacos

上一篇:asp.net如何制作页脚

下一篇:Linux多进程和多线程的一次gdb调试

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》