activemq 环境搭建和整合

 activemq-all-5.15.0  spring 4.3.9  centos 7

更多干货


介绍:

使用activemq 实现业务解耦



步骤

1安装 centos 7
2安装docker
3安装activemaq

Linux安装介绍

http://mirrors.cn99.com/centos/7/isos/x86_64/


Centos 版本                 

CentOS-7-x86_64-DVD-1708.iso                                                         

下载地址

http://mirrors.cn99.com/centos/7/isos/x86_64/

Vm 版本

12

客户端

Xshell 5

虚拟机安装路径

E:\vm\centosDevlop

登录帐号

Nick

密码

123456

ip

192.168.14.128

 

 


基础命令

查看ip                                                             

Ifconfig -a

Centos的yum源更换为国内的阿里云源

 

Vi 查找

命令模式下输入“/字符串”,例如“/Section 3”。

查看监听端口

 

# netstat -lntp

 配置静态ip

1           

# cd /etc/sysconfig/network-scripts                            

# vi ifcfg-ens33

2

TYPE=Ethernet

BOOTPROTO=static

DEFROUTE=yes

PEERDNS=yes

PEERROUTES=yes

IPV4_FAILURE_FATAL=no

IPV6INIT=yes

IPV6_AUTOCONF=yes

IPV6_DEFROUTE=yes

IPV6_PEERDNS=yes

IPV6_PEERROUTES=yes

IPV6_FAILURE_FATAL=no

HWADDR="00:0C:29:F2:0F:56"

NAME=ens33

DEVICE=ens33

ONBOOT=yes

IPADDR=192.168.1.188

NETMASK=255.255.255.0

GATEWAY=192.168.1.1

DNS1=192.168.1.1

3

# cd /etc/udev/rules.d/

# rm -f 70-persistent-ipoib.rules

4

# systemctl stop NetworkManager

# systemctl disable NetworkManager

重新启动网络:

# systemctl start network.service

 

docker 安装

Docker CE配置

卸载旧版本

$ sudo yum remove docker \

                  docker-common \

                  docker-selinux \

                  docker-engine

执行以下命令安装依赖包:

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

国内源

$ sudo yum-config-manager \

    --add-repo \

    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

如果需要最新版本的 Docker CE 请使用以下命令:

$ sudo yum-config-manager --enable docker-ce-edge

$ sudo yum-config-manager --enable docker-ce-test

安装 Docker CE

更新 yum 软件源缓存,并安装 docker-ce。

 

$ sudo yum makecache fast

$ sudo yum install docker-ce

使用脚本自动安装

$ curl -fsSL get.docker.com -o get-docker.sh

$ sudo sh get-docker.sh --mirror Aliyun

执行这个命令后,脚本就会自动的将一切准备工作做好,并且把 Docker CE 的 edge 版本安装在系统中。

启动 Docker CE

$ sudo systemctl enable docker

$ sudo systemctl start docker

建立 docker 用户组

默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。

 

建立 docker 组:

 

$ sudo groupadd docker

将当前用户加入 docker 组:

 

$ sudo usermod -aG docker $USER

镜像加速

对于使用 systemd 的系统,用 systemctl enable docker 启用服务后,编辑 /etc/systemd/system/multi-user.target.wants/docker.service 文件,找到 ExecStart= 这一行,在这行最后添加加速器地址 --registry-mirror=<加速器地址>,如:

 

ExecStart=/usr/bin/dockerd --registry-mirror=https://jxus37ad.mirror.aliyuncs.com

注:对于 1.12 以前的版本,dockerd 换成 docker daemon。

 

重新加载配置并且重新启动。

 

$ sudo systemctl daemon-reload

$ sudo systemctl restart docker

信息查看

# docker version

查看信息

# docker info

查看我们正在运行的容器

# docker ps -a

查看映射端口

docker port pmm-server

查看应用进程日志

docker logs -f pmm-server

查看应用进程

docker top pmm-server

重启

docker start pmm-server

停止

docker stop pmm-server

我们可以使用 docker rm 命令来删除不需要的容器

docker rm pmm-server

列出镜像列表

docker images

 

REPOSTITORY:表示镜像的仓库源

TAG:镜像的标签

IMAGE ID:镜像ID

CREATED:镜像创建时间

SIZE:镜像大小

查询出Pid

docker inspect --format "{{ .State.Pid}}" <container-id>

然后通过得到的Pid执行

nsenter --target 6537 --mount --uts --ipc --net --pid

 

 

 

 


active MQ 配置

下载
docker pull webcenter/activemq
配置
docker run --name activemq -p 61616:61616 -p 8161:8161 -e ACTIVEMQ_ADMIN_LOGIN=admin -e ACTIVEMQ_ADMIN_PASSWORD=123 --restart=always -d webcenter/activemq


与Spring mvc 整合

版本说明

Activemq 5.15.0    Spring 版本 4.3.9

<activemq.version>5.15.0</activemq.version>                                         
 
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>${activemq.version}</version>
</dependency>

<spring.version>4.3.9.RELEASE</spring.version>

注意:activemqAll 5.15.0对应的Spring 版本是 4.3.9 不过spring的版本不匹配会报错。

配置文件:

