前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊nacos address的getCluster

聊聊nacos address的getCluster

作者头像
code4it
发布2019-10-31 22:22:08
4510
发布2019-10-31 22:22:08
举报
文章被收录于专栏:码匠的流水账

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

ServerListController

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

代码语言:javascript
复制
@RestController
public class ServerListController {

    @Autowired
    private ServiceManager serviceManager;

    @Autowired
    private AddressServerGeneratorManager addressServerBuilderManager;

    /**
     * @param product will get Ip list of that products to be associated
     * @param cluster will get Ip list of that product cluster to be associated
     * @return
     */
    @RequestMapping(value = "/{product}/{cluster}", method = RequestMethod.GET)
    public ResponseEntity getCluster(@PathVariable String product,
                                     @PathVariable String cluster) {

        String productName = addressServerBuilderManager.generateProductName(product);
        String serviceName = addressServerBuilderManager.generateNacosServiceName(productName);
        Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName);
        if (service == null) {

            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + product + " not found.");
        }

        if (!service.getClusterMap().containsKey(cluster)) {

            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + product + ",cluster=" + cluster + " not found.");
        }

        Cluster clusterObj = service.getClusterMap().get(cluster);
        return ResponseEntity.status(HttpStatus.OK).body(addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false)));
    }
}
  • getCluster方法首先通过addressServerBuilderManager.generateProductName(product)获取productName,通过addressServerBuilderManager.generateNacosServiceName(productName)获取serviceName
  • 然后通过serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName)获取service,之后判断service.getClusterMap()是否包含cluster,不包含返回404
  • 最后通过service.getClusterMap().get(cluster)获取cluster,然后通过addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false))返回结果

AddressServerGeneratorManager

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

代码语言:javascript
复制
@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);
    }

    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 String generateResponseIps(List<Instance> instanceList) {

        StringBuilder ips = new StringBuilder();
        instanceList.forEach(instance -> {
            ips.append(instance.getIp() + ":" + instance.getPort());
            ips.append("\n");
        });

        return ips.toString();
    }

    //......
}
  • generateProductName方法对于name为空或者是nacos的,返回nacos.as.default,否则返回nacos.as.前缀加name;generateNacosServiceName会返回DEFAULT_GROUP@@加rawServiceName,默认rawServiceName是不包含DEFAULT_GROUP的;generateResponseIps方法将instanceList转为ip及port并用换行拼接的string

小结

  • getCluster方法首先通过addressServerBuilderManager.generateProductName(product)获取productName,通过addressServerBuilderManager.generateNacosServiceName(productName)获取serviceName
  • 然后通过serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName)获取service,之后判断service.getClusterMap()是否包含cluster,不包含返回404
  • 最后通过service.getClusterMap().get(cluster)获取cluster,然后通过addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false))返回结果

doc

  • ServerListController
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-10-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ServerListController
  • AddressServerGeneratorManager
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档