CentOS6.5安装RabbitMQ

本文详细介绍了在CentOS6.5上安装RabbitMQ的步骤,包括上传安装包、配置文件修改、启动服务以及启用Web管理界面。同时,提供了Docker配置的指导,以及在SpringMVC和SpringBoot环境下整合RabbitMQ的配置和简单操作示例。

1.上传三个安装包

esl-erlang_17.3-1~centos~6_amd64.rpm
esl-erlang-compat-R14B-1.el6.noarch.rpm
rabbitmq-server-3.4.1-1.noarch.rpm

依次安装,前两个需要联网,最后一个本地安装

yum -y install esl-erlang_17.3-1~centos~6_amd64.rpm
yum -y install esl-erlang-compat-R14B-1.el6.noarch.rpm
rpm -ivh rabbitmq-server-3.4.1-1.noarch.rpm

2.配置

将配置文件模板复制到etc目录并编辑

cp /usr/share/doc/rabbitmq-server-3.4.1/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config
vim /etc/rabbitmq/rabbitmq.config

修改某行loopback,注意去掉最后的逗号和前面的%注释

{loopback_users, []}

设置开机启动

chkconfig rabbitmq-server on

开启web管理页面并启动

rabbitmq-plugins enable rabbitmq_management
service rabbitmq-server start

然后在主机中通过地址:http://192.168.56.101:15672即可访问到管理界面,默认用户名密码都是guest

3.Docker配置

// 启动docker
systemctl start docker
// 下载docker镜像
docker pull rabbitmq:management
// 创建docker容器
docker run -di --name=name_rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 15671:15671 -p 15672:15672 -p 25672:25672 rabbitmq:management

浏览器即可访问http://192.168.xxx.xxx:15672

4.使用说明

SpringMVC导包情况:

<!-- RabbitMQ -->
<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>3.5.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>1.4.5.RELEASE</version>
</dependency>

配置文件

mq.host=127.0.0.1
mq.username=queue
mq.password=1234
mq.port=8001
# 统一XML配置中易变部分的命名
mq.queue=test_mq

Spring整合RabbitMQ配置文件

<?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:context="http://www.springframework.org/schema/context"
     xmlns:util="http://www.springframework.org/schema/util"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:rabbit="http://www.springframework.org/schema/rabbit"
     xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util-3.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/rabbit
     http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
     

	<!-- 引入配置文件 -->
	<context:property-placeholder location="classpath:application.properties"/>
	<util:properties id="appConfig" location="classpath:application.properties"></util:properties>

    <!-- RabbitMQ start -->
    <!-- 连接配置 -->
    <rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}"
        password="${mq.password}" port="${mq.port}"  />
        
    <rabbit:admin connection-factory="connectionFactory"/>
    
    <!-- 消息队列客户端 -->
    <rabbit:template id="amqpTemplate" exchange="${mq.queue}_exchange" connection-factory="connectionFactory"  />
    
    <!-- queue 队列声明 -->
    <!-- 
        durable 是否持久化 
        exclusive 仅创建者可以使用的私有队列,断开后自动删除 
        auto-delete 当所有消费端连接断开后,是否自动删除队列 -->
    <rabbit:queue id="test_queue" name="${mq.queue}_testQueue" durable="true" auto-delete="false" exclusive="false" />
    
    <!-- 交换机定义 -->
    <!-- 
        交换机:一个交换机可以绑定多个队列,一个队列也可以绑定到多个交换机上。
        如果没有队列绑定到交换机上,则发送到该交换机上的信息则会丢失。
        
        direct模式:消息与一个特定的路由器完全匹配,才会转发
        topic模式:按模式匹配
     -->
    <rabbit:topic-exchange name="${mq.queue}_exchange" durable="true" auto-delete="false">
        <rabbit:bindings>
            <!-- 设置消息Queue匹配的pattern (direct模式为key) -->
            <rabbit:binding queue="test_queue" pattern="${mq.queue}_patt"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>
    
    <bean name="rabbitmqService" class="com.enh.mq.RabbitmqService"></bean>
    
    <!-- 配置监听 消费者 -->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
        <!-- 
            queues 监听队列,多个用逗号分隔 
            ref 监听器 -->
        <rabbit:listener queues="test_queue" ref="rabbitmqService"/>
    </rabbit:listener-container>
</beans>

简单操作:

// 生产者
@Service
public class Producer {
    @Autowired
    private AmqpTemplate amqpTemplate;
    public void sendQueue(String exchange_key, String queue_key, Object object) {
        // convertAndSend 将Java对象转换为消息发送至匹配key的交换机中Exchange
        amqpTemplate.convertAndSend(exchange_key, queue_key, object);
    }
}
// 消费者
public class RabbitmqService implements MessageListener {
    public void onMessage(Message message) {
        System.out.println("消息消费者 = " + message.toString());
    }
}

Controller使用生产者发送消息给消费者

@Autowired
private Producer producer;
@Value("#{appConfig['mq.queue']}")
private String queueId;
    
@ResponseBody
@RequestMapping("/sendQueue")
public String testQueue() {
    try {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("data", "hello rabbitmq");
        // 注意:第二个属性是 Queue 与 交换机绑定的路由
        producer.sendQueue(queueId + "_exchange", queueId + "_patt", map);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "发送完毕";
}

SpringBoot导包情况:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>

修改配置文件

server:
  port: 8080
spring:
  rabbitmq:
    host: 127.0.0.1
    username: guest
    password: guest
    virtual-host: /
    # 以下mq配置只在生产者中配置,在消费者中不用配置
    template: # 定义一些模板,这里是因为下面生产者启动了重试机制,所以定义了重试retry的参数
      retry:
        enabled: true
        initial-interval: 10000ms # 第一次失败后重试时间是10s
        max-interval: 30000ms # 最大重试时间为30s,也就是超过30s的重试周期认为生产失败了
        multiplier: 2 # 重试周期倍数是2,即第二次失败后等10*2=20s重试,第三次失败等30s重试
      exchange: project.item.exchange # 默认交换机,在代码中不配就使用这个交换机
    publisher-confirms: true

生产者示例:

@RunWith(SpringRunner.class)
@SpringBootTest
public class Producer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void sendMsg() {
        rabbitTemplate.convertAndSend("item.insert", "direct");
    }
}

消费者示例:

@Component
public class Consumer {

    /**
     * 有了注解中的声明,如果queue不存在,则会自动建立
     * value绑定了队列,exchange绑定了交换机,key绑定了RoutingKey
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "item.insert.queue", durable = "true"),
            exchange = @Exchange(value = "project.item.exchange", type = ExchangeTypes.TOPIC),
            key = {"item.insert"}
    ))
    public void getMsg(String msg) {
        System.out.println("处理queue的消息:" + msg);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值