SpringbootはApolloを統合して、最新の値をリアルタイムで監視および更新します

SpringbootはApolloを統合して、最新の値をリアルタイムで監視および更新します

序文:

ApolloはCtripのオープンソース構成センターコンポーネントです。Apollo構成センターを使用する場合、ホットアップデートプロパティのコードを自分で作成する必要があることがよくあります。Apolloはホットアップデートも提供しますが、現在は@Valueアノテーション付きのホットアップデートのみをサポートしています。いくつかのシナリオ次に、使用する前にコレクションまたはBeanにカプセル化するなど、Apolloによって取得された結果をすべて処理するため、ここで問題が発生します。つまり、Apollo構成は更新されますが、Beanまたはカプセル化されたコレクションは更新されていない。

解決

ホットアップデートを使用するには、コレクションにパッケージ化します。

オプション1:

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(",")));

オプションII:

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

おすすめ

転載: blog.csdn.net/qq_40093255/article/details/108507219