Update Nacos config docs

pull/2513/head
Freeman Lau 3 years ago
parent 6f04a247fd
commit c04f3b6b58

@ -5,7 +5,7 @@
Spring Boot 2.4.0 版本开始默认不启动 bootstrap 容器
本项目演示如何在 Spring boot >= 2.4.0 版本不启用 bootstrap 容器情况下如何使用 nacos
[Nacos](https://github.com/alibaba/Nacos) 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
***<font color=red>适用于 Spring boot >= 2.4.0 并且使用 import 方式导入配置,将不会再默认拉取配置,需要手动配置 dataId。</font>***
## 示例

@ -0,0 +1,99 @@
# Nacos Config 2.4.x Example
## Project Instruction
Spring Boot version 2.4.0 does not start the bootstrap container by default.
This project demonstrates how to use nacos when Spring boot >= 2.4.0 version does not enable the bootstrap container.
***<font color=red>Applicable to Spring boot >= 2.4.0 and import the configuration using the import method, the configuration will no longer be pulled by default, and the dataId needs to be configured manually.</font>***
## Demo
### How to use
1. First, modify the pom.xml file and introduce Nacos Config Starter
```xml
<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>
</dependency>
```
2. Configure Nacos Config metadata in the application's src/main/resources/***application.yml*** configuration file
```yaml
server:
port: 8888
spring:
application:
name: nacos-config-import-example
cloud:
nacos:
config:
group: DEFAULT_GROUP
server-addr: 127.0.0.1:8848
config:
import:
- optional:nacos:test.yml
- optional:nacos:test01.yml?group=group_02
- optional:nacos:test02.yml?group=group_03&refreshEnabled=false
```
3. Create test.yml in nacos
```yaml
configdata:
user:
age: 21
name: freeman
map:
hobbies:
- art
- programming
intro: Hello, I'm freeman
users:
- name: dad
age: 20
- name: mom
age: 18
```
4. After completing the above operations, the application will obtain the corresponding configuration from Nacos Config and add it to the PropertySources of Spring Environment
```java
// controller
@RestController
public class UserController {
@Autowired
private UserConfig userConfig;
@GetMapping
public String get() throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(userConfig);
}
}
// ConfigurationProperties
@ConfigurationProperties(prefix = "configdata.user")
public class UserConfig {
private String name;
private Integer age;
private Map<String, Object> map;
private List<User> users;
// getters and setters ...
public static class User {
private String name;
private Integer age;
// getters and setters ...
}
}
```
Verify dynamic refresh
access http://localhost:8888
Then modify the configuration from nacos, and visit again to verify that the dynamic configuration takes effect.

@ -3,7 +3,8 @@
## 项目说明
本项目演示如何使用 Nacos Config Starter 完成 Spring Cloud 应用的配置管理。
注意: 适用于 spring boot 版本低于 2.4.0
***<font color=red>注意: 适用于 spring boot 版本低于 2.4.0,如果版本高于 2.4.0,考虑使用 import 方式导入配置。</font>***
[Nacos](https://github.com/alibaba/Nacos) 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

@ -5,6 +5,8 @@
This example illustrates how to use Nacos Config Starter implement externalized configuration for Spring Cloud applications.
Note: Applicable to spring boot version lower than 2.4.0
***<font color=red>Note: Applicable to spring boot versions lower than 2.4.0, if the version is higher than 2.4.0, consider using import to import the configuration.</font>***
[Nacos](https://github.com/alibaba/Nacos) an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
## Demo

Loading…
Cancel
Save