Springboot 与Storm整合

网上很多资料基本都是在拓扑提交的时候进行spring的初始化操作,这种方式在本地测试是没有问题的,当真正部署到storm集群中就会报错,spring的bean都没有初始化.为什么会这样呢? 需要了解storm的工作原理,执行storm的jar命令storm提交拓扑后,nimbus会将我们的jar交给supervisor节点,supervisor收到jar之后,会进行初始化操作,即会调用我们bolt中的prepare方法.所以springboot的初始化操作应该放到bolt的prepare方法中.好了,直接上干货.

Bolt:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.storm.Config;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseBasicBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

public class KpiSplitBolt extends BaseBasicBolt {

	private static final long serialVersionUID = 6516085700578340155L;
	private static final Logger LOG = LoggerFactory.getLogger(KpiSplitBolt.class);
	private Map appIdTtIdMap = new ConcurrentHashMap<>();
	private StringRedisTemplate redisTemplate;

	private AsKpiStatService asKpiStatService;//自己定义的service
	
	@Override
	public void prepare(Map stormConf, TopologyContext context) {
		asKpiStatService = SpringBeanGetter.getBean(AsKpiStatService.class);
		redisTemplate = SpringBeanGetter.getBean(StringRedisTemplate.class);
	}
	
	@Override
	public void execute(Tuple input, BasicOutputCollector collector) {
        //这里可以使用asKpiStatService和redisTemplate对象了
    }

SpringBeanGetter:(SpringBoot在这里的static块中完成,application.properties可以指定jar外部的位置)

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) //取消数据源自动加载功能
public class SpringBeanGetter {
	private static final Logger LOG = LoggerFactory.getLogger(SpringBeanGetter.class);

	private static final String START_ARGS_NAME = "BMC_CONF";
	private static final ApplicationContext context;
	static {
		String[] args = new String[] {"-Dspring.config.location=" +     
                System.getenv("STORM_CONF_DIR") + File.separator + "application.properties"}; 
		LOG.info("start-args:{}", args[0]);
		LOG.info("start-spring:{}", System.currentTimeMillis());
		context = SpringApplication.run(SpringBeanGetter.class, args);
		LOG.info("end-spring:{}", System.currentTimeMillis());
	}
	
	public static Object getBean(String name) {
		return context.getBean(name);
	}

	public static  T getBean(Class c) {
		return context.getBean(c);
	}
	
}

由于使用的是static块,所以只会初始化一次,多线程(多个bolt或spout)情况下,不必担心多次进行初始化.

你可能感兴趣的:(Java,Storm,springboot)