rocketmq 负载均衡源码

http://blog.csdn.net/qq_36569036/article/details/53447122

 

com.alibaba.rocketmq.client.impl.producer.TopicPublishInfo.selectOneMessageQueue(String)

  • 启动负载均衡的服务:producer根据roundbin方式轮询topic下的所有队列来实现发送方的负载均衡。
   

//生产者默认负载均衡算法
com.alibaba.rocketmq.client.impl.producer.DefaultMQProducerImpl.sendDefaultImpl(Message, CommunicationMode, SendCallback)

 /**
     * 如果lastBrokerName不为null,则寻找与其不同的MessageQueue
     */
    public MessageQueue selectOneMessageQueue(final String lastBrokerName) {
        if (lastBrokerName != null) {
            int index = this.sendWhichQueue.getAndIncrement();
            for (int i = 0; i < this.messageQueueList.size(); i++) {
                int pos = Math.abs(index++) % this.messageQueueList.size();
                MessageQueue mq = this.messageQueueList.get(pos);
                if (!mq.getBrokerName().equals(lastBrokerName)) {
                    return mq;
                }
            }

            return null;
        }
        else {
            int index = this.sendWhichQueue.getAndIncrement();
            int pos = Math.abs(index) % this.messageQueueList.size();
            return this.messageQueueList.get(pos);
        }
    }

com.alibaba.rocketmq.client.consumer.AllocateMessageQueueStrategy


/**
 * Copyright (C) 2010-2013 Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.rocketmq.client.consumer;

import java.util.List;

import com.alibaba.rocketmq.common.message.MessageQueue;



SendResult result = producer.send(msg, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
                    Integer id = (Integer) arg;
                    int index = id % mqs.size();
                    return mqs.get(index);
                }
            }, orderId);



/**
 * 生产者队列选择器
 * 
 * @author shijia.wxr<vintage.wang@gmail.com>
 * @since 2013-7-25
 */
public interface MessageQueueSelector {
    public MessageQueue select(final List<MessageQueue> mqs, final Message msg, final Object arg);
}




/**消费者
 * Consumer队列自动分配策略
 * 
 * @author shijia.wxr<vintage.wang@gmail.com>
 * @since 2013-7-24
 */
public interface AllocateMessageQueueStrategy {
    /**
     * 给当前的ConsumerId分配队列
     * <br/>
     * chen.si 这里的使用场景如下:
     * 			前提: 1. 一个topic对应多个分区队列  
     * 				2. 这个topic有一个consumer group进行消费
     * 				3. 有多个consumer(1个进程内多个 或者 多个进程 或者多个机器),这些consumer属于同一个consumer group
     *          在同一个group中的多个consumer中,他们需要区分的均衡的唯一的消费某个分区队列,这时候还不能多个consumer重复消费某个队列。
     *          需要一个算法,能唯一的确定均衡分配,将不同的队列分配给不同的consumer。 
     *          这个算法的难点: 必须保证不同的进程 独立的 计算属于自己的消费队列, 而且这里的消费队列不能重复
     * 
     * @param currentCID
     *            当前ConsumerId
     * @param mqAll
     *            当前Topic的所有队列集合,无重复数据,且有序
     * @param cidAll
     *            当前订阅组的所有Consumer集合,无重复数据,且有序
     * @return 分配结果,无重复数据
     */
    public List<MessageQueue> allocate(//
            final String currentCID,//
            final List<MessageQueue> mqAll,//
            final List<String> cidAll//
    );
}

 

转载于:https://my.oschina.net/xiaominmin/blog/1592252

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值