专栏首页码匠的流水账聊聊nacos address的postCluster

聊聊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;
    }

    //......
}
  • postCluster方法接收product、cluster、ips参数;其中productName通过addressServerGeneratorManager.generateProductName(product)生成;clusterName通过addressServerManager.getDefaultClusterNameIfEmpty(cluster)生成
  • 之后通过通过addressServerGeneratorManager.generateNacosServiceName(productName)生成serviceName,然后通过serviceManager.createServiceIfAbsent创建service
  • 之后校验下ip地址,校验通过的话,会通过addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray)生成instanceList,然后遍历instanceList执行naming模块中的serviceManager的registerInstance方法

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;
    }

    //......
}
  • AddressServerGeneratorManager的generateProductName方法对于name为空或者是nacos的,返回nacos.as.default,否则返回nacos.as.前缀加name
  • generateNacosServiceName会返回DEFAULT_GROUP@@加rawServiceName,默认rawServiceName是不包含DEFAULT_GROUP的
  • generateInstancesByIps方法遍历ipArray,挨个创建instance,注意这里设置了ephemeral为false

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);
    }
      
    //......
}
  • getRawProductName方法对于name为空或者是name已经是nacos的返回nacos,否则返回原值;getRawClusterName内部调用的是getDefaultClusterNameIfEmpty方法,它对于name是空或者name已经是serverlist的返回serverlist,否则返回原值;splitIps方法根据AddressServerConstants.MULTI_IPS_SEPARATOR来分割字符串为数组

小结

  • postCluster方法接收product、cluster、ips参数;其中productName通过addressServerGeneratorManager.generateProductName(product)生成;clusterName通过addressServerManager.getDefaultClusterNameIfEmpty(cluster)生成
  • 之后通过通过addressServerGeneratorManager.generateNacosServiceName(productName)生成serviceName,然后通过serviceManager.createServiceIfAbsent创建service
  • 之后校验下ip地址,校验通过的话,会通过addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray)生成instanceList,然后遍历instanceList执行naming模块中的serviceManager的registerInstance方法

doc

  • AddressServerClusterController

本文分享自微信公众号 - 码匠的流水账(geek_luandun)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-10-26

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Netty整理(三) 顶

    JDK ByteBuffer 共用读写索引,每次读写操作都需要Flip() 扩容麻烦,而且扩...

    算法之名
  • 怎样成为一个优秀的架构师?

    架构师是一个既能掌控整体又能洞悉局部瓶颈并依据具体的业务场景给出解决方案的团队领导型人物。看似完美的“人格模型”背后,是艰辛的探索。

    一个会写诗的程序员
  • JAVA基础复习(重点)

    (2)一个.java源文件中可以声明多个类,但是只能有一个public修饰的类,文件名要和public修饰的类名保持一致;

    阮键
  • JavaSE笔记

    (2)一个.java源文件中可以声明多个类,但是只能有一个public修饰的类,文件名要和public修饰的类名保持一致;

    阮键
  • 2019 金九银十多家BAT大厂Android面试真题锦集干货整理

    又到了金九银十的面试季,自己也不得不参与到这场战役中来,其实是从去年底就开始看,android的好机会确实不太多,但也还好,3年+的android开发经历还是有...

    Android技术干货分享
  • zookeeper特性与节点说明

    这些问题可以统一归纳为多节点协调问题,如果靠节点自身进行协调这是非常不可靠的,性能上也不可取。必须由一个独立的服务做协调工作,它必须可靠,而且保证性能。

    程序员小强
  • leetcode478. Generate Random Point in a Circle

    假设现在已知圆的圆心的x和y坐标,以及该圆的半径radius。要求写一个随机点生成器,要求该生成器生成的点必须在圆内,且每一个点被生成的概率为相等的。规定圆周上...

    眯眯眼的猫头鹰
  • 学生怎么入门Android?这四点很重要!

    既然大家已决定学Android,相信你对Android已有些了解,知道应该学习什么语言,这里我就不细说了,Android首先要学的是Java se,也就是JAV...

    Android技术干货分享
  • Python | Python学习之多线程详解

    python的threading模块是对thread做了一些包装的,可以更加方便的被使用,线程的方法和进程的基本相似,这里就不多赘述,下面举几个栗子:

    咸鱼学Python
  • 460道Java后端面试高频题答案版【模块十:SpringMVC】

    对于 SpringMVC 并没有太多的学习经验分享给大家,因为我是通过某培训机构的视频学习的 SpringMVC。我个人认为 SpringMVC 的高频面试点有...

    乔戈里

扫码关注云+社区

领取腾讯云代金券