Jstorm+Spring+mybatis整合
在现有的jstorm框架下,有一个需求:jstorm要对接mysql数据库的实时读取数据, 通过bolt处理,可能要调用service层的框架,最后保存到数据库。
在网上寻找了一下,发现storm集成spring的资料非常少,有的也只是简单描述,现把搭建过程的一些问题和注意事项详细列出。
1、pom文件、jstorm+spring+mybatis 网上大把的资料,不在这里详细累述。需要注意就是版本兼容问题,可以到www.mvnreposity.com去查看一下
2、jstorm容器集成spring容器,不能采用@autowire 注入的方式,只能在component(spout/bolt)中获取bean,可以写一个公共类,方便以后相同操作。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class SpringContext implements ApplicationContextAware{ private static ClassPathXmlApplicationContext applicationContext ; public static synchronized void SpringContextInit(){ if (applicationContext==null){ applicationContext =new ClassPathXmlApplicationContext( new String[]{"application.xml"}); // applicationContext.start(); } } public static <T> T getBean(String name,Class<T> clazz){ if (applicationContext==null){ SpringContext.SpringContextInit(); } return applicationContext.getBean(name,clazz); } @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { applicationContext=(ClassPathXmlApplicationContext) arg0; } }
3、序列化问题:通过本地测试 加不加序列化都无所谓 都能跑通, 建议加上,因为现在还没有上线集群测试
4、将获取bean的操作 放在open和prepare里、在spout中获取数据源的方法放在open中,nextTuple只负责提交数据到stream中、在bolt中处理数据的方法放在excute中。