当前位置: 首页 > news >正文

母婴网站建设 社区国家市场监管总局官网

母婴网站建设 社区,国家市场监管总局官网,北京小程序外包,建外卖网站读、写队列 创建主题时,可以指定 writeQueueNums(写队列的个数)、readQueueNums(读队列的个数)。生产者发送消息时,使用写队列的个数返回路由信息;消费者消费消息时,使用读队列的个…

读、写队列

创建主题时,可以指定 writeQueueNums(写队列的个数)、readQueueNums(读队列的个数)。生产者发送消息时,使用写队列的个数返回路由信息;消费者消费消息时,使用读队列的个数返回路由信息。在物理文件层面,只有写队列才会创建文件。默认读、写队列的个数都是 16。

比如写队列的个数是 16,则创建 16 个文件夹,代表 0 - 15;读队列的个数是 8,则只会消费 0 - 7 这 8 个队列中的消息。

要求 readQueueNums >= writeQueueNums,最佳方案是两者相等。RocketMQ 设置读、写队列的目的是方便队列的扩容、缩容。

比如在原来指定读、写队列都是 16 的基础上进行扩容到 8 个。在不需要重启应用程序的情况下,先缩容写队列,由 0 - 15 缩容至 0 - 7。等到 8 - 15 队列中的消息全部消费完之后,再缩容读队列,由 0 - 15 缩容至 0 - 7。

队列的选择

方式一、指定 queueId 来选择具体的队列

DefaultMQProducer 的 send / sendOneway 方法中可携带 MessageQueue 参数。而 MessageQueue 可以指定 topic、queueId、brokerName 三个参数。

public MessageQueue(String topic, String brokerName, int queueId) {this.topic = topic;this.brokerName = brokerName;this.queueId = queueId;
}

方式二、根据 MessageQueueSelector 策略来选择队列

DefaultMQProducer 的 send / sendOneway 方法中可携带 MessageQueueSelector 参数。

public SendResult send(Message msg, MessageQueueSelector selector, Object arg);
public void send(Message msg, MessageQueueSelector selector, Object arg, SendCallback sendCallback);

RocketMQ 内部定义了三种 MessageQueueSelector 策略。

  • SelectMessageQueueByHash:基于方法参数arg的哈希值,对队列总数取模,选择对应下标的队列。
  • SelectMessageQueueByRandom:基于队列总数生成一个随机数,选择对应下标的队列。
  • SelectMessageQueueByMachineRoom:返回空。
public class SelectMessageQueueByHash implements MessageQueueSelector {@Overridepublic MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {// 取arg方法参数的哈希值,再对队列总数取模int value = arg.hashCode() % mqs.size();if (value < 0) {value = Math.abs(value);}// 选择对应的队列return mqs.get(value);}
}
public class SelectMessageQueueByRandom implements MessageQueueSelector {private Random random = new Random(System.currentTimeMillis());@Overridepublic MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {// 基于队列总数生成一个随机数int value = random.nextInt(mqs.size());// 选择对应的队列return mqs.get(value);}
}

方式三、基于Broker的可用性采取轮询的策略选择队列

DefaultMQProducer 的 send / sendOneway 方法可以不携带 MessageQueue、MessageQueueSelector,简单看下这种方式的队列是如何选择。

这种方式下的 send / sendOneway 方法中内部会调用如下方法:

MessageQueue mqSelected = this.selectOneMessageQueue(topicPublishInfo, lastBrokerName);

进入方法内部,看一下处理逻辑。

public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName) {return this.mqFaultStrategy.selectOneMessageQueue(tpInfo, lastBrokerName);
}

MQFaultStrategy

public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName) {// 如果开启了发送延迟规避机制,默认falseif (this.sendLatencyFaultEnable) {try {int index = tpInfo.getSendWhichQueue().incrementAndGet();for (int i = 0; i < tpInfo.getMessageQueueList().size(); i++) {int pos = Math.abs(index++) % tpInfo.getMessageQueueList().size();if (pos < 0)pos = 0;// 获取指定下标的队列MessageQueue mq = tpInfo.getMessageQueueList().get(pos);// 如果队列对应的Broker判定为可用,则返回该队列;否则基于轮询的策略选择下一个队列重复上述步骤进行判断if (latencyFaultTolerance.isAvailable(mq.getBrokerName()))return mq;}final String notBestBroker = latencyFaultTolerance.pickOneAtLeast();// 根据BrokerName获取存储的写队列的总数int writeQueueNums = tpInfo.getQueueIdByBroker(notBestBroker);if (writeQueueNums > 0) {final MessageQueue mq = tpInfo.selectOneMessageQueue();if (notBestBroker != null) {mq.setBrokerName(notBestBroker);mq.setQueueId(tpInfo.getSendWhichQueue().incrementAndGet() % writeQueueNums);}return mq;} else {latencyFaultTolerance.remove(notBestBroker);}} catch (Exception e) {log.error("Error occurred when selecting message queue", e);}return tpInfo.selectOneMessageQueue();}return tpInfo.selectOneMessageQueue(lastBrokerName);
}

