一起学Storm -- (1) Storm入门

要想学好一门工具,要先从什么地方开始呢? 先从它的官方说明书来开始。

Apache Storm

Storm是用来干哈的? 为啥要用Storm?

Apache Storm is a free and open source distributed realtime computation system. Apache Storm makes it easy to reliably process unbounded streams of data, doing for realtime processing what Hadoop did for batch processing. Apache Storm is simple, can be used with any programming language, and is a lot of fun to use!
Apache Storm has many use cases: realtime analytics, online machine learning, continuous computation, distributed RPC, ETL, and more. Apache Storm is fast: a benchmark clocked it at over a million tuples processed per second per node. It is scalable, fault-tolerant, guarantees your data will be processed, and is easy to set up and operate.
Apache Storm integrates with the queueing and database technologies you already use. An Apache Storm topology consumes streams of data and processes those streams in arbitrarily complex ways, repartitioning the streams between each stage of the computation however needed. Read more in the tutorial.

由官方文档可以看出它是用来处理流数据的分布式实时计算系统,是Apache大数据的一个得力小伙伴。主要采用的设计思想也是为Mapreduce但与Hadoop的不同的是,Hadoop是做的启动停止的批处理而Strom不需要不断启停而是不停的处理流数据,同时也不需要经过磁盘的IO处理,因此计算延时比较低。

在工作中原来在业务中处理kafka消费的时候,要自己写大量的代码,来进行消费操作,费劲不说还特别占用资源。kafka的消费一个分区只能对应一个消费者,要想提高消费的效率,只能扩分区,扩分区也不大现实,如果下游有大量的消息积压,扩展分区之后首先需要消费者进行重启,同时还需要生产者停止消费。因此在代码中采用多线程来进行消费是更加合适的解决方案。

然而使用Storm之后,就不必这么费劲了,Storm帮我们实现了多线程的消费。看图说话:
一起学Storm -- (1) Storm入门
我们可以看到Storm是由水龙头(Spout)和水滴闪电(Bolt)组成的,Spout是数据源是数据流的输入方,Bolt是用来中间对数据处理的组件,在箭头上传输的是数据流就是一个个的消息(Tuple)。
同事画了一张图也蛮不错,由这张图可以很明显的看出来,Storm帮助我们做了多线程的消费,同时各个处理的bolt还做了很好的解耦处理。
一起学Storm -- (1) Storm入门

Storm 如何保证数据一致性

在消息队列的学习中,了解到消息队列发给消费端的重复消息是无法避免的,那么为了避免造成相同的消息出现分布式情况下的数据不一致性,因此必须要保证相同的消息只能发往相同的处理节点。比如在kafka分区时使用业务主键进行hash分区,让broker发送消息时保证相同消息发往相同分区。

  • 在原来的java业务代码:
    使用Docker组成的消费集群,单台节点机器只能消费一个分区,因此加入多线程消费来提高消费能力。在消费端多线程消费时对业务主键进行了hash,保证相同主键的消息发往相同的线程进行处理。
  • Storm解决方案
    Storm使用KafkaSpout用于从kafka拉取数据,且KafkaSpout的并行度要与topic分片数保持一致,因为一个kafka分片只能被一个客户端线程消费,也就是说一个kafka spout task只会拉取同一个分片的数据。
    在做bolt消费处理时,还要注意,与spout之间的分组策略要选用fieldsGrouping 源表主键,这样才能保证同一源表主键被同一bolt处理的顺序性。