消息集群架构

对于发送方来说的关键几要素

  • topic

消息的主题,由用户定义。类似于知乎的话题,Producer发送消息的时候需要指定发送到某一个topic下面,Consumer从某一个topic下面消费消息。

  • tag

每次发送一条消息的时候,给消息加一个Tag,方便Consumer过滤消息

  • message

消息,负载发送的消息的信息。在生产者,服务端和 消费者之间传输

  • queue

queue就是metaq中具体用来存数消息的数据结构,每一个topic下面对应多个queue,以目录的形式分开存储在磁盘上。我们在最初的网络结构中就描述了brokerserver集群,然而对应到具体某一个brokerserver的存储,就是queue+commitlog

之前介绍brokerserver的时候有简单提过DefaultMessageStore类,这是metaq消息存储的默认实现类,里面存储的具体配置,也包含了commitlog,consumequeue,关于消息加载,刷盘,恢复,清理等存储设计都在这里。

  • Group

消费者可以是多个消费者共同消费一个 topic 下的消息,每个消费者消费部分消息。这些消费者就组成一个分组,拥有同一个分组名称,通常也称为消费者集群。

集群消费,一条消息只会被同一个group里一个消费者消费。 不同group之间相互不影响。

广播消费,一条消息会被同一个group里每一个消费端消费

消息发送者实例化

Producer 消息生产者,负责产生消息,一般由业务系统负责产生消息。

一般一个应用创建一个Producer,由应用来维护此对象,可以设置为全局对象或者单例。

消息子类型用 tags 来标识,tags 可以由应用自由设置。只有发送消息设置了 tags, 消费方在订阅消息时,才可以利用 tags 在 broker 做消息过滤。

我们可以看出start()方法主要做这么几件事:

  1. 检查配置是否正确,checkConfig();
  2. 注册produce,registerProducer();
  3. mQClientFactory.start();
  4. 向所有的broker发送的Heartbeat。

发送方的核心服务:

 //Start request-response channel
this.mQClientAPIImpl.start();
//Start various schedule tasks
this.startScheduledTask();
//Start pull service
this.pullMessageService.start();
//Start rebalance service
this.rebalanceService.start();
//Start push service
this.defaultMQProducer.getDefaultMQProducerImpl().start(false);

消息发送流程

metaQ的底层通信还是基于netty的。

消息载体

private String topic;
private int flag;
private Map<String, String> properties;
private byte[] body; private int queueId;
private int storeSize;
private long queueOffset;
private int sysFlag;
private long bornTimestamp;
private SocketAddress bornHost;
private long storeTimestamp;
private SocketAddress storeHost;
private String msgId;
private long commitLogOffset;
private int bodyCRC;
private int reconsumeTimes;
private long preparedTransactionOffset;

这里需要强调的是,Message是为了便于metaq逻辑操作而定义的偏向于业务逻辑的类,实际上的网络传输类是RemotingCommand,结构如下

 public ByteBuffer encode() {
int length = 4;
byte[] headerData = this.buildHeader();
length += headerData.length;
if (this.body != null) {
length += body.length;
}
ByteBuffer result = ByteBuffer.allocate(4 + length);
result.putInt(length);
result.putInt(headerData.length);
result.put(headerData);
if (this.body != null) {
result.put(this.body);
}
result.flip();
return result;
}

producer的send()方法:



客户端从zookeeper上获取publish的topic对应的broker和分区列表(按照brokerId和partition的顺序排列组织成一个有序的分区列表),生产者在发送消息的时候必须选择一个分区来发送消息,发送的时候按照从头到尾循环往复的方式选择来发送消息。

默认调用的是同步发送方法。

调用netty中的 com.alibaba.rocketmq.remoting.netty.NettyRemotingClient#invokeSync

  public RemotingCommand invokeSync(String addr, final RemotingCommand request, long timeoutMillis)
throws InterruptedException, RemotingConnectException, RemotingSendRequestException,
RemotingTimeoutException {
final Channel channel = this.getAndCreateChannel(addr);
if (channel != null && channel.isActive()) {
try {
if (this.rpcHook != null) {
this.rpcHook.doBeforeRequest(addr, request);
}
RemotingCommand response = this.invokeSyncImpl(channel, request, timeoutMillis);
if (this.rpcHook != null) {
this.rpcHook.doAfterResponse(RemotingHelper.parseChannelRemoteAddr(channel),
request, response);
}
return response;
}
catch (RemotingSendRequestException e) {
log.warn("invokeSync: send request exception, so close the channel[{}]", addr);
this.closeChannel(addr, channel);
throw e;
}
catch (RemotingTimeoutException e) {
log.warn("invokeSync: wait response timeout exception, the channel[{}]", addr);
throw e;
}
}
else {
this.closeChannel(addr, channel);
throw new RemotingConnectException(addr);
}
}

同时可以设置调用异步发送方法。

调用com.alibaba.rocketmq.remoting.netty.NettyRemotingClient#invokeAsync

 public void invokeAsync(String addr, RemotingCommand request, long timeoutMillis,
InvokeCallback invokeCallback) throws InterruptedException, RemotingConnectException,
RemotingTooMuchRequestException, RemotingTimeoutException, RemotingSendRequestException {
final Channel channel = this.getAndCreateChannel(addr);
if (channel != null && channel.isActive()) {
try {
if (this.rpcHook != null) {
this.rpcHook.doBeforeRequest(addr, request);
}
this.invokeAsyncImpl(channel, request, timeoutMillis, invokeCallback);
}
catch (RemotingSendRequestException e) {
log.warn("invokeAsync: send request exception, so close the channel[{}]", addr);
this.closeChannel(addr, channel);
throw e;
}
}
else {
this.closeChannel(addr, channel);
throw new RemotingConnectException(addr);
}
}

