对RabbitMQ的详细说明大家可以查阅其他资料,现在我们直接进入正题,开始Dubbo框架的基础上集成rabbitMQ!
第一步(在pom.xml中引入依赖)
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
第二步(创建消息生产者配置文件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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://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>
第三步(创建消息监听者配置文件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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://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.rabbitmq.controller.RecvHandler" />
<!-- 定义消费者监听队列 -->
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener ref="queuehandler" queues="queue" />
</rabbit:listener-container>
</beans>
第四步(创建rabbit.properties//rabbitmq的配置参数)
# rabbit ip
rabbit.host=127.0.0.1
# rabbit 端口
rabbit.port=5672
# rabbit 用户名
rabbit.username=guest
# rabbit 密码
rabbit.password=guest
切记web.xml中一定要引入我们新加入的rabbit配置文件
第五步(创建消息监听类 RecvHandler)
```java
package com.rabbitmq.controller;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import java.io.IOException;
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();
}
}
}
第六步(在控制层发送消息 RabbitController //可以结合当前需求灵活应用)
package com.rabbitmq.controller;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
@RequestMapping("/mq")
public class RabbitController {
@Autowired
private RabbitTemplate rabbitTemplate;
@RequestMapping("/rabbit")
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);
}
}
打包运行!就会发送控制层输出了我们发送的消息,监听类也会进行打印
我是可爱的小猪,我的id是1,我的名字是pig
我是可爱的小猪,我的id是2,我的名字是cat
一定要有耐心!为了集成rabbitMQ用了好几个小时的时间!大家也要加油呀!!!