设为首页 收藏本站
查看: 1637|回复: 0

[经验分享] Flume采用zookeeper管理配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-27 17:44:44 | 显示全部楼层 |阅读模式
  Flume支持通过zookeeper来管理Agent的配置,但是这是一个实验性的功能。配置文件必须先上传到zookeeper中。以下Agent在Zookeeper节点树的结构:
  

- /flume
|- /a1 [Agent配置文件]
|- /a2 [Agent
配置文件]
处理配置文件的类:  
   DSC0000.jpg
  

org.apache.flume.node.PollingZooKeeperConfigurationProvider : 如果zookeeper指定的路径有变更,就从Zookeeper重新获取配置文件。

org.apache.flume.node.StaticZooKeeperConfigurationProvider : 启动Flume后,不会重新加载配置文件,即使Zookeeper的配置文件有变更。

org.apache.flume.agent.embedded.MemoryConfigurationProvider : 从存储中读取配置文件。传入数据格式是Map。

org.apache.flume.node.PollingPropertiesFileConfigurationProvider : 定时冲硬盘读取配置文件。  
  


  org.apache.flume.node.AbstractZooKeeperConfigurationProvider创建Zookeeper客户端:
  

  protected CuratorFramework createClient() {
return CuratorFrameworkFactory.newClient(zkConnString,
new ExponentialBackoffRetry(1000, 1));
}
  Flume采用Curator作为zookeeper的客户端,Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeeper客户端的开发量。
  


  
  Curator的maven配置:

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>apache-curator</artifactId>
<version>2.9.0</version>
</dependency>
  


  Zookeeper还有一个原生态的客户端,maven配置:

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
  
  


  使用原生态的客户端,上传配置文件flume-zookeeper.properties到zookeeper集群:
  

@Test
public void uploadFileToZK() throws KeeperException, InterruptedException {
String propFilePath = &quot;D:\\flume-zookeeper.properties&quot;;
ZooKeeper zk = null;
try {
zk = new ZooKeeper(&quot;10.0.1.85:2181,10.0.1.86:2181,10.0.1.87:2181&quot;, 300000, new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println(&quot;已经触发了&quot; + event.getType() + &quot;事件!&quot;);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (zk.exists(&quot;/flume&quot;, true) == null) {
zk.create(&quot;/flume&quot;, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
InputStream is = null;
ByteArrayOutputStream bytestream = null;
byte[] data = null;
try {
is = new FileInputStream(propFilePath);
bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
data = bytestream.toByteArray();
System.out.println(new String(data));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bytestream.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 创建一个目录节点
Stat stat = zk.exists(&quot;/flume/a1&quot;, true);
if (stat == null) {
zk.create(&quot;/flume/a1&quot;, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} else {
zk.delete(&quot;/flume/a1&quot;, stat.getVersion());
zk.create(&quot;/flume/a1&quot;, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
@Test
public void get() throws KeeperException, InterruptedException {
ZooKeeper zk = null;
try {
zk = new ZooKeeper(&quot;10.0.1.85:2181,10.0.1.86:2181,10.0.1.87:2181&quot;, 300000, new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println(&quot;已经触发了&quot; + event.getType() + &quot;事件!&quot;);
}
});
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(new String(zk.getData(&quot;/flume/a1&quot;, true, null)));
}

flume-zookeeper.properties配置文件内容:  
  

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
a1.sources.r1.channels = c1
a1.sources.r1.type = AVRO
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 41414
a1.sinks.k1.channel = c1
a1.sinks.k1.type = file_roll
a1.sinks.k1.sink.directory = /logs/
a1.sinks.k1.sink.rollInterval = 0
a1.channels = c1
a1.sources = r1
a1.sinks = k1
  
  

avro获取数据,通过Memory Channel写入文件中。


配置文件上传到Zookeeper后,通过如下命令启动Flume:
$ bin/flume-ng agent –conf conf -z 10.0.1.85:2181,10.0.1.86:2181,10.0.1.87:2181 -p /flume –name a1 -Dflume.root.logger=INFO,console

参数名称
默认&#20540;
描述

z

Zookeeper的连接字符串hostname:port列表通过逗号分隔。
p
/flume
Agent配置文件的根路径。
zookeeper中的配置如果有更新,Flume会通过PollingZooKeeperConfigurationProvider类的refreshConfiguration方法重新加载配置:private void refreshConfiguration() throws IOException {
LOGGER.info(&quot;Refreshing configuration from ZooKeeper&quot;);
byte[] data = null;
ChildData childData = agentNodeCache.getCurrentData();
if (childData != null) {
data = childData.getData();
}
flumeConfiguration = configFromBytes(data);
eventBus.post(getConfiguration());
}

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-144291-1-1.html 上篇帖子: Flume SimpleAsyncHbaseEventSerializer 类解析 下篇帖子: Flume分布式日志系统(三)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表