赞
踩
使用nacos作为配置中心,字段没有被负值
@Value报错
GrpcClient报错
字段刷新
localhost:8848 登陆成功后左上角显示 nacos 版本,此处以2.1为例
docker命令,注意docker宿主机此处端口必须是 1000 的偏移量
docker run --env MODE=standalone --name nacos -d -p 8848:8848 -p 9848:9848 -v /home/docker-data/nacos-data/logs:/home/nacos/logs -v /home/docker-data/nacos-data/conf:/home/nacos/conf nacos/nacos-server
<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>2021.1</version> <exclusions> <exclusion> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-client --> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-client</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
说明:
由于 加载nacos的configuration优先级很高,application.yml满足不了,必须使用bootstrap.yml来配置访问nacos服务端的配置信息
bootstrap.yml
spring: application: name: global cloud: nacos: config: server-addr: ip:8848 group: DEFAULT_GROUP username: nacos password: nacos namespace: b14aef81-2b0e-472d-8b79-e98e4582c3d8 enabled: true extension-configs: - dataId: global.properties group: DEFAULT_GROUP refresh: true - dataId: global_common.properties group: DEFAULT_GROUP refresh: true
ext-config 以过时,使用extension-configs。
读取配置文件的顺序:
源码:
loadSharedConfiguration(composite);
loadExtConfiguration(composite);
loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);
loadSharedConfiguration 对应配置 shared-configs
loadExtConfiguration 对应配置 本地+ extension-configs + spring.application.name。
以当前配置为例 读取nacos服务端三次,分别是 global.properties、global_common.properties、global
loadApplicationConfiguration中又读取了一次global.properties,以设置的profile 修改dataid放到 Set<PropertySource<?>> propertySources 集合中,源码如下:
for (String profile : environment.getActiveProfiles()) {
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
fileExtension, true);
}
package com.example.springcloudgateway7050; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/config") @RefreshScope public class HelloController { @Value("${first_global_config:123}") private String first_global_config; @Value("${hello_global_config:321}") private String hello_global_config; private ConfigurableApplicationContext context; @RequestMapping("/sayHello") public String sayHello() { return first_global_config+ " | " + hello_global_config; } @RequestMapping("/sayRefreshHello") public String sayRefreshHello() { return context.getEnvironment().getProperty("first_global_config") + " || " + context.getEnvironment().getProperty("hello_global_config"); } @Autowired public void context(ConfigurableApplicationContext context) { this.context = context; } }
@Value 遵从spring就可以
至于怎么刷新的大家看下源码就知道了,有时间再细讲。
(ps: 有问题请再评论留言)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。