nacosServiceManager的removeInstance有什么作用
这篇文章主要介绍“nacos ServiceManager的removeInstance有什么作用”,在日常操作中,相信很多人在nacos ServiceManager的removeInstance有什么作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”nacos ServiceManager的removeInstance有什么作用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
10年的建水网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整建水建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“建水网站设计”,“建水网站推广”以来,每个客户项目都认真落实执行。
序
本文主要研究一下nacos ServiceManager的removeInstance
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 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); } public List substractIpAddresses(Service service, boolean ephemeral, Instance... ips) throws NacosException { return updateIpAddresses(service, UtilsAndCommons.UPDATE_INSTANCE_ACTION_REMOVE, ephemeral, ips); } public List updateIpAddresses(Service service, String action, boolean ephemeral, Instance... ips) throws NacosException { Datum datum = consistencyService.get(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), ephemeral)); Map oldInstanceMap = new HashMap<>(16); List currentIPs = service.allIPs(ephemeral); Map map = new ConcurrentHashMap<>(currentIPs.size()); for (Instance instance : currentIPs) { map.put(instance.toIPAddr(), instance); } if (datum != null) { oldInstanceMap = setValid(((Instances) datum.value).getInstanceList(), map); } // use HashMap for deep copy: HashMap instanceMap = new HashMap<>(oldInstanceMap.size()); instanceMap.putAll(oldInstanceMap); for (Instance instance : ips) { if (!service.getClusterMap().containsKey(instance.getClusterName())) { Cluster cluster = new Cluster(instance.getClusterName(), service); cluster.init(); service.getClusterMap().put(instance.getClusterName(), cluster); Loggers.SRV_LOG.warn("cluster: {} not found, ip: {}, will create new cluster with default configuration.", instance.getClusterName(), instance.toJSON()); } if (UtilsAndCommons.UPDATE_INSTANCE_ACTION_REMOVE.equals(action)) { instanceMap.remove(instance.getDatumKey()); } else { instanceMap.put(instance.getDatumKey(), instance); } } if (instanceMap.size() <= 0 && UtilsAndCommons.UPDATE_INSTANCE_ACTION_ADD.equals(action)) { throw new IllegalArgumentException("ip list can not be empty, service: " + service.getName() + ", ip list: " + JSON.toJSONString(instanceMap.values())); } return new ArrayList<>(instanceMap.values()); } //...... }
removeInstance方法通过substractIpAddresses获取移除执行instances之后的instanceList,然后通过consistencyService.put方法进行更新;substractIpAddresses方法执行updateIpAddresses方法,其action参数值为UtilsAndCommons.UPDATE_INSTANCE_ACTION_REMOVE,它先使用service.allIPs(ephemeral)获取instance列表,针对remove动作会根据instance.getDatumKey()将其从instanceMap中移除
Service.allIPs
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/core/Service.java
public class Service extends com.alibaba.nacos.api.naming.pojo.Service implements Record, RecordListener{ private static final String SERVICE_NAME_SYNTAX = "[0-9a-zA-Z@\\.:_-]+"; @JSONField(serialize = false) private ClientBeatCheckTask clientBeatCheckTask = new ClientBeatCheckTask(this); private String token; private List owners = new ArrayList<>(); private Boolean resetWeight = false; private Boolean enabled = true; private Selector selector = new NoneSelector(); private String namespaceId; /** * IP will be deleted if it has not send beat for some time, default timeout is 30 seconds. */ private long ipDeleteTimeout = 30 * 1000; private volatile long lastModifiedMillis = 0L; private volatile String checksum; /** * TODO set customized push expire time: */ private long pushCacheMillis = 0L; private Map clusterMap = new HashMap<>(); //...... public List allIPs(boolean ephemeral) { List allIPs = new ArrayList<>(); for (Map.Entry entry : clusterMap.entrySet()) { allIPs.addAll(entry.getValue().allIPs(ephemeral)); } return allIPs; } //...... }
Service的allIPs方法会遍历clusterMap,然后通过Cluster.allIPs(ephemeral)收集Instance
Cluster.allIPs
nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/core/Cluster.java
public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implements Cloneable { private static final String CLUSTER_NAME_SYNTAX = "[0-9a-zA-Z-]+"; /** * a addition for same site routing, can group multiple sites into a region, like Hangzhou, Shanghai, etc. */ private String sitegroup = StringUtils.EMPTY; private int defCkport = 80; private int defIPPort = -1; @JSONField(serialize = false) private HealthCheckTask checkTask; @JSONField(serialize = false) private SetpersistentInstances = new HashSet<>(); @JSONField(serialize = false) private Set ephemeralInstances = new HashSet<>(); @JSONField(serialize = false) private Service service; @JSONField(serialize = false) private volatile boolean inited = false; private Map metadata = new ConcurrentHashMap<>(); //...... public List allIPs(boolean ephemeral) { return ephemeral ? new ArrayList<>(ephemeralInstances) : new ArrayList<>(persistentInstances); } //...... }
Cluster的allIPs方法根据ephemeral来返回ephemeralInstances或者persistentInstances
小结
ServiceManager的removeInstance方法通过substractIpAddresses获取移除执行instances之后的instanceList,然后通过consistencyService.put方法进行更新;substractIpAddresses方法执行updateIpAddresses方法,其action参数值为UtilsAndCommons.UPDATE_INSTANCE_ACTION_REMOVE,它先使用service.allIPs(ephemeral)获取instance列表,针对remove动作会根据instance.getDatumKey()将其从instanceMap中移除
到此,关于“nacos ServiceManager的removeInstance有什么作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
分享标题:nacosServiceManager的removeInstance有什么作用
网页URL:http://azwzsj.com/article/jscheo.html