Talk nacos address of deleteCluster

sequence

In this paper, we look deleteCluster nacos address of

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<Instance> 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 receiving product, cluster, ips parameter; wherein productName generated by addressServerGeneratorManager.generateProductName (product); clusterName generated by addressServerManager.getDefaultClusterNameIfEmpty (cluster)
  • It is first acquired by the service serviceManager.getService, obtaining less than 404 returns; ips if empty, return 400; after acquiring instanceList by addressServerGeneratorManager.generateInstancesByIps ​​(serviceName, rawProductName, clusterName, ipArray)
  • Finally, by removing instance serviceManager.removeInstance (Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray (new Instance [instanceList.size ()])); ephemeral Note that the parameter 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<Service> {

    /**
     * Map<namespace, Map<group::serviceName, Service>>
     */
    private Map<String, Map<String, Service>> serviceMap = new ConcurrentHashMap<>();

    private LinkedBlockingDeque<ServiceKey> 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<String, Service> 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<Instance> instanceList = substractIpAddresses(service, ephemeral, ips);

        Instances instances = new Instances();
        instances.setInstanceList(instanceList);

        consistencyService.put(key, instances);
    }

    //......
}
复制代码
  • getService directly from serviceMap method according namespaceId map, then according to obtain Service serviceName; removeInstance method to obtain the service, and then again to remove the specified instance, the last update consistencyService

summary

  • deleteCluster receiving product, cluster, ips parameter; wherein productName generated by addressServerGeneratorManager.generateProductName (product); clusterName generated by addressServerManager.getDefaultClusterNameIfEmpty (cluster)
  • It is first acquired by the service serviceManager.getService, obtaining less than 404 returns; ips if empty, return 400; after acquiring instanceList by addressServerGeneratorManager.generateInstancesByIps ​​(serviceName, rawProductName, clusterName, ipArray)
  • Finally, by removing instance serviceManager.removeInstance (Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray (new Instance [instanceList.size ()])); ephemeral Note that the parameter is false

doc

Guess you like

Origin juejin.im/post/5db54b416fb9a0205717ab06