1.1 啓動rabbitmq服務器java
1.3 建立用戶node
1.4 給用戶添加管理員角色git
1.5 設置用戶在虛擬主機上的權限github
set_permissions -p vhostSJJ shijunjie ".*" ".*" ".*"web
1.6 安裝基於web的管理插件spring
按照上面配置好後,能夠在管理界面上看到入下信息數據庫
附:rabbitmq的一些經常使用命令 安全
rabbitmq的安裝、啓動和中止服務器
rabbitmq-service.bat install
rabbitmq-service.bat start
rabbitmq-service.bat stopmvc
列出全部queue
rabbitmqctl list_queues
列出指定queue的信息
rabbitmqctl list_queues [the queue name] messages_ready messages_unacknowledged
列出全部exchange
rabbitmqctl list_exchanges
列出全部binding
rabbitmqctl list_bindings
安裝基於web的管理插件
rabbitmq-plugins.bat enable rabbitmq_management
關閉節點
# rabbitmqctl stop
2.中止RabbitMQ應用
# rabbitmqctl stop_app
3.啓動RabbitMQ應用
# rabbitmqctl start_app
4.顯示RabbitMQ中間件各類信息
# rabbitmqctl status
5.重置RabbitMQ節點
# rabbitmqctl reset
# rabbitmqctl force_reset
從它屬於的任何集羣中移除,從管理數據庫中移除全部數據,例如配置過的用戶和虛擬宿主, 刪除全部持久化的消息。
force_reset命令和reset的區別是無條件重置節點,無論當前管理數據庫狀態以及集羣的配置。若是數據庫或者集羣配置發生錯誤才使用這個最後 的手段。
注意:只有在中止RabbitMQ應用後,reset和force_reset才能成功。
6.循環日誌文件
# rabbitmqctl rotate_logs[suffix]
7.集羣管理
# rabbitmqctl cluster clusternode…
用戶管理
1.添加用戶
# rabbitmqctl add_user username password
2.刪除用戶
# rabbitmqctl delete_user username
3.修改密碼
# rabbitmqctl change_password username newpassword
4.列出全部用戶
# rabbitmqctl list_users
權限控制1.建立虛擬主機
# rabbitmqctl add_vhost vhostpath
2.刪除虛擬主機
# rabbitmqctl delete_vhost vhostpath
3.列出全部虛擬主機
# rabbitmqctl list_vhosts
4.設置用戶權限
# rabbitmqctl set_permissions [-p vhostpath] username regexp regexp regexp
5.清除用戶權限
# rabbitmqctl clear_permissions [-p vhostpath] username
6.列出虛擬主機上的全部權限
# rabbitmqctl list_permissions [-p vhostpath]
7.列出用戶權限
# rabbitmqctl list_user_permissions username
2.創建一個maven項目.而後咱們開始配置項目.
2.1 因爲是spring整合,咱們須要加入spring的依賴.
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.8.RELEASE</version> </dependency>
2.2加入rabbitmq和spring的整合依賴
<!--rabbitmq依賴 --> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.3.5.RELEASE</version> </dependency>
2.3定義消息生產者和消息發送者
exchange有幾種,這裏我只測試了兩種, 經過分別定義兩個exchange去綁定direct和topic..
首先, 定義消息生產者, 經過配置將template連接connect-factory並注入到代碼中使用.
package me.shijunjie.producer; import java.io.IOException; import javax.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.stereotype.Service; @Service public class MessageProducer { private Logger logger = LoggerFactory.getLogger(MessageProducer.class); @Resource(name="amqpTemplate") private AmqpTemplate amqpTemplate; @Resource(name="amqpTemplate2") private AmqpTemplate amqpTemplate2; public void sendMessage(Object message) throws IOException { logger.info("to send message:{}", message); amqpTemplate.convertAndSend("queueTestKey", message); amqpTemplate.convertAndSend("queueTestChris", message); amqpTemplate2.convertAndSend("shijj.xxxx.wsdwd", message); } }
而後咱們定義消息消費者, 這裏,我定義了三個消費者, 經過監聽消息隊列, 分別接受各自所匹配的消息.
第一個消費者, 接受direct的消息, 他的exchange爲exchangeTest, rout-key爲queueTestKey
package me.shijunjie.consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class MessageConsumer implements MessageListener { private Logger logger = LoggerFactory.getLogger(MessageConsumer.class); @Override public void onMessage(Message message) { logger.info("consumer receive message------->:{}", message); } }
第二個消費者, 接受direct的消息(爲了測試一個exchange能夠發送多個消息), 他的exchange爲exchangeTest, rout-key爲queueTestChris.
package me.shijunjie.consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class ChrisConsumer implements MessageListener { private Logger logger = LoggerFactory.getLogger(ChrisConsumer.class); @Override public void onMessage(Message message) { logger.info("chris receive message------->:{}", message); } }
第三個消費者, 接受topic的消息他的exchange爲exchangeTest2, pattern爲shijj.*.. .*能夠匹配一個, .#能夠匹配一個或多個..
package me.shijunjie.consumer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class ShijjConsumer implements MessageListener { private Logger logger = LoggerFactory.getLogger(ShijjConsumer.class); @Override public void onMessage(Message message) { logger.info("shijj receive message------->:{}", message); } }
而後就是關鍵rabbit整合spring的配置文件.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.2.xsd"> <!--配置connection-factory,指定鏈接rabbit server參數 --> <rabbit:connection-factory id="connectionFactory" virtual-host="vhostSJJ" username="shijunjie" password="wssjj123" host="123.206.228.200" port="5672" /> <!-- <rabbit:connection-factory id="connectionFactory" username="test2" password="test2" host="123.206.228.200" port="5672" /> --> <!--經過指定下面的admin信息,當前producer中的exchange和queue會在rabbitmq服務器上自動生成 --> <rabbit:admin id="connectAdmin" connection-factory="connectionFactory" /> <!--定義queue --> <rabbit:queue name="queueTest" durable="true" auto-delete="false" exclusive="false" declared-by="connectAdmin" /> <!-- 定義direct exchange,綁定queueTest --> <rabbit:direct-exchange name="exchangeTest" durable="true" auto-delete="false" declared-by="connectAdmin"> <rabbit:bindings> <rabbit:binding queue="queueTest" key="queueTestKey"></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!--定義rabbit template用於數據的接收和發送 --> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="exchangeTest" /> <!-- 消息接收者 --> <bean id="messageReceiver" class="me.shijunjie.consumer.MessageConsumer"></bean> <!-- queue litener 觀察 監聽模式 當有消息到達時會通知監聽在對應的隊列上的監聽對象 --> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener queues="queueTest" ref="messageReceiver" /> </rabbit:listener-container> <!--定義queue --> <rabbit:queue name="queueChris" durable="true" auto-delete="false" exclusive="false" declared-by="connectAdmin" /> <!-- 定義direct exchange,綁定queueTest --> <rabbit:direct-exchange name="exchangeTest" durable="true" auto-delete="false" declared-by="connectAdmin"> <rabbit:bindings> <rabbit:binding queue="queueChris" key="queueTestChris"></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 消息接收者 --> <bean id="receiverChris" class="me.shijunjie.consumer.ChrisConsumer"></bean> <!-- queue litener 觀察 監聽模式 當有消息到達時會通知監聽在對應的隊列上的監聽對象 --> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener queues="queueChris" ref="receiverChris" /> </rabbit:listener-container> <!-- 分隔線 --> <!--配置connection-factory,指定鏈接rabbit server參數 --> <rabbit:connection-factory id="connectionFactory2" virtual-host="vhostSJJ" username="shijunjie" password="wssjj123" host="123.206.228.200" port="5672" /> <!--經過指定下面的admin信息,當前producer中的exchange和queue會在rabbitmq服務器上自動生成 --> <rabbit:admin id="connectAdmin2" connection-factory="connectionFactory2" /> <!--定義queue --> <rabbit:queue name="queueShijj" durable="true" auto-delete="false" exclusive="false" declared-by="connectAdmin2" /> <!-- 定義direct exchange,綁定queueTest --> <rabbit:topic-exchange name="exchangeTest2" durable="true" auto-delete="false" declared-by="connectAdmin2"> <rabbit:bindings> <rabbit:binding queue="queueShijj" pattern="shijj.#"></rabbit:binding> </rabbit:bindings> </rabbit:topic-exchange> <!--定義rabbit template用於數據的接收和發送 --> <rabbit:template id="amqpTemplate2" connection-factory="connectionFactory2" exchange="exchangeTest2" /> <!-- 消息接收者 --> <bean id="recieverShijj" class="me.shijunjie.consumer.ShijjConsumer"></bean> <!-- queue litener 觀察 監聽模式 當有消息到達時會通知監聽在對應的隊列上的監聽對象 --> <rabbit:listener-container connection-factory="connectionFactory2"> <rabbit:listener queues="queueShijj" ref="recieverShijj" /> </rabbit:listener-container> </beans>
以及個人application.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <import resource="classpath*:rabbitMQ.xml" /> <!-- 掃描指定package下全部帶有如@controller,@services,@resource,@ods並把所註釋的註冊爲Spring Beans --> <context:component-scan base-package="me.shijunjie.consumer, me.shijunjie.producer" /> <!-- 激活annotation功能 --> <context:annotation-config /> <!-- 激活annotation功能 --> <context:spring-configured /> </beans>
3.所有配置完成以後進行測試
package me.shijunjie.test; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import me.shijunjie.producer.MessageProducer; public class TestClass { private Logger logger = LoggerFactory.getLogger(TestClass.class); private ApplicationContext context = null; @Before public void setUp() throws Exception { context = new ClassPathXmlApplicationContext("application.xml"); } @Test public void should_send_a_amq_message() throws Exception { MessageProducer messageProducer = (MessageProducer) context.getBean("messageProducer"); int a = 100; while (a > 0) { messageProducer.sendMessage("Hello, I am amq sender num :" + a--); try { //暫停一下,好讓消息消費者去取消息打印出來 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
運行後