SpringBoot整合RabbitMQ

引入依赖

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

配置文件

yaml 复制代码
spring:
  rabbitmq:
    host: xuewei.world
    port: 5672
    username: xuewei
    password: 123456
    virtual-host: /

消息模型之Sample

开发生产者

java 复制代码
@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testHello(){
  rabbitTemplate.convertAndSend("hello","hello world");
}

开发消费者

java 复制代码
@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer {

    @RabbitHandler
    public void receive1(String message){
        System.out.println("message = " + message);
    }
}

消息模型之Work Queues

开发生产者

java 复制代码
@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testWork(){
  for (int i = 0; i < 10; i++) {
    rabbitTemplate.convertAndSend("work","hello work!");
  }
}

开发消费者

java 复制代码
@Component
public class WorkCustomer {
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(String message){
        System.out.println("work message1 = " + message);
    }

    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String message){
        System.out.println("work message2 = " + message);
    }
}

消息模型之Publish-Subscribe

开发生产者

java 复制代码
@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testFanout() throws InterruptedException {
  rabbitTemplate.convertAndSend("logs","","这是日志广播");
}

开发消费者

java 复制代码
@Component
public class FanoutCustomer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(name="logs",type = "fanout")
    ))
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue, //创建临时队列
            exchange = @Exchange(name="logs",type = "fanout")  //绑定交换机类型
    ))
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

消息模型之Routing-Direct

开发生产者

java 复制代码
@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testDirect(){
  rabbitTemplate.convertAndSend("directs","error","error 的日志信息");
}

开发消费者

java 复制代码
@Component
public class DirectCustomer {

    @RabbitListener(bindings ={
            @QueueBinding(
                    value = @Queue(),
                    key={"info","error"},
                    exchange = @Exchange(type = "direct",name="directs")
            )})
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings ={
            @QueueBinding(
                    value = @Queue(),
                    key={"error"},
                    exchange = @Exchange(type = "direct",name="directs")
            )})
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

消息模型之Routing-Topic

开发生产者

java 复制代码
@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testTopic(){
  rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息");
}

开发消费者

java 复制代码
@Component
public class TopCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    key = {"user.*"},
                    exchange = @Exchange(type = "topic",name = "topics")
            )
    })
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    key = {"user.#"},
                    exchange = @Exchange(type = "topic",name = "topics")
            )
    })
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}
相关推荐
智能工业品检测-奇妙智能9 小时前
springboot对接阿里云短信
人工智能·vue·springboot·阿里云短信
懈尘11 小时前
【实战分享】智慧养老系统核心模块设计 —— 健康监测与自动紧急呼叫
java·后端·websocket·mysql·springboot·livekit
JustMove0n13 小时前
互联网大厂Java面试全流程问答及技术详解
java·jvm·redis·mybatis·dubbo·springboot·多线程
Javatutouhouduan1 天前
SpringBoot整合reids:JSON序列化文件夹操作实录
java·数据库·redis·html·springboot·java编程·java程序员
Msshu1231 天前
多协议快充取电芯片 支持与主板MCU共用D+D-网络可取电可与电脑传输数据
elasticsearch·sqlserver·flink·rabbitmq·storm
Mr.45671 天前
SpringBoot整合RabbitMQ进阶:告别繁琐,用统一配置管理所有队列与交换机
spring boot·rabbitmq
only-qi1 天前
RabbitMQ 深度解析:从架构原理到消息全链路可靠性保障
分布式·架构·rabbitmq
2401_848009721 天前
RabbitMQ整合springboot
spring boot·rabbitmq·java-rabbitmq
智能工业品检测-奇妙智能1 天前
SpringBoot整合FFmpeg的方法
人工智能·ffmpeg·springboot·deepseek·openclaw
常利兵2 天前
Spring Boot 4.0 牵手RabbitMQ:注解魔法开启消息之旅
spring boot·rabbitmq·java-rabbitmq