温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nacos address中deleteCluster的原理及用法

发布时间:2021-06-24 11:50:05 来源:亿速云 阅读:189 作者:chen 栏目:大数据

本篇内容主要讲解“nacos address中deleteCluster的原理及用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“nacos address中deleteCluster的原理及用法”吧!

本文主要研究一下nacos address的deleteCluster

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接收product、cluster、ips参数;其中productName通过addressServerGeneratorManager.generateProductName(product)生成;clusterName通过addressServerManager.getDefaultClusterNameIfEmpty(cluster)生成

  • 它首先通过serviceManager.getService获取service,获取不到则返回404;若ips为空,返回400;之后通过addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray)获取instanceList

  • 最后通过serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray(new Instance[instanceList.size()]))移除instance;注意这里的ephemeral参数为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方法直接从serviceMap中根据namespaceId获取map,然后再根据serviceName获取Service;removeInstance方法先获取service,然后再移除指定的instance,最后更新到consistencyService

小结

  • deleteCluster接收product、cluster、ips参数;其中productName通过addressServerGeneratorManager.generateProductName(product)生成;clusterName通过addressServerManager.getDefaultClusterNameIfEmpty(cluster)生成

  • 它首先通过serviceManager.getService获取service,获取不到则返回404;若ips为空,返回400;之后通过addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray)获取instanceList

  • 最后通过serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray(new Instance[instanceList.size()]))移除instance;注意这里的ephemeral参数为false

到此,相信大家对“nacos address中deleteCluster的原理及用法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI