这篇文章主要讲如何在linux服务器上安装activemq
一、整体架构说明
本次安装的activemq版本为5.9.0,为单节点服务。
二、准备工作
http://archive.apache.org/dist/activemq/apache-activemq/5.9.0/apache-activemq-5.9.0-bin.tar.gz 下载activemq5.9.0安装包
https://mvnrepository.com/artifact/mysql/mysql-connector-java 下载连接mysql的驱动jar包mysql-connector-java-5.1.32.jar
三、安装jdk1.8
比较简单,这里只写一下jdk的环境变量:
echo "export JAVA_HOME=/usr/java/jdk1.8.0_91
export JAVA_BIN=$JAVA_HOME/bin
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH" >> /etc/profile
source /etc/profile
四、安装mysql
mysql的安装比较简单,安装步骤略(如有需要,可留言)
并在mysql中创建库activemq
mysql>create database activemq;
五、安装activemq
mv apache-activemq-5.9.0-bin.tar.gz /usr/local/
cd /usr/local/
tar -zxvf apache-activemq-5.9.0-bin.tar.gz
把连接mysql的驱动jar包放到相应的目录下
mv mysql-connector-java-5.1.32.jar /usr/local/apache-activemq-5.9.0/lib/optional/
六、修改主配置文件
cd /usr/local/apache-activemq-5.9.0/conf/
备份主配置文件
mv activemq.xml activemq.xml_bak
修改主配置文件activemq.xml:
- 实现数据的持久化
1.1在beans标签中添加数据库连接信息
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.0.11:3306/activemq?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="maxActive" value="200"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
1.2注释掉默认的kahadb,使用jdbc持久化
<persistenceAdapter>
<!--kahaDB directory="${activemq.data}/kahadb" /-->
<jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#mysql-ds" useDatabaseLock="false" transactionIsolation="4" />
</persistenceAdapter>
1.3将所有的消息策略修改为“持久化的”,在broker标签的最后,加上如下配置:
<plugins>
<!-- 将所有消息的传输模式,修改为"PERSISTENT" -->
<forcePersistencyModeBrokerPlugin persistenceFlag="true"/>
</plugins>
- 修改内存中存储的消息的数量(超过这个数量后,老的消息将会被剔除或保存在temp store中)
<pendingMessageLimitStrategy>
<!--constantPendingMessageLimitStrategy limit="1000"/-->
<constantPendingMessageLimitStrategy limit="10000"/>
</pendingMessageLimitStrategy>
- 添加安全机制,创建消息通讯用户(即为你代码中配置的用户名和密码)
activemq如果不加入安全机制的话,任何人只要知道消息服务的具体地址(包括 ip,端口,消息地址 [队列或者主题地址]),都可以肆无忌惮的发送、接收消息。
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="product" password="123456" groups="user"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
此处定义了一个product用户,密码为123456,角色为user。
修改完后的activemq.xml文件完整内容如下:
<!-- Allows us to use system properties as variables in this configuration file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:${activemq.conf}/credentials.properties</value>
</property>
</bean>
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.0.11:3306/active_mq?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="maxActive" value="200"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
<!-- Allows log searching in hawtio console -->
<bean id="logQuery" class="org.fusesource.insight.log.log4j.Log4jLogQuery"
lazy-init="false" scope="singleton"
init-method="start" destroy-method="stop">
</bean>
<!--
The <broker> element is used to configure the ActiveMQ broker.
-->
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic=">" >
<!-- The constantPendingMessageLimitStrategy is used to prevent
slow topic consumers to block producers and affect other consumers
by limiting the number of messages that are retained
For more information, see:
http://activemq.apache.org/slow-consumer-handling.html
-->
<pendingMessageLimitStrategy>
<!--constantPendingMessageLimitStrategy limit="1000"/-->
<constantPendingMessageLimitStrategy limit="10000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<!--
The managementContext is used to configure how ActiveMQ is exposed in
JMX. By default, ActiveMQ uses the MBean server that is started by
the JVM. For more information, see:
http://activemq.apache.org/jmx.html
-->
<managementContext>
<managementContext createConnector="false"/>
</managementContext>
<!--
Configure message persistence for the broker. The default persistence
mechanism is the KahaDB store (identified by the kahaDB tag).
For more information, see:
http://activemq.apache.org/persistence.html
-->
<persistenceAdapter>
<!--kahaDB directory="${activemq.data}/kahadb" /-->
<jdbcPersistenceAdapter dataDirectory="${activemq.base}/data" dataSource="#mysql-ds" useDatabaseLock="false" transactionIsolation="4" />
</persistenceAdapter>
<!--
The systemUsage controls the maximum amount of space the broker will
use before disabling caching and/or slowing down producers. For more information, see:
http://activemq.apache.org/producer-flow-control.html
-->
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage percentOfJvmHeap="70" />
</memoryUsage>
<storeUsage>
<storeUsage limit="100 gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="50 gb"/>
</tempUsage>
</systemUsage>
</systemUsage>
<!--
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&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
<!-- destroy the spring context on shutdown to stop jetty -->
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
</shutdownHooks>
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="product" password="123456" groups="user"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
<plugins>
<!-- 将所有消息的传输模式,修改为"PERSISTENT" -->
<forcePersistencyModeBrokerPlugin persistenceFlag="true"/>
</plugins>
</broker>
<!--
Enable web consoles, REST and Ajax APIs and demos
The web consoles requires by default login, you can disable this in the jetty.xml file
Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
-->
<import resource="jetty.xml"/>
七、启动activemq
cd /usr/local/apache-activemq-5.9.0/bin/
./activemq start
这时,activemq管理页面http://192.168.0.11:8161/的默认用户名密码为admin/admin,可以进入新、老管理界面
八、修改管理页面的用户名和密码
修改admin的密码下需要修改两个文件,分别为jetty-realm.properties和users.properties
修改jetty-realm.properties
admin: a1b2c3, admin
user: user, user
修改users.properties
admin=a1b2c3
重启服务,这时候可以用admin/a1b2c3登录管理页面(http://192.168.0.11:8161)
九、对消息持久化的验证
当所有配置完成,我们重启服务之后,对activemq中生产6条消息,并且暂时不消费,这时候会发现,在管理页面可以看到6条消息
并且在对应的mysql中的activemq这个库中会自动生成3张表,分别为
activemq_acks:ActiveMQ的签收信息
activemq_lock:ActiveMQ的锁信息
activemq_msgs:ActiveMQ的消息
并且在activemq_msgs表中,会存在6条数据,如图所示
即使是重启activemq之后,打开管理页面,也是显示6条数据,activemq_msgs表中也是6条数据,证明消息的确是持久化的。
待开启消费者消费这6条数据之后,activemq_msgs表中的数据也将不存在。
十、修改activemq的内存配置
这边把最大内存由1G改为4G(根据实际情况调整)
修改bin/activemq
#Set jvm memory configuration
if [ -z "$ACTIVEMQ_OPTS_MEMORY" ] ; then
ACTIVEMQ_OPTS_MEMORY="-Xms1G -Xmx4G"
fi