您好,登錄后才能下訂單哦!
這篇文章主要介紹“nacos address中getCluster的原理和作用是什么”,在日常操作中,相信很多人在nacos address中getCluster的原理和作用是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”nacos address中getCluster的原理和作用是什么”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
本文主要研究一下nacos address的getCluster
nacos-1.1.3/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java
@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))返回結果
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); } 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))返回結果
到此,關于“nacos address中getCluster的原理和作用是什么”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。