1.安裝Erlang和RabbitMqjava
在安裝RabbitMq以前須要安裝的Erlang(esl-erlang_22.0_windows_amd64.exe): https://pan.baidu.com/s/1_ExgTzvCRN0byIYXgiA16gnode
須要安裝的RabbitMq(rabbitmq-server-3.7.18.exe):https://pan.baidu.com/s/10UuuukYibnpmm16RtUZw3Qweb
安裝時這兩個軟件沒有特殊設置,一路贊成接受就好了。apache
2.開啓RabbitMq的管理npm
安裝完成後,找到RaabitMq的sbin所在的目錄,在個人Win10系統上,這個路徑是C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.18\sbin,而後打開控制檯,進入這個目錄.windows
而後輸入>rabbitmq-plugins enable rabbitmq_management瀏覽器
C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.18\sbin>rabbitmq-plugins enable rabbitmq_management Enabling plugins on node rabbit@DESKTOP-8IDBHPK: rabbitmq_management The following plugins have been configured: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatch Applying plugin configuration to rabbit@DESKTOP-8IDBHPK... The following plugins have been enabled: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatch started 3 plugins. C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.18\sbin>
這一步的目的是爲了能使用RabbitMq的控制界面。網絡
3.查看RabbitMq的控制界面。架構
打開瀏覽器,在地址欄輸入 http://localhost:15672,在彈出的登陸頁中都輸入guest,登陸進去,你將看到下面畫面。maven
看到這個頁面後,意味着RabbitMq的安裝配置都完成了,下面就能夠寫代碼了。
4.書寫代碼往隊列裏發消息
接下來打開SpringToolSuite或者其它Eclipse,新建一個Maven工程,在pom.xml裏添加如下紅色粗體部分的dependency。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.9</version> </dependency> </dependencies> </project>
題外話:不用Maven工程,直接用java工程也可,只是你須要本身去找對應的jar包,稍顯麻煩。但網絡很差用的時候還必須這麼作。
建立完工程後就能夠寫 消息發送者 代碼了。
package com.test; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; /** * Hello world! * */ public class Sender { public static void main( String[] args ) throws IOException, TimeoutException { ConnectionFactory factory=new ConnectionFactory(); factory.setHost("localhost"); Connection conn=factory.newConnection(); Channel channel=conn.createChannel(); String queueName="MsgQue"; channel.queueDeclare(queueName, false, false, false, null); String message="hello!RabbitMq"; channel.basicPublish("", queueName, null, message.getBytes()); channel.close(); conn.close(); } }
執行後不報錯就說明消息發到隊列裏面去了。
5.書寫代碼從隊列裏取消息
在同一個工程裏另外起一個類,書寫下面的代碼:
package com.test; 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; public class Comsumer { public static void main( String[] args ) throws IOException, TimeoutException { ConnectionFactory factory=new ConnectionFactory(); factory.setHost("localhost"); Connection conn=factory.newConnection(); Channel channel=conn.createChannel(); String queueName="MsgQue"; channel.queueDeclare(queueName, false, false, false, null); Consumer consumer=new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body) throws IOException { String msg=new String(body,"UTF-8"); System.out.println("收到消息:"+msg); } }; channel.basicConsume(queueName, true, consumer); } }
這個類啓動後就處於阻塞狀態,阻塞就像一條船堵在運河(channel)上,這時從運河上飄來的東西(消息)都會被船(消息消費者)截獲住。
下面是控制檯的輸出,其中第一條消息是消費者啓動前發上去的,第二條是消費者處於阻塞時再發的,兩條消息都被消費者收取(截獲)了。
收到消息:你好!RabbitMq 收到消息:hello!RabbitMq
下圖顯示了消費者的狀態:
好了,到此任務完成,恭喜你又作了一個Hello World程序。
參考書:
1.《瘋狂Spring Cloud微服務架構實戰》楊恩雄 編著
--END-- 2019年9月28日10:36:05