一、ActiveMQ支持的协议
除了TCP协议之外,ActiveMQ还支持其他的很多协议,比如:AMQP、UDP、SSL、HTTP(S)、VM、MQTT、NIO等,具体可参考官网协议配置选项,也可以根据官网进行调优参数设置。
协议 | 描述 |
---|---|
TCP | 默认的协议,性能相对可以 |
NIO | 基于TCP协议,进行了扩展和优化 |
UDP | 性能比TCP更好,但是不具有可靠性 |
SSL | 安全链接 |
HTTP(S) | 基于HTTP或HTTPS |
VM | VM本身不是协议,当客户端和代理在同一个Java虚拟机(VM)中运行时,它们之间需要通信,但不想占用网络通道,而是直接通信时可以使用该方式 |
二、配置ActiveMQ的协议和端口
在ActiveMQ安装目录的conf目录下有个activemq.xml配置文件,该文件中有个<transportConnectors>标签用来配置支持的协议,各个协议使用的端口也是在这里配置。默认配置内容如下:
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
ActiveMQ并不是只能同时支持一种协议,而是可以同时支持多种,也就是说,连接到ActiveMQ服务器的生产者和消费者可以是不同的协议。
生产中为了提高MQ服务器的性能,我们大多会采用NIO的方式,因此我们可以将NIO的协议添加到支持的协议中,配置如下:
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<!-- 新加的NIO协议,器端口为61618 -->
<transportConnector name="nio" uri="nio://0.0.0.0:61618?trace=true"/>
</transportConnectors>
在管理控制台可以看到当前支持的协议:
配置了NIO后,在使用时我们只需要将协议的前缀改为nio即可:但并不是所有的协议都是如此,有些协议还需要改变编码
spring:
activemq:
broker-url: nio://192.168.2.107:61618
三、配置ActiveMQ同时支持NIO和多种协议
默认情况下,除了NIO协议外其他协议都是采用BIO的网络IO模型,我们可以通过配置的方式使NIO同时支持其他多种协议,即使用其他协议时,采用的网络IO模型也是NIO。
ActiveMQ支持自动探测协议类型的配置,配置时采用“auto+nio”的形式,示例如下:后面还可以跟一些参数,具体参看官网
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="nio" uri="nio://0.0.0.0:61618?trace=true"/>
<transportConnector name="auto+nio" uri="auto+nio://0.0.0.0:61608"/>
</transportConnectors>
此时只要客户端连接时使用的端口是61608,那么具体使用的协议就可以被侦测到,这种方式可同时支持多种协议,且这些协议都是使用的NIO网络IO模型。
spring:
activemq:
broker-url: tcp://192.168.2.107:61608 #协议可以是tcp、nio、STOMP、AMQP、MQTT,只要端口是61608即可