SpringBoot构建微服务实战 之 Profile决策(二)

前言

最近刚上线的一个项目,由于在Integration、UAT、PRO三种不同的环境上配置文件都不同,而且差别还比较大所以就用了SpringBoot中的spring.profile.active元配置项来做不同ENV中配置文件读取的策略。
使用 spring.profiles.active 参数,搭配@Profile注解,可以实现不同环境下(开发、测试、生产)配置参数的切换

案例

  • spring.profiles.active
    根据springboot的配置文件命名约定,结合active可在不同环境引用不同的properties外部配置。
    参考官方文档:
    在这里插入图片描述

    根据文档描述,我们除application.properties外,还可以根据命名约定(
    命名格式:application-{profile}.properties)来配置,如果active赋予的参数没有与使用该命名约定格式文件相匹配的话,app则会默认从名为application-default.properties 的配置文件加载配置。
    如:spring.profiles.active=hello-world,sender,dev 有三个参数,其中 dev 正好匹配下面配置中的application-dev.properties 配置文件,所以app启动时,项目会先从application-dev.properties加载配置,再从application.properties配置文件加载配置,如果有重复的配置,则会以application.properties的配置为准。(配置文件加载顺序详见官方文档:24. Externalized Configuration)
    在这里插入图片描述
    在application.properties文件中指定profile:

    #配置文件读取策略(dev-uat-pro:分别为开发、测试。生成环境,启动时优先读取指定的策略配置文件)
    spring.profiles.active=dev
    
  • @Profile
    通过@Profile注解匹配active参数,动态加载内部配置
    参考官方文档:
    在这里插入图片描述

    • @Profile注解使用范围:@Configration 和 @Component 注解的类及其方法,其中包括继承了@Component的注解:@Service、@Controller、@Repository等…

    • @Profile可接受一个或者多个参数,例如:

      @Profile({"tut1","hello-world"})
      @Configuration
      public class Tut1Config {
          @Bean
          publicQueuehello() {
              return newQueue("hello");
          }
          @Profile("receiver")
          @Bean
          publicTut1Receiverreceiver() {
              return newTut1Receiver();
          }
          @Profile("sender")
          @Bean
          publicTut1Sendersender() {
              return newTut1Sender();
          }
      }
      
      

      当 spring.profiles.active=hello-world,sender 时,该配置类生效,且第一个@Bean和第三个@Bean生效
      如果spring.profiles.active=hello-world ,则该配置文件生效,第一个@Bean生效
      如果spring.profiles.active=sender ,该配置文件未生效,所以下面的@Bean都不会生效
      如此,当我们的项目需要运行在不同环境,特异化配置又比较多,该注解的优势是相当明显的!

    • @Profile 注解取反.

      @Component
      public class Tut1CommindLineRunner {
          @Profile("usage_message")
          @Bean
          publicCommandLineRunnerusage() {
              return strings -> {
                  System.out.println("This app uses Spring Profiles to control its behavior.\n");
                  System.out.println("Sample usage: java -jar rabbit-tutorials.jar --spring.profiles.active=hello-world,sender");
              };
          }
          @Profile("!usage_message")
          @Bean
          publicCommandLineRunnertutorial() {
              return newRabbitAmqpTutorialsRunner();
          }
      }
      
      

      该@Profile注解接收了两个类似于取反一样的配置参数,该用法我没有找到相关的官方文档,这是在rabbitmq官网,关于springboot amqp 的一段代码。
      通过查看源代码,我发现,其实这个就是一个类似于取反的意思,这里理解为:匹配不上该配置参数的时候,启用该配置,相关源代码:

      package org.springframework.core.env;
      //...
      public abstract classAbstractEnvironmentimplementsConfigurableEnvironment {
          //...
          public booleanacceptsProfiles(String... profiles) {
              Assert.notEmpty(profiles,"Must specify at least one profile");
              String[] var2 = profiles;
              int var3 = profiles.length;
              for(intvar4 =0;var4 < var3;++var4) {
                  String profile = var2[var4];
                  if(StringUtils.hasLength(profile) && profile.charAt(0) == 33) {
                      if(!this.isProfileActive(profile.substring(1))) {
                          return true;
                      }
                  } else if(this.isProfileActive(profile)) {
                      return true;
                  }
              }
              return false;
          }
      
          protected booleanisProfileActive(String profile) {
              this.validateProfile(profile);
              Set<String> currentActiveProfiles =this.doGetActiveProfiles();
              return currentActiveProfiles.contains(profile) || currentActiveProfiles.isEmpty() &&this.doGetDefaultProfiles().contains(profile);
          }
          //...
      }
      

      “!”对应的char 编码为:33
      此处:StringUtils.hasLength(profile) && profile.charAt(0) == 33 ,如果配置以 “!”开头,则将“!”后的字符串与配置进行匹配,并取反,如果为true,则返回true,即active没有配置该配置参数,则返回true,即启动该配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值