Manage application configurations
This topic walks you through building a demo Spring Cloud application that uses Spring Cloud Alibaba Nacos Config for configuration management, then deploying it on SAE and managing its configurations through the SAE console.
Keeping configuration values in code makes deployments fragile—changing a single setting requires rebuilding and redeploying the application. By externalizing configuration with Nacos, you can update values at runtime without touching the code or restarting the service. SAE integrates with open-source Nacos through Application Configuration Management (ACM). After you deploy an application on SAE, the SAE console manages and pushes configuration changes directly to running instances.
This topic uses the nacos-config-example demo application. Download nacos-config-example.zip to follow along.
Prerequisites
Before you begin, ensure that you have:
-
Maven downloaded and added to your system PATH
-
A local Nacos Server running in standalone mode (see Start Nacos Server below)
-
A configuration created in the local Nacos Server console (see Create a configuration in local Nacos Server below)
Start Nacos Server
-
Download and extract the Nacos Server package.
-
Go to the
nacos/bindirectory and run the startup command for your OS:-
Linux, Unix, or macOS:
sudo sh startup.sh -m standalone -
Windows:
startup.cmd -m standalone
The
-m standaloneflag starts Nacos in standalone mode instead of cluster mode (the default). On Windows, double-clickingstartup.cmdstarts it in cluster mode and fails. To fix this, open the file and setMODE="standalone". See Quick Start for Nacos for details. -
Create a configuration in local Nacos Server
-
Log in to the Nacos Server console. The default username and password are both
nacos. -
In the left navigation pane, click Configurations. On the Configurations page, click the
icon in the upper-right corner. -
On the Create configuration page, set the following parameters and click Publish:
-
Data ID:
nacos-config-example.properties -
Group:
DEFAULT_GROUP -
Configuration Content:
test.name=nacos-config-test
-
Step 1: Use Nacos Config to manage application configurations
Version compatibility
This example uses Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1. Choose the Spring Cloud Alibaba version that matches your Spring Cloud release:
| Spring Cloud release | Spring Cloud Alibaba version |
|---|---|
| Greenwich | 2.1.1.RELEASE |
| Finchley | 2.0.1.RELEASE |
| Edgware | 1.5.1.RELEASE (discontinued; do not use) |
Build the application
-
Create a Maven project named
nacos-config-example. -
Add the following dependencies to
pom.xml. This example uses Spring Cloud Greenwich, sospring-cloud-starter-alibaba-nacos-configis version2.1.1.RELEASE.<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.1.1.RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> -
In
src/main/java, create thecom.aliware.edaspackage. -
In the
com.aliware.edaspackage, create theNacosConfigExampleApplicationstartup class:import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class NacosConfigExampleApplication { public static void main(String[] args) { SpringApplication.run(NacosConfigExampleApplication.class, args); } } -
In the
com.aliware.edaspackage, create theEchoControllercontroller:-
@Value("${test.name}")injects the value of thetest.namekey from Nacos Config. -
@RefreshScopecauses the bean to be re-created when Nacos pushes a configuration change, so the injected value updates automatically without a restart. Without this annotation, the application reads the configuration only at startup and ignores subsequent changes.
import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope public class EchoController { @Value("${test.name}") private String userName; @RequestMapping(value = "/") public String echo() { return userName; } } -
-
In
src/main/resources, createbootstrap.propertiesand add the following configuration to connect the application to your local Nacos Server.127.0.0.1:8848is the default Nacos Server address;18081is the application's service port. Change the address and port if your Nacos Server runs elsewhere.spring.application.name=nacos-config-example server.port=18081 spring.cloud.nacos.config.server-addr=127.0.0.1:8848For all available configuration options, see Configuration reference.
-
Run the
mainmethod inNacosConfigExampleApplicationto start the application locally.
Step 2: Deploy the application on SAE
After testing locally, deploy the application on SAE. For deployment instructions, see Deploy applications.
How SAE overrides Nacos settings
When the application runs on SAE, SAE automatically injects the following Nacos connection details at a higher priority than your bootstrap.properties values: the Nacos Server IP address and service port, namespace, access key, secret key, and context path. No changes to bootstrap.properties are required—you can keep or remove the original values.
Runtime environment
The first time you deploy via the SAE console using Deployment with JAR Packages, set Application Runtime Environment to Standard Java Application Runtime Environment.
Create the matching configuration in SAE
Before deploying, recreate the same configuration in SAE's configuration management so the application can read it after deployment.
-
Log in to the SAE console. In the left navigation pane, click Namespace, then select a region from the top menu bar.
-
On the Namespace page, click the name of the target namespace.
-
In the left navigation pane, choose Distributed Configuration Management > Configurations. On the Configurations page, click Create Configuration.
-
In the Create configuration panel, set the parameters and click Create.
Parameter Description Data ID The configuration identifier. Use nacos-config-example.propertiesfor this example. For uniqueness, follow thepackage.classnaming convention—for example,com.taobao.tc.refund.log.level.Group The configuration group. Use DEFAULT_GROUPfor this example. For uniqueness, use theproduct name:module nameformat—for example,ACM:Test. Groups support authentication.Data Encryption Enables encryption for configurations that contain sensitive values. Requires Key Management Service (KMS) activation and ACM permission grants. Encrypted configurations have a Data ID prefixed with `cipher-`. See Create and use an encrypted configuration. Configuration Format The format of the configuration content. Select TEXT for this example. Configuration Content The configuration content. Enter test.name=nacos-config-testfor this example.Configuration Description An optional description for the configuration. More Configurations Application (under More Configuration) The application name the configuration belongs to. Label (under More Configuration) Labels for organizing configurations. Up to five labels per configuration.
Step 3: Verify the result
After the application starts on SAE, verify that it reads the configuration from SAE correctly.
-
Run the following command. Replace
<application instance IP>and<service port>with your application's actual values.curl http://<application instance IP>:<service port>Example:
curl http://192.168.0.34:8080The command returns
nacos-config-testif the application loaded the configuration successfully. > Tip: If the command does not return the expected value, check the application logs in the SAE console to confirm that Nacos connected successfully, and verify that the Data ID and Group in SAE match the values inbootstrap.properties. -
In the SAE console, update the configuration content from
nacos-config-testtonacos-config-test2, then run thecurlcommand again.curl http://192.168.0.34:8080The command now returns
nacos-config-test2, confirming that SAE pushed the updated configuration to the running application without a restart. The@RefreshScopeannotation onEchoControllertriggers the automatic refresh.
Configuration reference
Add any of the following items to bootstrap.properties as needed.
| Configuration item | Key | Default | Required | Description |
|---|---|---|---|---|
| Server address | spring.cloud.nacos.config.server-addr |
None | Yes | The address of Nacos Server. |
| Data ID prefix | spring.cloud.nacos.config.prefix |
${spring.application.name} |
No | The prefix of the Data ID. |
| Group | spring.cloud.nacos.config.group |
DEFAULT_GROUP |
No | The configuration group. |
| Data ID suffix and configuration format | spring.cloud.nacos.config.file-extension |
properties |
No | The Data ID suffix and configuration file format. Supported values: properties, yaml, yml. |
| Content encoding | spring.cloud.nacos.config.encode |
UTF-8 |
No | The encoding of the configuration content. |
| Retrieval timeout | spring.cloud.nacos.config.timeout |
3000 |
No | The timeout for retrieving configuration, in milliseconds. |
| Namespace | spring.cloud.nacos.config.namespace |
None | No | Isolates configurations across environments (for example, development, test, and production). |
| Relative path | spring.cloud.nacos.config.context-path |
None | No | The relative path of the server API. |
| Endpoint | spring.cloud.nacos.config.endpoint |
UTF-8 |
No | The endpoint of a service in the region. Use this to dynamically resolve the server address. |
| Enable listener and auto-refresh | spring.cloud.nacos.config.refresh.enabled |
true |
No | Enables configuration listening and auto-refresh. Leave this at the default value. |
For the full list of configuration items, see the Spring Cloud Alibaba Nacos Config documentation.
What's next
-
Deploy applications — Deploy your Spring Cloud application on SAE using JAR packages or other methods.
-
Create and use an encrypted configuration — Protect sensitive configuration values with KMS-backed encryption.