第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Spring Boot 2.3 新特配置文件屬性跟蹤

標(biāo)簽:
Java

背景

当我们使用 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
    image

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消