这两个方法一个为同步,一个为异步。而同步与异步的主要实现则来自于ResponseFuture类。通过countDownLatch的使用,完成了同步操作(详情参看此类的putResponse方法及waitResponse方法);通过传递invokeCallback完成异步操作

pull类型消息中间件-消息发布者(一)的更多相关文章

  1. push类型消息中间件-消息发布者(二)

    1.消息发布者声明 我们以spring的方式来声明一个消息发布者: <bean id="operateLogsMessageManager" class="com. ...

  2. pull类型消息中间件-消息消费者(二)

    消费者的实例化 关于consumer的默认实现,metaq有两种: DefaultMQPullConsumer:由业务方主动拉取消息 DefaultMQPushConsumer:通过业务方注册回调方法 ...

  3. pull类型消息中间件-消息服务端(三)

    部署架构 消息存储 存储结构 MetaQ的存储结构是一种物理队列+逻辑队列的结构.如下图所示: Producer生产消息,根据消息的topic选择topic对应某一个分区,然后发送到这个分区对应的Br ...

  4. push类型消息中间件-消息订阅者(一)

    1.订阅者的声明方式 我们以spring组件化的方式,声明一个消息订阅者,对于消息订阅者关心的主要有: topic: 一级消息类型(又名消息主题).如TRADE 消息类型:二级消息类型,区别同一Top ...

  5. push类型消息中间件-消息服务端(三)

    1.连接管理 网络架构原来是使用是自己开发的网络框架Gecko,Gecko默认为每个网络连接分配64KB的内存,支持1000个网络连接,就需要大概64MB的内存.后来采用Netty重构了网络服务层. ...

  6. AspNetWebApi管线中如果定义两种类型的消息处理程序(全局/路由)

    AspNetWebApi管线中如果定义两种类型的消息处理程序(全局/路由) 在AspNetWebApi管线中存在两种类型的消息处理程序(Message Handler) 1.全局消息处理程序,所有的请 ...

  7. SIP消息类型和消息格式

    转自:http://blog.chinaunix.net/uid-1797566-id-2840904.html sip消息类型和消息格式 SIP是一个基于文本的协议,使用的是UTF-8字符集. SI ...

  8. Python操作rabbitmq系列(四):根据类型订阅消息

    在上一章中,所有的接收端获取的所有的消息.这一章,我们将讨论,一些消息,仍然发送给所有接收端.其中,某个接收端,只对其中某些消息感兴趣,它只想接收这一部分消息.如下图:C1,只对error感兴趣,C2 ...

  9. ActiveMQ 处理不同类型的消息

    ActiveMQ 中的消息都继承自 org.apache.activemq.command.BaseCommand 类. broker 处理消息的调用栈如下: TransportConnection ...

随机推荐

  1. java反射的运用场景

    1.反射的好处是:可以在运行时确认对象以及方法. 2.下面举个简单的例子来说下反射的运用场景: 假如一款游戏有一个配置文件,配置文件里有个renderType设置了游戏启动时调用哪个RenderHan ...

  2. Win7下python Scrapy一站式搭建全攻略(内附相关下载链接)

    写在前面: 好久没有登录博客了,意外看到之前的几篇文章都有不错的阅读量,开心极了,不过没有什么点赞和评论,大概是没有给大家带来什么切实的帮助吧.o(*////▽////*)q. 最近在NTU负责一个国 ...

  3. Hibernate缓存配置

    一级缓存 Hibernate的一级缓存是由Session提供的,因此它只存在于Session的生命周期中,当程序调用save(),update(),saveorupdate()等方法 及调用查询接口l ...

  4. js Get中文乱码 转码

    encodeURI  escape post: location.href = "ActivitiesOnSale?CurrId=" + Cid + "&CNam ...

  5. springmvc+mybatis+spring 整合 SSM

    A 调用摄像头拍照,自定义裁剪编辑头像 [新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统]B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技 ...

  6. [转]Numpy使用MKL库提升计算性能

    from:http://unifius.wordpress.com.cn/archives/5 系统:Gentoo Linux (64bit, Kernel 3.7.1)配置:Intel(R) Cor ...

  7. const 那点事儿

    修饰变量时 const char 与 char const 是等价的,例如 const char a = 'a'; char const a = 'a'; 表示变量a不能再被赋予其他值. 到指针时情况 ...

  8. myBatis系列之七:事务管理

    myBatis系列之七:事务管理 如果在操作时,如果运行时错误自动进行回滚,需要以下两个配置 @Transactional()public void save(User user) { userDao ...

  9. C++ 中的计时器

    在Java中,有时候会要测试程序的性能,所以会采用System.currentTimeMillis()等类库函数去测试时间开销. 在C++中同样可以完成此功能. 头文件: #include<ct ...

  10. 【ARM】S3C6410芯片的启动流程

    S3C6410芯片的启动流程 (1) 上电后首先运行iRom(BL0)内的代码,主要完成时钟和看门狗等外围器件的初始化.(2) 拷贝SD卡或者NnadFlash中的前4k(BL1)代码到片内ram(垫 ...