一、概述



二、Exchange(交換器)型別
目前有四種:direct、 fanout、 topic、headers
(1)direct 路由鍵(routing key)與佇列名(name)完全一致

(2)fanout 交換器下所有佇列都接收到訊息

(3)topic 將路由鍵和某個模式進行匹配 ; #匹配0個或多個單詞,*匹配一個單詞

三、安裝RabbitMQ
-
安裝erlang,下載地址:http://www.erlang.org/downloads,雙擊.exe檔案進行安裝就好,安裝完成之后將erlang的安裝目錄(C:\ProgramFiles\erl9.1\bin,具體請查看安裝路徑)加入到Path中,最后打開命令列,輸入erl,如果出現erlang的版本資訊就表示erlang語言環境安裝成功;
-
安裝RabbitMQ,下載地址:http://www.rabbitmq.com/download.html,同樣雙擊.exe進行安裝,修改RabbitMQ安裝目錄為 D:\SinoWeb\RabbitMQ;
-
安裝RabbitMQ-Plugins
安裝方法是:打開命令列cd進入rabbitmq的sbin目錄(D:\SinoWeb\RabbitMQ\rabbitmq_server-3.6.12\sbin),輸入一下安裝命令
安裝命令: rabbitmq-plugins enable rabbitmq_management
rabbitmq-plugins enable rabbitmq_web_stomp rabbitmq_stomp rabbitmq_web_stomp_examples -
插件安裝完之后,雙擊rabbitmq-server.bat,在瀏覽器輸入http://localhost:15672進行驗證,你會看到登陸界面,輸入用戶名:guest,密碼:guest你就可以進入管理界面;
-
創建用戶
使用guest賬戶登陸,點擊Admin標簽,然后點擊 Add a user,在新增用戶的表單中輸入以下內容
Username:sino
Password:sino3210
Tags: administrator
點擊Add User按鈕創建用戶,
創建完畢后 新創建的用戶會顯示No access,點擊剛才創建的sino用戶名,在新頁面中, 點擊Set permission按鈕,
四、使用RabbitMQ
(1)點擊Exchanges按鈕

(2)點擊Quene按鈕

(3)點擊Exchange,系結quene

五、SpringBoot整合
1、組態檔添加屬性
spring.rabbitmq.host=
spring.rabbitmq.username=
spring.rabbitmq.password=
spring.rabbitmq.port=
2、代碼
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private AmqpAdmin amqpAdmin;
//發送和接收資訊
@Test
void contextLoads() {
//1、direct型別交換器,單個routingKey ----SUCCESS
rabbitTemplate.convertAndSend("text.exchange", "text.quene",
"我是發送到text.exchange交換器,text.quene消息佇列的資訊");
//2、fauout型別交換器,routingKey不傳引數 -----SUCCESS
rabbitTemplate.convertAndSend("text.exchange2", "",
"我是發送到text.exchange2交換器,fauout訊息佇列的資訊");
//3、fauout型別交換器,不管是否傳參routingKey,都發送到交換器下所以的佇列 ----ERROR
rabbitTemplate.convertAndSend("text.exchange2", "text.quene",
"我是發送到text.exchange2交換器,text.quene單獨測驗fauout訊息佇列的資訊");
//4、direct型別交換器,routingKey不傳引數,所以的佇列都不接收資訊 ----ERROR
rabbitTemplate.convertAndSend("text.exchange", "",
"我是發送到text.exchange交換器,所以佇列資訊");
//5、接收資訊
Object convert = rabbitTemplate.receiveAndConvert("text4.quene");
System.err.println(convert);
}
//創建交換器和佇列
@Test
public void createExchange(){
//創建交換器
amqpAdmin.declareExchange(new DirectExchange("rabDirect.exchange"));
amqpAdmin.declareExchange(new FanoutExchange("rabFanout.exchange"));
//創建佇列
amqpAdmin.declareQueue(new Queue("allen.quene", true));
//系結佇列
amqpAdmin.declareBinding(new Binding("allen.quene",
Binding.DestinationType.QUEUE, "rabFanout.exchange",
"allen.quene", null));
amqpAdmin.declareBinding(new Binding("allen.quene",
Binding.DestinationType.QUEUE, "rabDirect.exchange",
"allen.quene", null));
}
備注:當傳輸的訊息是Map型別或物體類時,RabbitMQ接收的訊息是轉化亂碼的,需轉成json格式
添加 MyAmqpConfig 配置類
@Test
void contextLoads() {
Map<String, Object> map = new HashMap<>();
map.put("msg", "這是第二個訊息");
map.put("data", Arrays.asList("HelloWorld", "小米", new Date()));
rabbitTemplate.convertAndSend("test.direct", "five.quene", map);
}
@Configuration
public class MyAmqpConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
3、使用@RabbitListener監聽訊息佇列的內容
(1)@EnableRabbit注解開啟RabbitMQ
(2)在方法上標注@RabbitListener
@EnableRabbit
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Service
public class ReceiveService {
@RabbitListener(queues = "text.quene")
public void receive(String message){
System.err.println(message);
System.err.println(message.getBody());
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/289512.html
標籤:其他
上一篇:5G時代,浪風口上的音視頻技術
