All Products
Search
Document Center

Enterprise Distributed Application Service:Manage application configurations

更新時間:Aug 15, 2023

When you develop Spring Cloud applications, you can use Nacos to manage application configurations in an on-premises environment. 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.

This topic describes how to develop a sample application from scratch and use Spring Cloud Alibaba Nacos Config to manage the configuration of the application. You can also download the nacos-config-example demo to use 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 open source Nacos.

Before you begin

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

  • Download Maven and configure 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 use one of the following methods to start Nacos Server. The methods vary based on your OS:

      • Linux, UNIX, or macOS: Run the sh startup.sh -m standalone command.

      • Windows: double-click the startup.cmd file to run the file.

      Note

      standalone specifies that the standalone mode rather than the cluster mode runs. By default, the startup.cmd file is started in cluster mode. If you double-click the startup.cmd file to run the file in Windows, the file fails to be started. In this case, you must add MODE="standalone" to the startup.cmd file. For more information, see Quick Start for Nacos.

  • Create a configuration in the on-premises Nacos Server console.

    1. Log on to the on-premises Nacos Server console. The default username and password are nacos.

    2. In the left-side navigation pane, choose ConfigManagement > Configurations. In the upper-right corner of the configurations page, click the 增加图标 icon to create a configuration.

    3. On the Create Configuration page, configure the following parameters and click Publish:

      • Data ID: Enter nacos-config-example.properties in the field.

      • Group: Select DEFAULT_GROUP.

      • Format: Select Properties.

      • Configuration Content: Enter test.name=nacos-config-test in the code editor.

Use Spring Cloud Alibaba Nacos Config to manage the configuration

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

  2. Add dependencies to the pom.xml file.

    In this example, Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 are used.

    <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 this example, Spring Cloud Greenwich is used. The version of Spring Cloud Alibaba for Spring Cloud Greenwich is 2.1.1.RELEASE.

    • The version of Spring Cloud Alibaba for Spring Cloud Finchley 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 the com.aliware.edas package.

  4. In the com.aliware.edas package, create the NacosConfigExampleApplication startup class for the nacos-config-example project.

        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 simple EchoController controller. 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 the bootstrap.properties configuration file. In the bootstrap.properties file, add the following configuration items to specify the address of Nacos Server.

    In the following configuration items, 127.0.0.1:8848 is the address of Nacos Server, and 18081 is the service port.

    If your Nacos Server is deployed on another machine, change the IP address and port to the relevant ones. If you have other requirements, add the relevant configuration items in 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 the NacosConfigExampleApplication class to start the application.

Verify the result in an on-premises environment

Visit http://127.0.0.1:18081 in your browser. A value of nacos-config-test is returned. This value is the value of the test.name key that you configured in the Create a configuration in the on-premises Nacos Server console step of this topic.

Deploy an application to EDAS

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

  1. Create the same configuration in EDAS as the configuration created in the on-premises Nacos Server console.

    1. Log on to the EDAS console.

    2. In the left-side navigation pane, choose Application Management > Microservice Configurations.

    3. In the top navigation bar of the Configurations page, select a region where you want to create the configuration. From the Microservices Namespace drop-down list in the upper part, select a microservices namespace for the configuration. Then, click Create Configuration.

    4. In the Create Configuration panel, configure the same parameter settings as the parameter settings configured in the on-premises Nacos Server console, and click Create.

      • Data ID: Enter nacos-config-example.properties in the field.

      • Group: Select DEFAULT_GROUP.

      • Format: Select Properties.

      • Configuration Content: Enter test.name=nacos-config-test in the code editor.

      Note

      The Region and Microservice Namespace parameters were configured on the Configurations page. You cannot modify the parameters in this panel.

  2. Deploy the application to EDAS based on your business requirements. For more information, see Overview (ECS) and Overview (Kubernetes).

What to do next

After your application is deployed to EDAS, you can continuously iterate and update the application. 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 provides the feature of interconnection between on-premises and cloud applications. After you enable the feature for an application and start the application in an on-premises environment, the effect is the same as deploying the application to EDAS. The configuration of the application can be read from the configuration management module of EDAS, and mutual calls can be implemented between the application and another cloud application.

Configuration items for reference

If you have other requirements, you can refer to 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

The group.

Data ID suffix and configuration file format

spring.cloud.nacos.config.file-extension

properties

The suffix of the data ID. This configuration item also specifies the format of the configuration file. The default value is properties. YAML and YML formats are supported.

Encoding method of the configuration content

spring.cloud.nacos.config.encode

UTF-8

The encoding method of the configuration content.

Timeout period for retrieving the configuration

spring.cloud.nacos.config.timeout

3000

Unit: milliseconds.

Namespace of the configuration

spring.cloud.nacos.config.namespace

Namespaces are widely used to isolate 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 the configuration items, see the open source Spring Cloud Alibaba Nacos Config documentation.