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

What is the function of nacos ServiceManager's updateInstance?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what is the role of updateInstance of nacos ServiceManager". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Order

This paper mainly studies the updateInstance of nacos ServiceManager.

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 updateInstance (String namespaceId, String serviceName, Instance instance) throws NacosException {Service service = getService (namespaceId, serviceName); if (service = = null) {throw new NacosException (NacosException.INVALID_PARAM, "service not found, namespace:" + namespaceId + ", service:" + serviceName) } if (! service.allIPs (). Contains (instance)) {throw new NacosException (NacosException.INVALID_PARAM, "instance not exist:" + instance);} addInstance (namespaceId, serviceName, instance.isEphemeral (), instance);} public void addInstance (String namespaceId, String serviceName, boolean ephemeral, Instance...) Ips) throws NacosException {String key = KeyBuilder.buildInstanceListKey (namespaceId, serviceName, ephemeral); Service service = getService (namespaceId, serviceName); List instanceList = addIpAddresses (service, ephemeral, ips); Instances instances = new Instances (); instances.setInstanceList (instanceList); consistencyService.put (key, instances);} public List addIpAddresses (Service service, boolean ephemeral, Instance...) Ips) throws NacosException {return updateIpAddresses (service, UtilsAndCommons.UPDATE_INSTANCE_ACTION_ADD, 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 () partitionConfig.getTaskDispatchPeriod ()) {for (Server member: dataSyncer.getServers ()) {if (NetUtils.localServer (). Equals (member.getKey () {continue } SyncTask syncTask = new SyncTask (); syncTask.setKeys (keys); syncTask.setTargetServer (member.getKey ()) If (Loggers.DISTRO.isDebugEnabled () & & StringUtils.isNotBlank (key)) {Loggers.DISTRO.debug ("add sync task: {}", JSON.toJSONString (syncTask));} dataSyncer.submit (syncTask, 0) } lastDispatchTime = System.currentTimeMillis (); dataSize = 0;} catch (Exception e) {Loggers.DISTRO.error ("dispatch sync task failed.", e);}

The addTask method of TaskDispatcher fetches the specified TaskScheduler from taskSchedulerList and executes its addTask method; the addTask method of TaskScheduler adds data to the queue, while the run method constantly fetches data from queue and then executes syncTask through dataSyncer

SyncTask

Nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/SyncTask.java

Public class SyncTask {private List keys; private int retryCount; private long lastExecuteTime; private String targetServer; public List getKeys () {return keys;} public void setKeys (List keys) {this.keys = keys;} public int getRetryCount () {return retryCount;} public void setRetryCount (int retryCount) {this.retryCount = retryCount;} public long getLastExecuteTime () {return lastExecuteTime } public void setLastExecuteTime (long lastExecuteTime) {this.lastExecuteTime = lastExecuteTime;} public String getTargetServer () {return targetServer;} public void setTargetServer (String targetServer) {this.targetServer = targetServer;}}

SyncTask contains keys and targetServer attributes, where targetServer is used to tell DataSyncer which server to perform the sync operation to

Summary

UpdateInstance verifies whether the instance to be updated exists through service.allIPs (). Contains (instance), throws NacosException if it does not exist, and executes the addInstance method if it exists

The addInstance method, which takes the service, then executes addIpAddresses, and finally executes the consistencyService.put;addIpAddresses call to the updateIpAddresses method, whose action parameter is UtilsAndCommons.UPDATE_INSTANCE_ACTION_ADD

The updateIpAddresses method first gets the datum from the consistencyService, then obtains the currentIPs through the service.allIPs method, then sets the oldInstanceMap according to the datum, deletes the UtilsAndCommons.UPDATE_INSTANCE_ACTION_REMOVE type, and the rest of the action sends the instance method to the instanceMap

This is the end of the content of "what is the use of nacos ServiceManager's updateInstance"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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