RocketMQ概述及源码获取

1. 前言

为什么要用RocketMQ,官方文档是这样描述的:

Based on our research, with increased queues and virtual topics in use, ActiveMQ IO module reaches a bottleneck. We tried our best to solve this problem through throttling, circuit breaker or degradation, but it did not work well. So we begin to focus on the popular messaging solution Kafka at that time. Unfortunately, Kafka can not meet our requirements especially in terms of low latency and high reliability, see here for details. In this context, we decided to invent a new messaging engine to handle a broader set of use cases, ranging from traditional pub/sub scenarios to high volume real-time zero-loss tolerance transaction system. We believe this solution can be beneficial, so we would like to open source it to the community.

主要的意思就是ActiveMQ IO 遇到了瓶颈,通过字节流、降级等方法效果不佳,而Kafka 在低延迟和高可用也无法满足需求,这个消息队列也就诞生了 ,从传统的订阅/发布模式到大批量实时零损失,16年也就向社区开源了。

2. 特性

RocketMQ github官方文档
官方文档中对特性做了详细的说明:

  1. 订阅与发布
  2. 消息顺序
  3. 消息过滤
  4. 消息可靠性(高可用)
  5. 至少一次
  6. 回溯消费
  7. 事务消息
  8. 定时消息
  9. 消息重试
  10. 消息重投
  11. 流量控制
  12. 死信队列

3. 获取源码并快速开始

GitHub直接clone或者下载即可。

下载后注意一下事项,RocketMQ 源码里面配置是 Java1.8 版本,所以,你在跑源码或测试的时候请使用Java1.8,或者你也可以将 rocketmq 里面的配置改为Java的更高版本,但是不建议,对于初学者来说,很容易出问题。

看看目录结构:

目录结构

接下来就开始跑官方demo

1. 配置BrokerStartup和NamesrvStartup

  • 新建一个文件夹rocketmq,你自己放个位置就行了,作为rocketmq运行目录,在里面新建conf、logs、store三个文件夹,将源代码中的broker.conf、logback_broker.xml、logback_namesrv.xml文件放到conf目录中,并修改broker.conf 中配置
    brokerClusterName = DefaultCluster
    brokerName = broker-a
    brokerId = 0
    deleteWhen = 04
    fileReservedTime = 48
    brokerRole = ASYNC_MASTER
    flushDiskType = ASYNC_FLUSH
    
    #nameserver ip
    namesrvAddr=127.0.0.1:9876
    
    #存储路径
    storePathRootDir=D:/rocketmq/store
    # commitLog 存储路径
    storePathCommitLog=D:/rocketmq/store/commitlog
    # 消费队列存储路径
    storePathConsumeQueue=D:/rocketmq/store/consumequeue
    # 消费索引存储路径
    storePathIndex=D:/rocketmq/store/index
    # checkePoint 文件存储路径
    storeCheckpoint=D:/rocketmq/store/checkpoint
    # abort 文件存储路径
    abortFile=D:/rocketmq/store/abort

对应修改自己的就行了。

  • Edit Configuration:配置BrokerStartup和NamesrvStartup

Configuration

BrokerStartup需要配置program arguments,NamesrvStartup 只需要配置Environment variables ,都是如上图。(注:在相应模块下找到启动项,启动一下就能找到配置了)

2. Producer和Consumer

生产者和消费者里面都没有设置nameserver,一定要设置好,在conf里面的配置

    //Producer里面设置
    producer.setNamesrvAddr("127.0.0.1:9876");
    
    //Consumer里面设置
    consumer.setNamesrvAddr("127.0.0.1:9876");

3. 依次启动

依次启动BrokerStartup、NamesrvStartup、Producer和Consumer

运行结果:

4. 小结&参考资料

小结

本章只是简单介绍了 rocketmq 和利用 IDEA 直接跑源码,方式有很多,例如 maven 编译运行等,为了方便学习源码,这种简单方式最棒。

参考资料

发布了42 篇原创文章 · 获赞 48 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/chachapaofan/article/details/103198427