You can get the project source code and project information from the video introduction link to effect demo
Nacos
Nacos provides a number of powerful features: service discovery, health check. Nacos supports both DNS-based and RPC-based service discovery. Nacos also provides real-time health checking of services, preventing requests from being sent to unhealthy hosts and services. And Nacos also provides a visual console to facilitate the management of instances and other information. Nacos also provides a dynamic configuration service that allows us to manage the application and service configuration of all environments in a centralized, externalized and dynamic way.
Nacos is one of the registration and configuration centers that I use the most in the process of developing my own projects, and the community of Nacos is more active and the code is easier to read than others. Here is the official Nacos website: Nacos official website I won’t go into too much detail about the features of Nacos in this article. In this chapter, I will use the interfaces exposed by Nacos to accomplish the service registration function and service discovery function of the project.
Completing this chapter will also give you a deeper understanding of the underlying principles of Nacos, the registry principle. The following are some of the articles I have written in the process of learning Nacos, interested can look at. [Using Nacos to implement dynamic thread pooling techniques and Nacos configuration file update listener events](https://blog.csdn.net/Zhangsama1/article/details/131227567?ops_request_misc=%257B%2522request% 255Fid%2522%253A%2522169812851116800213040303%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id =169812851116800213040303&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1rank_v31_ecpm-1 -131227567-null-null.nonecase&utm_term=Nacos&spm=1018.2226.3001.4450) [[Source Code Analysis] How does Nacos use the AP protocol to accomplish data synchronization between servers?] (https://link.juejin.cn?target=https%3A%2F%2Fblog.csdn.net%2FZhangsama1%2Farticle%2Fdetails%2F132143057%3Fops_request_misc%3D% 25257B%252522request%25255Fid%252522%25253A%252522169812851116800213040303%252522%25252C%252522scm%252522%25253A%25253A 25252220140713.130102334.pc%25255Fblog.%252522%25257D%26request_id%3D169812851116800213040303%26biz_id%3D0%26utm_medium%26 3Ddistribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1rank_v31_ecpm-4-132143057-null-null.nonecase%26utm_term% 3DNacos%26spm%3D1018.2226.3001.4450 “https://blog.csdn.net/Zhangsama1/article/details/132143057?ops_request_misc=%257B%2522request% 255Fid%2522%253A%2522169812851116800213040303%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id =169812851116800213040303&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1rank_v31_ecpm-4 -132143057-null-null.nonecase&utm_term=Nacos&spm=1018.2226.3001.4450”) [[Source Code Analysis] How does the Nacos server side update as well as save registry information?] (https://blog.csdn.net/Zhangsama1/article/details/132141120?ops_request_misc=%257B%2522request%255Fid%2522%253A% 2522169812851116800213040303%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id= 169812851116800213040303&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1rank_v31_ecpm-5- 132141120-null-null.nonecase&utm_term=Nacos&spm=1018.2226.3001.4450) [Implementation of Nacos auto-registration principle as well as service registration update and how to save it to the registry](https://link.juejin.cn?target =https%3A%2F%2Fblog.csdn.net%2FZhangsama1%2Farticle%2Fdetails%2F132145216%3Fops_request_misc%3D%25257B%252522request%25255Fid% 252522%25253A%2522169812851116800213040303%252522%25252C%252522scm%252522%25253A%252220140713.130102334.pc%25255Fblog.%252522%252522%252522 25257D%26request_id%3D169812851116800213040303%26biz_id%3D0%26utm_medium%3Ddistribute.pc_search_result.none-task-blog-2blogfirst _rank_ecpm_v1rank_v31_ecpm-6-132145216-null-null.nonecase%26utm_term%3DNacos%26spm%3D1018.2226.3001.4450 “https://blog.csdn.net/ Zhangsama1/article/details/132145216?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169812851116800213040303%2522%252C% 2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=169812851116800213040303&biz_id=0&utm_medium=distribute .pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1rank_v31_ecpm-6-132145216-null-null.nonecase&utm_term=Nacos&spm= 1018.2226.3001.4450”)
Why I chose Nacos was briefly explained in a previous post, so I’ll list a few reasons in detail here:
- Nacos provides allows me to manage all services and metadata in the data center from the perspective of building a microservices platform, the specific reasons you can look at the above my analysis of the Nacos source code, Nacos will be the services of fine-grained division into their respective instances, and we can manage the information of these instances
- Nacos supports both DNS-based and RPC-based service discovery, which means that it provides us with strong service discovery options.
- Nacos provides real-time health checking of services, preventing requests from being sent to unhealthy hosts or service instances, i.e., security.
- Dynamic configuration services allow you to manage application configuration and service configuration across all environments in a centralized, externalized, and dynamic way, which I’ve leveraged before for dynamic configuration of thread pools [check out this article for more details] (https://blog.csdn.net/Zhangsama1/article/details/ 131227567?spm=1001.2014.3001.5502)
Define the service registration and subscription methods
In this step, we will need to define some interfaces that gateway items use to connect to Nacos, the registry, to implement the interfaces that will link our items to the registry. To register a service to the registry, we need to initialize, register, unregister, and subscribe to the service, i.e., we need to write the following interface to provide such an interface and implement the interface methods in the specific registry instance later.
1 | public interface RegisterCenter { |
After implementing the interface, we need to provide a method that listens for configuration changes in the registry. This is a particularly important feature of Nacos as a registry and configuration center, and the interface definition is as follows:
1 | public interface RegisterCenterListener { |
Service Information Loading and Configuration
Based on the above service registration and subscription interfaces, we can roughly write out how to register our gateway to Nacos. Of course, we don’t have a specific implementation of how to register with the Nacos registry, but we can write out a general way to call it.
1 |
|
One of the more important ones is this line of code, which is to load the service provider
1 | ServiceLoader.load(RegisterCenter.class) |
ServiceLoader is a tool for loading service providers in Java, often used to implement a service provider framework. Its role is to find and load service provider implementation classes for specified interfaces or abstract classes that are dynamically registered into the system at runtime so that other components or applications can use their functionality.
Specifically, here is what it does and how it is used:
- Service Interface Definition: First, you need to define a service interface or abstract class, which is an abstract description of the different implementations you want. In your example, RegisterCenter.class seems to be a service interface.
- Service Provider Implementation: Different modules or libraries can provide different implementations of the service interface, these implementation classes can be developed independently of the application and can be loaded at runtime.
- Service provider registration: Each service provider implementation class requires a file to be created in the META-INF/services directory with the fully qualified name of the service interface and the fully qualified name of the service provider implementation class. This tells the Java runtime which classes implement the service interface.
- Load service providers: Using ServiceLoader.load(RegisterCenter.class), you can load all registered service provider implementation classes. This returns a ServiceLoader object that you can iterate over to get instances of all loaded implementation classes.
This mechanism allows applications to dynamically switch between and use different service provider implementations without modifying the source code, thus increasing the scalability and flexibility of the application. It is commonly used in frameworks and libraries to allow developers to plug in their own implementations such as database drivers, loggers, plug-ins, etc.
Implement registering gateways to the registry
To register the gateway to the registry, we first need to introduce the Nacos client dependency.
1 |
|
After that, we can register the service using the service registration method provided in the Nacos client. The way is as follows:
1 |
|
Here you need to have an understanding of the Nacos source code in order for you to understand how to register a service instance to the Nacos registry, which on its side asks for information such as the service’s ip, port, and service name. After completing this step, we have probably successfully registered the service with Nacos.
Implementing Service Subscription
Here we start to realize the service subscription, in order to realize the service subscription, we need to pull all the information of the service above Nacos, and the service information will be constantly updated, so we also need to use a timed task to constantly update our service subscription information. To subscribe to Nacos service information, we need to use the Nacos event listener, NamingEvent. In the Nacos registry, NamingEvent is an event object that represents events related to the service namespace (Naming). NamingEvent is used to listen for and handle changes to the Service Instance in the namespace so that the application can dynamically update the list of Service Instances based on these changes to stay in sync with the registry.
Specifically, NamingEvent is mainly used for the following purposes:
- Listening for changes to Service Instances: The Nacos registry can contain a large number of Service Instances that may change as services come online, go offline, changes to instance metadata, etc. NamingEvent allows applications to register listeners to be notified when changes to Service Instances occur.
- Dynamically update the service instance list: By listening to NamingEvent, applications can get real-time status changes about service instances, so they can update the list of service instances they maintain in a timely manner to ensure that they are using the most up-to-date information about the service instances.
- Implementing Load Balancing: Applications can implement a load balancing policy based on the information provided by NamingEvent, such as selecting appropriate service instances to serve service requests. The load balancing policy can be adjusted based on the availability, health state, and other metadata of the service instances.
- Dynamic routing: Some applications may need to implement dynamic routing, where routing rules are dynamically updated based on changes to service instances to ensure that requests are correctly routed to available service instances.
The rough code implementation is as follows:
1 |
|
At this point, we are done pulling in the latest configuration information at a time when information changes in the Nacos registry. That is, we are done subscribing to the registry.