Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle and usage of deleteCluster in nacos address

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the principle and usage of deleteCluster in nacos address". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the principle and usage of deleteCluster in nacos address.

Order

This paper mainly studies the deleteCluster 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; /. @ RequestMapping (value = "", method = RequestMethod.DELETE) public ResponseEntity deleteCluster (@ RequestParam (required = false) String product, @ RequestParam (required = false) String cluster, @ RequestParam 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); ResponseEntity responseEntity = ResponseEntity.status (HttpStatus.OK) .body ("product=" + rawProductName + ", cluster=" + rawClusterName + "delete success."); try {String serviceName = addressServerGeneratorManager.generateNacosServiceName (productName); Service service = serviceManager.getService (Constants.DEFAULT_NAMESPACE_ID, serviceName) If (service = = null) {responseEntity = ResponseEntity.status (HttpStatus.NOT_FOUND) .body ("product=" + rawProductName + "not found.");} else {if (StringUtils.isBlank (ips)) {/ / delete all ips from the cluster responseEntity = ResponseEntity.status (HttpStatus.BAD_REQUEST) .body ("ips must not be empty.") } else {/ / delete specified ip list String [] ipArray = addressServerManager.splitIps (ips); String checkResult = AddressServerParamCheckUtil.checkIps (ipArray); if (AddressServerParamCheckUtil.CHECK_OK.equals (checkResult)) {List instanceList = addressServerGeneratorManager.generateInstancesByIps (serviceName, rawProductName, clusterName, ipArray) ServiceManager.removeInstance (Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray (new Instance [instanceList.size ()]));} else {responseEntity = ResponseEntity.status (HttpStatus.BAD_REQUEST) .body (checkResult) } catch (Exception e) {responseEntity = ResponseEntity.status (HttpStatus.INTERNAL_SERVER_ERROR) .body (e.getCause ());} return responseEntity;} / /.}

DeleteCluster receives product, cluster and ips parameters, where productName is generated by addressServerGeneratorManager.generateProductName (product) and clusterName is generated by addressServerManager.getDefaultClusterNameIfEmpty (cluster)

It first obtains service through serviceManager.getService, and returns 404 if it cannot get it. If ips is empty, it returns 400. Then it acquires instanceList through addressServerGeneratorManager.generateInstancesByIps (serviceName, rawProductName, clusterName, ipArray).

Finally, remove the instance; through serviceManager.removeInstance (Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray (new Instance [instanceList.size (). Note that the ephemeral parameter here is false.

ServiceManager

Nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java

@ Component@DependsOn ("nacosApplicationContext") public class ServiceManager implements RecordListener {/ * * Map * / private Map serviceMap = new ConcurrentHashMap (); private LinkedBlockingDeque toBeUpdatedServicesQueue = new LinkedBlockingDeque (1024 * 1024); private Synchronizer synchronizer = new ServiceStatusSynchronizer (); private final Lock lock = new ReentrantLock (); @ Resource (name = "consistencyDelegate") private ConsistencyService consistencyService; @ Autowired private SwitchDomain switchDomain; @ Autowired private DistroMapper distroMapper; @ Autowired private ServerListManager serverListManager; @ Autowired private PushService pushService Private final Object putServiceLock = new Object () /. Public Service getService (String namespaceId, String serviceName) {if (serviceMap.get (namespaceId) = = null) {return null;} return chooseServiceMap (namespaceId) .get (serviceName);} public Map chooseServiceMap (String namespaceId) {return serviceMap.get (namespaceId);} public void removeInstance (String namespaceId, String serviceName, boolean ephemeral, Instance... Ips) throws NacosException {Service service = getService (namespaceId, serviceName); removeInstance (namespaceId, serviceName, ephemeral, service, ips);} public void removeInstance (String namespaceId, String serviceName, boolean ephemeral, Service service, Instance... Ips) throws NacosException {String key = KeyBuilder.buildInstanceListKey (namespaceId, serviceName, ephemeral); List instanceList = substractIpAddresses (service, ephemeral, ips); Instances instances = new Instances (); instances.setInstanceList (instanceList); consistencyService.put (key, instances);} /.}

The getService method directly obtains the map from the serviceMap according to the namespaceId, and then obtains the Service;removeInstance according to the serviceName. First, the service is obtained, and then the specified instance is removed, and finally updated to the consistencyService.

Summary

DeleteCluster receives product, cluster and ips parameters, where productName is generated by addressServerGeneratorManager.generateProductName (product) and clusterName is generated by addressServerManager.getDefaultClusterNameIfEmpty (cluster)

It first obtains service through serviceManager.getService, and returns 404 if it cannot get it. If ips is empty, it returns 400. Then it acquires instanceList through addressServerGeneratorManager.generateInstancesByIps (serviceName, rawProductName, clusterName, ipArray).

Finally, remove the instance; through serviceManager.removeInstance (Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray (new Instance [instanceList.size (). Note that the ephemeral parameter here is false.

At this point, I believe you have a deeper understanding of "the principle and usage of deleteCluster in nacos address". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report