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 true the socket close call happens asynchronously. This parameter should be set to false for protocols like STOMP, that are commonly used in situations where a new connection is created for each read or write. Doing so ensures the socket close call happens synchronously. A synchronous close prevents the broker from running out of available sockets owing to the rapid cycling of connections. |
connectionTimeout | 30000 | If >=1 the value sets the connection timeout in milliseconds. A value of 0 denotes no timeout. Negative values are ignored. |
daemon | false | If true the transport thread will run in daemon mode. Set this parameter to true when embedding the broker in a Spring container or a web container to allow the container to shut down correctly. |
dynamicManagement | false | If true the TransportLogger can be managed by JMX. |
ioBufferSize | 8 * 1024 | Specifies the size of the buffer to be used between the TCP layer and the OpenWire layer where wireFormat based marshaling occurs. |
jmxPort | 1099 | (Client Only) Specifies the port that will be used by the JMX server to manage the TransportLoggers . This should only be set, via URI, by either a client producer or consumer as the broker creates its own JMX server. Specifying an alternate JMX port is useful for developers that test a broker and client on the same machine and need to control both via JMX. |
keepAlive | false | If true , enables TCP KeepAlive on the broker connection to prevent connections from timing out at the TCP level. This should not be confused with KeepAliveInfo messages as used by the InactivityMonitor . |
logWriterName | default | Sets the name of the org.apache.activemq.transport.LogWriter implementation to use. Names are mapped to classes in the resources/META-INF/services/org/apache/activemq/transport/logwriters directory. |
maximumConnections | Integer.MAX_VALUE | The maximum number of sockets allowed for this broker. |
minmumWireFormatVersion | 0 | The minimum remote wireFormat version that will be accepted (note the misspelling). Note: when the remote wireFormat version is lower than the configured minimum acceptable version an exception will be thrown and the connection attempt will be refused. A value of 0 denotes no checking of the remote wireFormat version. |
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 soLinger when the value is > -1 . When set to -1 the soLinger socket option is disabled. |
soTimeout | 0 | Sets the socket’s read timeout in milliseconds. A value of 0 denotes no timeout. |
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 128K . A value of 0 indicates that this parameter is ignored. |
startLogging | true | If true the TransportLogger object of the Transport stack will initially write messages to the log. This parameter is ignored unless trace=true . |
tcpNoDelay | false | If true the socket’s option TCP_NODELAY is set. This disables Nagle’s algorithm for small packet transmission. |
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 Log4j logger: log4j.logger.org.apache.activemq.transport.TransportLogger=DEBUG . |
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: [0,64] . Valid string values: EF , AF[1-3][1-4] or CS[0-7] . With JDK 6, only works when the JVM uses the IPv4 stack. To use the IPv4 stack set the system property java.net.preferIPv4Stack=true . Note: it’s invalid to specify both ‘diffServ and typeOfService’ at the same time as they share the same position in the TCP/IP packet headers |
typeOfService | 0 | (Client only) The preferred Type of Service value to be set on outgoing packets. Valid integer values: [0,256] . With JDK 6, only works when the JVM is configured to use the IPv4 stack. To use the IPv4 stack set the system property java.net.preferIPv4Stack=true . Note: it’s invalid to specify both ‘diffServ and typeOfService’ at the same time as they share the same position in the TCP/IP packet headers. |
useInactivityMonitor | true | When false the InactivityMonitor is disabled and connections will never time out. |
useKeepAlive | true | When true KeepAliveInfo messages are sent on an idle connection to prevent it from timing out. If this parameter is false connections will still timeout if no data was received on the connection for the specified amount of time. |
useLocalHost | false | When true local connections will be made using the value localhost instead of the actual local host name. On some operating systems, such as OS X , it’s not possible to connect as the local host name so localhost is better. |
useQueueForAccept | true | When true accepted sockets are placed onto a queue for asynchronous processing using a separate thread. |
wireFormat | default | The name of the wireFormat factory to use. |
wireFormat.* | N/A | Properties with this prefix are used to configure the wireFormat . |
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>