背景

当我们使用 spring boot 在多环境打包,配置属性在不同环境的值不同,如下:

spring:
profiles:
active: @project.profile@ #根据maven 动态配置profile
---
spring:
profiles: dev
demo: lengleng_dev
---
spring:
profiles: prd
demo: lengleng_prd

或者使用 spring cloud 配置中心 (nacos/config)等

再有就是 应用配置的同一个属性,值的来源可能来自配置文件、环境变量、启动参数等等。 很多情况由于如上配置的复杂性,应用在读取配置的时候,并不是我们预期的值,比如我们想使用是配置文件 dev 环境的值,却被环境变量的 或者其他的数据覆盖等,这些往往只有等我们运行时,输出日志才能发现错误原因。

解决方案

spring boot 2.3 Actuator 提供 /actuator/configprops 端点 (之前版本也有此端点,但是行为发生变化了 /actuator/env 保持一致 ),提供对配置文件属性跟踪功能,方便我们在 spring boot 应用中,实时的获取配置文件实际加载值

如何使用

  • 引入 actuator 依赖
<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;
}
  • 访问 Endpoint 实时获取配置文件的值

特殊说明

  • configprops Endpoint 会对敏感字段默认脱敏 ,默认关键字类
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'
  • 当配置类的某个属性值为空时, 通过 /actuator/configprops 访问,不会展示此属性。

总结

  • configprops 端点对应 ConfigurationPropertiesReportEndpoint 类, 通过阅读 可以了解从 PropertySource 获取配置的技巧

  • 应用场景: CI 在执行单元测试的前置应该通过此端点判断配置是否和预期一致,避免无用执行条件

  • 以上源码可以参考: https://github.com/lltx/spring-boot-course

项目推荐: Spring Cloud 、Spring Security OAuth2的RBAC权限管理系统 欢迎关注

Spring Boot 2.3 新特配置文件属性跟踪的更多相关文章

  1. Spring Boot 2.0 新特性和发展方向

    以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...

  2. Spring Boot 2(一):Spring Boot 2.0新特性

    Spring Boot 2(一):Spring Boot 2.0新特性 Spring Boot依赖于Spring,而Spring Cloud又依赖于Spring Boot,因此Spring Boot2 ...

  3. 【2.0新特性】Spring Boot 2.0新特性

    以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...

  4. Spring Boot实践——Spring Boot 2.0 新特性和发展方向

    出自:https://mp.weixin.qq.com/s/EWmuzsgHueHcSB0WH-3AQw 以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Jav ...

  5. Spring Boot应用的后台运行配置

    酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...

  6. Spring boot 的 properties 属性值配置 application.properties 与 自定义properties

    配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...

  7. Spring Boot 启动(二) 配置详解

    Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...

  8. JAR(Spring Boot)应用的后台运行配置

    酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用主类 使用Mave ...

  9. Spring Boot应用的后台运行配置(转载)

    作者:程序猿DD 酱油一篇,整理一下关于Spring Boot后台运行的一些配置方式.在介绍后台运行配置之前,我们先回顾一下Spring Boot应用的几种运行方式: 运行Spring Boot的应用 ...

随机推荐

  1. 🎊 Element UI 新春快报

    新年好,Element UI 开发团队给各位支持我们的开发者们拜个晚年,祝大家在新的一年里工作没 bug, 天天不加班. 在过去一年里,Element UI 团队在稳定维护 Vue 2.x 版本的同时 ...

  2. 3分钟学会如何上手supervisor看门狗

    软硬件环境 centos7.6.1810 64bit cat /etc/redhat-release #查看系统版本 supervisor 3.4.0 python 2.7.5 supervisor简 ...

  3. SpringBoot2.1整合Mybatis-Generator以及tk.mybatis

    1:添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  4. IntelliJ Idea Error Address localhost 1099 is already in use.

        Reference: https://stackoverflow.com/questions/38986910/intellij-idea-address-localhost1099-is-a ...

  5. Linux系统编程【4】——文件系统

    pwd命令的作用 Linux的文件系统比较庞大,所以笔者从pwd这一命令入手,在实现的过程中加深对文件系统的了解. 输入:man pwd 从指导文档中可以看到,pwd命令的作用是显示出当前所处位置,以 ...

  6. 理解ASP.NET Core 中的WebSocket

    在本文中,我们将详细介绍RFC 6455 WebSocket规范,并配置一个通用的.NET 5应用程序通过WebSocket连接与SignalR通信. 我们将深入底层的概念,以理解底层发生了什么. 关 ...

  7. This is my first blog----文件操作基本流程

    一,文件操作基本流程. 计算机系统分为: 计算机硬件, 操作系统, 应用程序三部分 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件 ...

  8. git的回滚与撤销【reset and revert】

    git的工作流程-- 3个区域 工作区:我们可以看到的文件内容 在操作 git add 之前的!! 缓存区:是不可见的  已经git add操作,还没git commit -m "" ...

  9. MySQL入门(1)——基础操作

    MySQL入门(1)--基础操作 创建数据库 创建基本数据库: create database db_admin; 创建基本数据库(等价于CREATE DATABASE): create schema ...

  10. python 常用的库

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...