立即注册 登录
About云-梭伦科技 返回首页

langke93的个人空间 https://www.aboutyun.com/?1415 [收藏] [复制] [分享] [RSS]

日志

storm的序列化与spring的结合方式记录

热度 1已有 1576 次阅读2016-8-4 17:48 | spring, 记录

storm的序列化问题及与spring的结合方式
The lifecycle of a bolt or spout is as follows:

1. Created on client side (from where you submit the topology) and
serialized using Java serialization
2. Serialized component is sent to all the tasks
3. Each task executing that component deserializes the component
4. The task calls "prepare" (for bolts) or "open" (for spouts) on the
component before it starts executing.

So if you need to do something like connect to a database, you should do
that in the "prepare" or "open" method. 

出现这个问题主要是对一个spout/bolt的生命周期不是很了解导致, 一般来说spout/bolt的生命周期如下:
1.在提交了一个topology之后(是在nimbus所在的机器么?), 创建spout/bolt实例(spout/bolt在storm中统称为component)并进行序列化.
2.将序列化的component发送给所有的任务所在的机器
3.在每一个任务上反序列化component.
4.在开始执行任务之前, 先执行component的初始化方法(bolt是prepare, spout是open).

因此component的初始化操作应该在prepare/open方法中进行, 而不是在实例化component的时候进行.

后来我又看了这篇文章"Twitter Storm源代码分析之Topology的执行过程(http://xumingming.sinaapp.com/647/twitter-storm-code-analysis-topology-execution/)", 不过里面没有提到Nathan所说的这些内容. 

知道了上面的过程之后, 于是我开始改造我的代码, 那么该如何将storm与spring结合起来呢? 于是我又开始在group中Google之, 发现有个韩国人写了一个storm-spring-example(https://github.com/mykidong/storm-spring-example), 于是下载下来研究了一番, 原来需要在每个spout/bolt维护自己的ApplicationConext, 于是将一个all in one 的spring打散成spring-spout.xml, spring-bolt-xxx.xml. 然后在open/prepare方法中加载对应的spring配置文件. 

改造完成之后, 运行OK!

路过

雷人

握手

鲜花

鸡蛋

发表评论 评论 (1 个评论)

回复 ljlinux2012 2017-3-2 00:16
ddddddddd

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条