How to load multiple configuration files?

One configuration file can be injected via @nacospropertysource. If the configuration needs to be sorted and stored or some configuration needs to be shared, multiple @nacospropertysource annotations can be used to load multiple configuration files in a project.

@NacosPropertySource(dataId = "nacos-springboot", autoRefreshed = true)
@NacosPropertySource(dataId = "redis", autoRefreshed = true) @SpringBootApplication public class NacosSpringBootApp { public static void main(String[] args) { SpringApplication.run(NacosSpringBootApp.class, args); }}Copy the code

Or @ NacosPropertySources

@NacosPropertySources({
	@NacosPropertySource(dataId = "nacos-springboot", autoRefreshed = true),
	@NacosPropertySource(dataId = "redis", autoRefreshed = true)})Copy the code

You can specify multiple data-ids directly as configuration files, as we’ll explain later in the Spring Cloud.

How do I listen when the configuration is changed?

Let’s start with an older method, which is too cumbersome, as follows:

@NacosInjected
private ConfigService configService;

@PostConstruct
public void init() {
	try {
		onMessage();
	} catch (NacosException e) {
		e.printStackTrace();
	}
}
	
public void onMessage() throws NacosException {
	configService.addListener("nacos-springboot"."DEFAULT_GROUP", new AbstractListener() { public void receiveConfigInfo(String config) { System.err.println(config); }}); }Copy the code

Add a listener using ConfigService to listen for specific configuration files

The following uses the annotation mode to listen, which is simple and convenient and direct, as follows:

@NacosConfigListener(dataId = "nacos-springboot")
public void onMessage(String config) {
    System.out.println(config);
}
Copy the code

Configuration listeners also support multi-type conversions. The above list simply fetches the entire configuration string. We’ll cover more types of conversions later.

Does it support multiple languages?

Nacos itself provides an Http interface through which configuration can be manipulated. However, currently the SDK only supports Java, and other languages will be supported later, but it has not been developed yet. We can see that there is a plan to support multiple languages through the listing code in the background.

IO /zh-cn/docs/…

Which is better than Apollo?

Contrast this piece we can according to their own needs to do a comparison, as long as it meets your needs, it is good for you this framework.

If you’re already using Apollo, I suggest you stay with it. It’s too much work.

If you’re not using Apollo yet, this is a good time to do a detailed comparison when choosing a configuration center. Compare in terms of function points, stability, usage, deployment, etc.

If you use Dubbo in your company, I think Nacos is a good alternative to ZK to add a configuration center.

Is it easy to deploy?

Nacos deployment is very simple, you can download the official compiled package directly, unzip the configuration file and start. The same is true for cluster deployment, which we’ll cover separately later.

At the same time, it also supports Docker deployment, which is convenient for friends familiar with Docker.