SpringBoot集成RabbitMQ(生产者)

默认读者已经对SpringBoot和RabbitMQ比较熟悉

SpringBoot集成RabbitMQ(生产者)的步骤如下:

  1. 创建SpringBoot工程
  2. Maven添加 spring-boot-starter-amqp
  3. 编写application.properties配置RabbitMQ的信息
  4. 编写交换机、队列、绑定配置类
  5. 在业务逻辑代码中注入RabbitTemplate
  6. 调用RabbitTemplate的方法,完成消息推送

1. 添加依赖


在pom.xml添加依赖:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2. 编写application.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5276
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/

3. 编写交换机、队列、绑定配置类

2.1 方法一:直接new

Bean配置:

package com.lqk.producer;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lqk
 * @Date 2021/6/14
 * @Description
 */
@Configuration
public class ProducerConfig {

    public static final String EXCHANGE_NAME = "exchange_name";
    public static final String QUEUE_NAME = "topic_queue_name";

    @Bean
    public Queue myQueue(){
        Queue queue = new Queue(QUEUE_NAME, true, false, false);
        return queue;
    }

    @Bean
    public Exchange myExchange(){
        FanoutExchange fanoutExchange = new FanoutExchange(EXCHANGE_NAME, true, false);
        return fanoutExchange;
    }
    
    @Bean
    public Binding myBinding(@Qualifier("myQueue") Queue queue, @Qualifier("myExchange") Exchange exchange){
        Binding binding = BindingBuilder.bind(queue).to(exchange).with("test.*").noargs();
        return binding;
    }
}

2.2 方法二:建造者模式

package com.lqk.producer;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 刘乾坤
 * @Date 2021/6/14
 * @Description
 */
@Configuration
public class ProducerConfig {

    public static final String EXCHANGE_NAME = "exchange_name";
    public static final String QUEUE_NAME = "topic_queue_name";

    @Bean
    public Queue myQueue(){
        // 持久化构造。  非持久化的构造使用nonDurable,想要定义其它的属性,在build之前继续调用对应的方法设置
        Queue queue = QueueBuilder.durable(QUEUE_NAME).build();
        return queue;
    }

    @Bean
    public Exchange myExchange(){
        // 想要定义其它的属性,在build之前继续调用对应的方法设置
        Exchange build = ExchangeBuilder.fanoutExchange(EXCHANGE_NAME).build();
        return build;
    }

    @Bean
    public Binding myBinding(@Qualifier("myQueue") Queue queue, @Qualifier("myExchange") Exchange exchange){
        Binding binding = BindingBuilder.bind(queue).to(exchange).with("test.*").noargs();
        return binding;
    }
}

4. 注入RabbitTemplate,发送消息

@SpringBootTest
@RunWith(SpringRunner.class)
class SpringBootRabbitMqProducerApplicationTests {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    void contextLoads() {
        rabbitTemplate.convertAndSend(ProducerConfig.EXCHANGE_NAME, "test.hello", "hi~ha");
    }

}

5. 结果


成功创建交换机
image.png

成功创建队列
image.png

成功发送消息

image.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值