RabbltMQ整合SpringBoot

前期准备

1.搭建依赖

<!--引入RabbitMQ集成依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置yaml配置文件

server:
    port: 9999
spring:
    application:
        name: rabbltmq_springboot
    rabbitmq:
      host: 101.42.157.178
      port: 5672
      username: ems
      password: 123
      virtual-host: /ems

helloword模型

生产者

@RunWith(SpringRunner.class)
class RabbltmqSpringbootApplicationTests {

    //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //helloword
    @Test
    public void testHello() {
        /*
         * 将定义的消息给到相对应的队列中
         * 参数1:队列名称
         * 参数2:消息内容
         * */
        rabbitTemplate.convertAndSend("hello","hello world");
    }

消费者

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

work模型使用

生产者

@Test
void contextLoads() {
    for (int i = 0; i < 10; i++) {
        rabbitTemplate.convertAndSend("work","hello work!");
        // 生产端没有指定交换机只有routingKey和Object。
        //消费方产生work队列,放在默认的交换机(AMQP default)上。
        //而默认的交换机有一个特点,只要你的routerKey的名字与这个
        //交换机的队列有相同的名字,他就会自动路由上。 
        //生产端routingKey 叫work ,消费端生产work队列。
        //他们就路由上了               
    }

消费者

@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);
}

Fanout 广播模型

生产者

@Test
void contextLoads() {
    rabbitTemplate.convertAndSend("logs","","这是日志广播"); // 参数1为交换机,参数2为路由key,“”表示为任意路由,参数3为消息内容
}

消费者

@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模型

生产者

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

消费者

@RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue, // 创建临时队列
                    key = {"info", "error"}, // 路由key
                    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);
    }
}

Topic 订阅模型(动态路由模型)

生产者

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

消费者

@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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值