ActiveMQ支持的传输协议

 连接到ActiveMQ

 Connector:ActiveMQ提供的,用来实现连接通信的功能。包括:client-to-broker、broker-to-broker。ActiveMQ允许客户端使用多种协议来进行连接。

    client-to-broker模式一般是配置文件中的transportConnector配置

    broker-to-broker:一般是指网络(network)===后面学习 

ActiveMQ支持的client-to-broker的通信协议如下:最常用的也就是TCP和NIO

1.TCP:这个也是缺省值

2.NIO

3.UDP

4.SSL

5.Http(s)

6.VM:如果客户端和broker在一个虚拟机内的话,通过VM协议通讯在VM内通讯,从而减少网络传输的开销

配置transportConnectors,位于conf/activemq.xml,大致如下:

        <!--
            The transport connectors expose ActiveMQ over a given protocol to
            clients and other brokers. For more information, see:

            http://activemq.apache.org/configuring-transports.html
        -->
        <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&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

文件的注释也注明了更详细协议以及参数的配置参考:http://activemq.apache.org/configuring-transports.html

1.Transmission Control Protocol (TCP)---常用

1.这是默认的Broker配置,TCP的Client监听端口是61616。

2.在网络传输数据前,必须要序列化数据,消息是通过一个叫wire protocol的来序列化成字节流。默认情况下,ActiveMQ把wire protocol叫做OpenWire,它的目的是促使网络上的效率和数据快速交互。 

3.TCP连接的URI形式:tcp://hostname:port?key=value&key=value。其中前面部分是必须的,参数是可选的
4.TCP传输的优点:

  TCP协议传输可靠性高,稳定性强。
  高效性:字节流方式传递,效率很高。
  有效性、可用性:应用广泛,支持任何平台。

2.New I/O API Protocol (NIO)---常用

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

2.适合使用NIO协议的场景:
  可能有大量的Client去链接到Broker上可能对于Broker有一个很迟钝的网络传输NIO的实现比TCP需要更少的线程去运行,所以建议使用NIO协议。

  可能对于Broker有一个很迟钝的网络传输。


我们在上面的配置文件中增加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&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="nio" uri="nio://0.0.0.0:61618?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

程序中修改对应的url即可,如下:

private static final String url = "nio://0.0.0.0:61618/";

3.User Datagram Protocol (UDP)

1.UDP和TCP的区别:

  (1)TCP是一个原始流的传递协议,意味着数据包是有保证的,换句话说,数据包是不会被复制和丢失的。UDP是不会保证数据包的传递的。

  (2)TCP也是一个稳定可靠的数据包传递协议,意味着数据在传递的过程中不会被丢失。这样确保了在发送和接收之间能够可靠的传递。相反,UDP仅仅是一个链接协议,所以它没有可靠性之说。

2. 从上面可以得出:TCP是被用在稳定可靠的场景中使用的;UDP通常用在快速数据传递和不怕数据丢失的场景中,还有ActiveMQ通过防火墙时,只能用UDP。
3. UDP连接的URI形式:udp://hostname:port?key=value

4. Secure Sockets Layer Protocol (SSL) 安全链路保护

  连接的URI形式:ssl://hostname:port?key=value

5.Hypertext Transfer Protocol (HTTP/HTTPS)

  1.像web和email等服务需要通过防火墙来访问的,Http可以使用这种场合。
  2.连接的URI形式:http(https)://hostname:port?key=value

6.VM Protocol (VM)

1.  VM transport允许在VM内部通信,从而避免了网络传输的开销。这时候采用的连接不是socket连接,而是直接的方法调用。
2.  第一个创建VM连接的客户会启动一个embed VM broker(内嵌虚拟机服务),接下来所有使用相同的broker name的VM连接都会使用这个broker。当这个broker上所有的连接都关闭的时候,这个broker也会自动关闭。

3. 连接的URI形式:vm://brokerName?key=value
4. Java中嵌入的方式 vm:broker:(tcp://localhost:61616)?brokerName=embddedbroker&persistent=false

原文地址:https://www.cnblogs.com/qlqwjy/p/10463661.html