RabbitMQ(windows)安裝與SpringBoot整合RabbitMQ

最近一些SpringBoot整合RabbitMQ技術文檔和博客,整理一下自己的理解,本文主要分爲兩點,rabbitmq的安裝 和springboot整合rabbitmq。

一、RabbitMQ(Windows)安裝

關於消息隊列的原理,小編也收藏了一些大佬的博客(https://blog.csdn.net/AngryFyj/article/details/86487851)這裏就不再去詳細說明了,這裏主要說明一下RabbitMQ的安裝,雖然網上也有很多這方面的博客,但是還是整理一下,畢竟別人寫的都是別的感受,還有這個因爲是小編自己玩的,所以是Windows下的安裝。

rabbitMQ是一個在AMQP協議標準基礎上完整的,可服用的企業消息系統。它遵循Mozilla Public License開源協議,採用 Erlang 實現的工業級的消息隊列(MQ)服務器,Rabbit MQ 是建立在Erlang OTP平臺上。所以一般來說安裝 RabbitMQ 之前要安裝 Erlang ,根據操作系統不同官網提供了相應的安裝說明:WindowsDebian / UbuntuRPM-based LinuxMac

1、安裝erland ,通過官方下載頁面(http://www.erlang.org/downloads),然後直接打開一直下一步,完成安裝。

2、安裝rabbitmq,官方下載地址(https://www.rabbitmq.com/download.html),然後打開完成安裝。

3、配置,激活rabbitmq's management plugin

找到自己的安裝目錄

使用RabbitMQ 管理插件,可以更好的可視化方式查看Rabbit MQ 服務器實例的狀態。

打開命令窗口:(因爲小編已經安裝好,所以圖片是網上找別人的)

輸入命令:(注意,要加雙引號)

"D:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.10\sbin\rabbitmq-plugins.bat" enable rabbitmq_management

這樣,就安裝好插件了,是不是能使用了呢?別急,需要重啓服務才行,使用命令:

net stop RabbitMQ && net start RabbitMQ

這時候的,也許會出現這種結果:

“發生錯誤:發生系統錯誤 5。  拒絕訪問。”

這是什麼鬼?查了下,原來,5代表的是:不是系統管理員權限。

問題解決方案:使用管理員打開cmd再執行此命令:

到這裏算是安裝完成,可以使用瀏覽器打開 http://localhost:15672 訪問Rabbit Mq的管理控制檯(使用默認的guest用戶登錄即可,密碼一樣)

 

關於rabbitmq的安裝大家可以參考(https://www.cnblogs.com/ericli-ericli/p/5902270.html

https://baijiahao.baidu.com/s?id=1605656085633071281&wfr=spider&for=pc

二、SpringBoot整合RabbitMQ

springboot整合rabbitmq並沒有想象中那麼複雜,主要與代碼的方式說明一下:

1、創建一個springboot項目

2、pom.xml文件添加依賴

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>

3、在application.properties添加rabbitmq的配置(這只是最基本的配置,大家可以直接添加其他更復雜的配置)

spring.rabbitmq.host = localhost
spring.rabbitmq.port = 5672
spring.rabbitmq.username = guest
spring.rabbitmq.password = guest

到這裏,配置什麼的就完成,接下來就是擼代碼的事了

4、Exchange分發消息時根據類型的不同分發策略有區別,目前共四種類型:direct、fanout、topic、headers ,這就只講了topicd,其他類大家可以自己瞭解一下

topic 交換器
topic 交換器通過模式匹配分配消息的路由鍵屬性,將路由鍵和某個模式進行匹配,此時隊列需要綁定到一個模式上。它將路由鍵和綁定鍵的字符串切分成單詞,這些單詞之間用點隔開。它同樣也會識別兩個通配符:符號“#”和符號“”。#匹配0個或多個單詞,匹配不多不少一個單詞。

a、創建交換機隊列,並綁定

package com.ajwensome.mq.topic;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class TopicRabbitConfig {
	final static String message = "Q1";
	static final String messages = "Q2";

	/**
	 * 創建隊列
	 * 
	 * @return
	 */
	@Bean
	public Queue queueMessage() {
		return new Queue(TopicRabbitConfig.message);
	}

	/**
	 * 創建隊列
	 * 
	 * @return
	 */
	@Bean
	public Queue queueMessages() {
		return new Queue(TopicRabbitConfig.messages);
	}

	/**
	 * 交換機
	 * 
	 * @return
	 */
	@Bean
	public TopicExchange exchange() {
		return new TopicExchange("topicExchange");
	}

	/**
	 * 綁定交換機和隊列
	 * 
	 * @param queueMessages
	 * @param exchange
	 * @return
	 */
	@Bean
	public Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
		return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#"); // route key 路由規則
	}

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

}

寫好這個大家就可以啓動項目試一下,然後可以在rabbitmq的控制檯看到創建好的交接,和隊列(下圖,大家可以看到我註冊topicExchange關聯了Q1,Q2兩個隊列)

接下來就是如何發送消息和接收消息的問題了

b、消息消費者 與 消息發送者

package com.ajwensome.mq.topic;

import java.io.IOException;
import java.util.Map;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

@Component
@RabbitListener(queues = "Q2")
public class TopicReceiver2 {
    @RabbitHandler
    public void process(String message ,@Headers Map<String,Object> headers,Channel channel) throws IOException{
        System.out.println("接收到信息了:》》》》Receiver2 topic.messages: "+message);
        long deliveryTag = (long)headers.get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);
    }
}
package com.ajwensome.mq.topic;

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

@Component
public class TopicSender {
    @Autowired
    AmqpTemplate amqpTemplate;

    public void send1(){
        String context = "hi,i am message 1222222222222222222222222";
        System.out.println("Sender: "+context);
        amqpTemplate.convertAndSend("topicExchange","topic.message",context);
    }

    public void send2(){
        String context = "hi , i am messages 21111111111111111111111111";
        System.out.println("Sender: "+context);
        //topic.messages 是 route key 
        amqpTemplate.convertAndSend("topicExchange","topic.messages",context);
    }
}

 

發送方法和接收方法已經定義好,接下來測試一下

c、測試

@RunWith(SpringRunner.class)
@SpringBootTest
public class MqApplicationTests {
	@Autowired
	TopicSender topicSender;



	@Test
	public void TestTopic(){
		topicSender.send1();
		topicSender.send2();
	}


}

運行結果

個人拙見,還希望能幫到大家。

參考文檔:https://blog.csdn.net/zhuzhezhuzhe1/article/details/80454956

                  https://www.cnblogs.com/ericli-ericli/p/5902270.html

                  https://baijiahao.baidu.com/s?id=1605656085633071281&wfr=spider&for=pc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章