spring.profiles.active配置解析、

11654 ワード

1、springcloudを配置する時、spring.profiles.activeの役割が分かりません.文書を見に行きました.

公式文書を見る:2.1 Evironment Repository:http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html#_encryptionand_decrypt
spring.profiles.active配置解析、_第1张图片
2.1 Evironment Repository Where Shore the configration data for the Config Server?The streategy that governs this behaviour is the Evironment Repository,serving Evironment oject.This Evironment is a show copy of the doman from the Spring Eviroment
『appplication』は、which maps to spring.appplication.name on the client side.{profile}which maps to spring.profiles.active on the client.{label}aahich Spserver siver ver thererererererererererererereaaaaaafffffrererererererereininininininininininininininininaaaaaaaaaaaaaaaaaaaafffffffrerererererererererererererererererererererererereinininininininininininininininはい、loading configration files from a spring.name equal to the{aplication}parameter,and spring.profiles.active equal to the{profiles}parameter.Prcededencrules forprofifiles Spfiles splalalalalalathe proproproproprofirererererererererererererererererererererererererererererererererererererererererererererereres s s s s s s s s s spspspaaasosososososothethethethethethetheaaaaaaaaaathethethethethethe proprole profiles,the last one wins(simiar to adding entries to a Map)
The follwing sample client appration has this boot stration:
bootstrap.yml.
spring:
  application:
    name: foo
  profiles:
    active: dev,mysql
(As usual with a Spring Boot application、these properties could also be set by environment variables or comand line argments).
If the repository isfile- based,the server creates n Enevironment from appplication.yml(shred between alalalclients)and foo.yml(with foo.ymmmmmmmmmmmmmltatatitititipreededededededededededededededededededededededencce).If theaf theeeeeeeeeeeeeeemitheeemitheemitheaf theaf theemithe af theaf theemimimimimimimimimimimimimimimimimimimimitheefileefile).If there are profile-specific YAML files,these are also appied with higher precedefaults.Higher precedefites to a PropertySource rested earlier Spare the Eviroment.appinment.appine.appine.appine.appine.appine.approment.
You can set spring.cloud.com nfig.server.accept-empty to false so that Server would return a HTTP 404 status,if the appication is not found.By default,this flags set true.
公式文書によると、以下の点が分かります.
spring.profiles.active:dev
1、dev、pro、testはそれぞれ:開発、生産、テスト環境の配置2、私達はappication.propertiesのほかに、ネーミングの約束(ネーミングフォーマット:aplication-properties)によってもいいです.を選択して設定します.activeが与えられたパラメータが、この名前を使って約束されたフォーマットファイルと一致していない場合、appはデフォルトでappliation-default.propertiesという名前のプロファイルから配置されます.起動時、プロジェクトはまずaplication-dev.propertiesから構成をロードして、更にappication.propertiesから構成をロードします.
2、多環境構成:
spring.profiles.active配置解析、_第2张图片
Spring Cloud Cofigalso includes support forcorerererererererererererererererererererererererererererererererererererererererererererererererererererererererereeeemens with patterntttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t ample:
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: special*/dev*,*special*/dev*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo
If{appication}/{profile}does not match any of the patterns,it uses the default URI defined under spring.cloud.server.uri.In the above example,for the"simple"fireporation/the mation mple"/the the matle.repository matches all auttication names beginning with local in all profiles(the/*suffix is added atomatic ally to any pattern that dost have a profile matcher)
YAML配列を使って以下のように構成されています.
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            development:
              pattern:
                - '*/development'
                - '*/staging'
              uri: https://github.com/development/config-repo
            staging:
              pattern:
                - '*/qa'
                - '*/production'
              uri: https://github.com/staging/config-repo
3、@Profileの使用
  • 使用範囲:@Configrationと@Component注釈の種類と方法は、@Componentの注釈を引き継いだものを含みます.@Service、@Controller、@Repositoryなど…
  • は、1つまたは複数のパラメータを受け入れることができる:
  • /**
     * @auther SyntacticSugar
     * @data 2018/12/5 0005   10:40
     */
    @Profile("{dev},{dev1}")
    @Component
    public class DemoConfig {
        @Bean
        public DemoConfigOne profile1(){
            return new DemoConfigOne("dev");
        }
        @Profile("dev1")
        @Bean
        public DemoConfigTwo profile2(){
            return new DemoConfigTwo("dev1");
        }
        @Profile("dev2")
        @Bean
        public DemoConfigThr profile3(){
            return new DemoConfigThr("dev2");
        }
    }
    
    spring.profiles.active=dev,dev 1の場合には、このプロファイルは有効で、最初と二番目の@Benが有効です.spring.profiles.active=devの場合は、このプロファイルは有効になります.最初の@Benが有効になります.spring.profiles.active=dev 1の場合、このプロファイルは有効になりません.
    推薦原文:https://blog.csdn.net/swordsnapliu/article/details/78540902 および:http://www.leftso.com/blog/111.html 原文の中には官製文書と一致しないところがあります.事情を考慮して参考します.