Apollo GitHub地址(除了代码,都是中文):https://github.com/ctripcorp/apollo
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
开始之前多说两句,这个集成实际很简单,只要是你会使用Flink,会使用Apollo,那么你一定会集成。没有Apollo基础知识的同学可能会看不懂,并且满脑疑问,如果你看完了确实不明白,建议看下Apollo在Github上提供的基础知识(中文的,不要怂)。
1. Maven Dependency
com.ctrip.framework.apollo
apollo-client
1.5.1
2. 在 resources下建立 META-INF/app.properties文件
在 app.properties文件中添加app.id属性(app.id应用程序的唯一标识,在Apollo配置中心配置,两处务必保持一致)。
app.id=flink-apollo-demo
3.编码
Apollo提供了Java客户端动态获取Apollo配置中心配置的参数信息。主要有两个部分:
1) 获取配置信息的接口
Config config = ConfigService.getAppConfig(); // 获取配置,Config接口提供了众多获取参数的方法
2) 监听配置变更事件的Listener(此处用于打印参数变更日志,或做一些其他业务上的事情)
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()));
}
}
});
package com.flink.apollo;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.TimeUnit;
/**
* 本地跑需要加环境变量 ENV=dev;APOLLO_META=http://xianyu01:8080
*/
public class FlinkIntegrateApolloDemo {
private static final Logger LOG = LoggerFactory.getLogger(FlinkIntegrateApolloDemo.class);
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.addSource(new RichSourceFunction() {
private Config config;
@Override
public void open(Configuration parameters) throws Exception {
config = ConfigService.getAppConfig();
config.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
LOG.info(" #business Changes for namespace {}", changeEvent.getNamespace());
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
LOG.info("Found change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType());
}
}
});
}
@Override
public void run(SourceContext ctx) throws Exception {
while (true) {
ctx.collect(config.getProperty("checkpoint", "default"));
TimeUnit.SECONDS.sleep(1);
}
}
@Override
public void cancel() {
}
}).print("######");
env.execute();
}
}
4 启动
Apollo客户端依赖于AppId,Apollo Meta Server等环境信息来工作。app.properties里的配置解决了AppId的问题, Apollo Meta Server的信息配置由很多方式,这里我们配置为环境变量。
运行结果就不贴了,有疑问多看看Apollo官网。