栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合rabbitmq

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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 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;

    
    @RequestMapping("/mq")
    @ResponseBody
    public void mq(){
        mqSender.send("hello");
    }

    
    @RequestMapping("/mq/fanout")
    @ResponseBody
    public void mq01(){
        mqSender.sendExchange("hello");
    }

    
    @RequestMapping("/mq/direct01")
    @ResponseBody
    public void mq02(){
        mqSender.sendDirect01("hello,red");
    }
    
    @RequestMapping("/mq/direct02")
    @ResponseBody
    public void mq03(){
        mqSender.sendDirect02("hello,green");
    }

    
    @RequestMapping("/mq/topic01")
    @ResponseBody
    public void mq04(){
        mqSender.sendTopic01("hello,red");
    }
    
    @RequestMapping("/mq/topic02")
    @ResponseBody
    public void mq05(){
        mqSender.sendTopic02("hello,green");
    }

    
    @RequestMapping("/mq/header01")
    @ResponseBody
    public void mq06(){
        mqSender.sendHeader01("hello,header01");
    }
    
    @RequestMapping("/mq/header02")
    @ResponseBody
    public void mq07(){
        mqSender.sendHeader02("hello,header02");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/996457.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号