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 Application Configuration Management 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.

Preparations

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

  • 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.
      • On Linux, UNIX, or macOS, run the sh startup.sh -m standalone command.
      • On 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 Add 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 format: Properties
      • Configuration content: test.name=nacos-config-test

Use Nacos Config to manage application configurations

  1. Create a Maven project that is named 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 Spring Cloud Alibaba version is 2.0.1.RELEASE.
    • The version of Spring Cloud Alibaba for Spring Cloud Edgware is 1.5.1.RELEASE.
    Note Spring Cloud Edgware is discontinued. We recommend that you do not use Spring Cloud Edgware to develop applications.
  3. In src\main\java, create a package that is named com.aliware.edas.
  4. In the com.aliware.edas package, create a startup class that is 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 that is named 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 directory, create a configuration file that is named bootstrap.properties, and add the following configuration in bootstrap.properties 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 Configuration items for reference.

     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 the test.name key that you configure in the on-premises Nacos Server console. For more information, see Create a configuration in the on-premises Nacos Server console.

Deploy an application to EDAS

Before you deploy an application to EDAS, you must create the same configuration by using the configuration management feature of EDAS as that created in the on-premises Nacos Server console so that the configuration can be synchronized after the application is deployed.

  1. Create the same configuration in EDAS as that created in the on-premises Nacos Server console.
    1. Log on to the EDAS console.
    2. In the left-side navigation pane, choose Configuration Management > Microservice Configurations.
    3. In the top navigation bar, select a region. On the Configurations page, select a microservice namespace from the Microservice Namespace drop-down list. Then, click Create Configuration.
    4. In the Create Configuration panel, set the parameters to the values of the corresponding parameters that are set when the configuration is created in the on-premises Nacos Server console, and click Create.
      • Data ID: nacos-config-example.properties
      • Group: DEFAULT_GROUP
      • Configuration format: Properties
      • Configuration content: test.name=nacos-config-test
      Note The region and microservice namespace are selected in the previous step, and you cannot change them in this panel.
  2. Deploy an application to EDAS based on your business requirements. For more information, see Overview and Overview.

What to do next

After your application is deployed to EDAS, your application needs to be continuously iterated and updated. If you start Nacos Server, update an application, and verify the result in an on-premises environment, and then deploy the application to EDAS for online verification each time, development efficiency is affected. Therefore, EDAS supports interconnection between on-premises and cloud applications. After an application for which interconnection between on-premises and cloud applications is enabled is started in an on-premises environment, the effect is the same as that of deploying the application to EDAS. You can directly read the configurations in EDAS, the application can call the applications in the cloud, and the applications in the cloud can call the application.

Configuration items for reference

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

Parameter key Default value Description
Server address spring.cloud.nacos.config.server-addr N/A N/A
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 The group to which the configuration item that you want to create belongs.
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 endpoint of a service in the region. You can dynamically obtain the server address based on the endpoint.
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.