環(huán)境:SpringBoot2.7.12 + SpringCloud2021.0.7
如果使用 Spring Boot 配置日志設(shè)置,則應(yīng)將此配置放在 bootstrap.[yml | properties] 中,以便將其應(yīng)用于所有事件。注意:為了讓 Spring Cloud 正確初始化日志配置,不能使用自定義前綴。例如,在初始化日志系統(tǒng)時,Spring Cloud 無法識別使用 custom.loggin.logpath 的情況。
應(yīng)用程序會偵聽EnvironmentChangeEvent事件,并以幾種標(biāo)準(zhǔn)方式對變化做出反應(yīng)。當(dāng)EnvironmentChangeEvent事件被監(jiān)聽到時,它將收到發(fā)生變化的keys,應(yīng)用程序?qū)鋈缦绿幚恚?span style="display:none">DFZ28資訊網(wǎng)——每日最新資訊28at.com
你也可以自定義監(jiān)聽EnvironmentChangeEvent事件
@Componentpublic class PackApplicationEventListener implements ApplicationListener<EnvironmentChangeEvent> { @Override public void onApplicationEvent(EnvironmentChangeEvent event) { System.out.println(event.getKeys()) ; }}
那些只能初始化一次的 Bean 上使用 @RefreshScope 注解。如果某個 Bean 是 "不可變"的,則必須使用 @RefreshScope 注解或通過如下配置指明完整的類名:
spring: cloud: refresh: extra-refreshable: com.pack.PackUser
注意:
如果你使用的數(shù)據(jù)源 Bean 是 HikariDataSource,則無法刷新。這是 spring.cloud.refresh.never-refreshable 的默認(rèn)值。如果需要刷新,請選擇不同的數(shù)據(jù)源實現(xiàn)。
Spring Cloud 有一個環(huán)境預(yù)處理器,用于在本地解密屬性值。可以使用 {cipher}* 形式的加密值,只要存在有效的密鑰,它們就會在主應(yīng)用程序上下文獲得環(huán)境設(shè)置之前被解密。要在應(yīng)用程序中使用加密功能,需要在類路徑中包含 Spring Security RSA(Maven 坐標(biāo):org.springframework.security:spring-security-rsa)。
#加密配置encrypt: key: aaaabbbbccccdddd salt: dead#---#加密關(guān)鍵信息db: password: '{cipher}6c05a3e62aa1f71b814fd283fc15197ec18a83b67d9da27dcb63c1b3925d68c1'
這里默認(rèn)使用的AES算法,所以通過如下方式生成密文即可
TextEncryptor textEncryptor = new EncryptorFactory("xxx").create("xxxx") ;textEncryptor.encrypt(...)
對于 Spring Boot Actuator 應(yīng)用程序,還提供了一些額外的管理端點(diǎn):
通過 spring.factories添加 PropertySourceLocator 類型的 Bean 來添加其他屬性源。如下示例:
public class CustomPropertySourceLocator implements PropertySourceLocator { @Override public PropertySource<?> locate(Environment environment) { Map<String, Object> values = new HashMap<>() ; values.put("config.mq.queue", "pack.test.queue") ; MapPropertySource source = new MapPropertySource("PACK", values) ; return source ; }}
在spring.factories中添加如下配置
org.springframework.cloud.bootstrap.BootstrapConfiguration=/com.pack.CustomPropertySourceLocator
以上是本篇文章的全部內(nèi)容,希望對你有幫助。
本文鏈接:http://www.www897cc.com/showinfo-26-79989-0.htmlSpringCloud項目開發(fā)中實用技巧總結(jié)
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com