When developing Spring Cloud applications, you can use Nacos (https://nacos.io) to manage application configurations locally. As Nacos is an open-source version of Application Configuration Management (ACM), after deploying an application to Enterprise Distributed Application Service (EDAS), you can manage and push application configurations through EDAS-integrated ACM.

You can follow the instructions in this topic to develop an application sample from scratch and use Spring Cloud Alibaba Nacos Config for configuration management. You can also directly download the demo nacos-config-example.

Note Spring Cloud Alibaba Nacos Config integrates Nacos and Spring Cloud frameworks and supports configuration injection specifications of Spring Cloud.

Preparations

Before you start development, be sure to complete the following tasks:

  • Download Maven and set the environment variables.
  • Download the latest version of Nacos Server.
  • Start Nacos Server.
    1. Decompress the downloaded Nacos Server package.
    2. Go to the nacos/bin directory and start Nacos Server as follows:
      • For Linux, UNIX, or MacOS: Run the sh startup.sh -m standalone command.
      • For Windows: Double-click the startup.cmd file to run the file.

  • Create a configuration in the local Nacos Server console.
    1. Log on to the local Nacos Server console, with nacos as both the default user name and password.
    2. In the left-side navigation pane, click Configurations. On the Configurations page, click the Create icon Add icon in the upper-right corner.
    3. On the Create Configuration page, enter the following information and click Publish.
      • Data ID: nacos-config-example.properties
      • Group: DEFAULT_GROUP
      • Configuration Body: test.name=nacos-config-test

Implement configuration management with Nacos Config

  1. Create a Maven project named nacos-config-example.
  2. Add dependencies to the pom.xml file.

    The following takes Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 as an example.

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

    This example uses Spring Cloud Greenwich, and the corresponding Spring Cloud Alibaba version is 2.1.1.RELEASE.

    • If you are using Spring Cloud Finchley, the corresponding Spring Cloud Alibaba version is 2.0.1.RELEASE.
    • If you are using Spring Cloud Edgware, the corresponding Spring Cloud Alibaba version is 1.5.1.RELEASE.
    Note The Spring Cloud Edgware release has reached the end of life. Therefore, we do not recommend that you use this release to develop applications.
  3. Create a package named com.aliware.edas in src/main/java.
  4. In the com.aliware.edas package, create a startup class named NacosConfigExampleApplication for nacos-config-example.
        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 a simple controller named EchoController, automatically inject a property named userName, and add the @Value annotation to obtain the value of test.name from the configuration.
        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 the src\main\resources path, create a configuration file named bootstrap.properties, and add the following configuration in bootstrap.properties to set the registry address to the IP address of Nacos Server.

    127.0.0.1:8848 is the IP address of Nacos Server, and 18081 is the service port.

    If your Nacos Server is deployed on another machine, change the IP address and service port to the actual ones. If you have other requirements, add configuration items to the bootstrap.properties file by referring to Reference configuration items.

     spring.application.name=nacos-config-example
     server.port=18081
     spring.cloud.nacos.config.server-addr=127.0.0.1:8848                        
  7. Run the main function in NacosConfigExampleApplication to start the application.

Verify the result locally

Visit http://127.0.0.1:18081 in your browser. nacos-config-test is returned, which is the value you configured when creating a configuration in the local Nacos Server console, that is, the value of test.name.

Deploy applications to EDAS

After developing and testing your application in the local development environment, you can package and deploy it to EDAS. You can choose to deploy your Spring Cloud application to an Elastic Compute Service (ECS) cluster, a Container Service Kubernetes cluster, or an EDAS Serverless cluster based on your needs. For more information about how to deploy an application, see Application deployment overview (ECS) and Application deployment overview (Kubernetes clusters).

ACM integrated in EDAS is the GA version of Nacos. When you deploy an application to EDAS, EDAS sets the IP address and service port of Nacos Server, namespace, AccessKey ID, AccessKey secret, and context path by using a method with a higher priority.

Before deploying an application, you need to add the same configuration on the Configuration Management page of the EDAS console as that on the local Nacos Server. The procedure is as follows:

  1. Log on to the EDAS console.
  2. In the left-side navigation pane, choose Application Management > Configuration Management.
  3. On the Configuration Management page, select Region and Namespace, and click the Create icon Create on the right side of the page.
  4. On the Create Configuration page, set Data ID, Group, and Configuration Body, and click Publish.
    • Data ID: nacos-config-example.properties
    • Group: DEFAULT_GROUP
    • Configuration Body: test.name=nacos-config-test

We recommend that you use the console to deploy your application for the first time. If you choose to use JAR packages for initial deployment, be sure to select Standard Java application runtime environment for Application Runtime Environment when creating an application.

Verify the result

  1. After the deployment is completed, check the log to see whether the application is started.
  2. Run the curl http://<application instance IP>:<service port> command, for example, curl http://192.168.0.34:8080, to check whether the configuration body nacos-config-test is returned.
  3. Change the previous configuration body to nacos-config-test2 in the EDAS console, and then run the curl http://<application instance IP>:<service port> command, for example, curl http://192.168.0.34:8080, to check whether the configuration body nacos-config-test2 is returned.

Reference configuration items

If you have other requirements, see the following table and add configurations in the bootstrap.properties file.

Configuration item Key Default value Description
Server address spring.cloud.nacos.config.server-addr None None
Prefix of data ID spring.cloud.nacos.config.prefix ${spring.application.name} The prefix of the data ID.
Group spring.cloud.nacos.config.group DEFAULT_GROUP
Suffix of data ID and content file format spring.cloud.nacos.config.file-extension properties The suffix of the data ID, which is also the file format of the configuration body. The default value is properties, and yaml and yml are also supported.
Encoding mode of the configuration body spring.cloud.nacos.config.encode UTF-8 The encoding mode of the configuration.
Timeout period for retrieving the configuration spring.cloud.nacos.config.timeout 3000 Unit: ms.
Configured namespace spring.cloud.nacos.config.namespace One of the common scenarios is the isolation of configurations in different environments, for example, the isolation of resources in development, test, and production environments.
Relative path spring.cloud.nacos.config.context-path The relative path of the server API.
Endpoint spring.cloud.nacos.config.endpoint UTF-8 The domain name of a service in the region. You can retrieve the server address based on this domain name.
Enable listener and auto-refresh spring.cloud.nacos.config.refresh.enabled true The default value is true, and no modification is required.

For more information about configuration items, see the open-source Spring Cloud Alibaba Nacos Config.