SpringBoot集成RabbitMQ

使用Docker起RabbitMQ服务

# 拉取镜像
docker pull rabbitmq
# 启动服务
sudo docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 rabbitmq

# 进入docker
sudo docker exec -it rabbit bash

# 添加用户和授权
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"

添加依赖

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

rabbitMQ配置

spring.rabbitmq.host=10.236.93.211
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

队列配置

package cc.koit.hulk.common.configuration;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    public Queue queue(){
        return new Queue("hulk");
    }

}

消息发送

package cc.koit.hulk.common.helper;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQHelper {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void send(String content){
        amqpTemplate.convertAndSend("hulk",content);
    }
}

消息接收监听

package cc.koit.hulk.common.helper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "hulk")
public class RabbitMQReceiver {

    public static final Logger logger = LoggerFactory.getLogger(RabbitMQReceiver.class);
    @RabbitHandler
    public void process(String content){
        logger.info(content);
    }

}

单元测试

package cc.koit.hulk.service;

import cc.koit.hulk.common.helper.RabbitMQHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@SpringBootTest
@RunWith(SpringRunner.class)
@Component
public class RabbitTests {

    @Autowired
    private RabbitMQHelper rabbitMQHelper;

    @Test
    public void rabbitTest(){
        asynSend();
        asynSend();
        asynSend();
        asynSend();
    }

    private void asynSend(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                rabbitMQHelper.send(new StringBuffer().append(new Date()).append(" ").append(Thread.currentThread().getId()).append(" ").append(Math.random()).toString());
            }
        }).start();
    }

}

运行测试用例,我们可以看到控制台的输出:

2018-09-30 18:09:56.838  INFO 12676 --- [cTaskExecutor-1] c.k.hulk.common.helper.RabbitMQReceiver  : Sun Sep 30 18:08:53 CST 2018 25 0.46505812592621243
2018-09-30 18:09:56.839  INFO 12676 --- [cTaskExecutor-1] c.k.hulk.common.helper.RabbitMQReceiver  : Sun Sep 30 18:08:53 CST 2018 26 0.5954171414520313
2018-09-30 18:09:56.839  INFO 12676 --- [cTaskExecutor-1] c.k.hulk.common.helper.RabbitMQReceiver  : Sun Sep 30 18:08:53 CST 2018 27 0.3823963646328935
2018-09-30 18:09:56.840  INFO 12676 --- [cTaskExecutor-1] c.k.hulk.common.helper.RabbitMQReceiver  : Sun Sep 30 18:08:53 CST 2018 28 0.8280610350987802

这里模拟多个不同服务向队列中发送消息,我们消息全部都接收并且处理了。

,
© 2019 FunGa技术札记 All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero