Manage application configurations

Updated at:
Copy as MD

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:

Start Nacos Server

  1. Download and extract the Nacos Server package.

  2. Go to the nacos/bin directory 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 standalone flag starts Nacos in standalone mode instead of cluster mode (the default). On Windows, double-clicking startup.cmd starts it in cluster mode and fails. To fix this, open the file and set MODE="standalone". See Quick Start for Nacos for details.

Create a configuration in local Nacos Server

  1. Log in to the Nacos Server console. The default username and password are both nacos.

  2. In the left navigation pane, click Configurations. On the Configurations page, click the add icon icon in the upper-right corner.

  3. 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

  1. Create a Maven project named nacos-config-example.

  2. Add the following dependencies to pom.xml. This example uses Spring Cloud Greenwich, so spring-cloud-starter-alibaba-nacos-config is version 2.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>
  3. In src/main/java, create the com.aliware.edas package.

  4. In the com.aliware.edas package, create the NacosConfigExampleApplication startup 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);
        }
    }
  5. In the com.aliware.edas package, create the EchoController controller:

    • @Value("${test.name}") injects the value of the test.name key from Nacos Config.

    • @RefreshScope causes 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;
        }
    }
  6. In src/main/resources, create bootstrap.properties and add the following configuration to connect the application to your local Nacos Server. 127.0.0.1:8848 is the default Nacos Server address; 18081 is 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:8848

    For all available configuration options, see Configuration reference.

  7. Run the main method in NacosConfigExampleApplication to 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.

t5edYO3jaM

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.

  1. Log in to the SAE console. In the left navigation pane, click Namespace, then select a region from the top menu bar.

  2. On the Namespace page, click the name of the target namespace.

  3. In the left navigation pane, choose Distributed Configuration Management > Configurations. On the Configurations page, click Create Configuration.

  4. In the Create configuration panel, set the parameters and click Create.

    Parameter Description
    Data ID The configuration identifier. Use nacos-config-example.properties for this example. For uniqueness, follow the package.class naming convention—for example, com.taobao.tc.refund.log.level.
    Group The configuration group. Use DEFAULT_GROUP for this example. For uniqueness, use the product name:module name format—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-test for 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.

  1. 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:8080

    The command returns nacos-config-test if 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 in bootstrap.properties.

  2. In the SAE console, update the configuration content from nacos-config-test to nacos-config-test2, then run the curl command again.

    curl http://192.168.0.34:8080

    The command now returns nacos-config-test2, confirming that SAE pushed the updated configuration to the running application without a restart. The @RefreshScope annotation on EchoController triggers 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