IP:192.168.1.51
root用户操作
Connecting to 192.168.1.51:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Sun Mar 27 06:58:33 2016 from 192.168.1.5
[root@yxq ~]# pwd
/root
[root@yxq ~]# mkdir activemq
[root@yxq ~]# cd activemq/
上传安装包到服务器
解压安装
[root@yxq activemq]# ls
apache-activemq-5.11.1-bin.tar.gz
[root@yxq activemq]# tar -zxvf apache-activemq-5.11.1-bin.tar.gz
[root@yxq activemq]# mv apache-activemq-5.11.1 activemq
如果启动脚本 activemq 没有可执行权限,此时则需要授权(此步可选)
[root@yxq activemq]# chmod 755 /root/activemq/activemq-01/bin/activemq
防火墙中打开对应的端口
ActiveMQ 需要用到两个端口
一个是消息通讯的端口(默认为 61616)
一个是管理控制台端口(默认为 8161)可在 conf/jetty.xml 中修改,如下:
[root@yxq activemq]# vi /root/activemq/activemq-01/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>
[root@yxq activemq]# vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 61616 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8161 -j ACCEPT
启动mq
[root@yxq activemq]# /root/activemq/activemq-01/bin/activemq start
INFO: Loading '/root/activemq/activemq-01/bin/env'
INFO: Using java '/usr/jdk/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : '/root/activemq/activemq-01/data/activemq.pid' (pid '10232')
[root@yxq activemq]#
在浏览器打开访问
f以admin/admin登录
登录成功
安全配置(消息安全)
ActiveMQ 如果不加入安全机制的话,任何人只要知道消息服务的具体地址(包括 ip,端口,消息地址
[ 队列或者主题地址 ] , ) ,都可以肆无忌惮的发送、接收消息。关于 ActiveMQ 安装配置
http://activemq.apache.org/security.html
ActiveMQ 的消息安全配置策略有多种,我们以简单授权配置为例:
在 conf/activemq.xml 文件中在broker 标签最后加入以下内容即可:
[root@yxq activemq]# vi /root/activemq/activemq-01/conf/activemq.xml<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="yixq" password="yxq123456"
groups="users,admins" />
</users>
</simpleAuthenticationPlugin>
</plugins>
定义了一个yixq 用户,密码为 yxq123456,角色为 users,admins
设置 admin 的用户名和密码:
[root@yxq activemq]# vi /root/activemq/activemq-01/conf/jetty.xml<bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint"> <property name="name" value="BASIC" />
<property name="roles" value="admin" /> <property name="authenticate" value="true" />
</bean>
确保 authenticate 的值为 true(默认)
控制台的登录用户名密码保存在 conf/jetty-realm.properties 文件中,内容如下
[root@yxq activemq]# vi /root/activemq/activemq-01/conf/jetty-realm.properties# username: password [,rolename ...]
admin: yxq123456, admin
重启,再登录就是新设置的密码登录
[root@yxq activemq]# /root/activemq/activemq-01/bin/activemq restart
设置开机启动:
[root@yxq activemq]# vi /etc/rc.local
MQ 消息生产者也与消息消费者的 Demo 样例讲解
消息提供方:
package com.edu.demo.mqtest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.edu.demo.mqtest.MQProducer; import com.edu.demo.mqtest.params.MailParam; public class MQProducerTest { private static final Log log = LogFactory.getLog(MQProducerTest.class); public static void main(String[] args) { try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml"); context.start(); MQProducer mqProducer = (MQProducer) context.getBean("mqProducer"); // 邮件发送 MailParam mail = new MailParam(); mail.setTo("[email protected]"); mail.setSubject("ActiveMQ测试"); mail.setContent("通过ActiveMQ异步发送邮件!"); mqProducer.sendMessage(mail); context.stop(); } catch (Exception e) { log.error("==>MQ context start error:", e); System.exit(0); } finally { log.info("===>System.exit"); System.exit(0); } } }
package com.edu.demo.mqtest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.edu.demo.mqtest.MQProducer; import com.edu.demo.mqtest.params.MailParam; public class MQProducerTest { private static final Log log = LogFactory.getLog(MQProducerTest.class); public static void main(String[] args) { try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml"); context.start(); MQProducer mqProducer = (MQProducer) context.getBean("mqProducer"); // 邮件发送 MailParam mail = new MailParam(); mail.setTo("[email protected]"); mail.setSubject("ActiveMQ测试"); mail.setContent("通过ActiveMQ异步发送邮件!"); mqProducer.sendMessage(mail); context.stop(); } catch (Exception e) { log.error("==>MQ context start error:", e); System.exit(0); } finally { log.info("===>System.exit"); System.exit(0); } } }spring-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 基于Dubbo的分布式系统架构视频教程,吴水成,[email protected],学习交流QQ群:367211134 --> <!-- 采用注释的方式配置bean --> <context:annotation-config /> <!-- 配置要扫描的包 --> <context:component-scan base-package="com.edu.demo" /> <!-- 读入配置属性文件 --> <context:property-placeholder location="classpath:mq.properties" /> <!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 --> <aop:aspectj-autoproxy proxy-target-class="true" /> <import resource="spring-mq.xml" /> </beans>spring-mq.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" default-autowire="byName" default-lazy-init="false"> <!-- 基于Dubbo的分布式系统架构视频教程,吴水成,[email protected],学习交流QQ群:367211134 --> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <!-- ActiveMQ服务地址 --> <property name="brokerURL" value="tcp://192.168.1.51:61616" /> <property name="userName" value="yixq"></property> <property name="password" value="yxq123456"></property> </bean> <!-- ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory 可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。 要依赖于 activemq-pool包 --> <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> <property name="connectionFactory" ref="targetConnectionFactory" /> <property name="maxConnections" value="10" /> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="pooledConnectionFactory" /> </bean> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <!-- 队列模板 --> <bean id="activeMqJmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory"/> <property name="defaultDestinationName" value="com.edu.mqtest.v1"></property> </bean> </beans>
pom.xml配置
package com.edu.demo.mqtest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.edu.demo.mqtest.MQProducer; import com.edu.demo.mqtest.params.MailParam; public class MQProducerTest { private static final Log log = LogFactory.getLog(MQProducerTest.class); public static void main(String[] args) { try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml"); context.start(); MQProducer mqProducer = (MQProducer) context.getBean("mqProducer"); // 邮件发送 MailParam mail = new MailParam(); mail.setTo("[email protected]"); mail.setSubject("ActiveMQ测试"); mail.setContent("通过ActiveMQ异步发送邮件!"); mqProducer.sendMessage(mail); context.stop(); } catch (Exception e) { log.error("==>MQ context start error:", e); System.exit(0); } finally { log.info("===>System.exit"); System.exit(0); } } }