本文章是在網(wǎng)易云課堂的課程學(xué)習(xí)中編寫,部分圖片從網(wǎng)易云課堂ppt引用
一、ActiveMQ入門
1俱饿、ActiveMQ是什么
ActiveMQ是Apache出品,最流行的塌忽,能力強(qiáng)勁的開源消息總線拍埠。 ActiveMQ是一個(gè)完全支持JMS1.1和J2EE 1.4規(guī)范的JMS Provider實(shí)現(xiàn),盡管JMS規(guī)范出臺(tái)已經(jīng)是很久的事情了土居,但是JMS在當(dāng)今的J2EE應(yīng)用中間仍然扮演著特殊的地位枣购。
2嬉探、JMS規(guī)范
Java消息服務(wù)((Java Message Service,即JMS)應(yīng)用程序接口是一個(gè)Java平臺(tái)中關(guān)于面向消息中間件(MOM)的API棉圈,用于在兩個(gè)應(yīng)用程序之間涩堤,或分布式系統(tǒng)中發(fā)送消息,進(jìn)行異步通信分瘾。Java消息服務(wù)是一個(gè)與具體平臺(tái)無關(guān)的APl胎围,是客戶端API。
JMS對(duì)象模型:
JMS消息模型:
JMS消息結(jié)構(gòu):包含消息頭芹敌、消息屬性痊远、消息體
消息頭:關(guān)于消息的描述信息
消息屬性:消息的附加消息頭,屬性名可以自定義
消息體:傳輸?shù)臄?shù)據(jù)
3氏捞、ActiveMQ的特性
遵循JMS規(guī)范碧聪,只要熟悉JMS規(guī)范,就能操作ActiveMQ
支持多種編程語(yǔ)言:java液茎、C逞姿、php等
支持多種傳輸協(xié)議:http協(xié)議、UDP捆等、MTP滞造、AMQP等
有多種持久化方式:文件、數(shù)據(jù)等存儲(chǔ)方式
4栋烤、ActiveMQ如何安裝及使用
準(zhǔn)備CentOS7和JDK1.8環(huán)境
下載地址: http://activemq.apache.org/activemq-5158-release.html
解壓: tar -zxvf apache-activemq-5.15.8-bin.tar.gz -C /var
修改目錄名稱 mv /var/apache-activemq-5.15.8/ /var/activemq/
啟動(dòng): ./bin/activemq start
停止:./bin/activemq stop
操作練習(xí)
1谒养、創(chuàng)建一個(gè)systemd服務(wù)文件:vi /usr/lib/systemd/system/activemq.service
2、 放入內(nèi)容
[Unit]
Description=ActiveMQ service
After=network.target
[Service]
Type=forking
ExecStart=/var/activemq/bin/activemq start
ExecStop=/var/activemq/bin/activemq stop
User=root
Group=root
Restart=always
RestartSec=9
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=activemq
[Install]
WantedBy=multi-user.target
3明郭、 找到j(luò)ava命令所在的目錄 whereis java
4买窟、設(shè)置activemq配置文件/var/activemq/bin/env中的JAVA_HOME
# Location of the java installation
# Specify the location of your java installation using JAVA_HOME, or specify the
# path to the "java" binary using JAVACMD
# (set JAVACMD to "auto" for automatic detection)
JAVA_HOME="/usr/local/java/jdk1.8.0_181"
JAVACMD="auto"
5、 通過systemctl管理activemq啟停
- 啟動(dòng)activemq服務(wù):
systemctl start activemq
- 查看服務(wù)狀態(tài):
systemctl status activemq
- 創(chuàng)建軟件鏈接:
ln -s /usr/lib/systemd/system/activemq.service /etc/systemd/system/multi-user.target.wants/activemq.service
- 開機(jī)自啟:
systemctl enable activemq
- 檢測(cè)是否開啟成功(enable):
systemctl list-unit-files |grep activemq
6薯定、 防火墻配置始绍,Web管理端口默認(rèn)為8161(admin/admin),通訊端口默認(rèn)為61616
- 添加并重啟防火墻
firewall-cmd --zone=public --add-port=8161/tcp --permanent
firewall-cmd --zone=public --add-port=61616/tcp --permanent
systemctl restart firewalld.service
- 或者直接關(guān)閉防火墻:
systemctl stop firewalld.service
7话侄、 修改web管理系統(tǒng)的部分配置,配置文件在/var/activemq/conf
ActiveMQ的Web管理平臺(tái)是基于jetty運(yùn)行亏推,因此在/var/activemq/conf目錄可以看到j(luò)etty的配置文件
- 端口修改
<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"/>
<!--此處即為管理平臺(tái)的端口-->
<property name="port" value="8161"/>
</bean>
- 關(guān)閉登錄
<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="user,admin" />
<!-- 改為false即可關(guān)閉登陸 -->
<property name="authenticate" value="true" />
</bean>
- 其他配置:
/var/activemq/conf/jetty-realm.properties
## ---------------------------------------------------------------------------
# 在此即可維護(hù)賬號(hào)密碼,格式:
# 用戶名:密碼,角色
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: 123, user
8年堆、 JAVA客戶端的使用
- 標(biāo)準(zhǔn)客戶端使用
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.8</version>
</dependency>
- Spring中使用:
http://spring.io/guides/gs/messaging-jms/
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
<version>5.15.8</version>
<exclusions>
<exclusion>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<groupId>org.apache.geronimo.specs</groupId>
</exclusion>
</exclusions>
</dependency>