首先簡單的介紹一下MQ,MQ英文名MessageQueue,中文名也就是大家用的消息隊列,幹嘛用的呢,說白了就是一個消息的接受和轉發的容器,可用於消息推送。
ActiveMQ是由Apache出品的,一款最流行的,能力強勁的開源消息總線。ActiveMQ是一個完全支持JMS1.1和J2EE 1.4規範的 JMS Provider實現,它非常快速,支持多種語言的客戶端和協議,而且可以非常容易的嵌入到企業的應用環境中,並有許多高級功能,下面我們來安裝ActiveMQ單機版。
1.在官網下載ActiveMQ,並上傳到服務器
2.解壓安裝
# tar -zxvf apache-activemq-5.11.1-bin.tar.gz
3.如果啓動腳本activemq沒有可執行權限,此時則需要授權
# chmod 755 /opt/activeMQ/apache-activemq-5.11.1/bin/activemq
4.配置端口
ActiveMQ需要用到兩個端口,一個是消息通訊的端口(默認爲61616)
一個是管理控制檯端口(默認爲 8161)可在 conf/jetty.xml中修改,如下:
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
<!-- the default port number for the web console -->
<property name="host" value="0.0.0.0"/>
<property name="port" value="8161"/>
</bean>
注:配置完需要在防火牆中開放對應端口
5.啓動ActiveMQ
# /opt/activeMQ/apache-activemq-5.11.1/bin/activemq start
6.訪問ActiveMQ
點擊manage ActiveMQ出現賬號密碼校驗界面,默認賬號密碼爲:admin/admin
7.安全配置
ActiveMQ如果不加入安全機制的話,任何人只要知道消息服務的具體地址(包括 IP,端口,消息地址[隊列或者主題地址]),都可以肆無忌憚的發送,接收消息。所以我們要爲ActiveMQ進行安全配置,ActiveMQ的消息安全配置策略有多種,我們以簡單授權配置爲例。
7.1在conf/activemq.xml文件中在broker標籤最後加入以下內容即可:
# vim /opt/activeMQ/apache-activemq-5.11.1/conf/activemq.xml
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="roberto" password="roberto" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
定義了一個用戶賬號爲roberto密碼爲roberto,對應的角色爲users,admins
7.2 確保啓用認證
確保authenticate的值爲 true
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="user,admin" />
<!-- set authenticate=false to disable login -->
<property name="authenticate" value="true" />
</bean>
7.3 控制檯的登錄用戶名密碼保存在conf/jetty-realm.properties文件中,內容如下:
# vim /opt/activeMQ/apache-activemq-5.11.1/conf/jetty-realm.properties
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: roberto, admin
user: user, user
修改admin用戶的密碼爲roberto
7.4 重啓ActiveMQ
# /opt/activeMQ/apache-activemq-5.11.1/bin/activemq restart