SpringBoot整合RabbitMQ(各个模式如何发送一个消息)

本文介绍了如何在SpringBoot中整合RabbitMQ,并详细讲解了HelloWorld模式、Work模式、发布订阅模式、路由模式和topic模式的发送与消费消息过程,包括代码示例和模式特点。通过对比不同模式的运行结果,帮助读者深入理解RabbitMQ的消息处理机制。
摘要由CSDN通过智能技术生成

目录

 

前言

具体步骤

环境准备

1、准备一个SpringBoot工程

2、引入相关的jar包

3、配置一下yml

各个模式发送消息

1、HelloWorld模式(简单队列)

2、Work模式

3、发布订阅模式(也可叫广播模式)

4、路由模式

5、topic模式(也叫动态路由模式)

6、完整的代码结构如下

总结


前言

本例子只是SpringBoot整合一下RabbitMQ,在此环境下如何去发送一个消息(至于produce有木有发送到RabbitMQ,以及consumer有木有成功的拿到消息,暂且不说,下回再讲)

具体步骤

环境准备

1、准备一个SpringBoot工程

此处不多说了,利用IDEA几步即可完成,大概结构如下(consumer包用来存消费端代码)

2、引入相关的jar包

为了方便直接利用Junit来搞了

<dependency>
        <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

3、配置一下yml

spring:
  application:
    name: springboot-rabbitmq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    virtual-host: /test   #虚拟主机

各个模式发送消息

1、HelloWorld模式(简单队列)

特点:一个生产者对应一个消费者

消息生产端(produce):

@SpringBootTest(classes = SpringbootRabbitmqApplication.class)
@RunWith(SpringRunner.class)
class SpringbootRabbitmqApplicationTests {

    //注入rabbitmqTemplate,用于操作rabbitmq相关的方法
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void test_helloworld() {
        //MessageProperties是对消息的封装
        Message message = new Message("大家好,我是hello world".getBytes(),new MessageProperties());
        //MessageProperties 封装消息的一些属性,属性比较多,我这里只设置以下消息的持久化,PERSISTENT-持久化  NON_PERSISTENT-非持久化
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        //发送消息 这种模式比较简单,hello_world可以理解为队列的名字,虽然木有指定交换机但是它默认是有个交换机的,名字也是hello_world
        rabbitTemplate.convertAndSend("hello_world",message);
    }

}

消息消费端(consumer):  

观察控制台是否打印出消息内容

@Component
public class HelloWorldConsumer {
    //@RabbitListener可以标记在类上,如果标记在类上,那么需要在方法上加@RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(value = "hello_world")) //队列默认是持久化的,可以通过durable(是否持久化)和autoDelete(是否自动删除)去设置
    public void receive(Message message){
        //获取消息并且打印
        String msg = new String(message.getBody());
        System.out.println(msg);
    }
}

2、Work模式

特点:一个生产者对应多个消费者,但是一条消息只能被一个消费者消费!!!默认是平均消费的,例如发送10条消息,

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值