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
2
3
4
5
6
7
public interface ConfigCenter {

void init(String serverAddr, String env);

void subscribeRulesChange(RulesChangeListener listener);
}

1
2
3
4
5
public interface RulesChangeListener {

void onRulesChange(List rules);
}

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
2
3
4
5

com.alibaba.nacos
nacos-client
2.0.4

After that, nacos has provided us with configService, a class that helps us quickly pull Nacos configurations. The usage is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
private static final String DATA_ID = "api-gateway";

private String serverAddr;

private String env;

private ConfigService configService;

@Override
public void init(String serverAddr, String env) {
this.serverAddr = serverAddr;
this.env = env;

try {
this.configService = NacosFactory.createConfigService(serverAddr);
} catch (NacosException e) {
throw new RuntimeException(e);
}
}

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
2
3
4
5
6

String configJson = configService.getConfig(DATA_ID, env, 5000);

log.info("config from nacos: {}", configJson);
List rules = JSON.parseObject(configJson).getJSONArray("rules").toJavaList(Rule.class);

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Override
public void subscribeRulesChange(RulesChangeListener listener) {
try {

String configJson = configService.getConfig(DATA_ID, env, 5000);

log.info("config from nacos: {}", configJson);
List rules = JSON.parseObject(configJson).getJSONArray("rules").toJavaList(Rule.class);

listener.onRulesChange(rules);

configService.addListener(DATA_ID, env, new Listener() {

@Override
public Executor getExecutor() {
return null;
}

@Override
public void receiveConfigInfo(String configInfo) {
log.info("config from nacos: {}", configInfo);
List rules = JSON.parseObject(configInfo).getJSONArray("rules").toJavaList(Rule.class);
listener.onRulesChange(rules);
}
});

} catch (NacosException e) {
throw new RuntimeException(e);
}
}

More to the point is this line of code

1
2

configService.addListener(DATA_ID, env, new Listener()

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.