配置中心 nacos

1.pom依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

注意 nacos-client 版本问题:

<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>1.2.0</version>
</dependency>

2.配置文件

bootstrap.yml

server:
  port: 8888
spring:
  application:
    name: nacos-config
  cloud:
    nacos:
      discovery:
        server-addr: xxxxxx.com:8848
      config:
        server-addr: xxxxxx.com:8848
        file-extension: yaml

application.yml

spring:
  profiles:
    active: dev

3.主启动

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigApplication {
    public static void main(String[] args) { SpringApplication.run(NacosConfigApplication.class, args); }
}

4.controller使用

@RestController
@RefreshScope
public class ConfigController {
    @Value("${info:defaultValue}")
    private String info;
}

5.Data Id构成

${prefix}-${spring.profiles.active}.${file-extension}
prefix:默认为spring.application.name的值,可以通过spring.cloud.nacos.prefix配置
spring.profiles.active:当前环境对应的profile,如:dev,test,prod等
file-extension:配置文件格式,支持yaml,properties。通过spring.cloud.nacos.config.file-extension配置

posted @ 2021-08-19 10:38  一柒微笑  阅读(87)  评论(0编辑  收藏  举报