使用Springboot配置生产者、消费者RabbitMQ?

生产者服务

1、引入依赖以及配置rabbitmq

此时我们通过使用springboot来快速搭建一个生产者服务

XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

application.yml

java 复制代码
server:
  port: 8080

spring:
  application:
    name: producter
  rabbitmq:
    addresses: 192.168.118.100:5672
    username: guest
    password: guest
    virtual-host: /
    connection-timeout: 15000
2、定义队列、交换机以及绑定的routingkey

添加一个config包下添加文件

TopicRabbitConfig.java、MsgSender.java

java 复制代码
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;

//Topic类型交换机配置
@Configuration
public class TopicRabbitConfig {

    //配置队列
    @Bean
    public Queue queue1(){
        return new Queue("queue1");
    }

    @Bean
    public Queue queue2(){
        return new Queue("queue2");
    }

    //配置交换机
    @Bean
    public TopicExchange exchange(){
        return new TopicExchange("bootExchange");
    }

    //绑定队列到交换机并且执行routingkey,之后指定消费者即可通过指定队列来拿到信息
    @Bean
    public Binding bindingExchangeMessage1(Queue queue1,TopicExchange exchange){
        return BindingBuilder.bind(queue1).to(exchange).with("cat.red");
    }

    @Bean
    public Binding bindingExchangeMessage2(Queue queue2,TopicExchange exchange){
        return BindingBuilder.bind(queue2).to(exchange).with("*.red");
    }

}
java 复制代码
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


//@Description 发送消息到指定交换机

@Component
public class MsgSender {

    @Autowired
    private AmqpTemplate amqpTemplate;

    private static String EXCHANGE_NAME = "bootExchange";

    //发送信息的routingkey=>"cat.red"
    public void send1(){
        String routingKey = "cat.red";
        String msg = "this is my message,routingkey is "+routingKey;
        //交换机名称、routingkey以及发送的信息
        amqpTemplate.convertAndSend(EXCHANGE_NAME,routingKey,msg);
        System.out.println("已成功发送信息:"+msg);
    }

    //发送信息的routingkey=>"dog.red"
    public void send2(){
        String routingKey = "dog.red";
        String msg = "this is my message,routingkey is "+routingKey;
        //交换机名称、routingkey以及发送的信息
        amqpTemplate.convertAndSend(EXCHANGE_NAME,routingKey,msg);
        System.out.println("已成功发送信息:"+msg);
    }
}
3、创建一个测试类

调用两个方法:

java 复制代码
import com.changlu.productor.config.MsgSender;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class ProductorApplicationTests {

    @Autowired
    private MsgSender msgSender;

    @Test
    void sendMsg1() {
        msgSender.send1();
        msgSender.send2();
    }

}

当我用2.4.5的时候读不出来,推存使用高版本的spring boot版本

消费者服务

1、引入依赖以及配置rabbitmq
XML 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

application.yml

java 复制代码
server:
  port: 8081  # 设置8081端口

spring:
  application:
    name: consumer
  rabbitmq:
    addresses: 192.168.118.100:5672  # 同样rabbitmq运行端口默认为5672
    username: guest
    password: guest
    virtual-host: /
    connection-timeout: 15000
2、定义消费者并进行绑定监听

消费者1:绑定对应的queue1

java 复制代码
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

//queue1对应routingkey=>cat.red

@Component
@RabbitListener(queues = "queue1")
public class Consumer1 {

    @RabbitHandler
    public void process(String msg){
        System.out.println("queue1收到消息:"+msg);
    }

}

消费者2:绑定对应的queue2

java 复制代码
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

//queue2对应routingkey=>*.red

@Component
@RabbitListener(queues = "queue2")
public class Consumer2 {

    @RabbitHandler
    public void process(String msg){
        System.out.println("queue2收到消息:"+msg);
    }

}

测试

对于生产者服务或是消费者服务在该案例中任一一方启动都没有关系。本案例是topic类型的交换机,生产者服务先启动发送的消息会被暂时存储到指定队列中。

紧接着我们启动消费者服务,可以看到对应的queue1、queue2消费者分别收到了对应routingkey匹配的信息,此时我们可以来进行处理了!

相关推荐
..空空的人19 分钟前
C++基于protobuf实现仿RabbitMQ消息队列---项目设计
分布式·rabbitmq
我认不到你1 小时前
自定义注解实现 Redis Stream 消息监听
spring boot·redis
Knight_AL2 小时前
Spring Boot 的主要特性与传统 Spring 项目的区别
spring boot·后端·spring
黄俊懿2 小时前
【深入理解SpringCloud微服务】Gateway简介与模拟Gateway手写一个微服务网关
spring boot·后端·spring·spring cloud·微服务·gateway·架构师
用户2190326527352 小时前
别再到处try-catch了!SpringBoot全局异常处理这样设计
java·spring boot·后端
梁同学与Android2 小时前
Android ---【经验篇】阿里云 CentOS 服务器环境搭建 + SpringBoot项目部署(二)
android·spring boot·后端
用户2190326527352 小时前
SpringBoot自动配置:为什么你的应用能“开箱即用
java·spring boot·后端
喵了几个咪3 小时前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:kratos-bootstrap 入门教程(类比 Spring Boot)
spring boot·后端·微服务·golang·bootstrap
小满、3 小时前
RabbitMQ:Fanout、Direct、Topic 交换机、队列声明与消息转换器
java·分布式·消息队列·rabbitmq·spring amqp
Wang's Blog3 小时前
RabbitMQ: 分布式事务的最终一致性解决方案
分布式·rabbitmq