Nacos Config Example 项目说明 Spring Boot 2.4.0 版本开始默认不启动 bootstrap 容器,这里添加 Spring Cloud bootstrap
本项目演示如何在 Spring boot = 2.6.3 版本使用启用 spring cloud bootstrap 容器情况下如何使用 nacos
Nacos 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
示例 如何接入
首先,修改 pom.xml 文件,引入 Nacos Config Starter
Spring-cloud
Spring-Cloud-alibaba
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 <project > <properties > <spring.cloud.version > 2021.0.1</spring.cloud.version > <spring-boot-dependencies.version > 2.6.3</spring-boot-dependencies.version > <spring-cloud-alibaba-dependencies.version > 2021.0.1.0</spring-cloud-alibaba-dependencies.version > </properties > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-dependencies</artifactId > <version > ${spring-boot-dependencies.version}</version > <type > pom</type > <scope > import</scope > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > ${spring.cloud.version}</version > <type > pom</type > <scope > import</scope > </dependency > <dependency > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-alibaba-dependencies</artifactId > <version > ${spring-cloud-alibaba-dependencies.version}</version > <type > pom</type > <scope > import</scope > </dependency > </dependencies > </dependencyManagement > <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 > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-bootstrap</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-actuator</artifactId > </dependency > </dependencies > </project >
在应用的 /src/main/resources/bootstrap.yml 配置文件中配置 Nacos Config 元数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 spring: cloud: nacos: config: name: nacos-config-boot-example prefix: nacos-config-boot-example group: DEFAULT_GROUP server-addr: 192.168 .2 .230 :8848 file-extension: yaml refresh-enabled: true config: import: - optional:nacos:test.yml
在应用的 /src/main/resources/application.yml 配置文件中配置 Spring boot
1 2 3 4 5 6 7 8 9 server: port: 8888 spring: application: name: nacos-config-boot-example management: endpoint: health: show-details: always
在 nacos 添加配置
在 nacos 管理端添加,配置管理->配置列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 data: user: age: 1 name: maxzhao map: hobbies: - art - programming intro: Hello world users: - name: dad age: 20 - name: mom age: 18
1 curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos-config-import-example.yaml&group=DEFAULT_GROUP&content=configdata.user.age=1%0Aconfigdata.user.name=james%0Aconfigdata.user.age=17"
完成上述操作后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 @RefreshScope @RestController public class UserController { @Autowired private UserConfig userConfig; @Value("${data.user.name:default}") private String userName; @Value("${data.user.age:25}") private Integer age; @GetMapping public String get () { return "userName=>" + userName + " \nage=>" + age + " \nuserConfig=>" + userConfig.toString(); } } @RefreshScope @Configuration @ConfigurationProperties(prefix = "data.user") public class UserConfig implements Serializable { private String name; private Integer age; private Map<String, Object> map; private List<User> users; public String getName () { return name; } public void setName (String name) { this .name = name; } public Integer getAge () { return age; } public void setAge (Integer age) { this .age = age; } public Map<String, Object> getMap () { return map; } public void setMap (Map<String, Object> map) { this .map = map; } public List<User> getUsers () { return users; } public void setUsers (List<User> users) { this .users = users; } public static class User { private String name; private Integer age; public String getName () { return name; } public void setName (String name) { this .name = name; } public Integer getAge () { return age; } public void setAge (Integer age) { this .age = age; } @Override public String toString () { return "User{" + "name='" + name + '\'' + ", age=" + age + '}' ; } } @Override public String toString () { return "UserConfig{" + "name='" + name + '\'' + ", age=" + age + ", map=" + map + ", users=" + users + '}' ; } }
验证动态刷新
再从 nacos 修改配置, 再次访问即可验证动态配置生效
本文地址: https://github.com/maxzhao-it/blog/post/f034acb2/