What is the principle and function of getCluster in nacos address
This article mainly introduces "what is the principle and function of getCluster in nacos address". In daily operation, I believe that many people have doubts about the principle and function of getCluster in nacos address. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what is the principle and function of getCluster in nacos address?" Next, please follow the editor to study!
Order
This paper mainly studies the getCluster of nacos address.
ServerListController
Nacos-1.1.3/address/src/main/java/com/alibaba/nacos/address/controller/ServerListController.java
@ RestControllerpublic 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));}}
The getCluster method first obtains productName through addressServerBuilderManager.generateProductName (product) and serviceName through addressServerBuilderManager.generateNacosServiceName (productName)
Then get the service through serviceManager.getService (Constants.DEFAULT_NAMESPACE_ID, serviceName), and then determine whether service.getClusterMap () contains cluster and does not contain the return 404
Finally, get the cluster through service.getClusterMap () .get (cluster), and then return the result through addressServerBuilderManager.generateResponseIps (clusterObj.allIPs (false))
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) } 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 instanceList) {StringBuilder ips = new StringBuilder () InstanceList.forEach (instance-> {ips.append (instance.getIp () + ":" + instance.getPort ()); ips.append ("\ n");}); return ips.toString ();} / /.}
The generateProductName method 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. GenerateResponseIps method converts instanceList to ip and port and uses newline stitching string
Summary
The getCluster method first obtains productName through addressServerBuilderManager.generateProductName (product) and serviceName through addressServerBuilderManager.generateNacosServiceName (productName)
Then get the service through serviceManager.getService (Constants.DEFAULT_NAMESPACE_ID, serviceName), and then determine whether service.getClusterMap () contains cluster and does not contain the return 404
Finally, get the cluster through service.getClusterMap () .get (cluster), and then return the result through addressServerBuilderManager.generateResponseIps (clusterObj.allIPs (false))
At this point, the study of "what is the principle and function of getCluster in nacos address" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!