您好,登錄后才能下訂單哦!
這篇文章主要介紹“nacos address中g(shù)etCluster的原理和作用是什么”,在日常操作中,相信很多人在nacos address中g(shù)etCluster的原理和作用是什么問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”nacos address中g(shù)etCluster的原理和作用是什么”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
本文主要研究一下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方法首先通過(guò)addressServerBuilderManager.generateProductName(product)獲取productName,通過(guò)addressServerBuilderManager.generateNacosServiceName(productName)獲取serviceName
然后通過(guò)serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName)獲取service,之后判斷service.getClusterMap()是否包含cluster,不包含返回404
最后通過(guò)service.getClusterMap().get(cluster)獲取cluster,然后通過(guò)addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false))返回結(jié)果
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方法對(duì)于name為空或者是nacos的,返回nacos.as.default,否則返回nacos.as.前綴加name;generateNacosServiceName會(huì)返回DEFAULT_GROUP@@加rawServiceName,默認(rèn)rawServiceName是不包含DEFAULT_GROUP的;generateResponseIps方法將instanceList轉(zhuǎn)為ip及port并用換行拼接的string
getCluster方法首先通過(guò)addressServerBuilderManager.generateProductName(product)獲取productName,通過(guò)addressServerBuilderManager.generateNacosServiceName(productName)獲取serviceName
然后通過(guò)serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName)獲取service,之后判斷service.getClusterMap()是否包含cluster,不包含返回404
最后通過(guò)service.getClusterMap().get(cluster)獲取cluster,然后通過(guò)addressServerBuilderManager.generateResponseIps(clusterObj.allIPs(false))返回結(jié)果
到此,關(guān)于“nacos address中g(shù)etCluster的原理和作用是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。