When you develop Spring Cloud applications, you can use Nacos to manage application configurations locally. Nacos is the open source version of Application Configuration Management (ACM). After you deploy an application to Enterprise Distributed Application Service (EDAS), you can use the ACM integrated with EDAS to manage and push application configurations.
You can follow the instructions in this document to develop the sample application from scratch and use Spring Cloud Alibaba Nacos Config to manage configurations. You can also download the demo for this sample application: nacos-config-example.
Spring Cloud Alibaba Nacos Config integrates Nacos with the Spring Cloud framework and supports the configuration injection specification of Spring Cloud.
The configuration management feature of EDAS does not conflict with the configuration management feature of open source Nacos.
Prerequisites
Before you start, complete the following tasks:
Download Maven and set the environment variables.
Download the latest version of Nacos Server.
Start Nacos Server.
Decompress the downloaded Nacos Server package.
Go to the nacos/bin directory and start Nacos Server.
For Linux, Unix, or Mac systems, run the
sh startup.sh -m standalonecommand.For Windows systems, double-click the startup.cmd file.
Notestandalonespecifies that Nacos Server runs in standalone mode instead of cluster mode. By default, the startup.cmd file starts Nacos Server in cluster mode. If you double-click the startup.cmd file in Windows, the startup fails. To fix this, you must setMODE="standalone"in the startup.cmd file. For more information, see Nacos Quick Start.
Create a configuration in the local Nacos Server console.
Log on to the local Nacos Server console. The default username and password are both nacos.
In the navigation pane on the left, click Configurations. On the Configurations page, click the create configuration icon
in the upper-right corner.On the Create Configuration page, enter the following information and click Publish.
Data ID: nacos-config-example.properties
Group: DEFAULT_GROUP
Format: Properties
Content: test.name=nacos-config-test
Use Nacos Config to manage configurations
Create a Maven project named nacos-config-example.
Add dependencies to the pom.xml file.
This example uses Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1.
<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>The Spring Cloud version used in this example is Greenwich, which corresponds to Spring Cloud Alibaba version 2.1.1.RELEASE.
If you use the Spring Cloud Finchley version, the corresponding Spring Cloud Alibaba version is 2.0.1.RELEASE.
If you use the Spring Cloud Edgware version, the corresponding Spring Cloud Alibaba version is 1.5.1.RELEASE.
NoteThe lifecycle of the Spring Cloud Edgware version has ended. We recommend that you do not use this version to develop applications.
In \main\java, create the
com.aliware.edaspackage.In the
com.aliware.edaspackage, create theNacosConfigExampleApplicationstartup class for thenacos-config-exampleproject.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); } }In the
com.aliware.edaspackage, create a simple controller namedEchoController. This controller automatically injects a property nameduserNameand uses the@Valueannotation to retrieve 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; } }In the
src\main\resourcesdirectory, create a configuration file namedbootstrap.properties. In thebootstrap.propertiesfile, add the following configuration to specify the address of Nacos Server.In the following configuration,
127.0.0.1:8848is the address of Nacos Server, and18081is the service port.If your Nacos Server is deployed on another machine, change the IP address and port accordingly. If you have other requirements, you can add configurations to the bootstrap.properties file. For more information, see Configuration items.
spring.application.name=nacos-config-example server.port=18081 spring.cloud.nacos.config.server-addr=127.0.0.1:8848Run the main function in the
NacosConfigExampleApplicationclass to start the application.
Verify the results locally
Visit http://127.0.0.1:18081 in your browser. The return value is nacos-config-test. This is the value of the test.name key that you configured in the Create a configuration in the local Nacos Server step.
Deploy the application to EDAS
Before you deploy the application to EDAS, you must create a configuration in EDAS Configuration Management that is identical to the one on your local Nacos Server. This ensures that the configuration is synchronized after the application is deployed. Then, you can deploy the application to EDAS.
Create a configuration in EDAS that is identical to the one on your local Nacos Server.
-
Log on to the EDAS consoleEDAS console.
In the navigation pane on the left, choose .
On the Configurations page, select a region and a microservices namespace, and then click Create Configuration.
In the Create Configuration panel, set the parameters to match your local Nacos Server configuration, and then click Create.
Data ID: nacos-config-example.properties
Group: DEFAULT_GROUP
Format: Properties
Content: test.name=nacos-config-test
NoteThe Region and Microservices Namespace are selected on the Configurations page and cannot be set in this panel.
-
Deploy the application to EDAS. For more information, see Overview of application creation and deployment and Overview of application creation and deployment.
What to do next
After you deploy your application to EDAS, you will need to continuously iterate and upgrade it. The development cycle of starting Nacos Server locally, upgrading the application, verifying it locally, and then deploying it to EDAS for online verification can reduce development efficiency. To solve this issue, EDAS provides a device-cloud interconnection feature. When this feature is enabled, an application that is started locally behaves as if it were deployed to EDAS. It can directly read configurations from EDAS Configuration Management and interact with other applications in the cloud. For more information about device-cloud interconnection, see Introduction to device-cloud interconnection.
Configuration items
If you have other requirements, you can add configurations to the bootstrap.properties file as described in the following table.
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 content file format | spring.cloud.nacos.config.file-extension | properties | The suffix of the Data ID. This also specifies the format of the configuration content. The default format is properties. YAML and YML formats are also supported. |
Encoding of the configuration content | spring.cloud.nacos.config.encode | UTF-8 | The encoding of the configuration. |
Timeout for retrieving the configuration | spring.cloud.nacos.config.timeout | 3000 | The unit is ms. |
Configuration namespace | spring.cloud.nacos.config.namespace | A common use case is to isolate configurations for different environments, such as isolating resources between staging and production environments. | |
Relative path | spring.cloud.nacos.config.context-path | The relative path of the server-side API. | |
Endpoint | spring.cloud.nacos.config.endpoint | UTF-8 | The domain name of a service endpoint in a region. You can use this domain name to dynamically obtain the server address. |
Enable listener and auto-refresh | spring.cloud.nacos.config.refresh.enabled | true | The default value is true. You do not need to change this setting. |
For more information about configuration items, see the open source Spring Cloud Alibaba Nacos Config documentation.