您现在的位置是:首页 >技术杂谈 >SpringBoot集成RabbitMQ(生产者)网站首页技术杂谈

SpringBoot集成RabbitMQ(生产者)

仅此而已_ 2024-09-29 12:01:03
简介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

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。