springboot集成Apollo实时监听同步更新最新值
前言:
Apollo是携程开源的配置中心组件,在使用Apollo配置中心的时候,我们时常需要自己写热更新属性的代码,Apollo也提供了热更新,只不过目前只支持@Value注解的热更新,但一些场景下,我们都是对Apollo获取的结果进行处理后,例如封装成集合或者bean才使用,所以这里存在个问题,就是Apollo配置更新了,但是bean或者封装的集合没有更新。
解决方案
封装成集合使用热更新:
方案一:
采用Apollo自带的热更新(@Value)Apollo配置:
package com.lyj.demo;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* @author 凌兮
* @date 2020/8/18 14:48
* 退货险国家配置获取(配置在Apollo中)
*/
@Component
@Data
@Slf4j
public class ReturnInsuranceCountryConfig {
private static final String sKey = "s_insurance_country_config";
private static final String rKey = "r_insurance_country_config";
@Value("${" + sKey + ":''" + "}")
private String sConfig;
@Value("${" + rKey + ":''" + "}")
private String rConfig;
}
在需要的地方进行获取分割封装成集合:
alreadyConfigCountries = new ArrayList<>(Arrays.asList(returnInsuranceCountryConfig.getRConfig().split(",")));
alreadyConfigCountries = new ArrayList<>(Arrays.asList(returnInsuranceCountryConfig.getSConfig().split(",")));
方案二:
采用Apollo监听器,写一个实时监听获取配置更新的监听器
Apollo配置:
package com.lyj.demo;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
/**
* @author 凌兮
* @date 2020/8/18 14:48
* 退货险国家配置获取(配置在Apollo中)
*/
@Component
@Data
@Slf4j
public class ReturnInsuranceCountryConfig implements InitializingBean{
private static final String sKey = "s_insurance_country_config";
private static final String rKey = "r_insurance_country_config";
@Value("${" + sKey + ":''" + "}")
private String sConfig;
@Value("${" + rKey + ":''" + "}")
private String rConfig;
private List<String> sReturnInsuranceCountrys;
private List<String> rReturnInsuranceCountrys;
/**
* 实时更新获取Apollo最新值
* @Value:监听的表空间
* @interestedKeys:监听的key
* @param changeEvent Apollo配置变化事件
*/
@ApolloConfigChangeListener(value = {
"return_insurance_config"})
private void onChange(ConfigChangeEvent changeEvent) {
Set<String> changedKeys = changeEvent.changedKeys();
log.info("changedKeys : {}", changedKeys.toString());
for (String key : changedKeys) {
log.info("key : {}, configValue : {}", key, changeEvent.getChange(key));
ConfigChange change = changeEvent.getChange(key);
if (key.equals("s_insurance_country_config")) {
updateCountries(sReturnInsuranceCountrys, change.getNewValue());
}
if (key.equals("r_return_insurance_country_config")) {
updateCountries(rReturnInsuranceCountrys, change.getNewValue());
}
log.info("key : {}, olderValue : {}, new Value : {}", key, change.getOldValue(), change.getNewValue());
}
}
@Override
public void afterPropertiesSet() throws Exception {
sReturnInsuranceCountrys = new ArrayList<>(Arrays.asList(sConfig.split(",")));
rReturnInsuranceCountrys = new ArrayList<>(Arrays.asList(rConfig.split(",")));
}
/**
* 更新国家集合
*
* @param countries 源国家集合
* @param newCountries 更新后的国家字符串集合
*/
private void updateCountries(List<String> countries, String newCountries) {
countries.clear();
for(String country : newCountries.split(",")) {
countries.add(country);
}
}
}
在需要的地方直接获取即可:
@RequestMapping(value = "/test/returnInsurance")
public void returnInsuranceCountryTest() {
log.info("returnInsuranceCountry : {}", returnInsuranceCountryConfig.getRConfig());
log.info("returnInsuranceCountry : {}", returnInsuranceCountryConfig.getSConfig());
System.out.println(returnInsuranceCountryConfig.getSReturnInsuranceCountrys());
System.out.println(returnInsuranceCountryConfig.getRReturnInsuranceCountrys());
}
bean热更新
具体参考链接:https://www.jianshu.com/p/7d91cb5109a4
https://github.com/flying632/ConfigRefresh/blob/master/PropertiesRefresher.java
https://juejin.im/post/6844904182118350862