springboot整合消息隊列RabbitMQ

前言:

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內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: