共计 3316 个字符,预计需要花费 9 分钟才能阅读完成。
序
本文主要研究一下 nacos address 的 getCluster
ServerListController
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)) 返回结果
AddressServerGeneratorManager
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)) 返回结果
doc
- ServerListController
正文完