首頁 > 軟體

springboot整合訊息佇列RabbitMQ

2022-08-30 22:02:09

前言:

RabbitMQ常用的三種Exchange Type:fanout、direct、topic。

  • fanout:把所有傳送到該Exchange的訊息投遞到所有與它繫結的佇列中。
  • direct:把訊息投遞到那些binding key與routing key完全匹配的佇列中。
  • topic:將訊息路由到binding key與routing key模式匹配的佇列中。

這裡基於springboot整合​ ​訊息佇列​​,測試這三種Exchange。

  • 啟動RabbitMQ

雙擊執行rabbitmq-server.bat:

  • SpringBoot整合RabbitMQ——Direct模式(預設模式)

建立springboot web專案——傳送者springboot-sender

追加測試和rabbitmq所需的依賴:

<!-- 新增springboot對amqp的支援 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--新增測試包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>

修改組態檔application.yml  application.properties:

server:
port: 7001
spring:
application:
name: spirngboot-sender
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest

傳送的資訊可以是基本資料型別也可以是物件,這裡建立一個使用者物件

public class User implements Serializable{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

建立一個設定類:SenderConfiguration.java

一個名為queue1 佇列

@Configuration
public class SenderConfiguration {
@Bean
public Queue directQueue() {
return new Queue("queue1");
}
}

建立一個傳送資訊類:SenderService.java

傳送user物件給queue1佇列

@Component
public class SenderService {
@Autowired
private AmqpTemplate template;

public void sendUser() {
User user=new User();
user.setUsername("張三");
user.setPassword("123456");
template.convertAndSend("queue1",user);
}
}

建立一個測試類:TestRabbitMQ.java

@SpringBootTest(classes=SpringbootSenderApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestRabbitMQ {
@Autowired
private SenderService senderService;

@Test
public void testRabbit() {
senderService.sendUser();
}
}

執行testRabbit方法:

建立springboot web專案——接收者springboot-receiver

修改組態檔application.yml  application.properties:

server:
port: 7002
spring:
application:
name: spirngboot-receiver
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest

新增接收類:ReceiverService.java

@Component
public class ReceiverService {
@RabbitListener(queues="queue1")
public void receiveUser(User user) {
System.out.println("username:"+user.getUsername()+" password:"+user.getPassword());
}
}

執行啟動類:SpringbootApplication.java,結果:

資訊成功被接收。

SpringBoot整合RabbitMQ——Topic模式(模糊匹配)

步驟與Direct差不多。

傳送者:

修改設定類SenderConfiguration.java:

建立兩個佇列 topic1,topic2,建立一個topic交換器,繫結交換機和佇列以及繫結規則

@Test
public void testRabbit() {
senderService.sendUser();
}@Bean(name="topic1")
public Queue topicQueue1() {
return new Queue("topic1");
}
@Bean(name="topic2")
public Queue topicQueue2() {
return new Queue("topic2");
}

@Bean
public TopicExchange exchange() {
//建立一個topic交換器
return new TopicExchange("topicExchange");
}
@Bean
Binding bindingExchangeTopic1(@Qualifier("topic1") Queue queueMessage, TopicExchange exchange) {
//設定topic1繫結規則
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.queue");
}
@Bean
Binding bindingExchangeTopic2(@Qualifier("topic2") Queue queueMessages, TopicExchange exchange) {
//設定topic2繫結規則 *表示一個詞,#表示零個或多個詞
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
}

修改傳送類SenderService.java

User user=new User();
user.setUsername("張三");
user.setPassword("123456");
//傳送給topicExchange的交換機
template.convertAndSend("topicExchange","topic.queue",user);
template.convertAndSend("topicExchange","topic.anyting",user);

執行testRabbit方法:

成功廣播出去兩條資訊

接收者:

修改接收類ReceiverService.java

@RabbitListener(queues="fanout1")
public void receiveFanout1(User user) {
System.out.println("佇列:fanout1 username:"+user.getUsername()+" password:"+user.getPassword());
}
@RabbitListener(queues="fanout2")
public void receiveFanout2(User user) {
System.out.println("佇列:fanout2 username:"+user.getUsername()+" password:"+user.getPassword());
}

執行啟動類,結果:

訊息成功被傳送接收

到此這篇關於springboot整合訊息佇列RabbitMQ的文章就介紹到這了,更多相關springboot整合RabbitMQ內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com