This topic describes how to integrate a Microservices Governance (MSE) Nacos configuration center with Spring Cloud applications. This helps implement centralized configuration management and dynamic refresh for the applications.
Background information
In a microservices architecture, a large number of microservice application instances are deployed across multiple environments, such as development, test, and production environments. The hard-coded or static configuration files of the instances are managed in the traditional manner. This causes issues such as configuration inconsistency and delayed updates. The preceding issues can be resolved by integrating configuration centers with the instances. Configuration information can be dynamically modified, and stored and managed in a centralized manner. This ensures configuration isolation and synchronization between different environments and instances, and improves the flexibility and security of configuration updates.
Prerequisites
Maven is downloaded and environment variables are configured.
A Spring Cloud application is available. If no Spring Cloud application is available, you can use the demo application provided in this topic: nacos-service-config.zip.
A Nacos engine is created. For more information, see Create a Nacos engine.
ImportantIf your application does not reside in the same region as the MSE Nacos configuration center or is not deployed on an Alibaba Cloud instance, you must select Internet as the network type when you create the Nacos engine.
To ensure that your application can access MSE over the Internet, you must configure a whitelist. For more information, see Configure a public IP address whitelist.
A namespace is created. For more information, see Create a namespace.
Overview
You must integrate an MSE Nacos configuration center with the Spring Cloud application, publish the useLocalCache=true configuration, and then publish the useLocalCache=false configuration in the MSE Nacos console. Then, request the related configuration interfaces of the application by using the command-line interface (CLI) to obtain the useLocalCache values. You can verify the implementation of the dynamic refresh feature of the MSE Nacos configuration center based on the results of the two requests.
Create and publish a configuration: Create and publish a configuration in the MSE console.
Integrate an MSE Nacos configuration center: Use an MSE Nacos configuration center to manage the Spring Cloud application configurations based on sample code.
Verify the implementation of the dynamic refresh feature: Modify the configurations in the MSE console to use the dynamic refresh feature of the application.
Step 1: Create and publish a configuration
Log on to the MSE console, and select a region in the top navigation bar.
Find the instance that you want to manage and click the name of the instance.
In the left-side navigation pane, choose .
In the upper part of the page that appears, select the namespace that you created.
Click Add Configuration. In the Add Configuration panel, configure the parameters, as shown in the following figure.
NoteThe configurations in the following figure are only for reference. You can configure the parameters based on your business requirements. For more information about the parameters, see Create a configuration.

After you configure the parameters, click Release. The new configuration is displayed on the Configurations page.
Step 2: Integrate an MSE Nacos configuration center
Optional. If no Spring Cloud application is available, use the demo application provided in this topic.
Add the dependency of the MSE Nacos configuration center to the pom.xml file of the Spring Cloud application.
NoteThe versions of
Spring Cloud Alibaba,Spring Cloud, andSpring Bootmust be matched. You can visit theSpring Cloud Alibabaofficial website to view Release Notes, and select the versions that adapt to your application. We recommend that you upgrade the versions to2023.x.<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2021.0.4.0</version> </dependency>Add Nacos-related configurations to the configuration file of the Spring Cloud application. Sample configurations:
ImportantIf you want to specify a namespace for your application, enter the namespace ID.
# The address of your Nacos server. spring.cloud.nacos.config.server-addr=mse-XXX.nacos-ans.mse.aliyuncs.com:8848 # Specify the MSE Nacos namespace. If you want to access the default public namespace, set this parameter to null or leave this parameter empty. spring.cloud.nacos.config.namespace=24075300-XXXX-XXXX-XXXX-0c17aa9f744b # Import a specific configuration file. spring.config.import=nacos:mse-nacos-config-client.properties?group=DEFAULT_GROUP&refreshEnabled=trueOn the Instances page, you can view the public endpoint of the MSE Nacos configuration center. The public endpoint is in the
mse.XX.nacos.mse.aliyuncs.comformat.
After the preceding operations are complete, run the
mvn clean packagecommand to recompile and package the application.
Step 3: Verify the implementation of the dynamic refresh feature
The following verification operations are based on the demo Spring Cloud application and sample configurations provided in this topic. If you use other Spring Cloud applications or custom configurations of the MSE Nacos configuration center, verify the implementation of the dynamic refresh feature by referring to the following steps.
Start the Spring Cloud application and run the main function of the startup class.
Run the following command on the CLI to query
useLocalCacheof the MSE Nacos configuration center. The useLocalCache=trueconfiguration is returned.# Change the IP address and port number of your application. curl http://Application IP address:18083/getConfigChange the configuration to
useLocalCache=falsein the MSE console and publish the new configuration.Run the preceding command again. The useLocalCache=
falseconfiguration is returned, which indicates that the application can obtain the latestuseLocalCachevalue from the MSE Nacos configuration center without the need to restart the application.
References
For more information about how to migrate applications from an open source self-managed Nacos configuration center to an MSE Nacos configuration center, see Migrate applications from a self-managed to an MSE Nacos configuration center.
For more information about the service registration and discovery feature, see Register Spring Cloud applications with MSE Nacos registries.
For more information about how to integrate traffic gateways with microservice gateways, see Create an MSE cloud-native gateway.
For more information about the service governance and traffic protection features that can be used to decouple middleware from business, see Activate Microservices Governance.
For more information about SchedulerX, see Task Scheduling.