This topic describes how to develop and test a demo Spring Cloud application in an on-premises environment, manage the configurations of the application by using Spring Cloud Alibaba Nacos Config, and manage and push the configurations by using Serverless App Engine (SAE).
Prerequisites
Before you develop an application, make sure that the following operations are performed:
Download Maven and configure environment variables.
Start Nacos Server.
Download and decompress the Nacos Server package.
Go to the nacos/bin directory and start Nacos Server.
Linux, Unix, or macOS: Run the
sudo sh startup.sh -m standalonecommand.Windows: Run the
startup.cmd -m standalonecommand.
Notestandaloneindicates that the startup.cmd file is run in standalone mode, not cluster mode. By default, the startup.cmd file is started in cluster mode. If you double-click the startup.cmd file to run the file in a Windows system, the startup fails. In this case, you must configureMODE="standalone"in the startup.cmd file. For more information, see Quick Start for Nacos.
Create a configuration in the on-premises Nacos Server console.
Log on to the Nacos Server console on an on-premises server. The default username and password are
nacos).In the left-side navigation pane, click Configurations. On the Configurations page, click the
icon in the upper-right corner.On the Create Configuration page, configure the following parameters and click Publish:
Data ID: nacos-config-example.properties
Group: DEFAULT_GROUP
Configuration Content: test.name=nacos-config-test
Background
When you develop a Spring Cloud application, you can use Nacos to manage the configurations of the application. SAE is integrated with open source Nacos of Application Configuration Management (ACM). After you deploy an application on SAE, you can use SAE to manage and push the configurations of the application.
In this example, a demo Spring Cloud application is developed and Spring Cloud Alibaba Nacos Config is used to manage the configurations of the application. You can click nacos-config-example.zip to download the demo application.
Spring Cloud Alibaba Nacos Config integrates the Nacos and Spring Cloud frameworks and supports the configuration injection specification of Spring Cloud.
Step 1: Use Nacos Config to manage application configurations
Create a Maven project named nacos-config-example.
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.
NoteSpring Cloud Edgware is discontinued. We recommend that you do not use Spring Cloud Edgware to develop applications.
In src\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 the simpleEchoControllercontroller. TheuserNameproperty is automatically injected. Then, add the@Valueannotation 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; } }In the
src\main\resourcesdirectory, create thebootstrap.propertiesconfiguration file. In thebootstrap.propertiesfile, add the following configuration items to specify the address of Nacos Server.In the following configuration items,
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 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:8848Run the main function in the
NacosConfigExampleApplicationclass to start the application.
Step 2: Deploy the application on SAE
After you develop and test your application in an on-premises environment, you can deploy the application on SAE. For more information, see Deploy applications.
The configuration management center of SAE provides the official commercial version of Nacos Server. When you deploy an application on SAE, SAE specifies the following information at a higher priority: the IP address and the service port of Nacos Server, namespace, access-key, secret-key, and context-path. You do not need to configure additional settings. You can retain or delete the original configurations.
The first time you deploy an application in the SAE console, you must set the Application Runtime Environment parameter to Standard Java Application Runtime Environment if you select Deployment with JAR Packages when you create the application.

Before you deploy the application, use the configuration management feature of SAE to create the same application configurations as those in on-premises Nacos Server.
Log on to the SAE console. In the left-side navigation pane, click Namespace, and then select a region in the top menu bar.
On the Namespace page, click the name of the desired namespace.
On the page that appears, choose in the left-side navigation pane. On the Configurations page, click Create Configuration.
In the Create Configuration panel that appears, configure the parameters, and click Create. The following table describes the parameters.
Parameter
Description
Data ID
The ID of the configuration. In this example, nacos-config-example.properties is specified.
To ensure that the ID is globally unique, specify an ID in the format that follows the
package.classnaming convention. Example:com.taobao.tc.refund.log.level. We recommend that you set the class parameter to a configuration name that has a business meaning.Group
The name of the configuration group. In this example,DEFAULT_GROUP is used.
To ensure that the group name is globally unique, we recommend that you specify a group name in the
product name:module nameformat. Example:ACM:Test. You can perform authentication by group.Data Encryption
Specifies whether to encrypt the configuration data. If your configuration contains sensitive data, we recommend that you enable the data encryption feature to prevent the sensitive data from being leaked.
ImportantBefore you can enable the data encryption feature, you must activate Key Management Service (KMS) and grant ACM the permissions to use KMS for data encryption and decryption. The data encryption feature of ACM depends on KMS. You must activate KMS and grant the required permissions to ACM. The Data ID of an encrypted configuration starts with cipher-. For more information, see Create and use an encrypted configuration file.
Configuration Format
The format of the configuration content. In this example, TEXT is selected.
Configuration Content
The configuration content. In this example, test.name=nacos-config-test is specified.
Configuration Description
The description of the configuration.
More Configuration
Application
The name of the application to which the configuration belongs.
Label
The labels. You can manage configurations by label. You can add up to five labels.
Step 3: Verify the result
After you deploy an application, you can view the related logs to check whether the application is started.
Run the
curl http://<application instance IP>:<service port>command.For example, you can run the
curl http://192.168.0.34:8080command to check whether the configuration contentnacos-config-testis returned.Log on to the SAE console, change the original configuration content to
nacos-config-test2, and then run thecurl http://<application instance IP>:<service port>command.For example, you can run the
curl http://192.168.0.34:8080command to check whether the new configuration contentnacos-config-test2is returned.
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.