今天來學習下怎樣在Mac上安裝和使用RMQ。
網上找教程安裝MQ,大體氛圍兩種安裝方式:html
若是您的系統沒有安裝HomeBrew,則須要先安裝,查看是否安裝:java
brew --version Homebrew 2.4.3-49-g4290789 Homebrew/homebrew-core (git revision 08af7; last commit 2020-07-05) Homebrew/homebrew-cask (git revision 927fe; last commit 2020-07-05)
個人系統已經安裝過了,則不用再次安裝Brew,直接安裝RMQ便可。git
一、打開安裝HomeBrew網址,和一個新的終端,拷貝安裝命令到終端。github
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
二、按下回車鍵,命令自動執行,命令執行的過程當中會提示輸入密碼,此時輸入登陸mac系統的帳號的密碼。當命令執行結束後會出現一下提示:docker
➜ ~ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" Password: ==> You are using macOS 10.11. ==> We (and Apple) do not provide support for this old version. This installation may not succeed. After installation, you will encounter build failures with some formulae. Please create pull requests instead of asking for help on Homebrew\'s GitHub, Discourse, Twitter or IRC. You are responsible for resolving any issues you experience while you are running this old version. ==> This script will install: /usr/local/bin/brew /usr/local/share/doc/homebrew /usr/local/share/man/man1/brew.1 /usr/local/share/zsh/site-functions/_brew /usr/local/etc/bash_completion.d/brew /usr/local/Homebrew Press RETURN to continue or any other key to abort ==> Downloading and installing Homebrew...
在執行上述命令的時候,出現了這樣的錯誤:vim
Failed to connect to raw.githubusercontent.com port 443: Connection refused
在網上查找了下緣由,是由於域名被污染了,不可用,因此須要修改hosts
文件:瀏覽器
sudo vim /etc/hosts
# 加上一行 199.232.28.133 raw.githubusercontent.com
brew install rabbitmq
由於 rabbitMQ 是基於 erlang 語言開發的,就如同 activemq 須要安裝 java 環境同樣, 爲了使用 rabbitMQ 須要安裝 erlang環境,因此,咱們能夠看到上邊的命令顯示安裝 erlang 語言環境。bash
若是在安裝的時候出現鏡像源的問題,則能夠將homebrew鏡像源切換爲國內清華大學或阿里雲鏡像源,具體操做能夠參考這篇文章:解決國內mac沒法安裝homebrew神器。服務器
若是以上不能安裝,則可使用docker安裝,docker安裝很是簡單,只需拉取鏡像,由於環境已經都安裝好了。dom
2.1 拉取鏡像
docker pull rabbitmq
2.2 啓動
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
2.3 查看docker運行
> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cbb83a65ca7c rabbitmq:3-management "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq
4.訪問
啓動鏡像以後,瀏覽器內輸入 http://localhost:15672,默認的用戶名密碼都是guest,登陸後能夠在Admin那一列菜單內添加本身的用戶。
登陸以後的界面:
如何使用,請看該教學文檔:RMQ消息隊列學習
生產者:TestProducer.java
package cn.how2j; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; /** * 消息生成者 */ public class TestProducer { public final static String EXCHANGE_NAME="fanout_exchange"; public static void main(String[] args) throws IOException, TimeoutException { RabbitMQUtil.checkServer(); // 建立鏈接工廠 ConnectionFactory factory = new ConnectionFactory(); // 設置RMQ相關 factory.setHost("localhost"); Connection connection = factory.newConnection(); // 建立通道 Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); for(int i = 0; i < 100; i++){ String message = "direct 消息" + i; channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8")); System.out.println("發送消息: " + message); } // 關閉通道和鏈接 channel.close(); connection.close(); } }
消費者:TestCustomer.java
package cn.how2j; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import cn.hutool.core.util.RandomUtil; public class TestCustomer { public final static String EXCHANGE_NAME="fanout_exchange"; public static void main(String[] args) throws IOException, TimeoutException { //爲當前消費者取隨機名 final String name = "consumer-"+ RandomUtil.randomString(5); //判斷服務器是否啓動 RabbitMQUtil.checkServer(); // 建立鏈接工廠 ConnectionFactory factory = new ConnectionFactory(); //設置RabbitMQ地址 factory.setHost("localhost"); //建立一個新的鏈接 Connection connection = factory.newConnection(); //建立一個通道 Channel channel = connection.createChannel(); //交換機聲明(參數爲:交換機名稱;交換機類型) channel.exchangeDeclare(EXCHANGE_NAME,"fanout"); //獲取一個臨時隊列 String queueName = channel.queueDeclare().getQueue(); //隊列與交換機綁定(參數爲:隊列名稱;交換機名稱;routingKey忽略) channel.queueBind(queueName,EXCHANGE_NAME,""); System.out.println(name +" 等待接受消息"); //DefaultConsumer類實現了Consumer接口,經過傳入一個頻道, // 告訴服務器咱們須要那個頻道的消息,若是頻道中有消息,就會執行回調函數handleDelivery Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(name + " 接收到消息 '" + message + "'"); } }; //自動回覆隊列應答 -- RabbitMQ中的消息確認機制 channel.basicConsume(queueName, true, consumer); } }
先運行兩次 TestCustomer,啓動兩個消費者。
而後運行一次 TestProducer, 啓動生產者,生產100條信息。
此時就能夠看到如圖所示兩個消費者都能收到 這100條信息。
管理界面: