When you develop Spring Cloud applications, you can use Nacos (https://nacos.io) to manage application configurations in on-premises environments. Nacos is an open source version of Application Configuration Management. After you deploy an application to Enterprise Distributed Application Service (EDAS), you can manage and push application configurations by using the Application Configuration Management service that is integrated with EDAS.

You can follow the instructions in this topic to develop a sample application from scratch and use Spring Cloud Alibaba Nacos Config to manage application configurations. You can also download the nacos-config-example demo of the sample application.

Note
  • Spring Cloud Alibaba Nacos Config integrates Nacos and Spring Cloud frameworks and supports the configuration injection specification of Spring Cloud.
  • The configuration management feature of EDAS does not conflict with that of Nacos.

Prerequisites

Before you develop an application, make sure that the following operations are completed:

  • Download Maven and set environment variables.
  • Download the latest version of Nacos Server.
  • Start Nacos Server.
    1. Decompress the Nacos Server package that you download.
    2. Go to the nacos/bin directory and start Nacos Server.
      • 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 on-premises Nacos Server console.
    1. Log on to the on-premises Nacos Server console. Both the default username and password are nacos.
    2. In the left-side navigation pane, click Configurations. On the Configurations page, click the Create configuration icon icon in the upper-right corner.
    3. On the Create Configuration page, specify the following information and click Publish.
      • Data ID: nacos-config-example.properties
      • Group: DEFAULT_GROUP
      • Configuration Content: test.name=nacos-config-test

Use Nacos Config to manage application configurations

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

    Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 are used in this 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>                        

    Spring Cloud Greenwich is used in this example and the corresponding Spring Cloud Alibaba version is 2.1.1.RELEASE.

    • If you use Spring Cloud Finchley, the corresponding Spring Cloud Alibaba version is 2.0.1.RELEASE.
    • If you use 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 recommend that you do not use this release to develop applications.
  3. In the src\main\java path, create a package and name it com.aliware.edas.
  4. In the com.aliware.edas package, create a startup class for nacos-config-example and name it NacosConfigExampleApplication.
        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 and name it EchoController. The userName property is automatically injected. Then, add the @Value annotation to obtain the value of the test.name key 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 and name it bootstrap.properties. Then, add the following configuration in the bootstrap.properties file to specify the IP address of Nacos Server.

    In the following configuration, 127.0.0.1:8848 indicates 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 corresponding ones. If you have other requirements, add configuration items to the bootstrap.properties file. For more information, see 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 in an on-premises environment

Visit http://127.0.0.1:18081 in your browser. The value nacos-config-test is returned. This value is the value of test.name that you configure in the on-premises Nacos Server console. For more information, see Create a configuration in the on-premises Nacos Server console.

Reference configuration items

If you have other requirements, see the following table and add configuration items to the bootstrap.properties file.

Configuration item key Default value Description
Server address spring.cloud.nacos.config.server-addr None None
Data ID prefix spring.cloud.nacos.config.prefix ${spring.application.name} The prefix of the data ID.
Group spring.cloud.nacos.config.group DEFAULT_GROUP
Data ID suffix and content file format spring.cloud.nacos.config.file-extension properties The suffix of the data ID. This configuration item also specifies the file format of the configuration content. The default value is properties, and yaml and yml are also supported.
Encoding mode of the configuration content 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.
Namespace of the configuration spring.cloud.nacos.config.namespace Namespaces are widely used to isolate the configurations in different environments. For example, you can use namespaces to isolate the 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 dynamically 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. You do not need to modify this configuration item.

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