In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the principle and function of postCluster in nacos address". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Order
This paper mainly studies the postCluster of nacos address.
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 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;} / /.}
The postCluster method receives product, cluster, and ips parameters, where productName is generated by addressServerGeneratorManager.generateProductName (product) and clusterName is generated by addressServerManager.getDefaultClusterNameIfEmpty (cluster)
Then generate serviceName through addressServerGeneratorManager.generateNacosServiceName (productName), and then create service through serviceManager.createServiceIfAbsent
After that, the ip address is verified. If the verification is passed, the instanceList is generated through addressServerGeneratorManager.generateInstancesByIps (serviceName, rawProductName, clusterName, ipArray), and then traverses the instanceList to execute the registerInstance method of serviceManager in the naming module.
AddressServerGeneratorManager
Nacos-1.1.3/address/src/main/java/com/alibaba/nacos/address/component/AddressServerGeneratorManager.java
@ Componentpublic 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 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 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;} / /.}
The generateProductName method of AddressServerGeneratorManager returns nacos.as.default for name or nacos, otherwise it returns nacos.as. Prefix with name
GenerateNacosServiceName returns DEFAULT_GROUP@@ plus rawServiceName. Default rawServiceName does not contain DEFAULT_GROUP.
The generateInstancesByIps method traverses the ipArray and creates the instance one by one. Notice that the ephemeral is set to false.
AddressServerManager
Nacos-1.1.3/address/src/main/java/com/alibaba/nacos/address/component/AddressServerManager.java
@ Componentpublic 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);} /.}
The getRawProductName method returns the original value if it is empty for name or if name is already the return nacos of nacos, otherwise it returns the original value; what is called inside getRawClusterName is the getDefaultClusterNameIfEmpty method, which returns serverlist for name is empty or name is already serverlist; otherwise, the splitIps method splits the string into an array according to AddressServerConstants.MULTI_IPS_SEPARATOR
Summary
The postCluster method receives product, cluster, and ips parameters, where productName is generated by addressServerGeneratorManager.generateProductName (product) and clusterName is generated by addressServerManager.getDefaultClusterNameIfEmpty (cluster)
Then generate serviceName through addressServerGeneratorManager.generateNacosServiceName (productName), and then create service through serviceManager.createServiceIfAbsent
After that, the ip address is verified. If the verification is passed, the instanceList is generated through addressServerGeneratorManager.generateInstancesByIps (serviceName, rawProductName, clusterName, ipArray), and then traverses the instanceList to execute the registerInstance method of serviceManager in the naming module.
This is the end of the introduction of "what is the principle and function of postCluster in nacos address". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.