ActiveMQ官网activemq.apache.org/
传输配置选项http://activemq.apache.org/configuring-transports
ActiveMQ支持哪些协议
- ActiveMQ支持多种协议传输和传输方式,允许客户端使用多种协议连接。
- ActiveMQ支持的协议:AUTO、OpenWire、AMQP、Stomp、MQTT等。
- ActiveMQ支持的基础传输方式:VM、TCP、SSL、UDP、Peer、Multicast、HTTP(S)等,以及更高级的Failover、Fanout、Discovery、ZeroConf方式。
协议+nio/bio+基础传输方式
ActiveMQ的协议连接配置
在${ACTIVEMQ_HOME}/conf/activemq.xml
中,通过配置就可以使用多种传输方式。默认使用openwire。
ActiveMQ常用的传输方式及配置
TCP
- TCP
由于TCP具有可靠传输的特性,它在ActiveMQ中也是最常使用的一种协议。在默认的配置中,TCP连接的端口为61616。
- TCP配置格式
tcp://hostname:port?kev=value
- TCP配置参数说明
在服务器端配置时,参数要以“transport.”开头
在客户端连接时,参数省略“transport.”前缀
- 服务器端配置示例:
tcp://localhost:61616?transport.threadName&transport.trace=false&transport.soTimeout=60000
- 客户端配置示例:
tcp://localhost:61616?threadName&trace=false&soTimeout=60000
Transport Options(TCP配置参数说明)
Option Name | Default Value | Description |
backlog | 5000 | Specifies the maximum number of connections waiting to be accepted by the transport server socket. |
closeAsync | true | If |
connectionTimeout | 30000 | If |
daemon | false | If |
dynamicManagement | false | If |
ioBufferSize | 8 * 1024 | Specifies the size of the buffer to be used between the TCP layer and the OpenWire layer where |
jmxPort | 1099 | (Client Only) Specifies the port that will be used by the JMX server to manage the |
keepAlive | false | If |
logWriterName | default | Sets the name of the |
maximumConnections | Integer.MAX_VALUE | The maximum number of sockets allowed for this broker. |
minmumWireFormatVersion | 0 | The minimum remote |
socketBufferSize | 64 * 1024 | Sets the size, in bytes, for the accepted socket’s read and write buffers. |
soLinger | Integer.MIN_VALUE | Sets the socket’s option |
soTimeout | 0 | Sets the socket’s read timeout in milliseconds. A value of |
soWriteTimeout | 0 | Sets the socket’s write timeout in milliseconds. If the socket write operation does not complete before the specified timeout, the socket will be closed. A value of 0 denotes no timeout. |
stackSize | 0 | Set the stack size of the transport’s background reading thread. Must be specified in multiples of |
startLogging | true | If |
tcpNoDelay | false | If |
threadName | N/A | When this parameter is specified the name of the thread is modified during the invocation of a transport. The remote address is appended so that a call stuck in a transport method will have the destination information in the thread name. This is extremely useful when using thread dumps for degugging. |
trace | false | Causes all commands that are sent over the transport to be logged. To view the logged output define the |
trafficClass | 0 | The Traffic Class to be set on the socket. |
diffServ | 0 | (Client only) The preferred Differentiated Services traffic class to be set on outgoing packets, as described in RFC 2475. Valid integer values: |
typeOfService | 0 | (Client only) The preferred Type of Service value to be set on outgoing packets. Valid integer values: |
useInactivityMonitor | true | When |
useKeepAlive | true | When |
useLocalHost | false | When |
useQueueForAccept | true | When |
wireFormat | default | The name of the |
wireFormat.* | N/A | Properties with this prefix are used to configure the |
SSL
- SSL
SSL的作用:
1)认证用户和服务器,确保数据发送到正确的客户机和服务器;
2)加密数据以防止数据中途被窃取;
3)维护数据的完整性,确保数据在传输过程中不被改变。
需要一个安全连接的时候可以考虑使用SSL,适用于client和broker在公网的情况,如使用aws云平台等
http://activemq.apache.org/ssl-transport-reference.html
- SSL配置格式,可配置参数和TCP相同
ssl://localhost:61616
- SSL客户端配置
JMS客户端需要使用ActiveMQSslConnectionFactory类创建连接,brokerUrl以ssl://开头,以下是Spring配置示例
- SSL主机名验证
从ActiveMQ 5.15.6开始,ActiveMQ开始支持TLS主机名验证,默认情况下客户端启用了该验证,而服务器端没有启动。
- 服务器端配置
ssl://localhost:61616?transport.verifyHostName=true
- 客户端配置
ssl://localhost:61616?socket.verifyHostName=false
或者
ssl://localhost:61616?verifyHostName=false
NIO
- NIO
使用Java的NIO方式对连接进行改进,因为NIO使用线程池,可以复用线程,所以可以用更少的线程维持更多的连接。如果有大量的客户端,或者性能瓶颈在网络传输上,可以考虑使用NIO的连接方式。
- NIO配置格式,可配置参数和TCP相同
nio://hostname:port?key=value
NIO是OpenWire协议的传输方式,其它协议,像AMQP、MQTT、Stomp,也有NIO的实现,通常在协议前缀中加“+nio”来区别。示例:
mqtt+nio://localhost:1883
- NIO传输线程使用情况配置
从5.15.0开始,ActiveMQ支持调整NIO的传输协议,可以设置以下属性。
- NIO传输线程使用情况配置
属性可以在${ACTIVEMQ_HOME}/bin/env中配置,示例:
NIO SSL
- NIO SSL
从ActiveMQ 5.6版本开始,NIO可以支持和SSL搭配使用的传输连接。
- NIO+SSL配置格式,可配置参数和TCP相同
nio+ssl://0.0.0.0:61616
UDP
与面向连接,可靠的字节流服务的TCP不同,UDP是一个面向数据的简单传输连接,没有TCP的三次握手,所以性能大大强于TCP,但是是以牺牲可靠性为前提。适应于丢失也无所谓的消息。
- UDP配置格式
udp://localhost:8123
- UDP配置参数说明
属性 | 默认值 | 描述 |
trace | false | 为true时,将会记录传输发送的所有命令。 |
useLocalHost | true | 为true时,本机机器名被解析为localhost |
datagramSize | 4*1024 | 指定数据报的大小 |
HTTP(s)
- HTTP(s)
需要穿越防火墙,可以考虑使用HTTP(s),但由于HTTP(s)是短连接,每次创建连接的成本较高,所以性能最差。通过XML传输数据。
- HTTP配置格式
http://localhost:8080?param1=val1¶m2=val2
- HTTPS配置格式
https://localhost:8080?param1=val1¶m2=val2
VM
- VM
虚拟机协议(方法直调),使用场景是client和broker在同一个Java虚拟机内嵌的情况,无需网络通信的开销。
- VM配置格式
vm://brokerName?marshal=false&broker.persistent=false
- VM 配置参数说明
属性 | 默认值 | 描述 |
create | true | 如果不存在,则创建broker |
waitForStart | -1 | 如果>0,则表示等待代理启动的超时(以毫秒为单位)。值-1和0表示不等待。仅在ActiveMQ 5.2+支持 |
wireFormat | default | 命令的组装格式(协议) |
marshal | false | 强制发送的每个命令使用WireFormat进行格式转换 |
OpenWire协议
OpenWire协议是什么
OpenWire是Apache的一种跨语言的协议,允许从不同的语言和平台访问ActiveMQ,是ActiveMQ 4.x以后的版本默认的传输协议。
OpenWire是Apache的一种跨语言的协议,允许从不同的语言和平台访问ActiveMQ,是ActiveMQ 4.x以后的版本默认的传输协议。
http://activemq.apache.org/openwire-version-2-specification.html
http://activemq.apache.org/wire-protocol.html
OpenWire协议如何使用
OpenWire支持TCP、SSL、NIO、UDP、VM等传输方式,直接配置这些连接,就是使用的OpenWire协议,OpenWire有自己的配置参数,客户端和服务器端配置的参数名都通过前缀"wireFormat."表示。
http://activemq.apache.org/configuring-wire-formats.html
示例:
tcp://localhost:61616?wireFormat.cacheEnabled=false&wireFormat.tightEncodingEnabled=false
- OpenWire的配置参数说明
MQTT协议
MQTT(Message Queueing Telemetry Transport)消息队列遥测传输是IBM开发的一个即时通讯协议,已成为物联网通信的标准。
MQTT的发布订阅模型
MQTT 服务质量
- 什么是服务质量
服务质量(Qos)级别是一种关于发送者和接受者之间信息投递的保证协议。MQTT中有三种QoS级别:
至多一次(0)
至少一次(1)
只有一次(2)
- 为什么要有服务质量
QoS是MQTT的一个主要功能,它使得在不可靠的网络下进行通信变得更为简单,因为即便是在非常不可靠的网络下,协议也可以掌握是否需要重发消息并保证消息到达。它也能帮助客户端根据网络环境和程序逻辑来自由选择QoS。
- 它是怎样工作的 ---- QoS level 0
- 它是怎样工作的 ---- QoS level 1
针对客户端发布消息到服务器的消息流: - 它是怎样工作的 ---- QoS level 2
针对客户端发布消息到服务器的消息流:
- ActiveMQ 服务器端配置
- MQTT使用NIO
<transportConnector name="mqtt+nio" uri="mqtt+nio://localhost:1883"/>
- MQTT使用NIO + SSL
<transportConnector name="mqtt+nio" uri="mqtt+nio+ssl://localhost:1883"/>
- MQTT 也支持层次结构和通配符,但分隔符和ActiveMQ不同,映射关系:
作用 | ActiveMQ | MQTT |
分隔符 | . | / |
元素 | * | + |
字节点 | > | # |
- 示例
主题名:foo.blah.bar
在MQTT的客户端订阅时,可以为foo/+/bar
在JMS订阅时,可以为foo.*.bar
- Spring Integration 提供了MQTT协议的支持,通过Maven添加依赖即可使用。
<dependency>
<groupld>org.springframework.integration</groupld>
<artifactld>spring-integration-mqtt</artifactld>
<version>5.1.1.RELEASE</version>
</dependency>
官方教程:https://docs.spring.io/spring-integration/reference/html/mqtt.html
AUTO协议
AUTO自动检测协议
从ActiveMQ 5.13.0开始,ActiveMQ开始支持协议格式检测,可以自动检测OpenWire、STOMP、AMQP和MQTT。允许这4种类型的客户端共享一个传输。
如何使用AUTO协议
- AUTO使用TCP
<transportConnector name="auto" uri="auto://localhost:5671"/>
- AUTO使用SSL
<transportConnector name="auto+ssl" uri="auto+ssl://localhost:5671>
- AUTO使用NIO
<transportConnector name="auto+nio" uri="auto+nio://localhost:5671>
- AUTO使用NIO+SSL
<transportConnector name="auto+nio+ssl" uri="auto+nio+ssl://localhost:5671>