Apollo 中怎么使用springboot實(shí)現(xiàn)動(dòng)態(tài)刷新,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元泗水做網(wǎng)站,已為上家服務(wù),為泗水各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
在需刷新的字段上配置@Value注解,如:
@Value("${ve.auth-url}") private String authUrl;
bean使用@ConfigurationProperties注解目前還不支持自動(dòng)刷新,需要編寫額外的代碼。目前官方提供2種刷新方案
基于RefreshScope實(shí)現(xiàn)刷新
基于EnvironmentChangeEvent實(shí)現(xiàn)刷新
項(xiàng)目選用基于RefreshScope實(shí)現(xiàn)刷新。
@ConfigurationProperties(prefix = "partners") @Component("partnersConfig") @RefreshScope @Slf4j @Data public class PartnersConfig { private ListfundProviders = Lists.newArrayList(); private List logisticsCompanies = Lists.newArrayList(); private List insuranceCompanies = Lists.newArrayList(); }
其代碼實(shí)現(xiàn)如下:
@Component @Slf4j public class ApolloRefreshConfig { private final PartnersConfig partnersConfig; private final AnXinSignProperty anXinSignProperty; private final RefreshScope refreshScope; public ApolloRefreshConfig(final PartnersConfig partnersConfig, final AnXinSignProperty anXinSignProperty, final RefreshScope refreshScope) { this.partnersConfig = partnersConfig; this.anXinSignProperty = anXinSignProperty; this.refreshScope = refreshScope; } /** * 記錄配置更改事件 * * @param changeEvent 配置更改事件 * @return void * @author xux * @date 2021/4/9 */ public static void printChange(ConfigChangeEvent changeEvent) { SetchangeKeys = changeEvent.changedKeys(); if (!CollectionUtils.isEmpty(changeKeys)) { for (String changeKey : changeKeys) { ConfigChange configChange = changeEvent.getChange(changeKey); log.info("apollo changed , key:" + changeKey + "; old value:" + configChange.getOldValue() + "; new value:" + configChange.getNewValue()); } } } /** * 監(jiān)聽partner配置更改,value為監(jiān)聽的配置文件 * * @param configChangeEvent 配置更改事件 * @return void * @author xux * @date 2021/4/9 */ @ApolloConfigChangeListener(value = {"fund-provider-partners.properties", "insurance-companie-partners.properties", "logistics-company-partners.properties"}) private void refreshPartnersConfig(ConfigChangeEvent configChangeEvent) { printChange(configChangeEvent); log.info("before refresh {}", partnersConfig.toString()); refreshScope.refresh("partnersConfig"); log.info("after refresh {}", partnersConfig.toString()); } /** * 監(jiān)聽安心簽配置更改,value默認(rèn)監(jiān)聽application * * @param configChangeEvent 配置更改事件 * @return void * @author xux * @date 2021/4/9 */ @ApolloConfigChangeListener(interestedKeyPrefixes = "spring.cfca.anxinsign") private void refreshAnXinSignProperty(ConfigChangeEvent configChangeEvent) { printChange(configChangeEvent); log.info("before refresh {}", anXinSignProperty.toString()); refreshScope.refresh("anXinSignProperty"); log.info("after refresh {}", anXinSignProperty.toString()); } }
關(guān)于Apollo 中怎么使用springboot實(shí)現(xiàn)動(dòng)態(tài)刷新問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。