1、docker 安装
docker run -d --hostname my-rabbit --name some-rabbit rabbitmq:3docker run -d --network=host --hostname my-rabbit1 --name my-rabbit1 rabbitmq:3
2、添加admin管理员
添加用户
rabbitmqctl add_user admin admin
设置permissions
rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
设置用户角色
rabbitmqctl set_user_tags admin administrator
查看新添加的admin
rabbitmqctl list_users
查看用于的权限
rabbitmqctl list_permissions -p /
3、项目搭建
SpringMVC整合RabbitMQ
添加RabbitMQ的jar包,在pom.xml中添加:
<dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit</artifactId><version>2.1.2.RELEASE</version></dependency>
在src/main/resource 新建rabbit.properties,配置内容如下:
# rabbit iprabbit.host=127.0.0.1# rabbit 端口rabbit.port=5672# rabbit 用户名rabbit.username=admin# rabbit 密码rabbit.password=admin
在src/main/resource下新建spring-rabbit-send.xml,内容如下:
<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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><context:property-placeholder location="classpath:rabbit.properties" ignore-unresolvable="true" /><!-- 配置连接工厂 --><rabbit:connection-factory id="connectionFactory"host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}" /><!-- 定义mq管理 --><rabbit:admin connection-factory="connectionFactory" /><!-- 声明队列 --><rabbit:queue name="queue" auto-declare="true" durable="true" /><!-- 定义交换机绑定队列(路由模式) --><rabbit:direct-exchange name="IExchange" id="IExchange"><rabbit:bindings><rabbit:binding queue="queue" key="queuekey" /></rabbit:bindings></rabbit:direct-exchange><!-- 消息对象json转换类 --><bean id="jsonMessageConverter"class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" /><!-- 定义模版 --><rabbit:template id="rabbitTemplate"connection-factory="connectionFactory" exchange="IExchange"message-converter="jsonMessageConverter" /></beans>
在src/main/resource下新建spring-rabbit-recv.xml,内容如下:
<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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><context:property-placeholder location="classpath:rabbit.properties" ignore-unresolvable="true" /><!-- 配置连接工厂 --><rabbit:connection-factory id="connectionFactory"host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}" /><!-- 定义mq管理 --><rabbit:admin connection-factory="connectionFactory" /><!-- 声明队列 --><rabbit:queue name="queue" auto-declare="true" durable="true" /><!-- 定义消费者 --><bean name="queuehandler" class="com.climber.rabbitmq.RecvHandler" /><!-- 定义消费者监听队列 --><rabbit:listener-containerconnection-factory="connectionFactory"><rabbit:listener ref="queuehandler" queues="queue" /></rabbit:listener-container></beans>
定义RecvHandler类,具体代码如下:
public class RecvHandler implements MessageListener {private static final ObjectMapper MAPPER = new ObjectMapper();public void onMessage(Message msg) {try {// msg就是rabbitmq传来的消息,需要的同学自己打印看一眼// 使用jackson解析JsonNode jsonData = MAPPER.readTree(msg.getBody());System.out.println("我是可爱的小猪,我的id是" + jsonData.get("id").asText() + ",我的名字是" + jsonData.get("name").asText());} catch (IOException e) {e.printStackTrace();}}}
定义Controller层:
@Controllerpublic class RabbitController {@Autowiredprivate RabbitTemplate rabbitTemplate;@RequestMapping(value="rabbit",method=RequestMethod.GET)public void rabbit(){HashMap<String, String> map = new HashMap<String, String>();map.put("id", "1");map.put("name", "pig");//根据key发送到对应的队列rabbitTemplate.convertAndSend("queuekey", map);map.put("id", "2");map.put("name", "cat");//根据key发送到对应的队列rabbitTemplate.convertAndSend("queuekey", map);}}
