淺談SpringBoot2.3 新特配置文件屬性跟蹤
背景
當我們使用 spring boot 在多環(huán)境打包,配置屬性在不同環(huán)境的值不同,如下:
spring: profiles: active: @project.profile@ #根據(jù)maven 動態(tài)配置profile---spring: profiles: devdemo: lengleng_dev---spring: profiles: prddemo: lengleng_prd
或者使用 spring cloud 配置中心 (nacos/config)等
再有就是 應用配置的同一個屬性,值的來源可能來自配置文件、環(huán)境變量、啟動參數(shù)等等。 很多情況由于如上配置的復雜性,應用在讀取配置的時候,并不是我們預期的值,比如我們想使用是配置文件 dev 環(huán)境的值,卻被環(huán)境變量的 或者其他的數(shù)據(jù)覆蓋等,這些往往只有等我們運行時,輸出日志才能發(fā)現(xiàn)錯誤原因。
解決方案
spring boot 2.3 Actuator 提供 /actuator/configprops 端點 (之前版本也有此端點,但是行為發(fā)生變化了 /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'));}
配置個性化脫敏規(guī)則
management: endpoint: configprops: keys-to-sanitize: - ’aaa’ - ’bbb’
當配置類的某個屬性值為空時, 通過 /actuator/configprops 訪問,不會展示此屬性。
總結(jié)
configprops 端點對應 ConfigurationPropertiesReportEndpoint 類, 通過閱讀 可以了解從 PropertySource 獲取配置的技巧應用場景: CI 在執(zhí)行單元測試的前置應該通過此端點判斷配置是否和預期一致,避免無用執(zhí)行條件
以上源碼可以參考: https://github.com/lltx/spring-boot-course
到此這篇關于淺談SpringBoot2.3 新特配置文件屬性跟蹤的文章就介紹到這了,更多相關SpringBoot2.3 文件屬性跟蹤內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. 基于javaweb+jsp實現(xiàn)學生宿舍管理系統(tǒng)2. 多級聯(lián)動下拉選擇框,動態(tài)獲取下一級3. 如何封裝一個Ajax函數(shù)4. ASP.NET MVC實現(xiàn)樹形導航菜單5. Spring security 自定義過濾器實現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實例代碼)6. Java 接口和抽象類的區(qū)別詳解7. springboot返回modelandview頁面的實例8. python怎么運行代碼9. PHP擴展之URL編碼、解碼及解析——URLs10. Laravel Eloquent ORM高級部分解析
