本篇內(nèi)容介紹了“nacos address中postCluster的原理及作用是什么”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供武昌網(wǎng)站建設(shè)、武昌做網(wǎng)站、武昌網(wǎng)站設(shè)計(jì)、武昌網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、武昌企業(yè)網(wǎng)站模板建站服務(wù),十余年武昌做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
本文主要研究一下nacos address的postCluster
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參數(shù);其中productName通過addressServerGeneratorManager.generateProductName(product)生成;clusterName通過addressServerManager.getDefaultClusterNameIfEmpty(cluster)生成
之后通過通過addressServerGeneratorManager.generateNacosServiceName(productName)生成serviceName,然后通過serviceManager.createServiceIfAbsent創(chuàng)建service
之后校驗(yàn)下ip地址,校驗(yàn)通過的話,會通過addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray)生成instanceList,然后遍歷instanceList執(zhí)行naming模塊中的serviceManager的registerInstance方法
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,默認(rèn)rawServiceName是不包含DEFAULT_GROUP的
generateInstancesByIps方法遍歷ipArray,挨個創(chuàng)建instance,注意這里設(shè)置了ephemeral為false
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已經(jīng)是nacos的返回nacos,否則返回原值;getRawClusterName內(nèi)部調(diào)用的是getDefaultClusterNameIfEmpty方法,它對于name是空或者name已經(jīng)是serverlist的返回serverlist,否則返回原值;splitIps方法根據(jù)AddressServerConstants.MULTI_IPS_SEPARATOR來分割字符串為數(shù)組
postCluster方法接收product、cluster、ips參數(shù);其中productName通過addressServerGeneratorManager.generateProductName(product)生成;clusterName通過addressServerManager.getDefaultClusterNameIfEmpty(cluster)生成
之后通過通過addressServerGeneratorManager.generateNacosServiceName(productName)生成serviceName,然后通過serviceManager.createServiceIfAbsent創(chuàng)建service
之后校驗(yàn)下ip地址,校驗(yàn)通過的話,會通過addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray)生成instanceList,然后遍歷instanceList執(zhí)行naming模塊中的serviceManager的registerInstance方法
“nacos address中postCluster的原理及作用是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
新聞名稱:nacosaddress中postCluster的原理及作用是什么
鏈接地址:http://aaarwkj.com/article36/iggipg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站改版、網(wǎng)站內(nèi)鏈、App設(shè)計(jì)、域名注冊、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)