安装
1、安装rabbitmq,此命令会自动安装erlang依赖
sudo apt-get install rabbitmq-server
2、要想在浏览器登录管理页面,需要先安装插件
sudo rabbitmq-plugins enable rabbitmq_management
3、添加用户
sudo rabbitmqctl add_user username password
4、设置用户角色和权限
sudo rabbitmqctl set_user_tags username administrator sudo rabbitmqctl set_permissions -p / username ".*" ".*" ".*"
执行完以上命令,打开浏览器 http://ip:15672 就可以登录管理页面了
处于安全的考虑,guest这个默认的用户只能通过http://localhost:15672 来登录,其他的IP无法直接使用这个账号。
建立配置文件,新建rabbitmq配置文件 /etc/rabbitmq/rabbitmq.config , 内容如下:
[{rabbit, [{loopback_users, []}]}].
加入配置之后,重启服务就可以使用guest用户登录了。
更多相关管理命令:
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 //查看集群状态
Rabbitmq与Spring集成
1、添加pom依赖
<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一下的版本
2、添加配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
另外一个就省略不贴了。。。
使用 Stomp协议实现web客户端通信
1、rabbitmq安装stomp插件,官方链接
sudo rabbitmq-plugins enable rabbitmq_stomp
服务端和客户端具体配置请参考:待续。。。。。。