SpringBoot整合RabbitMQ

本文展示了如何在SpringBoot项目中整合RabbitMQ,创建生产者发送消息到交换机,以及配置消费者监听特定队列并处理消息。生产者通过RabbitTemplate发送消息,消费者使用@RabbitListener注解监听邮件和短信队列。
摘要由CSDN通过智能技术生成

SpringBoot整合RabbitMQ

创建一个空项目

image-20230227183845319

生产者

创建一个springboot模块myproducer

image-20230227183928309

image-20230227184009604

image-20230227184121856

yml配置文件
spring:
  rabbitmq:
    host: 192.168.118.130
    port: 5672
    virtual-host: /
    username: guest
    password: guest
发送消息
package com.wn.myproducer8080.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("/sendemail")
    public String sendemail(){
        //发送信息
        rabbitTemplate.convertAndSend("exchangetopic","email","email");
        return "sendemail";
    }
}

image-20230227184906655

String exchange:交换机名称

String routingkey:路由名

Object object:消息

消费者

创建一个SpringBoot模块mqcomsumer8081

在启动类上加注解

@EnableRabbit //开启基于注解的RabbitMQ模式

image-20230227185326238

创建一个配置类

创建交换机、队列、绑定关系

package com.wn.mqcomsumer8081.configuration;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConsumerConfiguration {
    /***
     * 交换机
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("exchangetopic");
    }
    /**
     * 队列
     */
    @Bean
    public Queue queueemail(){
        return new Queue("queueemail");
    }
    @Bean
    public Queue queuesms(){
        return new Queue("queuesms");
    }
    /**
     * 绑定
     */
    @Bean
    public Binding bindingemail(TopicExchange exchangetopic,Queue queueemail){
        return BindingBuilder.bind(queueemail).to(exchangetopic).with("#.email.#");
    }
    @Bean
    public Binding bindingsms(TopicExchange exchangetopic,Queue queuesms){
        return BindingBuilder.bind(queuesms).to(exchangetopic).with("#.sms.#");
    }

}

绑定还可以这样写
    //调用方法
	@Bean
    public Binding bindingemail(){
        return BindingBuilder.bind(queueemail()).to(topicExchange()).with("#.email.#");
    }
	//利用注解
    @Bean
    public Binding bindingemail(TopicExchange exchangetopic,
                                @Qualifier("queueemail") Queue queue){
        return BindingBuilder.bind(queue).to(exchangetopic).with("#.email.#");
    }
交换机默认属性

image-20230227192140130

队列默认属性

image-20230227192747679

监听
package com.wn.mqcomsumer8081.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
@Component
public class MyConsumer {
    @RabbitListener(queues = "queueemail")
    public void handlerEmail(String str, Message message, Channel channel){
        System.out.println("----------------");
        System.out.println("处理邮件");
        System.out.println("str:"+str);
        System.out.println("message:"+message);
        System.out.println("channel:"+channel);
        System.out.println("----------------");
    }
    @RabbitListener(queues = "queuesms")
    public void handlerSms(String str, Message message, Channel channel){
        System.out.println("----------------");
        System.out.println("处理短信");
        System.out.println("str:"+str);
        System.out.println("message:"+message);
        System.out.println("channel:"+channel);
        System.out.println("----------------");
    }
}

注解@RabbitListener,会监控指定的队列

处理方法可以添加三个参数:

String str:消息体

Message message:完整的消息对象

Channel channel:信道

注意:导包不要导错了import com.rabbitmq.client.Channel;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值