RabbitMQ学习笔记九:SpringBoot集成RabbitMQ

一 创建springboot项目

 

不选择依赖的话,也可以直接创建好项目后 再在pom.xml文件中导入依赖

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

二 配置yml

spring:
  rabbitmq:
    host: 127.0.0.1 #主机ip
    port: 5672  #端口号
    username: root  #登陆账号
    password: root123  #登陆密码
    virtual-host: /zz    #虚拟主机

三 测试hello模型

创建消息生产者

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class Tests {

    @Autowired
    //注入rabbitTemplate对象
    private RabbitTemplate rabbitTemplate;

    // 测试hello模型
    @Test
    public void test(){
        //第一个参数:队列名称
        //第二个参数:发送消息的内容
        rabbitTemplate.convertAndSend("hello","hello wor1d");
    }

}

运行Test方法 发现rabbitmq web端并没有生成消息

这里是因为mq没有监听到消费者

创建消费者

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 消费者
 */
@Component
// rabbit消费者监听  指定接受那个队列的消息
// 若要设置队列其他属性 可以在@Queue注解后添加参数 默认生成的队列是持久化,非独占,不是自动删除的队列
//@Queue(value = "hello",durable = "true",autoDelete = "true") 所有参数都是String类型
@RabbitListener(queuesToDeclare =@Queue(value = "hello"))
public class HelloCustomer {

    //@RabbitHandler代表接收到消息后的回调方法
    @RabbitHandler
    public void getMessage(String message){
        System.out.println("message = "+message);
    }

}

运行测试hello模型的test方法

发现控制台打印出了消息

web端也出现了hello这个队列

这里注意: 因为我是新创建的springboot项目, 也是加载的新的依赖,导致我在测试的时候一直接收不到消息,试了很多次,也不确定具体是不是这个问题,但是我clear 和 install maven之后,程序就OK了。

四 测试其他模型

消息生产者都在一个类里面

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class Tests {

    @Autowired
    //注入rabbitTemplate对象
    private RabbitTemplate rabbitTemplate;

    // 测试hello模型
    @Test
    public void test(){
        //第一个参数:队列名称
        //第二个参数:发送消息的内容
        rabbitTemplate.convertAndSend("hello","hello wor1d");
    }

    // 测试work模型
    @Test
    public void testWork(){
        for (int i = 0; i <10 ; i++) {
            rabbitTemplate.convertAndSend("work","work test"+i);
        }
    }

    // 测试fanout模型
    @Test
    public void testFanout(){
        rabbitTemplate.convertAndSend("logs","","fanout模型发送的消息");
    }

    // route模型
    @Test
    public void testRoute(){
        rabbitTemplate.convertAndSend("directs","info","发送info路由消息");
    }


    // topic模型
    @Test
    public void testTopic(){
        rabbitTemplate.convertAndSend("topics","user.save","发送topic路由消息");
    }
}

测试work模型

消费者

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class WorkCustomer {

    @RabbitListener(queuesToDeclare = @Queue(value = "work"))
    public void test(String message){
        System.out.println("message1 :"+message);
    }

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

运行

测试fanout模型

消费者

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class FanoutCustomer {

    // 绑定队列和交换机
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue, //不指定value 就是创建一个临时队列
            exchange =@Exchange(value = "logs",type = "fanout"))
    }) //绑定交换机)
    public void test(String message){
        System.out.println("message1 = "+message);
    }

    // 绑定队列和交换机
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue, //不指定value 就是创建一个临时队列
                    exchange =@Exchange(value = "logs",type = "fanout"))
    }) //绑定交换机)
    public void test2(String message){
        System.out.println("message2 = "+message);
    }
}

运行

测试route模型

消费者

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RouteCustomer {
    @RabbitListener@RabbitListener(bindings = {
            @QueueBinding(value = @Queue, //创建一个临时队列
                    exchange =@Exchange(value = "directs", type = "direct"),//指定交换机名称
                    key = {"info","warn"}) //指定路由key
    })
    public void test(String message){
        System.out.println("message1 = "+message);
    }


    @RabbitListener@RabbitListener(bindings = {
            @QueueBinding(value = @Queue, //创建一个临时队列
                    exchange =@Exchange(value = "directs", type = "direct"),//指定交换机名称和类型 默认为direct类型
                    key = {"info"}) //指定路由key
    })
    public void test1(String message){
        System.out.println("message2 = "+message);
    }
}

运行

测试topic模型

消费者

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class TopicCustomer {

    @RabbitListener
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue, //创建一个临时队列
                    exchange =@Exchange( name = "topics",type = "topic"),//指定交换机名称
                    key = {"user.save","user.*"}) //指定路由key
    })
    public void test(String message){
        System.out.println("message1 = "+message);
    }

    @RabbitListener
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue, //创建一个临时队列
                    exchange =@Exchange( name = "topics",type = "topic"),//指定交换机名称
                    key = {"user.update","user.#"}) //指定路由key
    })
    public void test1(String message){
        System.out.println("message2 = "+message);
    }

    @RabbitListener
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue, //创建一个临时队列
                    exchange =@Exchange( name = "topics",type = "topic"),//指定交换机名称
                    key = {"ceshi.update","ceshi.#"}) //指定路由key
    })
    public void test2(String message){
        System.out.println("message3 = "+message);
    }

}

运行

更多:

RabbitMQ学习笔记一:了解及在Linux下安装RabbitMQ

RabbitMQ学习笔记二:管理界面初识和管理命令行

RabbitMQ学习笔记三:HelloWorld模型(直连模型)

RabbitMQ学习笔记四:连接工具类的封装

RabbitMQ学习笔记五:Work模型

RabbitMQ学习笔记六:Fanout模型

RabbitMQ学习笔记七:Routing订阅模型

RabbitMQ学习笔记八:Topic模型

RabbitMQ学习笔记九:SpringBoot集成RabbitMQ​​​​​​​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只不秃头的小菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值