当前位置:网站首页>Part 01: distributed registry
Part 01: distributed registry
2022-07-22 11:57:00 【TaoYuanming of the Western Wei Dynasty】
author : TaoYuanming of the Western Wei Dynasty
Blog : https://blog.springlearn.cn/
A big copy of the world code , Copying around can improve , See if you can copy !
One 、 What is a registry
What is? Registry Center
, Registry Center
It is often encountered under the distributed application system . For distributed system applications, it is horizontal expansion . Here's the picture User App
This service , have 2 Servers
But when users come in from the gateway to access , How does the gateway know this User App
How many services are there and what is the network address of each service ? So we need a place to collect the address and name of each application .
Often this place is called Registry Center
. Applications in a distributed environment will register their own network address to this place at startup , And naming .
Two 、 The role of the registry
But can the registry only register services ?
Of course not. , If you only do this thing , Then isn't it a waste of your own resources ? The registration center should do
Okay , Let's look at the distributed environment , How much is the location of the Registration Center important
了 , If there is no registry , The services on the network are Blind person
了 . So this system
We should reconsider his design Usability
, And even if the registration center hangs up , Don't be blind between services . At least leave one to the Registration Center Restart time
.
Okay , I know so much , Let's start designing directly .
3、 ... and 、 Design registry
3.1 ip The address for
The point to pay attention to here is not to get it 127.0.0.1 This local loopback address . There are many similar tool Networks , This is not the point . Just have a look .
public class HostUtils {
private static String ip;
private static String hostName;
private static void resolveHost() throws Exception {
InetAddress localHost = InetAddress.getLocalHost();
hostName = localHost.getHostName();
ip = localHost.getHostAddress();
if (localHost.isLoopbackAddress()) {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet4Address)) {
ip = inetAddress.getHostAddress();
}
}
}
}
}
public static String getIp() {
return ip;
}
public static String getHostName() {
return hostName;
}
static {
try {
resolveHost();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.2 Communication protocol & Technology selection
Communication protocols are generally designed to be compatible with multilingual environments , Best used http agreement . Because it's widely used . Of course, you can also customize the Protocol . Here's a recommended one
Xiaobian is based on Netty Encapsulated communication framework mojito Here we mainly consider versatility , Use
http agreement . Because now microservices are used Spring Cloud More of , Therefore, the framework selection of our registry uses Spring Boot
Here we can use SpringBoot Of Endpoint Implement your own extension points , At the same time, you can also use the self-contained health examination ability of the service , Condition monitoring .
The implementation idea is described below
3.3 Interaction process
3.3.1 Server capability
- Provide service registration interface
- Data can fall into the database , Also record the memory cache , Large amount of services can be considered redis
- At the same time, it should fall into the file , Avoid the database hanging and restore it from the file to the cache
- Provide service offline interface
- When the service is offline , Actively call the offline interface of the Registration Center , The registration center should provide the ability to passively accept offline
- When the service condition is bad during the rotation training , Actively mark the service as offline , When the service state is restored, it is marked as normal
- Provide service health examination ability
- Rotate the health check interface of the registered machine , Dynamically update node status
- Start timing task , Regularly send status check , Check the client service status
- Provide service discovery interface
- By passing the service name , Return the service list corresponding to the service name
- Provide application configuration capability
- Achieve service isolation , Do not configure a service , Push to all servers , Just push to the corresponding node of this service
- Can actively initiate configuration change messages
3.3.2 Client capabilities
- Service registration capability
- Realization
ApplicationContextInitializer
Read the application configuration when the application starts , Then register yourself in the Registration Center - Configure the registry , Pull it down at the same time , Put it in Spring Configuration environment
- Realization
- Take the initiative to go offline when the service is restarted or down
- utilize
@PreDestroy
Destruction method , Or register Jvm Hooks Program
- utilize
- Service discovery capability
- Get the service list interface according to the service name , At the same time, cache a local copy
- If the registry does not respond , Be able to use local cache , Give the registry a time to restart
- Listen for server configuration information
- Refresh the monitored configuration changes to Spring Containers Dynamic binding configuration scheme reference
- Open the health check
Four 、 Common registries
Registration center comparison
4.1 Zookeeper
It can be used ZooKeeper To do it : Unified configuration management 、 Unified naming service 、 Distributed lock 、 Cluster management .
ZooKeeper Data structure of , Follow Unix File systems are very similar , It can be seen as a tree , Each node is called ZNode. Each node can be identified by a path , Structure diagram is as follows :
that ZooKeeper The star " Trees " What are the characteristics ??ZooKeeper We call this the Znode,Znode There are two types :
brief / temporary (Ephemeral): When the client and the server are disconnected , Created Znode( node ) It will be deleted automatically
persistent (Persistent): When the client and the server are disconnected , Created Znode( node ) Does not delete
in addition Zookeeper It provides the ability of node monitoring , When a node changes, a notification event will be initiated . We can do some things according to this feature , For example, if the node is deleted, remove the services registered on the node .
4.2 Eureka
Eureka What is it ?
Eureka yes SpringBoot The default registry component . There is no ability to configure
4.3 Consul
Consul Is a tool for service discovery and configuration .Consul It's distributed , Highly available , And it's highly scalable , And it's easy to develop and use . It provides a fully functional control panel , The main features are : Service discovery 、 health examination 、 Key value storage 、 Security service communication 、 Multi-data center 、ServiceMesh.Consul In the design, many functions to be used in distributed service governance are included .
4.4 Nacos
Nacos Commit to discovering 、 Configure and manage microservices .Nacos Provides an easy-to-use feature set , Help you realize dynamic service discovery 、 Service configuration management 、 Service and traffic management .Nacos Build... More quickly and easily 、 Deliver and manage microservice platforms . Nacos Is to build in “ service ” Modern application architecture centered ( For example, the microservices paradigm 、 Cloud native paradigm ) Service infrastructure .Nacos To support as RPC Registry Center , for example : Support Dubbo frame ; Also have the ability of microservice registry , for example :SpringCloud frame .
5、 ... and 、 summary
The registration centers on the market have the above , The core logic thinking is not bad . among Consul and Nacos It supports configuration . If we develop a registry , There is no need to study its source code .
Only when we want to be a registration center , At this time, we can study the source code . For general development use , Just know what he does .
边栏推荐
- The online information office strictly investigates and deals with the behavior of inducing minors to participate in live broadcast reward: the chaos in the live broadcast reward industry must be rect
- 请求转发和请求重定向的区别
- The US Senate preliminarily passed the US $52billion "chip bill", and she took the opportunity to "invest in stocks"!
- 基于射频指纹的LoRa网络安全方案研究
- let 与 var 的区别
- 鼠标行为HHT变换的工业互联网用户身份认证
- The difference between request forwarding and request redirection
- Redis' cache penetration, breakdown, avalanche
- EN 1504-4: structural bonding of concrete structure products - CE certification
- 全球地下水模拟与监测:机遇与挑战
猜你喜欢
ctfhub(rce智慧树)
Deep analysis: could not run 'torch vision:: NMS' with arguments from the' CUDA 'backend
001_Kubernetes入门
电缆故障监测vr虚拟仿真教学软件的好处
Week 5 Linear Models for Classification (Part A)
师傅教你~LNMP源码搭建
Is it just an illusion that the encryption market soared to more than $1trillion? The crypto bear market is far from bottoming out
300million
请求转发和请求重定向的区别
Delete adjacent files with different names in the folder & adapt to the situation that the label name corresponds to the image name during neural network training
随机推荐
Is it just an illusion that the encryption market soared to more than $1trillion? The crypto bear market is far from bottoming out
LeetCode·987.二叉树的垂直遍历·桶排序·模拟
let 与 var 的区别
User identity authentication of industrial Internet based on HHT transformation of mouse behavior
The online information office strictly investigates and deals with the behavior of inducing minors to participate in live broadcast reward: the chaos in the live broadcast reward industry must be rect
Leetcode skimming -- drip record 020
Tail recursive call process sorting
Redis是如何实现点赞、取消点赞的?(荣耀典藏版)
CDH5、CDH6部署指南(稳)
Meta:从移动技术发展和创新来预测元宇宙的潜在影响
Multithreading et haute concurrence day09
基于SSM+MySQL+Bootstrap+JQuery的在线购物商城电子商务系统
Redis' cache penetration, breakdown, avalanche
Translation of multiple UAV exploration of an unknown region
Library management system based on jsp+servlet+mysql+bootstrap+css
为什么都说软件测试岗是巨坑?10年测试人告诉你千万别上当
马斯克上传大脑很疯狂,Neuralink却早已一地鸡毛
Why do you work as a test / development programmer? Description of all partners
[learning notes] take you to learn 01trie from 0
30出头成为复旦博导,陈思明:敲代码和写诗,我两样都要