1、网络协议:
ActiveMQ 支持的协议有 TCP 、 UDP、NIO、SSL、HTTP(S) 、VM
这是activemq 的activemq.xml 中配置文件设置协议的地方
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumCon nections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnect ions=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConn ections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnect ions=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnection s=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
1、默认是使用 openwire 也就是 tcp 连接
2、默认的Broker 配置,TCP 的Client 监听端口 61616 ,在网络上传输数据,必须序列化数据,消息是通过一个 write protocol 来序列化为字节流。默认情况 ActiveMQ 会把 wire protocol 叫做 Open Wire ,它的目的是促使网络上的效率和数据快速交互 。
2、使用tcp 的一些优化方案:
tcp://hostname:port?key=value
它的参数详情参考:http://activemq.apache.org/tcp-transport-reference
各种协议对比 : http://activemq.apache.org/configuring-version-5-transports.html
3、 NIO 协议为activeMQ 提供更好的性能:
适合NIO 使用的场景:
1 当有大量的Client 连接到Broker 上 , 使用NIO 比使用 tcp 需要更少的线程数量,所以使用 NIO
2 可能对于 Broker 有一个很迟钝的网络传输, NIO 的性能高于 TCP
连接形式:
nio://hostname:port?key=value
修改 activemq.xml 使之支持 NIO 协议:
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumCon nections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnect ions=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConn ections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnect ions=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnection s=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="nio" uri="nio://0.0.0.0:61618?trace=true"/> <!-- 这是添加的 -->
</transportConnectors>
而使用 NIO 协议,代码修改量极小,只需同时将消息生产者和消费者的 URL 修改即可:
//public static final String ACTIVEMQ_URL = “tcp://192.168.17.3:61616”;
public static final String ACTIVEMQ_URL = “nio://192.168.17.3:61618”; //nio网络io模型
修改之后即可正确运行
4、 NIO 增强:
URI 格式以 nio 开头,表示这个端口使用 tcp 协议为基础的NIO 网络 IO 模型,但这样设置让它只支持 tcp 、 nio 的连接协议。如何让它支持多种协议?
Starting with version 5.13.0, ActiveMQ supports wire format protocol detection. OpenWire, STOMP, AMQP, and MQTT can be automatically detected. This allows one transport to be shared for all 4 types of clients.
使用 : auto+nio+ssl(ssl连接 nio网络io模型)
官网介绍 : http://activemq.apache.org/auto
使用 auto 的方式就相当于四合一协议 : STOMP AMQP MQTT TCP NIO
auto 就像是一个网络协议的适配器,可以自动检测协议的类型,并作出匹配
配置文件修改:
……
连接:
消息发送成功
同样代码只需修改 URI
public static final String ACTIVEMQ_URL = "nio://192.168.17.3:61608";
对于 NIO 和 tcp 的代码相同,但不代表使用其他协议代码相同,因为底层配置不同,其他协议如果使用需要去修改代码