1、docker 安装

  1. docker run -d --hostname my-rabbit --name some-rabbit rabbitmq:3
  2. docker run -d --network=host --hostname my-rabbit1 --name my-rabbit1 rabbitmq:3

2、添加admin管理员

添加用户

  1. rabbitmqctl add_user admin admin

设置permissions

  1. rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"

设置用户角色

  1. rabbitmqctl set_user_tags admin administrator

查看新添加的admin

  1. rabbitmqctl list_users

查看用于的权限

  1. rabbitmqctl list_permissions -p /

3、项目搭建

SpringMVC整合RabbitMQ

添加RabbitMQ的jar包,在pom.xml中添加:

  1. <dependency>
  2. <groupId>org.springframework.amqp</groupId>
  3. <artifactId>spring-rabbit</artifactId>
  4. <version>2.1.2.RELEASE</version>
  5. </dependency>

在src/main/resource 新建rabbit.properties,配置内容如下:

  1. # rabbit ip
  2. rabbit.host=127.0.0.1
  3. # rabbit 端口
  4. rabbit.port=5672
  5. # rabbit 用户名
  6. rabbit.username=admin
  7. # rabbit 密码
  8. rabbit.password=admin

在src/main/resource下新建spring-rabbit-send.xml,内容如下:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/rabbit
  6. http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  11. <context:property-placeholder location="classpath:rabbit.properties" ignore-unresolvable="true" />
  12. <!-- 配置连接工厂 -->
  13. <rabbit:connection-factory id="connectionFactory"
  14. host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}" />
  15. <!-- 定义mq管理 -->
  16. <rabbit:admin connection-factory="connectionFactory" />
  17. <!-- 声明队列 -->
  18. <rabbit:queue name="queue" auto-declare="true" durable="true" />
  19. <!-- 定义交换机绑定队列(路由模式) -->
  20. <rabbit:direct-exchange name="IExchange" id="IExchange">
  21. <rabbit:bindings>
  22. <rabbit:binding queue="queue" key="queuekey" />
  23. </rabbit:bindings>
  24. </rabbit:direct-exchange>
  25. <!-- 消息对象json转换类 -->
  26. <bean id="jsonMessageConverter"
  27. class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
  28. <!-- 定义模版 -->
  29. <rabbit:template id="rabbitTemplate"
  30. connection-factory="connectionFactory" exchange="IExchange"
  31. message-converter="jsonMessageConverter" />
  32. </beans>

在src/main/resource下新建spring-rabbit-recv.xml,内容如下:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/rabbit
  6. http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  9. http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  11. <context:property-placeholder location="classpath:rabbit.properties" ignore-unresolvable="true" />
  12. <!-- 配置连接工厂 -->
  13. <rabbit:connection-factory id="connectionFactory"
  14. host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}" password="${rabbit.password}" />
  15. <!-- 定义mq管理 -->
  16. <rabbit:admin connection-factory="connectionFactory" />
  17. <!-- 声明队列 -->
  18. <rabbit:queue name="queue" auto-declare="true" durable="true" />
  19. <!-- 定义消费者 -->
  20. <bean name="queuehandler" class="com.climber.rabbitmq.RecvHandler" />
  21. <!-- 定义消费者监听队列 -->
  22. <rabbit:listener-container
  23. connection-factory="connectionFactory">
  24. <rabbit:listener ref="queuehandler" queues="queue" />
  25. </rabbit:listener-container>
  26. </beans>

定义RecvHandler类,具体代码如下:

  1. public class RecvHandler implements MessageListener {
  2. private static final ObjectMapper MAPPER = new ObjectMapper();
  3. public void onMessage(Message msg) {
  4. try {
  5. // msg就是rabbitmq传来的消息,需要的同学自己打印看一眼
  6. // 使用jackson解析
  7. JsonNode jsonData = MAPPER.readTree(msg.getBody());
  8. System.out.println("我是可爱的小猪,我的id是" + jsonData.get("id").asText() + ",我的名字是" + jsonData.get("name").asText());
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. }

定义Controller层:

  1. @Controller
  2. public class RabbitController {
  3. @Autowired
  4. private RabbitTemplate rabbitTemplate;
  5. @RequestMapping(value="rabbit",method=RequestMethod.GET)
  6. public void rabbit(){
  7. HashMap<String, String> map = new HashMap<String, String>();
  8. map.put("id", "1");
  9. map.put("name", "pig");
  10. //根据key发送到对应的队列
  11. rabbitTemplate.convertAndSend("queuekey", map);
  12. map.put("id", "2");
  13. map.put("name", "cat");
  14. //根据key发送到对应的队列
  15. rabbitTemplate.convertAndSend("queuekey", map);
  16. }
  17. }