默認讀者已經(jīng)對SpringBoot和RabbitMQ比較熟悉
SpringBoot集成RabbitMQ(生產(chǎn)者)的步驟如下:
- 創(chuàng)建SpringBoot工程
- Maven添加 spring-boot-starter-amqp
- 編寫application.properties配置RabbitMQ的信息
- 編寫交換機、隊列、綁定配置類
- 在業(yè)務(wù)邏輯代碼中注入RabbitTemplate
- 調(diào)用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(){
// 持久化構(gòu)造。 非持久化的構(gòu)造使用nonDurable,想要定義其它的屬性,在build之前繼續(xù)調(diào)用對應(yīng)的方法設(shè)置
Queue queue = QueueBuilder.durable(QUEUE_NAME).build();
return queue;
}
@Bean
public Exchange myExchange(){
// 想要定義其它的屬性,在build之前繼續(xù)調(diào)用對應(yīng)的方法設(shè)置
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,發(fā)送消息
@SpringBootTest
@RunWith(SpringRunner.class)
class SpringBootRabbitMqProducerApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
void contextLoads() {
rabbitTemplate.convertAndSend(ProducerConfig.EXCHANGE_NAME, "test.hello", "hi~ha");
}
}
5. 結(jié)果
成功創(chuàng)建交換機
成功創(chuàng)建隊列
成功發(fā)送消息文章來源:http://www.zghlxwxcb.cn/news/detail-618524.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-618524.html
到了這里,關(guān)于SpringBoot集成RabbitMQ(生產(chǎn)者)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!