当咱们使用 spring boot 在多环境打包,配置属性在不一样环境的值不一样,以下:java
spring: profiles: active: @project.profile@ #根据maven 动态配置profile --- spring: profiles: dev demo: lengleng_dev --- spring: profiles: prd demo: lengleng_prd 复制代码
或者使用 spring cloud 配置中心 (nacos/config)等git
再有就是 应用配置的同一个属性,值的来源可能来自配置文件、环境变量、启动参数等等。 不少状况因为如上配置的复杂性,应用在读取配置的时候,并非咱们预期的值,好比咱们想使用是配置文件 dev 环境的值,却被环境变量的 或者其余的数据覆盖等,这些每每只有等咱们运行时,输出日志才能发现错误缘由。github
spring boot 2.3 Actuator 提供 /actuator/configprops
端点 (以前版本也有此端点,可是行为发生变化了 /actuator/env
保持一致 ),提供对配置文件属性跟踪功能,方便咱们在 spring boot 应用中,实时的获取配置文件实际加载值。web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
复制代码
configprops
端点management: endpoints: web: exposure: include: 'configprops' 复制代码
@Data @Component @ConfigurationProperties("demo") public class DemoConfig { private String username; private String password; } 复制代码
public class Sanitizer { private static final String[] REGEX_PARTS = { "*", "$", "^", "+" }; private static final Set<String> DEFAULT_KEYS_TO_SANITIZE = new LinkedHashSet<>(Arrays.asList("password", "secret", "key", "token", ".*credentials.*", "vcap_services", "sun.java.command")); } 复制代码
management: endpoint: configprops: keys-to-sanitize: - 'aaa' - 'bbb' 复制代码
configprops 端点对应 ConfigurationPropertiesReportEndpoint 类, 经过阅读 能够了解从 PropertySource
获取配置的技巧spring
应用场景: CI 在执行单元测试的前置应该经过此端点判断配置是否和预期一致,避免无用执行条件bash
以上源码能够参考: github.com/lltx/spring… markdown