默认读者已经对SpringBoot和RabbitMQ比较熟悉
SpringBoot集成RabbitMQ(生产者)的步骤如下:
- 创建SpringBoot工程
- Maven添加 spring-boot-starter-amqp
- 编写application.properties配置RabbitMQ的信息
- 编写交换机、队列、绑定配置类
- 在业务逻辑代码中注入RabbitTemplate
- 调用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. 结果
成功创建交换机
成功创建队列
成功发送消息文章来源:https://www.toymoban.com/news/detail-618524.html
文章来源地址https://www.toymoban.com/news/detail-618524.html
到了这里,关于SpringBoot集成RabbitMQ(生产者)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!