NACOS
nacos是阿里开源的微服务注册中心+配置中心.本文主要讲nacos作为配置中心的功能.相比传统的Spring Cloud Config需要bus消息总线+消息队列来实现配置动态刷新,nacos自身即可刷新配置,极大简化的开发成本.
nacos的注册中心与配置中心分开使用不同的jar包,本文只使用nacos-config.
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
nacos属性
namespace
- 命名空间,通常用于区分dev/master等运行环境.默认即public
group
- 第二层级,通常用来划分项目.默认即DEFAULT_GROUPP
- Data Id
dataId使用${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}**.其中**-active**可省略,即**${spring.application.name}.${spring.cloud.nacos.config.file-extension}. 一些多环境通用配置项可以使用这种方式来配置.
file-extension
- 支持yml和properties
yml
spring:
application:
name: nacos-config
profiles:
active: online
cloud:
nacos:
config:
server-addr: localhost:1111
file-extension: yml
server:
port: 8084
使用@RefreshScope注解来实现配置动态刷新
@RefreshScope
public class NacosController {
@Value("${trevis.hobby}")
private String hobby;
配置共享
- 使用${spring.application.name}.${spring.cloud.nacos.config.file-extension}方式.
2.使用shared-configs
spring:
application:
name: nacos-config
profiles:
active: dev
cloud:
nacos:
config:
server-addr: localhost:1111
file-extension: yml
shared-configs: nacos-config-shared.yml
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。