ActiveMQ 筆記(五)ActiveMQ的傳輸協議


個人博客網:https://wushaopei.github.io/    (你想要這里多有)

面試思考題:

  1. 默認的61616端口如何更改
  2. 你生產上的連接協議如何配置的?使用tcp嗎?

一、Activemq的傳輸協議

傳輸協議官網連接:http://activemq.apache.org/configuring-version-5-transports.html

1、定義:

ActiveMQ支持的client-broker通訊協議有:TVP、NIO、UDP、SSL、Http(s)、VM。

其中配置Transport Connector的文件在ActiveMQ安裝目錄的conf/activemq.xml中的<transportConnectors>標簽之內。
見下圖實際配置:

[root@wsp conf]# pwd
/myactiveMQ/apache-activemq-5.15.9/conf
[root@wsp conf]# vim activemq.xml

這是activemq 的activemq.xml 中配置文件設置協議的地方:

在上文給出的配置信息中,URI描述信息的頭部都是采用協議名稱:例如

描述amqp協議的監聽端口時,采用的URI描述格式為“amqp://······”;
描述Stomp協議的監聽端口時,采用URI描述格式為“stomp://······”;
唯獨在進行openwire協議描述時,URI頭卻采用的“tcp://······”。這是因為ActiveMQ中默認的消息協議就是openwire

注意:Activemq中,默認是使用 openwire 也就是 tcp 連接

默認的Broker 配置,TCP 的Client 監聽端口 61616 ,在網絡上傳輸數據,必須序列化數據,消息是通過一個 write protocol 來序列化為字節流。默認情況 ActiveMQ 會把 wire protocol 叫做 Open Wire ,它的目的是促使網絡上的效率和數據快速交互 。

2、傳輸協議有哪些

 ActiveMQ 支持的協議有 TCP 、 UDP、NIO、SSL、HTTP(S) 、VM 、WS、Stomp等

2.1 Transmission Control Protocol(TCP)默認

(1) 這是默認的Broker配置,TCP的Client監聽端口61616

 (2)在網絡傳輸數據前,必須要先序列化數據,消息是通過一個叫wire protocol的來序列化成字節流。

 (3)TCP連接的URI形式如:tcp://HostName:port?key=value&key=value,后面的參數是可選的。

 (4) TCP傳輸的的優點:

             (4.1)TCP協議傳輸可靠性高,穩定性強
             (4.2)高效率:字節流方式傳遞,效率很高
             (4.3)有效性、可用性:應用廣泛,支持任何平台

(5)關於Transport協議的可選配置參數可以參考官網

http://activemq.apache.org/configuring-version-5-transports.html

2.2 New I/O API Protocol(NIO)

(1)NIO協議和TCP協議類似,但NIO更側重於底層的訪問操作。它允許開發人員對同一資源可有更多的client調用和服務器端有更多的負載。
(2)適合使用NIO協議的場景:
             (2.1)可能有大量的Client去連接到Broker上,一般情況下,大量的Client去連接Broker是被操作系統的線程所限制的。因此,NIO的實現比TCP需要更少的線程去運行,所以建議使用NIO協議。
             (2.2)可能對於Broker有一個很遲鈍的網絡傳輸,NIO比TCP提供更好的性能。
(3)NIO連接的URI形式:nio://hostname:port?key=value&key=value
(4)關於Transport協議的可選配置參數可以參考官網http://activemq.apache.org/configuring-version-5-transports.html

2.3 AMQP協議

Advanced Message Queuing Protocol,一個提供統一消息服務的應用層標准高級消息隊列協議,是應用層協議的一個開放標准,為面向消息的中間件設計。基於此協議的客戶端與消息中間件可傳遞消息,並不受客戶端/中間件不同產品,不同開發語言等條件限制。

2.4 Stomp協議

STOP,Streaming Text Orientation Message Protocol,是流文本定向消息協議,是一種為MOM(Message Oriented Middleware,面向消息中間件)設計的簡單文本協議。

2.5 MQTT協議

MQTT(Message Queuing Telemetry Transport,消息隊列遙測傳輸)是IBM開發的一個即時通訊協議,有可能成為物聯網的重要組成部分。該協議支持所有平台,幾乎可以把所有聯網物品和外部連接起來,被用來當作傳感器和致動器(比如通過Twitter讓房屋聯網)的通信協議。

擴展:Github

小總結:

3、NIO案例演示(啟用NIO協議)

(1)說明:

官網:http://activemq.apache.org/configuring-version-5-transports.html

The NIO Transport
Same as the TCP transport, except that the New I/O (NIO) package is used, which may provide better performance. The Java NIO package should not be confused with IBM’s AIO4J package.
To switch from TCP to NIO, simply change the scheme portion of the URI. Here’s an example as defined within a broker’s XML configuration file.

Trying to use nio transport url on the client side will instantiate the regular TCP transport. For more information see the NIO Transport Reference

(2)修改 activemq.xml 使之支持 NIO 協議:

<transportConnectors> <transportConnector name="nio" uri="nio://0.0.0.0:61618?trace=true" /> </transportConnectors>

如果你不特別指定ActiveMQ的網絡監聽端口,那么這些端口都講使用BIO網絡IO模型所以為了首先提高單節點的網絡吞吐性能,我們需要明確指定ActiveMQ網絡IO模型。
如下所示:URI格式頭以“nio”開頭,表示這個端口使用以TCP協議為基礎的NIO網絡IO模型。

 <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";

修改之后即可正確運行

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

官網介紹 : http://activemq.apache.org/auto

使用 auto 的方式就相當於四合一協議 : STOMP AMQP MQTT TCP NIO

<transportConnector name="auto+nio" uri="auto+nio://localhost:5671"/>

auto 就像是一個網絡協議的適配器,可以自動檢測協議的類型,並作出匹配。使用"+"符號來為端口設置多種特性

<transportConnector name="auto" uri="auto://localhost:5671?auto.protocols=default,stomp"/>

配置文件修改:

        …… 

<transportConnector name="auto+nio" uri="auto+nio://0.0.0.0:61608?maximumConnections=1000
&wireFormat.maxFrameSize=104857600&org.apache.activemq.transport.nio.SelectorManager.corelPoolSize=20
&org.apache.activemq.transport.nio.SelectorManager.maximumPoolSize=50"/>

連接:

消息發送成功

同樣代碼只需修改 URI

public static final String ACTIVEMQ_URL = "nio://192.168.17.3:61608";

對於 NIO 和 tcp 的代碼相同,但不代表使用其他協議代碼相同,因為底層配置不同,其他協議如果使用需要去修改代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM