当前位置:网站首页>[Nacos] what does nacosclient do during service registration
[Nacos] what does nacosclient do during service registration
2022-07-25 14:54:00 【Senior fishing Engineer】
Project integration
With springboot and nacos As an example .
sb Add to the project nacos-discovery rely on :
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.10</version>
</dependency>
nacos:
discovery:
server-addr: http://localhost:8848
namespace: public
username: nacos
password: nacos
auto-register: true
register:
service-name: sb-provider
healthy: true
Properties to pay attention to :
- auto-register: The default configuration is false, That is, by default, this instance will not be registered to nacos server
- register.service-name : Of the current instance service name, Can't be empty , Otherwise, after the project is started , Sign up to nacos server When an error . This is a nacos-client An internal verification of .
analysis
starter
Find our integration nacos client The use of jar package :nacos-discovery-spring-boot-starter
Look again nacos-discovery-spring-boot-autoconfigure , According to the name, we can see , This is a nacos The configuration of the assembly is here 
Look directly at spring.factories file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration
notice org.springframework.boot.autoconfigure.EnableAutoConfiguration In fact, I know where the key assembly code is , Then look directly NacosDiscoveryAutoConfiguration
The code is simple , from @Bean You can see that the automatic registration is through new NacosDiscoveryAutoRegister() Realized .
It's the same thing , adopt SpringEvent monitor WebServerInitializedEvent Events to achieve .
The previous verification logic is very simple , The key is namingService.registerInstance Here is the instance registration .
NamingService
@Override
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
NamingUtils.checkInstanceIsLegal(instance);
clientProxy.registerService(serviceName, groupName, instance);
}
NamingUtils.checkInstanceIsLegal(instance); Here is business verification , Verify whether the following two settings are correct :
- heart beat timeout must > heart beat interval
- ip delete timeout must > heart beat interval
ps: In fact, it is to verify whether the set timeout meets the requirements , The requirement is to be greater than the default heartbeat cycle . Very reasonable.
Then the highlight is clientProxy.registerService(serviceName, groupName, instance) This method .clientProxy It's an interface , It's packaged a lot of service Related operations . Um. , reasonable
So specifically registerService How did it happen
@Override
public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
getExecuteClientProxy(instance).registerService(serviceName, groupName, instance);
}
private NamingClientProxy getExecuteClientProxy(Instance instance) {
return instance.isEphemeral() ? grpcClientProxy : httpClientProxy;
}
Why choose a different one according to whether it is a temporary instance clientProxy?
it’s a question .
Regardless of the , Then look at
@Override
public void registerService(String serviceName, String groupName, Instance instance) throws NacosException {
NAMING_LOGGER.info("[REGISTER-SERVICE] {} registering service {} with instance {}", namespaceId, serviceName,
instance);
redoService.cacheInstanceForRedo(serviceName, groupName, instance);
doRegisterService(serviceName, groupName, instance);
}
redoService.cacheInstanceForRedo All you need to do is cache this instance .
private final ConcurrentMap<String, InstanceRedoData> registeredInstances = new ConcurrentHashMap<>();
public void cacheInstanceForRedo(String serviceName, String groupName, Instance instance) {
String key = NamingUtils.getGroupedName(serviceName, groupName);
InstanceRedoData redoData = InstanceRedoData.build(serviceName, groupName, instance);
//registeredInstances It's a map
synchronized (registeredInstances) {
registeredInstances.put(key, redoData);
}
}

InstanceRedoData There is a property called registered, Semantic is : Register or not .
You can see that the first time you put it into the cache, this attribute is false
Then look at doRegisterService
public void doRegisterService(String serviceName, String groupName, Instance instance) throws NacosException {
InstanceRequest request = new InstanceRequest(namespaceId, serviceName, groupName,
NamingRemoteConstants.REGISTER_INSTANCE, instance);
requestToServer(request, Response.class);
redoService.instanceRegistered(serviceName, groupName);
}
requestToServer Internally, it calls methods to server Registration instance , No more research .
public void instanceRegistered(String serviceName, String groupName) {
String key = NamingUtils.getGroupedName(serviceName, groupName);
synchronized (registeredInstances) {
InstanceRedoData redoData = registeredInstances.get(key);
if (null != redoData) {
redoData.setRegistered(true);
}
}
}
instanceRegistered Just cache the instance registered Change the field to true, Represents that this instance has been in server End registration succeeded .
The next study server How to deal with client Of service Registration request
边栏推荐
- 06. Neural network like
- [Nuxt 3] (十一) 传送 & 模块
- awk从入门到入土(24)提取指令网卡的ip
- Alibaba cloud installs mysql5.7
- 关于RDBMS和非RDBMS【数据库系统】
- 应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
- C language and SQL Server database technology
- 43 box model
- 37 元素模式(行内元素,块元素,行内块元素)
- 41 picture background synthesis - colorful navigation map
猜你喜欢

37 element mode (inline element, block element, inline block element)

I2C device driver hierarchy
![应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]

51 single chip microcomputer learning notes (1)

006操作符简介

45padding不会撑开盒子的情况
![[eloquence] negotiation persuasion skills and Strategies](/img/01/0fcbbfcf35a6d166c14ba4215eacac.jpg)
[eloquence] negotiation persuasion skills and Strategies

Niuke multi school E G J L

Resource not found: rgbd_launch 解决方案

EDA chip design solution based on AMD epyc server
随机推荐
[C topic] Li Kou 206. reverse the linked list
Live classroom system 05 background management system
English语法_不定代词 - other / another
MySQL sort
MySQL 45 talks about | 06 global locks and table locks: Why are there so many obstacles to adding a field to a table?
Awk from getting started to digging in (21) awk script debugging
The input input box of H5 page pops up the numeric keypad, which needs to support decimal points
C language and SQL Server database technology
LeetCode-198-打家劫舍
Thymeleaf notes
Filters get the data in data; Filters use data in data
[C topic] Li Kou 88. merge two ordered arrays
51 single chip microcomputer learning notes (1)
43 box model
(原创)自定义一个滚屏的RecyclerView
Resource not found: rgbd_ Launch solution
二维数组赋初值你会几种方法?
IP地址分类,判断一个网段是子网超网
kibana操作es
awk从入门到入土(21)awk脚本调试