Spring Cloud applications use Nacos for configuration management during local development. After you deploy to Enterprise Distributed Application Service (EDAS), Application Configuration Management (ACM) integrated with EDAS takes over configuration management and push automatically.
This tutorial walks through building a Spring Cloud application with Spring Cloud Alibaba Nacos Config:
Start Nacos Server locally and create a configuration
Build a Spring Boot application that reads from Nacos
Verify dynamic configuration refresh
Deploy the application to EDAS
To skip the build steps, download the nacos-config-example.zip demo project.
Spring Cloud Alibaba Nacos Config integrates Nacos with the Spring Cloud framework and follows the Spring Cloud configuration injection specification.
The EDAS configuration management feature does not conflict with open source Nacos.
Prerequisites
Before you begin, ensure that you have:
Maven installed and configured in your system PATH
The latest Nacos Server release downloaded
Step 1: Start Nacos Server
Extract the downloaded Nacos Server archive.
Navigate to the
nacos/bindirectory and start the server in standalone mode:Linux, UNIX, or macOS:
sh startup.sh -m standaloneWindows:
startup.cmd -m standalone
By default, startup.cmd launches in cluster mode. Double-clicking the file directly on Windows causes startup to fail. Either run the command above or add MODE="standalone" to startup.cmd. For details, see Nacos quick start.
Step 2: Create a local configuration
Open the Nacos Server console at http://127.0.0.1:8848/nacos. The default username and password are both
nacos.In the left-side navigation pane, choose ConfigManagement > Configurations.
Click the
icon in the upper-right corner to create a configuration.On the Create Configuration page, set the following parameters and click Publish:
Parameter Value Data ID nacos-config-example.propertiesGroup DEFAULT_GROUPFormat PropertiesConfiguration Content test.name=nacos-config-test
How the Data ID maps to your application
The Data ID follows this naming convention:
${prefix}-${spring.profiles.active}.${file-extension}| Component | Default | Override property |
|---|---|---|
prefix | Value of spring.application.name | spring.cloud.nacos.config.prefix |
spring.profiles.active | Current Spring profile. When empty, the hyphen is omitted: ${prefix}.${file-extension} | -- |
file-extension | properties | spring.cloud.nacos.config.file-extension. Supported formats: properties, yaml, yml |
In this tutorial, the application name is nacos-config-example with no active profile, so the Data ID is nacos-config-example.properties.
Step 3: Build the Spring Boot application
3a. Create a Maven project
Create a Maven project named nacos-config-example.
3b. Add dependencies
Add the following to pom.xml. This example uses Spring Boot 2.1.4.RELEASE with 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>Version compatibility
Match the Spring Cloud Alibaba version to your Spring Cloud release train:
| Spring Cloud release train | Spring Cloud Alibaba version |
|---|---|
| Greenwich | 2.1.1.RELEASE |
| Finchley | 2.0.1.RELEASE |
| ~~Edgware~~ | ~~1.5.1.RELEASE~~ (discontinued -- do not use) |
3c. Create the startup class
Under src/main/java, create the package com.aliware.edas and add the following NacosConfigExampleApplication class:
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);
}
}3d. Create a controller
In the same package, create an EchoController class. The @RefreshScope annotation enables dynamic configuration refresh: when test.name changes in Nacos, the application picks up the new value without a restart.
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;
}
}@RefreshScope recreates the bean when Nacos pushes a configuration change. The auto-refresh listener is enabled by default (spring.cloud.nacos.config.refresh.enabled=true).
3e. Configure the Nacos Server address
Under src/main/resources, create a bootstrap.properties file:
spring.application.name=nacos-config-example
server.port=18081
spring.cloud.nacos.config.server-addr=127.0.0.1:8848127.0.0.1:8848-- the Nacos Server address.18081-- the application port.
If Nacos Server runs on a different machine, update the address and port. See Configuration reference for additional properties.
Step 4: Run and verify locally
Verify the initial configuration
Run the
mainmethod inNacosConfigExampleApplicationto start the application.Open http://127.0.0.1:18081 in a browser. The response is
nacos-config-test-- the value oftest.namethat you set in Step 2.
Verify dynamic configuration refresh
In the Nacos Server console, go to ConfigManagement > Configurations and edit the
nacos-config-example.propertiesentry.Change
test.name=nacos-config-testtotest.name=nacos-config-updatedand click Publish.Refresh http://127.0.0.1:18081. The response now shows
nacos-config-updated, confirming that@RefreshScopeworks without an application restart.
Step 5: Deploy to EDAS
Recreate the same configuration in EDAS before deploying, so the application can read it from the EDAS configuration management module.
5a. Create the configuration in EDAS
Log on to the EDAS console.
In the left-side navigation pane, choose Application Management > Microservice Configurations.
On the Configurations page, select a Region in the top navigation bar and a Microservices Namespace from the dropdown. Then click Create Configuration.
In the Create Configuration panel, set the same parameters as the local Nacos configuration and click Create:
Parameter Value Data ID nacos-config-example.propertiesGroup DEFAULT_GROUPFormat PropertiesConfiguration Content test.name=nacos-config-test
The Region and Microservice Namespace values are inherited from the Configurations page and cannot be changed in this panel.
5b. Deploy the application
Deploy the application to EDAS. Choose a deployment method based on your infrastructure:
Streamline local-to-cloud development
After your application runs on EDAS, you can skip the local Nacos Server setup for subsequent iterations. EDAS provides a local-to-cloud interconnection feature that lets a locally running application:
Read configurations directly from the EDAS configuration management module
Call other cloud-deployed applications and receive calls from them
This gives you the full EDAS runtime behavior without redeploying for every change.
Configuration reference
Add any of these properties to bootstrap.properties to customize Nacos Config behavior:
| Configuration item | Key | Default | Description |
|---|---|---|---|
| Server address | spring.cloud.nacos.config.server-addr | -- | Nacos Server address (required) |
| Data ID prefix | spring.cloud.nacos.config.prefix | ${spring.application.name} | Prefix portion of the Data ID |
| Group | spring.cloud.nacos.config.group | DEFAULT_GROUP | Configuration group |
| Data ID suffix and format | spring.cloud.nacos.config.file-extension | properties | File format for the configuration content. Supported: properties, yaml, yml |
| Encoding | spring.cloud.nacos.config.encode | UTF-8 | Character encoding for configuration content |
| Timeout | spring.cloud.nacos.config.timeout | 3000 | Timeout for fetching configurations, in milliseconds |
| Namespace | spring.cloud.nacos.config.namespace | -- | Isolate configurations across environments (development, test, production) |
| Context path | spring.cloud.nacos.config.context-path | -- | Relative path of the Nacos Server API |
| Endpoint | spring.cloud.nacos.config.endpoint | UTF-8 | Region-specific endpoint for dynamic server address resolution |
| Auto-refresh | spring.cloud.nacos.config.refresh.enabled | true | Enable or disable the configuration change listener |
For the full list of configuration items, see the Spring Cloud Alibaba Nacos Config documentation.