springboot整合rabbitmq

1.Centos7安装rabbitmq

可以查看这个详细链接:https://blog.csdn.net/weixin_40584261/article/details/106826044

2.配置文件

# rabbitmq
spring.rabbitmq.host=服务器ip
spring.rabbitmq.username=用户名
spring.rabbitmq.password=密码
# 虚拟服务器
spring.rabbitmq.virtual-host=/
# 端口号
spring.rabbitmq.port=5672
# 消费者的最小数量
spring.rabbitmq.listener.simple.concurrency=10
# 消费者的最大数量
spring.rabbitmq.listener.simple.max-concurrency=10
# 限制消费者每次只能处理一条消息,处理完再继续下一条消息
spring.rabbitmq.listener.simple.prefetch=1
# 启动的时候是否默认启动容器,默认true
spring.rabbitmq.listener.simple.auto-startup=true
# 当消息被拒绝时候,是否重新进入队列
spring.rabbitmq.listener.simple.default-requeue-rejected=true
# 开启重试
spring.rabbitmq.template.retry.enabled=true
# 重试时间,默认1000ms
spring.rabbitmq.template.retry.initial-interval=1000
# 重试次数,默认是3
spring.rabbitmq.template.retry.max-attempts=3
# 重试最大间隔时间,默认10000ms
spring.rabbitmq.template.retry.max-interval=10000
# 重试的间隔乘数。比如配2.0,第一次就等10s,第二次就等20s,第三次就等30s
spring.rabbitmq.template.retry.multiplier=1

3.交换机Fanout、Direct、Topic、Header模式测试

模式的详细介绍可以去查看rabbitmq官网:https://www.rabbitmq.com/tutorials/tutorial-three-java.html

3.1配置类

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


@Configuration
public class RabbitMQConfig {

    private static final String QUEUE01 = "queue_fanout01";
    private static final String QUEUE02 = "queue_fanout02";
    private static final String EXCHANGE = "fanoutExchange";

    private static final String QUEUE_DIRECT01 = "queue_direct01";
    private static final String QUEUE_DIRECT02 = "queue_direct02";
    private static final String EXCHANGE_DIRECT = "directExchange";
    private static final String ROUTINGKEY01 = "queue.red";
    private static final String ROUTINGKEY02 = "queue.green";

    private static final String QUEUE_TOPIC01 = "queue_topic01";
    private static final String QUEUE_TOPIC02 = "queue_topic02";
    private static final String EXCHANGE_TOPIC = "topicExchange";
    private static final String ROUTINGKEY_TOPIC01 = "#.queue.#";
    private static final String ROUTINGKEY_TOPIC02 = "*.queue.#";

    private static final String QUEUE_HEADER01 = "queue_header01";
    private static final String QUEUE_HEADER02 = "queue_header02";
    private static final String EXCHANGE_HEADER = "headerExchange";

    // Fanout模式
    @Bean
    public Queue queue01() {
        return new Queue(QUEUE01);
    }
    @Bean
    public Queue queue02() {
        return new Queue(QUEUE02);
    }
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(EXCHANGE);
    }

    @Bean
    public Binding binding01(){
        return BindingBuilder.bind(queue01()).to(fanoutExchange());
    }
    @Bean
    public Binding binding02(){
        return BindingBuilder.bind(queue02()).to(fanoutExchange());
    }

    // Direct直连模式
    @Bean
    public Queue queueDirect01() {
        return new Queue(QUEUE_DIRECT01);
    }
    @Bean
    public Queue queueDirect02() {
        return new Queue(QUEUE_DIRECT02);
    }
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_DIRECT);
    }

    @Bean
    public Binding bindingDirect01(){
        return BindingBuilder.bind(queueDirect01()).to(directExchange()).with(ROUTINGKEY01);
    }
    @Bean
    public Binding bindingDirect02(){
        return BindingBuilder.bind(queueDirect02()).to(directExchange()).with(ROUTINGKEY02);
    }

    // Topic主题模式

    @Bean
    public Queue queueTopic01() {
        return new Queue(QUEUE_TOPIC01);
    }
    @Bean
    public Queue queueTopic02() {
        return new Queue(QUEUE_TOPIC02);
    }
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_TOPIC);
    }

    @Bean
    public Binding bindingTopic01(){
        return BindingBuilder.bind(queueTopic01()).to(topicExchange()).with(ROUTINGKEY_TOPIC01);
    }
    @Bean
    public Binding bindingTopic02(){
        return BindingBuilder.bind(queueTopic02()).to(topicExchange()).with(ROUTINGKEY_TOPIC02);
    }

    // Header模式
    @Bean
    public Queue queueHeader01() {
        return new Queue(QUEUE_HEADER01);
    }
    @Bean
    public Queue queueHeader02() {
        return new Queue(QUEUE_HEADER02);
    }
    @Bean
    public HeadersExchange headerExchange() {
        return new HeadersExchange(EXCHANGE_HEADER);
    }
    @Bean
    public Binding bindingHeader01(){
        Map<String,Object> map = new HashMap<>();
        map.put("color","red");
        map.put("speed","low");
        return BindingBuilder.bind(queueHeader01()).to(headerExchange()).whereAny(map).match();
    }
    @Bean
    public Binding bindingHeader02(){
        Map<String,Object> map = new HashMap<>();
        map.put("color","red");
        map.put("speed","fast");
        return BindingBuilder.bind(queueHeader02()).to(headerExchange()).whereAll(map).match();
    }
}

