ActiveMQ系列:ActiveMQ传输协议

前面有介绍与基础、应用实践部分,有兴趣的可以移步:

初步认识了ActiveMQ:https://blog.csdn.net/qq_26975307/article/details/98875098

结合JavaSE进行初尝试:https://blog.csdn.net/qq_26975307/article/details/98968854

详细讲讲JMS:https://blog.csdn.net/qq_26975307/article/details/99408962

JMS的可靠性:https://phubing.blog.csdn.net/article/details/99412285

结合 Spring,基于配置文件的使用 ActiveMQ:https://phubing.blog.csdn.net/article/details/99413883

结合 SpringBoot,基于 application.xml 使用ActiveMQ:https://blog.csdn.net/qq_26975307/article/details/99415899

此篇企图一窥 ActiveMQ 的传输协议,有兴趣可以留言共同探讨

1、可能遇到的面试题

这篇写的挺多的,就不重复了:https://msd.misuland.com/pd/2884250137616455834

另外,如果问:你生产上的链接协议如何配置的?使用tcp吗?(黑人问号,如果是TCP那么就是单机跑的ActiveMQ)

2、ActiveMQ的传输协议

2.1、ActiveMQ支持的网络协议

2.2、有哪些呢

不同的配置,MQ性能是不一样的

官网拜读:https://activemq.apache.org/connectivity

(1)Transmission Control Protocol(TCP)-- 默认

1、这是默认的Broker配置,TCP的Client监听端口616162.在网络传输数据前,必须要序列化数据,消息是通过一个叫wire protocol的来序列化成字节流。

2、默认情况下ActiveMQ把wire protocol叫做OpenWire,它的目的是促使网络上的效率和数据快速交互。

3、TCP连接的URI形式如:tcp://hostname:port?key=value&key=value,后面的参数是可选

4、TCP传输的优点:
(4.1)TCP协议传输可靠性高,稳定性强
(4.3)有效性、可用性:应用广泛,支持任何平台                                                                                                        (4.2)高效性:字节流方式传递,效率很高

5、关于Transport协议的可配置参数可以参考官网:http://activemq.apache.org/configuring-version-5-transports.html

(2)New I/O API Protocol(NIO)

ActiveMQ支持的client-broker通讯协议有:TCP、NIO、UDP、SSL、Http(s)、VM。

其中配置 Transport Connector 的文件在activeMQ安装目录的 conf/activemq.xml 中的 <transportConnectors> 标签之内。

1.NIO协议和TCP协议类似但NIO更侧重于底层的访问操作。它允许开发人员对同一资源可有更多的client调用和服务端有更

多的负载

2. 适合使用NIO协议的场景:

(2.1)可能有大量的Client去连接到Broker上,一般情况下,大量的Client去连接Broker是被操作系统的线程所限制的。因

此,NIO的实现比TCP需要更少的线程去运行,所以建议使用NIO协议

(2.2)可能对于Broker有一个很迟钝的网络传输,NIO比TCP提供更好的性能。

3. NIO连接的URI形式:nio/hostname:port?key = value

4.Transport Connector配置示例,参考官网去:

http://activemq.apache.org/configuring-version-5-transports.html

The NIO Transport

Same as the TCP transport, except that the New /O(NIO) package is used, which may provide better performance. The Java NIO package should not be confused with IBM's AlO4Jpackage.

To switch from TCP to Nlo, simply change the scheme portion of the URl. Here's an example as defined within a broker's XML configuration file.

<broker>
  ...
  <transportConnectors>
    <transportConnector name="nio" uri="nio://0.0.0.0:61616"/>  
  </<transportConnectors>
  ...
</broker>

(3)AMQP 协议

即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开

放标准,为面向消息的中间件设计。基于此协议的客户端与消息中间件可传递消息,并不受客户端/中间件不同产品,不同

开发语言等条件的限制。

ActiveMQ supports the AMQP 1.0 protocol which is an OASIS standard.

Availability

Available from ActiveMQ version 5.8 onward.

Enabling the ActiveMQ Broker for AMQP

To enable AMQP protocol support on the broker add the following transport connector Configuration referencing the amqp scheme in its URl:

<transportConnectors>
    <transportconnector name="amqp"uri="amqp://0.0.0.0:5672"/>
</transportConnectors>

It is enabled in the default ActiveMQ server configuration.For more help see Run Broker.

(4)MQTT协议

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)是IBM开发的一个即时通讯协议,有可能成为物联网

的重要组成部分。该协议支持所有平台,几乎可以把所有联网物品和外部连接起来,被用来当做传感器和致动器(比如通

过Twitter让房屋联网)的通信协议。

懒得看官网版:https://s2.ax1x.com/2019/08/29/mL4W38.png

怎么用呢?

Enabling the ActiveMQ Broker for MQTT

Its very easy to enable ActiveMQ for MQTT. Just add a connector to the broker using the MQTT URL.

<transportConnectors>
   <transportConnector name="mqtt" uri="mqtt://localhost:1883"/>
</transportConnectors>

扩展github:https://github.com/fusesource/mqtt-client

(如果你直接改的话,会出现下面这个错误:

Caused by:java.lang.ClassNotFoundException:org.fusesource.mqtt.codec.PINGRESP

我怎么知道?shift ...  你把tcp改成它就出现了呗,因为还需要配置,具体自行百度去,喔又不是做硬件的 ....)

(5)Secure Sockets Layer Protocol(SSL)

1、连接的URL形式:ssl://hostname:port?key=value

2、Transport Connector配置示例:

<transportConnectors>
   <transportConnector name="ssl" uri="ssl://localhost:61618?trace=true"/>
</transportConnectors>

(6)STORM 协议

STOMP,Streaming Text Orientated Message Protocol,是流文本定向消息协议,是一种为MOM(Message Oriented

Middleware,面向消息的中间件)设计的简单文本协议。

官网拜读:https://activemq.apache.org/stomp

<transportConnectors>
   <transportConnector name="stomp" uri="stomp://localhost:61613"/>
</transportConnectors>

(7)WebSocket  -- ws 协议

还是拜读官网吧:https://activemq.apache.org/websockets

2.3、NIO性能增强

(1)配置了 NIO 后性能应该可以,有没有更进一步的优化?

URI 格式头以 "nio” 开头,表示这个端口使用以TCP 协议为基础的 NIO 网络 IO 模型。但是这样的设置方式,只能使这个端

口支持 Openwire 协议。怎么既让这个端口支持 NIO 网络 IO 模型,又让它支持多个协议呢?

(2)使用auto关键字

依旧是官网拜读:https://activemq.apache.org/auto

Enabling AUTO over NIO

To configure ActiveMQ auto wire format detection over an NIO TCP connection use the auto+nio transport prefix. For example, add the following transport configuration in your XML file:

<transportConnector name="auto+nio" uri="auto+nio://localhost:5671"/>

(3)使用“+”符号来为端口设置多种特性

如果您不特别指定 ActiveMQ 的网络监听端口,那么这些端口都将使用 BIO 网络 IO 模型。(OpenWire,STOMP,AMQP..

…)

所以为了首先提高单节点的网络吞吐性能,我需要明确指定Active的网络IO模型,如下所示:URI 格式头以 ”nio” 开头,表

示这个端口使用以TCP协议为基础的 NIO 网络 IO 模型。

(4)如果既需要某一个端口支持NIO网络lO模型,又需要它支持多个协议?

 

未完待续......(下篇开始 ActiveMQ 的持久化机制)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

phubing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值