一、安裝rabbitmq,此命令會自動安裝erlang依賴html
sudo apt-get install rabbitmq-server
二、要想在瀏覽器登陸管理頁面,須要先安裝插件web
sudo rabbitmq-plugins enable rabbitmq_management
三、添加用戶spring
sudo rabbitmqctl add_user username password
四、設置用戶角色和權限瀏覽器
sudo rabbitmqctl set_user_tags username administrator sudo rabbitmqctl set_permissions -p / username ".*" ".*" ".*"
執行完以上命令,打開瀏覽器 http://ip:15672 就能夠登陸管理頁面了安全
處於安全的考慮,guest這個默認的用戶只能經過http://localhost:15672 來登陸,其餘的IP沒法直接使用這個帳號。ide
創建配置文件,新建rabbitmq配置文件 /etc/rabbitmq/rabbitmq.config , 內容以下:oop
[{rabbit, [{loopback_users, []}]}].
加入配置以後,重啓服務就可使用guest用戶登陸了。spa
更多相關管理命令:插件
sudo service rabbitmq-server start / rabbitmq-server -detached //啓動服務 sudo rabbitmqctl stop //關閉服務 sudo rabbitmqctl list_connections //查看連接 sudo rabbitmqctl list_queues //查看隊列 sudo rabbitmqctl list_channels //查看頻道 sudo rabbitmqctl list_users //查看所有用戶 sudo rabbitmqctl cluster_status //查看集羣狀態
一、添加pom依賴code
<dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.7.5.RELEASE</version> </dependency>
注意:amqp-client 5.x.x.x版本須要jdk1.8支持,因此若是使用的jdk是1.7就必須使用5.x.x.x一下的版本
二、添加配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:util="http://www.springframework.org/schema/util" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:rabbit="http://www.springframework.org/schema/rabbit" 9 xmlns:p="http://www.springframework.org/schema/p" 10 xsi:schemaLocation=" 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.1.xsd 13 http://www.springframework.org/schema/beans 14 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 15 http://www.springframework.org/schema/util 16 http://www.springframework.org/schema/util/spring-util-4.1.xsd 17 http://www.springframework.org/schema/aop 18 http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 19 http://www.springframework.org/schema/tx 20 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 21 http://www.springframework.org/schema/rabbit 22 http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> 23 24 <context:property-placeholder location="classpath:rabbitmq.properties"/> 25 26 <rabbit:connection-factory id="connectionFactory" host="${rabbit.hosts}" username="${rabbit.username}" password="${rabbit.password}" port="${rabbit.port}" virtual-host="${rabbit.virtualHost}"/> 27 <rabbit:admin connection-factory="connectionFactory"/> 28 29 <rabbit:queue id="first_queue" name="first_queue" durable="true" auto-delete="false" exclusive="false" /> 30 <rabbit:queue id="second_queue" name="second_queue" durable="true" auto-delete="false" exclusive="false" /> 31 32 <rabbit:direct-exchange name="first_exchange" durable="true" auto-delete="false" id="second_exchange"> 33 <rabbit:bindings> 34 <rabbit:binding queue="first_queue" key="first_queue"/> 35 <rabbit:binding queue="second_queue" key="second_queue"/> 36 </rabbit:bindings> 37 </rabbit:direct-exchange> 38 39 <bean id="firstConsumer" class="com.thinkgem.jeesite.modules.mq.consumer.FirstConsumer"></bean> 40 <bean id="secondConsumer" class="com.thinkgem.jeesite.modules.mq.consumer.SecondConsumer"></bean> 41 42 <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto"> 43 <rabbit:listener queues="first_queue" method="onMessage" ref="firstConsumer"/> 44 </rabbit:listener-container> 45 <rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto"> 46 <rabbit:listener queues="second_queue" method="onMessage" ref="secondConsumer"/> 47 </rabbit:listener-container> 48 49 </beans>
rabbitmq.properties以下:
rabbit.hosts=192.168.78.132 rabbit.username=admin rabbit.password=admin rabbit.port=5672 rabbit.virtualHost=/
消費者實現以下:
1 package com.thinkgem.jeesite.modules.mq.consumer; 2 3 import org.springframework.amqp.core.Message; 4 import org.springframework.amqp.core.MessageListener; 5 import org.springframework.beans.factory.annotation.Autowired; 6 7 8 public class FirstConsumer implements MessageListener { 9 10 @Override 11 public void onMessage(Message msg) { 12 try{ 13 String body = new String(msg.getBody(), "UTF-8"); 14 System.out.println("In::: " + body); 15 // 消息處理邏輯 16 17 18 }catch(Exception e){ 19 e.printStackTrace(); 20 } 21 } 22 23 }
另一個就省略不貼了。。。
一、rabbitmq安裝stomp插件,官方連接
sudo rabbitmq-plugins enable rabbitmq_stomp
服務端和客戶端具體配置請參考:待續。。。。。。