Web.xml

 <context-param>
                   <param-name>contextConfigLocation</param-name>
                   <param-value>
                            classpath:applicationContext.xml,classpath:activemq.xml
                   </param-value>
         </context-param>

activemq.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:amq="http://activemq.apache.org/schema/core"
      xmlns:jms="http://www.springframework.org/schema/jms"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd">
 
      <!-- ActiveMQ 连接工厂 -->
      <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
           <property name="brokerURL" value="${topic.brokerURL}" />
           <property name="userName" value="${topic.userName}" />
           <property name="password" value="${topic.password}" />
      </bean>
 
      <!-- spring Caching连接工厂 -->
      <bean id="activemqConnectionFactory"
           class="org.springframework.jms.connection.CachingConnectionFactory">
           <property name="targetConnectionFactory" ref="amqConnectionFactory"></property>
           <!-- Session缓存数量 -->
           <property name="sessionCacheSize" value="100" />
      </bean>
 
      <!-- 消息生产者 start -->
      <!-- 定义JmsTemplate的Queue类型 -->
      <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
           <constructor-arg ref="activemqConnectionFactory" />
           <property name="receiveTimeout" value="10000" />
           <!-- 队列模式 -->
           <property name="pubSubDomain" value="false" />
      </bean>
 
      <!-- 定义JmsTemplate的Topic类型 -->
      <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
            <constructor-arg ref="activemqConnectionFactory" />
           <!-- pub/sub模型(发布/订阅) -->
           <property name="pubSubDomain" value="true" />
      </bean>
      <!--消息生产者 end -->
 
      <!-- 消息消费者 start -->
 
      <!-- 定义Queue监听器 -->
<!-- <jms:listener-container destination-type="queue"
           container-type="default" connection-factory="activemqConnectionFactory"
           acknowledge="auto">
           <jms:listener destination="jjms.entQueue" ref="entQueueReceiver" />
      </jms:listener-container> -->
 
      <!-- 消息消费者 end -->
</beans> 



 

config.properties

#------------ activeMq ------------

topic.brokerURL=tcp://192.168.1.189:61616

topic.userName=admin

topic.password=123

 

 

 

 


QueueSender:


import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
 
/**
 *
 * @description 队列消息生产者,发送消息到队列
 *
 */
@Component("queueSender")
public class QueueSender {
 
         @Autowired
         @Qualifier("jmsQueueTemplate")
         private JmsTemplate jmsTemplate;// 通过@Qualifier修饰符来注入对应的bean
 
         /**
          * 发送一条消息到指定的队列(目标)
          *
          * @param queueName
          *            队列名称
          * @param message
          *            消息内容
          */
         public void send(String queueName, final String message) {
                   jmsTemplate.send(queueName, new MessageCreator() {
                            @Override
                            public Message createMessage(Session session) throws JMSException {
                                     return session.createTextMessage(message);
                            }
                   });
         }
 
}

EntQueueReceiver:

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
/**
 * 监听消息队列
 */
@Component("entQueueReceiver")
public class EntQueueReceiver implements MessageListener {
        
         private static final Logger logger = LoggerFactory.getLogger(EntQueueReceiver.class);
 
         @Override
         public void onMessage(Message message) {
                   try {
 
                            System.out.println("entQueueReceiver接收到消息:" + ((TextMessage) message).getText());
                            message.acknowledge();// 手动向broker确认接收成功,如果发生异常,就不反回ack
                   } catch (JMSException e) {
                            logger.error(e.getMessage(), e);
                   }
         }
}

JmsEvent


import java.util.Date;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
 
import rongji.framework.base.model.BaseEntity;
 
import com.fasterxml.jackson.annotation.JsonProperty;
 
@Entity
@Table(name = "CFG_UMS_EVENT")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "cfg_ums_event_sequence")
public class JmsEvent extends BaseEntity {
 
         private static final long serialVersionUID = 5414577938204042925L;
 
         /**
          * 状态
          */
         public enum Status {
                   /** 新增 **/
                   newAdd,
 
                   /** 已处理 **/
                   done
         }
 
         private Status status;
 
         private String topic;
 
         private String message;
 
         private int count;
 
         private Date updateTime;
 
         private Date createTime;
 
         @JsonProperty
         @Column(name = "status")
         public Status getStatus() {
                   return status;
         }
 
         public void setStatus(Status status) {
                   this.status = status;
         }
 
         @JsonProperty
         @Column(name = "topic")
         public String getTopic() {
                   return topic;
         }
 
         public void setTopic(String topic) {
                   this.topic = topic;
         }
 
         @JsonProperty
         @Column(name = "message")
         public String getMessage() {
                   return message;
         }
 
         public void setMessage(String message) {
                   this.message = message;
         }
 
         @JsonProperty
         @Column(name = "count")
         public int getCount() {
                   return count;
         }
 
         public void setCount(int count) {
                   this.count = count;
         }
 
         @JsonProperty
         @Column(name = "updateTime")
         public Date getUpdateTime() {
                   return updateTime;
         }
 
         public void setUpdateTime(Date updateTime) {
                   this.updateTime = updateTime;
         }
 
         @JsonProperty
         @Column(name = "createTime")
         public Date getCreateTime() {
                   return createTime;
         }
 
         public void setCreateTime(Date createTime) {
                   this.createTime = createTime;
         }
 
}

跟多相关文章



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值