攜程apllo 配置實時生效

公司使用的配置中心框架,爲攜程開源的Apollo。但是在使用過程中,在apollo 上配置一直不能實時生效,需要重新啓動服務纔可以。初步參看了一些博客,發現歸根結底還是要回歸到Apollo的官方文檔上,官方文檔和說明也還是相當的詳細。

附: apollo wiki:   https://github.com/ctripcorp/apollo/wiki

先附上直接驗證成功的demo

配置一個監聽器,apollo 更新配置後,會實時監聽並更新config


import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigFileChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.internals.YamlConfigFile;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.model.ConfigFileChangeEvent;
import com.ctrip.framework.foundation.Foundation;
import com.google.common.base.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ApolloAutoModifyConfig {
    private static final Logger logger = LoggerFactory.getLogger(ApolloAutoModifyConfig.class);
    private String DEFAULT_VALUE = "";
    private Config config;
    private Config yamlConfig;
    private Config publicConfig;
    private ConfigFile applicationConfigFile;
    private ConfigFile xmlConfigFile;
    private YamlConfigFile yamlConfigFile;

    private RefreshScope refreshScope;

   public static ApolloAutoModifyConfig getInstance;
   static {
       getInstance = new ApolloAutoModifyConfig();
   }

    private ApolloAutoModifyConfig() {
        ConfigChangeListener changeListener = new ConfigChangeListener() {
            @Override
            public void onChange(ConfigChangeEvent changeEvent) {
                logger.info("Changes-111111 for namespace {}", changeEvent.getNamespace());
                for (String key : changeEvent.changedKeys()) {
                    ConfigChange change = changeEvent.getChange(key);
                  
                    logger.info("Change-11111 - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                            change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                            change.getChangeType());
                }
            }
        };

        ConfigFileChangeListener listener = new ConfigFileChangeListener() {
            @Override
            public void onChange(ConfigFileChangeEvent changeEvent) {
                logger.info("Changes-22222 for namespace {}", changeEvent.getNamespace());
            }
        };
        config = ConfigService.getAppConfig();
        config.addChangeListener(changeListener);
        yamlConfig = ConfigService.getConfig("application.yaml");
        yamlConfig.addChangeListener(changeListener);

        //監聽其他公共的namespace
        /*publicConfig = ConfigService.getConfig("比如引用的其他公共配置");
        publicConfig.addChangeListener(changeListener);*/

        applicationConfigFile = ConfigService.getConfigFile("application", ConfigFileFormat.Properties);
        applicationConfigFile.addChangeListener(listener);

        // datasources.xml
        xmlConfigFile = ConfigService.getConfigFile("datasources", ConfigFileFormat.XML);
        xmlConfigFile.addChangeListener(new ConfigFileChangeListener() {
            @Override
            public void onChange(ConfigFileChangeEvent changeEvent) {
                logger.info(changeEvent.toString());
            }
        });
        // application.yaml
        yamlConfigFile = (YamlConfigFile) ConfigService.getConfigFile("application", ConfigFileFormat.YAML);
    }

    public String getConfig(String key) {
        String result = config.getProperty(key, DEFAULT_VALUE);
        if (DEFAULT_VALUE.equals(result)) {
            result = publicConfig.getProperty(key, DEFAULT_VALUE);
        }
        if (DEFAULT_VALUE.equals(result)) {
            result = yamlConfig.getProperty(key, DEFAULT_VALUE);
        }
        logger.info(String.format("Loading key : %s with value: %s", key, result));
        return result;
    }

    private void print(String namespace) {
        switch (namespace) {
            case "application":
                print(applicationConfigFile);
                return;
            case "xml":
                print(xmlConfigFile);
                return;
            case "yaml":
                printYaml(yamlConfigFile);
                return;
        }
    }

    private void print(ConfigFile configFile) {
        if (!configFile.hasContent()) {
            System.out.println("No config file content found for " + configFile.getNamespace());
            return;
        }
        System.out.println("=== Config File Content for " + configFile.getNamespace() + " is as follows: ");
        System.out.println(configFile.getContent());
    }

    private void printYaml(YamlConfigFile configFile) {
        System.out.println("=== Properties for " + configFile.getNamespace() + " is as follows: ");
        System.out.println(configFile.getConfigFileFormat());
    }

    private void printEnvInfo() {
        String message = String.format("AppId: %s, Env: %s, DC: %s, IP: %s", Foundation.app()
                        .getAppId(), Foundation.server().getEnvType(), Foundation.server().getDataCenter(),
                Foundation.net().getHostAddress());
        System.out.println(message);
    }

    public static void main(String[] args) throws IOException {
        ApolloAutoModifyConfig apolloConfigDemo = new ApolloAutoModifyConfig();
        apolloConfigDemo.printEnvInfo();
        System.out.println(
                "Apollo Config Demo. Please input key to get the value.");
        while (true) {
            System.out.print("> ");
            String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine();
            if (input == null || input.length() == 0) {
                continue;
            }
            input = input.trim();
            try {
                if (input.equalsIgnoreCase("application")) {
                    apolloConfigDemo.print("application");
                    continue;
                }
                if (input.equalsIgnoreCase("xml")) {
                    apolloConfigDemo.print("xml");
                    continue;
                }
                if (input.equalsIgnoreCase("yaml") || input.equalsIgnoreCase("yml")) {
                    apolloConfigDemo.print("yaml");
                    continue;
                }
                if (input.equalsIgnoreCase("quit")) {
                    System.exit(0);
                }
                apolloConfigDemo.getConfig(input);
            } catch (Throwable ex) {
                logger.error("some error occurred", ex);
            }
        }
    }
}

配置使用:

@Slf4j
@Component
@Data
public class ApolloBusinessConfig {

    private String mytest;

    //my.test: 爲apollo 配置的key
    public String getMyTest() {
        mytest = ApolloAutoModifyConfig.getInstance.getConfig("my.test");
        return mytest;
    }
}


//在業務場景使用:
@Autowired
ApolloBusinessConfig config;

String myTest = config.getMyTest();

myTest獲取的結果爲實時更新後的值

以上方式,只能使用ApolloBusinessConfig 的getXX 方法獲取最新值。如果業務中,在其他地方使用了下面的方式獲取值,獲取的值還是之前的,並不會實時更新。

@Value("${my.test}")

private String myTest;

總結:

ApolloAutoModifyConfig 的監聽,能實時更新緩存到本地或服務端的緩存文件配置,也能實時更新 

        config = ConfigService.getAppConfig();
config 在內存中的key 值,但是無法更新所有其他地方 注入@Value 或者在其他位置直接使用的key 值,綜合來說,即對於 my.test在內存中的其他scop 沒有刷新。查看官方文檔,說是使用 EnvironmentChangeEventRefreshScope 進行刷新,但是測試後,並沒有效果,不確定是不是和我們使用的springboot 2.0以下版本有關。後續在細作研究,看看怎麼實現更新

 

開發語言使用的是java 並感興趣的同學,可以直接跳轉到java客戶端的開發文檔 參考

https://github.com/ctripcorp/apollo/wiki/Java%E5%AE%A2%E6%88%B7%E7%AB%AF%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97

文檔摘取:

3.1.2 監聽配置變化事件

Config config = ConfigService.getAppConfig(); //config instance is singleton for each namespace and is never null
config.addChangeListener(new ConfigChangeListener() {
    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
        System.out.println("Changes for namespace " + changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
        }
    }
});

//獲取實時更新的value
String yourValue = config.getProperty("your key","your default value");

同時文檔介紹了 基於javaBean、spring xml 、springboot 的配置使用,但是真正說實時生效的是要參考源碼的demo 示例:

需要注意的是,@ConfigurationProperties如果需要在Apollo配置變化時自動更新注入的值,需要配合使用EnvironmentChangeEvent或RefreshScope。相關代碼實現,可以參考apollo-use-cases項目中的ZuulPropertiesRefresher.java和apollo-demo項目中的SampleRedisConfig.java以及SpringBootApolloRefreshConfig.java

根據上面源碼中的示例,試驗了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章