SpringBoot整合RabbitMQ

整合SpringBoot

1 快速构建一个SpringBoot项目
2,勾选web依赖和Rabbitmq依赖,或者手动在pom.xml文件中导入依赖

        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>

编写yml配置文件

spring:
  application:
    name: rabbitmq-springboot
  rabbitmq:
    host: 192.168.0.42
    port: 5672
    username: shop
    password: 123456
    virtual-host: /shop

Spring提供了一个RabbitTemplate模板,直接注入就可以使用了,以下为RabbitMQ各种模型的代码

@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
    //注入RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //hello world
    @Test
    public void test(){
        rabbitTemplate.convertAndSend("hello","hello world");
    }
    //work模型
    @Test
    public void testWork(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","work模型");
        }
    }
    //fanout广播模型
    @Test
    public void testFanout(){
        rabbitTemplate.convertAndSend("logs","","fanout发布的消息");
    }
    //router路由模式
    @Test
    public void testRouter(){
        rabbitTemplate.convertAndSend("directs","error","发送了info的key的路由信息");
    }
    //动态路由
    @Test
    public void testTopics(){
        rabbitTemplate.convertAndSend("topics","order.save","user.save的消息");
    }
}

消费者之HelloWord模型

 **/
//组件,被工厂监听
@Component
//消费者监听这个类 多个参数,对应业务 默认是持久化,非独占,不是自动删除的队列
@RabbitListener(queuesToDeclare = @Queue(value = "hello"/*,declare = "true",autoDelete = "true"*/))
public class HelloCustomer {
    @RabbitHandler
    public void receivel(String message){
        System.out.println("消息是"+message);
    }
}

消费者之Work模型

@Component
public class WorkCustomer {
    //一个消费者
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receivel1(String message){
        System.out.println("message1"+message);
    }
    //二个消费者
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receivel2(String message){
        System.out.println("message2"+message);
    }
}

fanout扇出模型

@Component
public class FanoutCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "logs",type = "fanout")//绑定的交换机
            )
    })
    public void receivel1(String message){
        System.out.println("message1"+message);
    }
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "logs",type = "fanout")//绑定的交换机
            )
    })
    public void receivel2(String message){
        System.out.println("message2"+message);
    }
}

路由模型

@Component
public class RouterCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,exchange = @Exchange(value = "directs",type = "direct"),
            key = {"info","error","warn"}
            )//指定交换机名称和类型
    })
    public void receivel1(String message){
        System.out.println("message1"+message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue,exchange = @Exchange(value = "directs",type = "direct"),
                    key = {"error"}//路由Key
            )//指定交换机名称和类型
    })
    public void receivel2(String message){
        System.out.println("message2"+message);
    }
}

动态路由模型

@Component
public class topicCustomer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(type="topic",name="topics"),
                    key = {"user.save","user.*"}
            )
    })
    public void receivel1(String message){
        System.out.println("message1"+message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(type="topic",name="topics"),
                    key = {"order.#","user.*","produce.*"}
            )
    })
    public void receivel2(String message){
        System.out.println("message2"+message);
    }
}

这就是SpringBoot整合RabbitMQ以及基本用法,结合代码和注释学习吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot框架可以很容易地与RabbitMQ进行集成。为了实现这个目标,你需要在项目的依赖项中添加两个关键的依赖项。首先,你需要添加spring-boot-starter-amqp依赖项,它提供了与RabbitMQ进行通信的必要类和方法。其次,你还需要添加spring-boot-starter-web依赖项,以便在项目中使用Web功能。 在你的项目中创建两个Spring Boot应用程序,一个是RabbitMQ的生产者,另一个是消费者。通过这两个应用程序,你可以实现消息的发送和接收。生产者应用程序负责将消息发送到RabbitMQ的消息队列,而消费者应用程序则负责从队列中接收并处理消息。这样,你就可以实现基于RabbitMQ的消息传递系统。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合RabbitMQ](https://blog.csdn.net/K_kzj_K/article/details/106642250)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Springboot 整合RabbitMq ,用心看完这一篇就够了](https://blog.csdn.net/qq_35387940/article/details/100514134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [undefined](undefined)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值