Springboot integrates Apollo to monitor and update the latest value in real time

Springboot integrates Apollo to monitor and update the latest value in real time

Foreword:

Apollo is an open source configuration center component of Ctrip. When using the Apollo configuration center, we often need to write code for hot update properties by ourselves. Apollo also provides hot updates, but currently only supports hot updates with @Value annotations, but in some scenarios Next, we all process the results obtained by Apollo, such as encapsulating it into a collection or bean before using it, so there is a problem here, that is, the Apollo configuration is updated, but the bean or the encapsulated collection is not updated.

solution

Package into a collection to use hot update:

Option One:

Use Apollo's own hot update (@Value) Apollo configuration:

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;
    
}

Get, split and encapsulate into collections where needed:

alreadyConfigCountries = new ArrayList<>(Arrays.asList(returnInsuranceCountryConfig.getRConfig().split(",")));
alreadyConfigCountries = new ArrayList<>(Arrays.asList(returnInsuranceCountryConfig.getSConfig().split(",")));

Option II:

Using Apollo listener, write a real-time listener for configuration updates

Apollo configuration:

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);
        }
    }
}

Get it directly where you need it:

@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 hot update

Specific reference link: https://www.jianshu.com/p/7d91cb5109a4

https://github.com/flying632/ConfigRefresh/blob/master/PropertiesRefresher.java

https://juejin.im/post/6844904182118350862

Guess you like

Origin blog.csdn.net/qq_40093255/article/details/108507219