3.2 生产者类

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息的发送者
 */
@Component
public class MQSender {

    private Log log = LogFactory.getLog(MQSender.class);

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendExchange(Object msg) {
        log.info("mq sendExchange msg:"+msg);
        rabbitTemplate.convertAndSend("fanoutExchange","",msg);
    }

    public void sendDirect01(Object msg) {
        log.info("mq sendDirect red msg:"+msg);
        rabbitTemplate.convertAndSend("directExchange","queue.red",msg);
    }

    public void sendDirect02(Object msg) {
        log.info("mq sendDirect green msg:"+msg);
        rabbitTemplate.convertAndSend("directExchange","queue.green",msg);
    }

    public void sendTopic01(Object msg) {
        log.info("mq sendTopic01 msg(QUEUE_TOPIC01接收):"+msg);
        rabbitTemplate.convertAndSend("topicExchange","queue.red.message",msg);
    }

    public void sendTopic02(Object msg) {
        log.info("mq sendTopic02 msg(QUEUE_TOPIC01和QUEUE_TOPIC02都接收):"+msg);
        rabbitTemplate.convertAndSend("topicExchange","message.queue.green.message",msg);
    }

    public void sendHeader01(String msg) {
        log.info("mq sendHeader01 msg(被两个队列接收):"+msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color","red");
        properties.setHeader("speed","fast");
        Message message = new Message(msg.getBytes(),properties);
        rabbitTemplate.convertAndSend("headerExchange","",message);
    }

    public void sendHeader02(String msg) {
        log.info("mq sendHeader02 msg(被queue_header01接收):"+msg);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("color","red");
        properties.setHeader("speed","normal");
        Message message = new Message(msg.getBytes(),properties);
        rabbitTemplate.convertAndSend("headerExchange","",message);
    }
}

3.3消费者类

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 消息的消费者
 */
@Component
public class MQReceiver {

    private Log log = LogFactory.getLog(MQReceiver.class);

    @RabbitListener(queues = "queue_fanout01")
    public void receive01(Object msg) {
        log.info("QUEUE01 receive msg:"+msg);
    }

    @RabbitListener(queues = "queue_fanout02")
    public void receive02(Object msg) {
        log.info("QUEUE02 receive msg:"+msg);
    }

    @RabbitListener(queues = "queue_direct01")
    public void receiveDirect01(Object msg) {
        log.info("queue_direct01 receive msg:"+msg);
    }

    @RabbitListener(queues = "queue_direct02")
    public void receiveDirect02(Object msg) {
        log.info("queue_direct02 receive msg:"+msg);
    }

    @RabbitListener(queues = "queue_topic01")
    public void receiveTopic01(Object msg) {
        log.info("queue_topic01 receive msg:"+msg);
    }

    @RabbitListener(queues = "queue_topic02")
    public void receiveTopic02(Object msg) {
        log.info("queue_topic02 receive msg:"+msg);
    }

    @RabbitListener(queues = "queue_header01")
    public void receiveHeader01(Message msg) {
        log.info("queue_header01 receive msg对象:"+msg);
        log.info("queue_header01 receive msg消息:"+new String(msg.getBody()));
    }

    @RabbitListener(queues = "queue_header02")
    public void receiveHeader02(Message msg) {
        log.info("queue_header02 receive msg对象:"+msg);
        log.info("queue_header02 receive msg消息:"+new String(msg.getBody()));
    }
}

3.4测试API

import com.hdu.seckill.model.User;
import com.hdu.seckill.rabbitmq.MQSender;
import com.hdu.seckill.utils.RespBeanUtil;
import com.hdu.seckill.vo.RespBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("test")
public class TestApi {

    private Log log = LogFactory.getLog(UserApi.class);

    @Resource
    private MQSender mqSender;

    /**
     * 测试发送Rabbitmq消息
     */
    @RequestMapping("/mq")
    @ResponseBody
    public void mq(){
        mqSender.send("hello");
    }

    /**
     * 测试Fanout模式发送Rabbitmq消息
     */
    @RequestMapping("/mq/fanout")
    @ResponseBody
    public void mq01(){
        mqSender.sendExchange("hello");
    }

    /**
     * 测试Direct模式发送Rabbitmq消息
     */
    @RequestMapping("/mq/direct01")
    @ResponseBody
    public void mq02(){
        mqSender.sendDirect01("hello,red");
    }
    /**
     * 测试Direct模式发送Rabbitmq消息
     */
    @RequestMapping("/mq/direct02")
    @ResponseBody
    public void mq03(){
        mqSender.sendDirect02("hello,green");
    }

    /**
     * 测试Topic模式发送Rabbitmq消息
     */
    @RequestMapping("/mq/topic01")
    @ResponseBody
    public void mq04(){
        mqSender.sendTopic01("hello,red");
    }
    /**
     * 测试Topic模式发送Rabbitmq消息
     */
    @RequestMapping("/mq/topic02")
    @ResponseBody
    public void mq05(){
        mqSender.sendTopic02("hello,green");
    }

    /**
     * 测试Header模式发送Rabbitmq消息
     */
    @RequestMapping("/mq/header01")
    @ResponseBody
    public void mq06(){
        mqSender.sendHeader01("hello,header01");
    }
    /**
     * 测试Header模式发送Rabbitmq消息
     */
    @RequestMapping("/mq/header02")
    @ResponseBody
    public void mq07(){
        mqSender.sendHeader02("hello,header02");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Listen·Rain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值