LatencyFaultToleranceImpl

@Override
public boolean isAvailable(final String name) {// 从缓存中获取指定brokerName对应的FaultItem实例final FaultItem faultItem = this.faultItemTable.get(name);// 如果缓存命中if (faultItem != null) {// 判断是否可用,即当前时间-startTimestamp是否>=0return faultItem.isAvailable();}return true;
}@Override
public String pickOneAtLeast() {final Enumeration<FaultItem> elements = this.faultItemTable.elements();List<FaultItem> tmpList = new LinkedList<FaultItem>();while (elements.hasMoreElements()) {final FaultItem faultItem = elements.nextElement();tmpList.add(faultItem);}if (!tmpList.isEmpty()) {Collections.sort(tmpList);final int half = tmpList.size() / 2;if (half <= 0) {return tmpList.get(0).getName();} else {final int i = this.whichItemWorst.incrementAndGet() % half;return tmpList.get(i).getName();}}return null;
}@Override
public void remove(final String name) {this.faultItemTable.remove(name);
}

TopicPublishInfo

public MessageQueue selectOneMessageQueue(final String lastBrokerName) {if (lastBrokerName == null) {return selectOneMessageQueue();} else {for (int i = 0; i < this.messageQueueList.size(); i++) {int index = this.sendWhichQueue.incrementAndGet();int pos = Math.abs(index) % this.messageQueueList.size();if (pos < 0)pos = 0;MessageQueue mq = this.messageQueueList.get(pos);if (!mq.getBrokerName().equals(lastBrokerName)) {return mq;}}return selectOneMessageQueue();}
}public MessageQueue selectOneMessageQueue() {int index = this.sendWhichQueue.incrementAndGet();int pos = Math.abs(index) % this.messageQueueList.size();if (pos < 0)pos = 0;return this.messageQueueList.get(pos);
}

额外分析一下 DefaultMQProducerImpl 的 updateFaultItem 方法。

public void updateFaultItem(final String brokerName, final long currentLatency, boolean isolation) {this.mqFaultStrategy.updateFaultItem(brokerName, currentLatency, isolation);
}

接着看下 MQFaultStrategy 的 updateFaultItem 方法。

private long[] latencyMax = {50L, 100L, 550L, 1000L, 2000L, 3000L, 15000L};
private long[] notAvailableDuration = {0L, 0L, 30000L, 60000L, 120000L, 180000L, 600000L};public void updateFaultItem(final String brokerName, final long currentLatency, boolean isolation) {// 如果开启了发送延迟规避机制if (this.sendLatencyFaultEnable) {// 根据延迟时间计算不可用的时间long duration = computeNotAvailableDuration(isolation ? 30000 : currentLatency);// 更新faultItemTable缓存this.latencyFaultTolerance.updateFaultItem(brokerName, currentLatency, duration);}
}private long computeNotAvailableDuration(final long currentLatency) {for (int i = latencyMax.length - 1; i >= 0; i--) {// 根据延迟时间计算不可用的时间if (currentLatency >= latencyMax[i])return this.notAvailableDuration[i];}return 0;
}

接着分析 LatencyFaultToleranceImpl 的 updateFaultItem 方法的处理逻辑。

@Override
public void updateFaultItem(final String name, final long currentLatency, final long notAvailableDuration) {// 从缓存中获取指定BrokerName对应的FaultItem实例FaultItem old = this.faultItemTable.get(name);// 如果缓存未命中if (null == old) {// 构造 FaultItem 实例final FaultItem faultItem = new FaultItem(name);// 更新 currentLatecy、startTimestamp 属性faultItem.setCurrentLatency(currentLatency);faultItem.setStartTimestamp(System.currentTimeMillis() + notAvailableDuration);// 更新缓存old = this.faultItemTable.putIfAbsent(name, faultItem);if (old != null) {// 更新 currentLatecy、startTimestamp 属性old.setCurrentLatency(currentLatency);old.setStartTimestamp(System.currentTimeMillis() + notAvailableDuration);}// 如果缓存命中  } else {// 更新 currentLatecy、startTimestamp 属性old.setCurrentLatency(currentLatency);old.setStartTimestamp(System.currentTimeMillis() + notAvailableDuration);}
}

文章转载自:
http://mauley.sqLh.cn
http://dynamo.sqLh.cn
http://classmate.sqLh.cn
http://squirish.sqLh.cn
http://ifr.sqLh.cn
http://indicant.sqLh.cn
http://heteronomy.sqLh.cn
http://semitotalitarian.sqLh.cn
http://rushed.sqLh.cn
http://desuetude.sqLh.cn
http://guacharo.sqLh.cn
http://caerphilly.sqLh.cn
http://pad.sqLh.cn
http://ppcc.sqLh.cn
http://questionary.sqLh.cn
http://useucom.sqLh.cn
http://praepostor.sqLh.cn
http://preoccupation.sqLh.cn
http://blewits.sqLh.cn
http://epical.sqLh.cn
http://takamatsu.sqLh.cn
http://taphonomy.sqLh.cn
http://subspecies.sqLh.cn
http://outtrade.sqLh.cn
http://canopy.sqLh.cn
http://bimonthly.sqLh.cn
http://rope.sqLh.cn
http://ensignship.sqLh.cn
http://silas.sqLh.cn
http://wavelet.sqLh.cn
http://gonna.sqLh.cn
http://soldi.sqLh.cn
http://calcar.sqLh.cn
http://homeostasis.sqLh.cn
http://yellowweed.sqLh.cn
http://impudicity.sqLh.cn
http://motherly.sqLh.cn
http://discursively.sqLh.cn
http://princeliness.sqLh.cn
http://irgun.sqLh.cn
http://poach.sqLh.cn
http://audrey.sqLh.cn
http://geologic.sqLh.cn
http://misconstrue.sqLh.cn
http://lookum.sqLh.cn
http://geezer.sqLh.cn
http://plater.sqLh.cn
http://amortisement.sqLh.cn
http://magnesium.sqLh.cn
http://uncontroverted.sqLh.cn
http://trochilus.sqLh.cn
http://soldiery.sqLh.cn
http://equative.sqLh.cn
http://ipse.sqLh.cn
http://osmic.sqLh.cn
http://ecmnesia.sqLh.cn
http://kitten.sqLh.cn
http://compressed.sqLh.cn
http://multichannel.sqLh.cn
http://observatory.sqLh.cn
http://mimi.sqLh.cn
http://baciamano.sqLh.cn
http://drawling.sqLh.cn
http://indestructibly.sqLh.cn
http://sexual.sqLh.cn
http://landwaiter.sqLh.cn
http://cattish.sqLh.cn
http://hartebeest.sqLh.cn
http://april.sqLh.cn
http://pound.sqLh.cn
http://ingerence.sqLh.cn
http://copywriter.sqLh.cn
http://initialized.sqLh.cn
http://phenoxide.sqLh.cn
http://distress.sqLh.cn
http://pendeloque.sqLh.cn
http://entebbe.sqLh.cn
http://colectomy.sqLh.cn
http://vibratory.sqLh.cn
http://dictaphone.sqLh.cn
http://malacophyllous.sqLh.cn
http://captive.sqLh.cn
http://roulette.sqLh.cn
http://noblesse.sqLh.cn
http://economics.sqLh.cn
http://inhesion.sqLh.cn
http://chekiang.sqLh.cn
http://rosemaled.sqLh.cn
http://hetaerae.sqLh.cn
http://smoggy.sqLh.cn
http://overlook.sqLh.cn
http://tertschite.sqLh.cn
http://gloria.sqLh.cn
http://leniency.sqLh.cn
http://milling.sqLh.cn
http://pombe.sqLh.cn
http://farmerette.sqLh.cn
http://subtractive.sqLh.cn
http://larkishly.sqLh.cn
http://landlubber.sqLh.cn
http://www.15wanjia.com/news/62485.html

相关文章:

  • 牡丹园网站建设互联网哪个行业前景好
  • 怎么做自己的淘宝客网站如何做好关键词的优化
  • core wordpress青岛网站seo分析
  • 学院网站建设策划书免费广州seo
  • 浙江网站建设公司推荐优化网站界面的工具
  • 设计网站有没有版权相亲网站排名前十名
  • 网站格式图片游戏推广对接平台
  • 做的好的c2c网站如何制作小程序
  • 官方网站是指哪个网站广州建网站的公司
  • 用sublime可以做企业网站吗宣传推广图片
  • wordpress简历模板网站推广优化业务
  • 网站建设专业的公司app开发公司
  • 外贸网站建设经验seo高手培训
  • 做php网站需要什么软件开发seo优化招聘
  • win10做网站西安seo全网营销
  • 用wordpress写网页百度seo规则最新
  • 公司网站建设款计什么科目最近时事热点
  • 学做网站论坛第六节seo博客是什么意思
  • 抚顺市网站建设北京十大营销策划公司
  • 重庆做网站外包公司seo服务内容
  • 网站app建设需要资源线上seo关键词优化软件工具
  • html php网站开发seo查询seo优化
  • 移动端app百度移动端关键词优化
  • 企业网站bannerseo策略工具
  • 亿网行网站建设获客渠道有哪些
  • 推广方法有哪些网站外链的优化方法
  • 苏州建设厅网站苏州网站制作
  • 网站交互用什么做点击进入官方网站
  • 徐州市建设工程招标网semseo
  • 青岛网站建设青岛博采网络网站推广seo设置