1、在配置springcloud 时候、不了解spring.profiles.active 有什么作用、看看文档去了只有、
》
查看官方文档:
2.1 Environment Repository:
http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html#_encryption_and_decryption
2.1 Environment Repository
Where should you store the configuration data for the Config Server? The strategy that governs this behaviour is the EnvironmentRepository, serving Environment objects. This Environment is a shallow copy of the domain from the Spring Environment (including propertySources as the main feature). The Environment resources are parametrized by three variables:
{application}, which maps to spring.application.name on the client side.
{profile}, which maps to spring.profiles.active on the client (comma-separated list).
{label}, which is a server side feature labelling a “versioned” set of config files.
Repository implementations generally behave like a Spring Boot application, loading configuration files from a spring.config.name equal to the {application} parameter, and spring.profiles.active equal to the {profiles} parameter. Precedence rules for profiles are also the same as in a regular Spring Boot application: Active profiles take precedence over defaults, and, if there are multiple profiles, the last one wins (similar to adding entries to a Map).
The following sample client application has this bootstrap configuration:
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 command line arguments).
If the repository is file-based, the server creates an Environment from application.yml (shared between all clients) and foo.yml (with foo.yml taking precedence). If the YAML files have documents inside them that point to Spring profiles, those are applied with higher precedence (in order of the profiles listed). If there are profile-specific YAML (or properties) files, these are also applied with higher precedence than the defaults. Higher precedence translates to a PropertySource listed earlier in the Environment. (These same rules apply in a standalone Spring Boot application.)
You can set spring.cloud.config.server.accept-empty to false so that Server would return a HTTP 404 status, if the application is not found.By default, this flag is set to true.
根据官方文档我们知道以下几点:
spring.profiles.active:dev
1、dev、pro、test分别为:开发、生产、测试环境配置
2、我们除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配置文件加载配置;
2、多环境配置:
Spring Cloud Config also includes support for more complex requirements with pattern matching on the application and profile name. The pattern format is a comma-separated list of {application}/{profile} names with wildcards (note that a pattern beginning with a wildcard may need to be quoted), as shown in the following example:
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 {application}/{profile} does not match any of the patterns, it uses the default URI defined under spring.cloud.config.server.git.uri. In the above example, for the “simple” repository, the pattern is simple/* (it only matches one application named simple in all profiles). The “local” repository matches all application names beginning with local in all profiles (the /* suffix is added automatically to any pattern that does not 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等…
- 可接受一个或者多个参数:
/**
* @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,dev1 时,该配置类生效,且第一个和第二个@Bean生效
如果spring.profiles.active=dev,则该配置文件生效,第一个@Bean生效
如果spring.profiles.active=dev1,该配置文件未生效,所以下面的@Bean都不会生效
推荐原文:https://blog.csdn.net/swordsnapliu/article/details/78540902
以及:http://www.leftso.com/blog/111.html
原文有些许出和官方文档不一致处、酌情参考;