springboot使用rabbitmq-Topic模式,親自實測能用!!!


0.項目目錄截圖

=====================================================================

springboot的版本:

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->

==========================================================================================================================================

引入jar包: 

<!-- springboot整合rabbitMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

========================================================================================================================

使用junit5做測試.

========================================================================================================================

1.application.yml配置文件

#SpringBoot整合RabbitMQ
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#connection-timeout: 15000

2.RabbitMqConfig.java

package com.nantian.rabbitmqtopic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Created by Administrator on 2018/4/10.
* 1. yaml
* 2. RabbitMqConfig
* a 創建queue
* b. 創建交換機TopicExchange
* c. 根據綁定規則將隊列綁定到相應的交換機上
* 3. sender
* 4. test
* 5. receiver
*/

@Configuration
public class RabbitMqConfig {

// a 創建queue
@Bean(name = "message") // 指定該參數名是message 見下面Qualifier("message")
public Queue queueMessage(){
return new Queue("topic.message"); // topic.message 是rounting-key,匹配規則
}

@Bean(name = "messages")
public Queue queueMessages(){
return new Queue("topic.messages");
}

// b. 創建交換機TopicExchange
@Bean
public TopicExchange exchange(){
return new TopicExchange("topicExchange");
}

/*
c. 根據綁定規則將隊列綁定到相應的交換機上(bindingKey)--Exchange Queues

*/
@Bean
public Binding bindingExchangeMessage(@Qualifier("message") Queue queueMessage,TopicExchange exchange){
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
}

@Bean
/**
* 將隊列"messages" 綁定到交換機上,綁定規則是 topic.messages
*
*/
public Binding bindingExchangeMessages(@Qualifier("messages")Queue queueMessages,TopicExchange exchange){
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
}

}

3.發送者:TopicSender.java

package com.nantian.rabbitmqtopic;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* Created by Administrator on 2018/4/10.
*/

@Component
public class TopicSender {

@Autowired
private AmqpTemplate amqpTemplate;
/**
* 交換機 rountingkey 發送的內容 發送消息到相應的Exchange
* 向名稱為 topicExchange 的交換機 發送滿足routingkey規則為 "topic.messages " 的內容為 "我是發送消息的內容"  的消息
*/
public void sendMessage(){
this.amqpTemplate.convertAndSend("topicExchange","topic.messages","我是發送消息的內容! ");
}

/**
* 消息一
*/
public void send1() {
String context = "hi, i am message 1";
System.out.println("Sender : " + context);
this.amqpTemplate.convertAndSend("topicExchange", "topic.message", context);
}

/**
* 消息2
*/
public void send2() {
String context = "hi, i am messages 2";
System.out.println("Sender : " + context);
this.amqpTemplate.convertAndSend("topicExchange", "topic.messages", context);
}

}
4.接受者:TopicReceiver.java

package com.nantian.rabbitmqtopic;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
* Created by Administrator on 2018/4/10.
*/
@Component
public class TopicReceiver {

@RabbitListener(queues ="topic.message" )
public void receiveMessage1(String str){
System.out.println("趙雲1:我是監聽topic.message的,僅滿足topic.message的過來 , "+str);
}

@RabbitListener(queues ="topic.messages" )
public void receiveMessage2(String str){
System.out.println("關羽2:我是監聽topic.# 的,滿足 topic.# 的都過來 , "+str);
}

}
5.測試類:TestTopicRabbitMq.java

package com.nantian;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.nantian.rabbitmqtopic.TopicSender;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestTopicRabbit {

@Autowired
private TopicSender topicSender;

@Test
public void contextLoads() {
topicSender.sendMessage();
}

/**
* 測試消息一
*/
@Test
public void send1() {
topicSender.send1();
}

/**
* 測試消息二
*/
@Test
public void send2() {
topicSender.send2();
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM