当前位置:网站首页>【Nacos】NacosClient在服务注册时做了什么
【Nacos】NacosClient在服务注册时做了什么
2022-07-25 09:16:00 【高级摸鱼工程师】
项目集成
以springboot和nacos的集成为例。
sb项目中添加nacos-discovery依赖:
<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
要注意的属性:
- auto-register: 这个配置默认是false,也就是默认不会将此实例注册到nacos server
- register.service-name :当前实例的service name,不能为空,否则项目启动完之后,注册到nacos server时会报错。这是nacos-client的内部的一个校验。
解析
starter
找到我们集成nacos client使用的jar包:nacos-discovery-spring-boot-starter
再看 nacos-discovery-spring-boot-autoconfigure , 根据命名就可以看出来,这是nacos装配的配置就在这里了
直接看 spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.boot.nacos.discovery.autoconfigure.NacosDiscoveryAutoConfiguration
看到org.springframework.boot.autoconfigure.EnableAutoConfiguration其实就懂关键的装配代码在哪里了,那就直接看NacosDiscoveryAutoConfiguration
代码很简单,从@Bean就可以看出自动注册是通过 new NacosDiscoveryAutoRegister() 实现的。
还是那一套,通过SpringEvent监听 WebServerInitializedEvent事件来实现。
前边几个校验逻辑很简单,那关键的就是 namingService.registerInstance 这里做实例注册的事情了。
NamingService
@Override
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
NamingUtils.checkInstanceIsLegal(instance);
clientProxy.registerService(serviceName, groupName, instance);
}
NamingUtils.checkInstanceIsLegal(instance); 这里是业务校验,校验了以下两个设置是否有误:
- heart beat timeout must > heart beat interval
- ip delete timeout must > heart beat interval
ps:其实就是校验设置的超时时间是否符合要求,要求就是要大于默认的心跳周期。很合理
那么重头戏就是clientProxy.registerService(serviceName, groupName, instance)这个方法了。clientProxy是一个接口,封装了很多service相关的操作。嗯,合理
那么具体来看registerService是怎么实现的
@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;
}
为什么根据是否是临时实例而选择不同的clientProxy?
it’s a question .
先不管了,接着看
@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 做的事情就是将本实例做个缓存。
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是个map
synchronized (registeredInstances) {
registeredInstances.put(key, redoData);
}
}

InstanceRedoData有个属性叫registered,语义是: 是否注册。
可以看到第一次放到缓存的时候这个属性是false
接着看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内部是调用方法去server注册实例,就不研究了。
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就是将刚才缓存的实例registered字段修改为true,代表此实例已在server端注册成功。
下一篇研究下server是怎么处理client的service注册请求的
边栏推荐
- The operation cannot be completed because a folder or file in it is already open in another program
- 360 degree drag panorama plug-in tpanorama.js
- 音乐人的 NFT 指南
- 为什么说DAO是未来的公司形式
- The simplest sklearn environment configuration tutorial in the whole network (100% success)
- This is the worst controller layer code I've ever seen
- 2022-7-14 JMeter pressure test
- How to connect tdengine with idea database tool?
- activemq--消息重试机制
- JS touch screen game source code ice and snow journey
猜你喜欢

This ten-year content industry infrastructure company is actually an invisible Web3 pioneer

Do you know these methods of MySQL database optimization?

Common tool classes under JUC package

A picture to quickly understand envoyfilter in istio

Unity ugui interaction (new ideas)

【npm】 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。

JS pop-up City filtering component matches mobile terminal

有误差的字符串型时间比较方法String.compareTo

NFT guide for musicians

Canvas text JS special effect composed of many circles
随机推荐
Troubleshooting error: NPM install emojis list failed
超赞的yolo目标检测训练所用垃圾分类数据集共享——标注好的约3000张
28. Slot
Arrange the array into the smallest number
The hole of scroll view in uniapp
Learn about spark project on Nebula graph
Redis learning notes
The annualization of financial products is 4%. How much profit can you get from buying 10000 yuan a month?
JMeter test plan cannot be saved solution
Disable module (attribute node) in LabVIEW
[NPM] the "NPM" item cannot be recognized as the name of cmdlets, functions, script files or runnable programs. Please check the spelling of the name. If the path is included, make sure the path is co
Additional: SQL statement area / county in the lower half (data table)
The simplest sklearn environment configuration tutorial in the whole network (100% success)
The operation cannot be completed because a folder or file in it is already open in another program
Yolov5 environment configuration
activemq--延迟投递和定时投递
51 single chip microcomputer controls nixie tube display
What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions
[machine learning] Finally, the important steps of machine learning modeling have been clarified
The development of art NFT