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 registerInstance in NacosNamingService

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

Share

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

This article introduces what is the role of registerInstance in NacosNamingService, the content is very detailed, interested friends can refer to, hope to be helpful to you.

NacosNamingService

Nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/naming/NacosNamingService.java

Public class NacosNamingService implements NamingService {private static final String DEFAULT_PORT = "8080"; private static final long DEFAULT_HEART_BEAT_INTERVAL = TimeUnit.SECONDS.toMillis (5); /. @ Override public void registerInstance (String serviceName, String ip, int port) throws NacosException {registerInstance (serviceName, ip, port, Constants.DEFAULT_CLUSTER_NAME);} @ Override public void registerInstance (String serviceName, String groupName, String ip, int port) throws NacosException {registerInstance (serviceName, groupName, ip, port, Constants.DEFAULT_CLUSTER_NAME) } @ Override public void registerInstance (String serviceName, String ip, int port, String clusterName) throws NacosException {registerInstance (serviceName, Constants.DEFAULT_GROUP, ip, port, clusterName);} @ Override public void registerInstance (String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException {Instance instance = new Instance (); instance.setIp (ip); instance.setPort (port); instance.setWeight Instance.setClusterName (clusterName); registerInstance (serviceName, groupName, instance);} @ Override public void registerInstance (String serviceName, Instance instance) throws NacosException {registerInstance (serviceName, Constants.DEFAULT_GROUP, instance);} @ Override public void registerInstance (String serviceName, String groupName, Instance instance) throws NacosException {if (instance.isEphemeral ()) {BeatInfo beatInfo = new BeatInfo () BeatInfo.setServiceName (NamingUtils.getGroupedName (serviceName, groupName)); beatInfo.setIp (instance.getIp ()); beatInfo.setPort (instance.getPort ()); beatInfo.setCluster (instance.getClusterName ()); beatInfo.setWeight (instance.getWeight ()); beatInfo.setMetadata (instance.getMetadata ()); beatInfo.setScheduled (false) Long instanceInterval = instance.getInstanceHeartBeatInterval (); beatInfo.setPeriod (instanceInterval = = 0? DEFAULT_HEART_BEAT_INTERVAL: instanceInterval); beatReactor.addBeatInfo (NamingUtils.getGroupedName (serviceName, groupName), beatInfo);} serverProxy.registerService (NamingUtils.getGroupedName (serviceName, groupName), groupName, instance);} /.}

The registerInstance method creates a BeatInfo for ephemeral's instance (default is ephemeral), adds it to the scheduling task through the beatReactor.addBeatInfo method, and then registers the service through serverProxy.registerService

BeatReactor

Nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/naming/beat/BeatReactor.java

Public class BeatReactor {private ScheduledExecutorService executorService; private NamingProxy serverProxy; public final Map dom2Beat = new ConcurrentHashMap (); public BeatReactor (NamingProxy serverProxy) {this (serverProxy, UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);} public BeatReactor (NamingProxy serverProxy, int threadCount) {this.serverProxy = serverProxy ExecutorService = new ScheduledThreadPoolExecutor (threadCount, new ThreadFactory () {@ Override public Thread newThread (Runnable r) {Thread thread = newThread (r); thread.setDaemon (true); thread.setName ("com.alibaba.nacos.naming.beat.sender"); return thread;}}) } public void addBeatInfo (String serviceName, BeatInfo beatInfo) {NAMING_LOGGER.info ("[BEAT] adding beat: {} to beat map.", beatInfo); dom2Beat.put (buildKey (serviceName, beatInfo.getIp (), beatInfo.getPort ()), beatInfo); executorService.schedule (new BeatTask (beatInfo), beatInfo.getPeriod (), TimeUnit.MILLISECONDS); MetricsMonitor.getDom2BeatSizeMonitor (). Set (dom2Beat.size ()) } public void removeBeatInfo (String serviceName, String ip, int port) {NAMING_LOGGER.info ("[BEAT] removing beat: {}: {} from beat map.", serviceName, ip, port); BeatInfo beatInfo = dom2Beat.remove (buildKey (serviceName, ip, port)); if (beatInfo = = null) {return;} beatInfo.setStopped (true) MetricsMonitor.getDom2BeatSizeMonitor () .set (dom2Beat.size ());} private String buildKey (String serviceName, String ip, int port) {return serviceName + Constants.NAMING_INSTANCE_ID_SPLITTER + ip + Constants.NAMING_INSTANCE_ID_SPLITTER + port;} class BeatTask implements Runnable {BeatInfo beatInfo; public BeatTask (BeatInfo beatInfo) {this.beatInfo = beatInfo } @ Override public void run () {if (beatInfo.isStopped ()) {return;} long result = serverProxy.sendBeat (beatInfo); long nextTime = result > 0? Result: beatInfo.getPeriod (); executorService.schedule (new BeatTask (beatInfo), nextTime, TimeUnit.MILLISECONDS);}

The addBeatInfo method of BeatReactor adds information to dom2Beat, and then the scheduling BeatTask;removeBeatInfo method removes the specified key from dom2Beat. Marking its stopped as true;BeatTask will first determine whether it is stopped. If not, send heartbeat information through serverProxy.sendBeat, calculate the time of the next schedule, and add scheduling tasks to executorService.

NamingProxy

Nacos-1.1.3/client/src/main/java/com/alibaba/nacos/client/naming/net/NamingProxy.java

Public class NamingProxy {private static final int DEFAULT_SERVER_PORT = 8848; private int serverPort = DEFAULT_SERVER_PORT; private String namespaceId; private String endpoint; private String nacosDomain; private List serverList; private List serversFromEndpoint = new ArrayList (); private long lastSrvRefTime = 0L; private long vipSrvRefInterMillis = TimeUnit.SECONDS.toMillis (30); private Properties properties; / /. Public long sendBeat (BeatInfo beatInfo) {try {if (NAMING_LOGGER.isDebugEnabled ()) {NAMING_LOGGER.debug ("[BEAT] {} sending beat to server: {}", namespaceId, beatInfo.toString ());} Map params = new HashMap (4); params.put ("beat", JSON.toJSONString (beatInfo)) Params.put (CommonParams.NAMESPACE_ID, namespaceId); params.put (CommonParams.SERVICE_NAME, beatInfo.getServiceName ()); String result = reqAPI (UtilAndComs.NACOS_URL_BASE + "/ instance/beat", params, HttpMethod.PUT); JSONObject jsonObject = JSON.parseObject (result); if (jsonObject! = null) {return jsonObject.getLong ("clientBeatInterval") } catch (Exception e) {NAMING_LOGGER.error ("[CLIENT-BEAT] failed to send beat:" + JSON.toJSONString (beatInfo), e);} return 0L;} public void registerService (String serviceName, String groupName, Instance instance) throws NacosException {NAMING_LOGGER.info ("[REGISTER-SERVICE] {} registering service {} with instance: {}", namespaceId, serviceName, instance) Final Map params = new HashMap (9); params.put (CommonParams.NAMESPACE_ID, namespaceId); params.put (CommonParams.SERVICE_NAME, serviceName); params.put (CommonParams.GROUP_NAME, groupName); params.put (CommonParams.CLUSTER_NAME, instance.getClusterName ()); params.put ("ip", instance.getIp ()); params.put ("port", String.valueOf (instance.getPort () Params.put ("weight", String.valueOf (instance.getWeight (); params.put ("enable", String.valueOf (instance.isEnabled (); params.put ("healthy", String.valueOf (instance.isHealthy (); params.put ("ephemeral", String.valueOf (instance.isEphemeral (); params.put ("metadata", JSON.toJSONString (instance.getMetadata () ReqAPI (UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.POST);} /.}

The sendBeat method of NamingProxy sends a PUT request to the / instance/beat interface; the registerService method sends a POST request to the / instance interface for service registration

Summary

The registerInstance method of NacosNamingService creates a BeatInfo for the instance of ephemeral (the default is ephemeral), adds it to the scheduling task through the beatReactor.addBeatInfo method, and then registers the service through serverProxy.registerService

About what the role of registerInstance in NacosNamingService is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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