当前位置:网站首页>Service registration of Nacos registry
Service registration of Nacos registry
2022-07-19 21:58:00 【InfoQ】
5 nacos Service registration of the registry
/nacos/v1/ns/instance
@RestController
@RequestMapping(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance")
public class InstanceController {
@CanDistro
@PostMapping
public String register(HttpServletRequest request) throws Exception {
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
serviceManager.registerInstance(namespaceId, serviceName, parseInstance(request));
return "ok";
}
}
public void registerInstance(String namespaceId, String serviceName, Instance instance) throws NacosException {
createEmptyService(namespaceId, serviceName, instance.isEphemeral());
Service service = getService(namespaceId, serviceName);
if (service == null) {
throw new NacosException(NacosException.INVALID_PARAM,
"service not found, namespace: " + namespaceId + ", service: " + serviceName);
}
addInstance(namespaceId, serviceName, instance.isEphemeral(), instance);
}
- Create an empty service , It's actually initialization serviceMap
- getService: from serviceMap According to the namespaceId and serviceName Get the service object
- call addInstance Add service instance .
createEmptyService
public void createServiceIfAbsent(String namespaceId, String serviceName, boolean local, Cluster cluster) throws NacosException {
Service service = getService(namespaceId, serviceName);
if (service == null) {
Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, serviceName);
service = new Service();
service.setName(serviceName);
service.setNamespaceId(namespaceId);
service.setGroupName(NamingUtils.getGroupName(serviceName));
// now validate the service. if failed, exception will be thrown
service.setLastModifiedMillis(System.currentTimeMillis());
service.recalculateChecksum();
if (cluster != null) {
cluster.setService(service);
service.getClusterMap().put(cluster.getName(), cluster);
}
service.validate();
putServiceAndInit(service);
if (!local) {
addOrReplaceService(service);
}
}
}
- according to namespaceId and serviceName From the cache Service example
- without , Create and call putServiceAndInit() Save to cache .
private void putServiceAndInit(Service service) throws NacosException {
putService(service);
service.init();
consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service);
consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service);
Loggers.SRV_LOG.info("[NEW-SERVICE] {}", service.toJSON());
}
- adopt putService Method to cache the service into memory
- service.init(); Establish a heartbeat detection mechanism , Continuously detect the time when all instances of the current service last sent heartbeat packets through scheduled tasks . If the timeout , Is set healthy by false Indicates that the service is unhealthy , And send service change events .
- consistencyService.listen Realize data consistency monitoring
summary :
- nacos Client pass Open API Send a service registration request in the form of
- The server receives the request :
- Construct a Service Object to save to Map Collection
- Use a scheduled task to establish a heartbeat detection mechanism for all instances under the current service .
- Synchronize service data based on data consistency protocol .nacos Service registration is very important for us , We need to understand the principle and registration process , Then analyze the source code , To better understand nacos How to realize the registration of services , Only in this way can we understand thoroughly nacos The operating mechanism of , What problems will we encounter in our work in the future , We can check in time , Rapid positioning , In this way, we can better improve efficiency and complete tasks in work and study . If you think this article is good , Please leave a comment like this ~~
边栏推荐
- 复制第二个”一加“?OPPO推独立品牌Realme,对标红米主攻印度市场!
- 数字化还没走远,数智化时代已经来临
- Win10 restores its own photo viewer
- AI系统前沿动态第42期:LLVM之父:为什么要重建AI基础设施软件;OSDI 2022论文评述
- 洛谷P1631 序列合并 题解
- LeetCode_ 39_ Combined sum
- CUDA编程
- 仪表故障诊断软件操作流程
- Hangzhou has introduced new policies for the integrated circuit industry, with unprecedented support
- 基于数据库的RBAC数据模型的权限控制
猜你喜欢
10GBASE XX points de connaissance pertinents
Applet cloud development: adding comments to cloud functions
Two practical skills sharing of SAP Fiori launchpad application
seaborn颜色
汉仪股份通过注册:年营收2.2亿 谢立群控制公司35%股权
softmax多分类
Alfred Workflow 豆瓣插件开发
2022-07-07 零散的知识
陈天奇、王威廉等人推荐:ACL最佳论文奖得主给新入行研究者的一点建议
Pyg builds gat to realize node classification
随机推荐
图像的仿射变换
【NER-0】-2016 NAACL BiLSTM-CRF
PCB线路板塞孔工艺重要性体现在哪里?
Two practical skills sharing of SAP Fiori launchpad application
現代CMake高級教程
Jupyter内核环境的管理
Redis cluster switching shows no cached master?
Introduction to smart contract security audit - random number
多层感知机(神经网络)与激活函数
Logistic regression and cross entropy
预测化学分子的nlogP——基于sklearn, deepchem, DGL, Rdkit的图卷积网络模型
使用php将url中携带的参数转成数组格式并赋值
基于数据库的RBAC数据模型的权限控制
Win10 恢复自带的照片查看器
【Renesas RA6M4开发板之DHT11温湿度读取】
Hengshuo semiconductor has passed the registration: the annual revenue is 580million, and the actual controller is Chinese Americans
MySQL notes (basic)
Economic Daily: don't treat digital collections as "money making" products
Envoy Distributed Link Tracking
丘钛股价暴涨的背后:获OPPO超100万个3D结构光模组订单