In the above article, we have roughly completed the service registration function of the registration center, in this article we will implement the configuration center configuration pull and configuration change listening function. Still first need to define a configuration center interface to initialize the configuration center configuration and configuration center information change listening event.
1 | public interface ConfigCenter { |
1 | public interface RulesChangeListener { |
After completing the simple interface definition, start thinking about how to implement the specific configuration pull. We still need to introduce the Nacos client first.
1 |
|
After that, nacos has provided us with configService, a class that helps us quickly pull Nacos configurations. The usage is as follows:
1 | private static final String DATA_ID = "api-gateway"; |
The above has completed the initialization of the configuration center, then we can use the methods provided by the configuration center to pull our configuration
1 |
|
Here you can parse your configuration any way you like.
As usual, we also need to subscribe to the configuration change event. The method is as follows:
1 | @Override |
More to the point is this line of code
1 |
|
He will help us add one of our listeners to the list of Nacos listeners, so that when the configuration of Nacos changes, we can listen to the event and execute the processing logic we want.
At this point, the integration of Nacos is simply complete. This module requires a deep understanding of the Nacos source code and interface, so it is recommended that you finish learning Nacos before reading this series of articles.