One - One Code All

Blog Content

Nacos配置中心 多环境、多维度、可共享 配置方案

运维   2020-05-30 08:47:48

bootstrap引导配置

Nacos Config相关的配置必须在bootstrap.yml或者bootstrap.properties文件内添加。默认的配置文件扩展名为properties

spring:
 application:
   name: alibaba-nacos-config-client
 # nacos config  
 cloud:
   nacos:
     config:
       server-addr: 127.0.0.1:8848

spring.application.name  spring-cloud-starter-alibaba-nacos-config依赖默认会使用该值的内容作为DATA-ID来匹配读取Nacos Config,读取规则下面介绍。

spring.cloud.nacos.config.server-addr 配置nacos server的地址信息。

管理后台

访问:Nacos Console,默认用户名/密码为:nacos/nacos

添加 Nacos Config

通过配置列表内添加配置信息,添加时DATA-ID的组成部分为:{spring.application.name}.{file-extension}  file-extension文件后缀名默认为properties,如果需要修改,在bootstrap文件内进行修改配置spring.cloud.nacos.config.file-extension的值。


通过@Value注解读取配置信息

通过applicationContext#getEnvironment#getProperty方法可以直接获取对应的Nacos Config的配置信息,当然SpringCloud Alibaba也同样支持通过@Value注解来获取配置信息。


@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

   @Value("${spring.mail.username:}")
   private String email;

   @RequestMapping("/test")
   public String test() {
       System.out.println(email);
       String returnStr =  "Hello Spring Cloud !  spring.mail.username: " + email ;
       return returnStr;
   }
}

需要从全局的配置内容中读取spring.mail.username的配置信息,如果没有找到则使用 冒号(:) 后的内容,当然这里我们没有添加任何的默认值,如果没有配置则为空字符串

ConfigController控制器上添加了注解@RefreshScope主要目的是来实时同步通过Nacos Console修改的配置内容。

@RefreshScope注解是SpringCloud内部提供,用于配置热加载

通过applicationContext#getEnvironment#getProperty 获取对应配置。

@SpringBootApplication
public class SpringCloudAlibabaNacosConfigPropertiesApplication {
   /**
    * logger instance
    */
   static Logger logger = LoggerFactory.getLogger(SpringCloudAlibabaNacosConfigPropertiesApplication.class);

   public static void main(String[] args) throws Exception {
       ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringCloudAlibabaNacosConfigPropertiesApplication.class, args);
       while (true) {
           //当动态配置刷新时,会更新到 Enviroment中,因此这里每隔一秒中从Enviroment中获取配置
           String userName = applicationContext.getEnvironment().getProperty("hengboy.name");
           String userAge = applicationContext.getEnvironment().getProperty("hengboy.age");
           logger.info("配置信息,名称:{},年龄:{}", userName, userAge);
           TimeUnit.SECONDS.sleep(1);
       }
   }
}

Data Id

  • Data Id的默认值为${spring.cloud.nacos.config.prefix}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}

  • spring.cloud.nacos.config.prefix的默认值为${spring.application.name}

  • spring.cloud.nacos.config.file-extension的默认值为properties

  • 当spring.profiles.active未配置时,则匹配${spring.application.name}.properties

  • 若设置了spring.profiles.active而Nacos中存在${spring.application.name}.properties时,若还存在${spring.application.name}-${spring.profiles.active}.properties,则默认匹配后者,若不存在,则会自动匹配前者

  • 由于Nacos建议且默认用spring.application.name作为Data Id的前缀,若要在不同服务中共享项目统一配置,则可以通过配置spring.cloud.nacos.config.shared-dataids或spring.cloud.nacos.config.refreshable-dataids来添加共享配置,前者不支持自动刷新,后者支持

  • 配置Namespace的时候不是通过名称,而是通过命名空间的ID。


支持多个profile 切换

支持线上多个profile,和本地profile 当在application.yml中使用spring.profiles.active:dev 时,此时如果有bootstrap.yml,如果nacos后台配置有nacos-config.properties和nacos-config-dev.properties,则读取的配置优先为profiles.active对应的配置内容,即nacos-config-dev.properties。如果nacos-config-dev.properties不存在,则读取为nacos-config.properties中设置的内容,如果此时删除bootstrap.yml 则读取的配置为本地application-dev.yml中的内容。

即: ${sprefix}-${profile}.properties > ${sprefix}-${profile}.properties > application-${profile}.properties


支持配置文件共享,可同时加载多个配置文件

spring:
 application:
   name: nacos-config-client
 # nacos config
 cloud:
   nacos:
     discovery:
       server-addr: 39.97.217.174:8848
     config:
       server-addr: 39.97.217.174:8848
       namespace: d1107543-c767-45a2-b8e4-d73203d6cc26
       # 配置文件后缀名为yaml
       file-extension: properties
       shared-dataids: shareconfig1.yml,shareconfig2.yml
       refreshable-dataids: shareconfig1.yml,shareconfig2.yml
       ext-config:
         - data-id: shareconfig3.yml
           group: SHARE3_GROUP
           refresh: true
         - data-id: shareconfig4.yml
           group: SHARE4_GROUP
           refresh: true


配置方式优缺点


  • shared-dataids
    • 适合于共享配置文件与项目默认配置文件处于相同Group时,直接两条命令就可以搞定

    • 优点:配置方便

    • 缺点:只能在同一Group中

  • ext-config
    • 它可以由开发者自定义要读取的共享配置文件的DataId、Group、refresh属性,这样刚好解决了shared-dataids存在的局限性。

    • 优点:可以与shared-dataids方案结合使用,用户自定义配置。灵活性强

    • 缺点:配置容易出错,要熟悉YAML语法





上一篇:使用configmap中的数值来定义容器的环境变量
下一篇:Grafana+Prometheus实现Ceph监控和钉钉告警

The minute you think of giving up, think of the reason why you held on so long.