spring 接入rabbitMq
springMvc-spring-cloud-Stream通过rabbitMq交互
这里就不赘述了,网上一搜一大把
rabbitMq安装
首先需要装rabbitmq,这里请参考官网资料。这里只给出Windows和linux 的rpm方式的安装链接。 Windows链接;linuxRPM链接。
生产者(springMVC)项目接入rabbitmq
- 新建xml配置
文件命名spring-rabbitmq.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<!-- rabbitmq begin -->
<!-- 创建connectionFactory -->
<bean id="rabbitConnectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<constructor-arg value="127.0.0.1"/>
<property name="host" value="127.0.0.1" />
<property name="port" value="5672" />
<property name="username" value="guest"/>
<property name="password" value="guest"/>
<property name="channelCacheSize" value="100" />
</bean>
<rabbit:admin id="amqpAdmin" connection-factory="rabbitConnectionFactory"/>
<!--声明队列queue-->
<rabbit:queue id="queue1" name="queue1" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="queue2" name="queue2 durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="queue3" name="queue3" durable="true" auto-delete="false" exclusive="false" />
<!-- 交换器与队列绑定exchange queue binging key 绑定 -->
<rabbit:topic-exchange name="queue.queue1" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="queue1" pattern="*.*.queue1Consumers" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:topic-exchange name="queue.queue2" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="queue2" pattern="*.*.queue2Consumers" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:topic-exchange name="queue.queue3" durable="true" auto-delete="false">
<rabbit:bindings>
<rabbit:binding queue="queue3" pattern="*.*.queue3Consumers" />
</rabbit:bindings>
</rabbit:topic-exchange>
<!-- 消息转换器 -->
<bean id="messageConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter" />
<!-- spring template声明 -->
<rabbit:template id="amqpTemplate" connection-factory="rabbitConnectionFactory" message-converter="messageConverter" />
<!-- rabbitmq end -->
</beans>
connectionFactory 连接工厂
- web.xml配置;
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-rabbitmq.xml
</param-value>
</context-param>
- 生产者代码
@Autowired
private AmqpTemplate amqpTemplate;
@Override
public void produce(final Order order) {
logger.info("回调队列destination: queue.queue1.queue1Consumers");
logger.info("order: " + order);
String json = JSON.toJSONString(order);
amqpTemplate.convertAndSend("queue.queue1.queue1Consumers",order);
}
消费者(spring-cloud-stream)项目接入rabbitmq
- 配置文件 application.yml
spring:
cloud:
stream:
bindings:
queue1: # 这个名字是一个通道的名称
destination: queue.queue1 # 目的,对应 MQ 是 exchange, 生成临时队列
content-type: text/plain
binder: local_rabbit # 绑定器名称
group: queue1Consumers # 进行操作的分组,实际上就表示持久化--消费端配置
queue2: # 这个名字是一个通道的名称
destination: queue.queue2 # 目的,对应 MQ 是 exchange, 生成临时队列
binder: local_rabbit # 绑定器名称
group: queue2Consumers # 进行操作的分组,实际上就表示持久化--消费端配置
queue3: # 这个名字是一个通道的名称
destination: queue.queue3 # 目的,对应 MQ 是 exchange, 生成临时队列
content-type: application/json # 设置消息类型,本次为对象json,如果是文本则设置“text/plain”
binder: local_rabbit # 绑定器名称
group: queue3Consumers # 进行操作的分组,实际上就表示持久化--消费端配置
--消费端配置
binders: # 在此处配置要绑定的rabbitmq的服务信息;
local_rabbit: # rabbitmq的服务名称
type: rabbit
environment:
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
- 自定义通道
public interface ReceviceQueueProcess {
public static final String queue1= "queue1"; // 输入通道名称
public static final String queue2= "queue2"; // 输入通道名称
public static final String queue3= "queue3"; // 输入通道名称
@Input(ReceviceQueueProcess.queue1)
public SubscribableChannel queue1();
@Input(ReceviceQueueProcess.queue2)
public SubscribableChannel queue2();
@Input(ReceviceQueueProcess.queue3)
public SubscribableChannel queue3();
}
- ribbitmq监听绑定器
@EnableBinding(ReceviceQueueProcess.class)
public class ReceiveMsgHandler {
private Logger logger = Logger.getLogger(getClass());
@StreamListener(ReceviceQueueProcess.queue1)
public void queue1(Message message) {
logger.info("发送知监听queue1");
try {
Object body = message.getPayload();
String json = body.toString();
logger.info("消息推送 body:" +json);
logger.info("get message from queue1: " + json);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@StreamListener(ReceviceQueueProcess.queue2)
public void queue2(Message message) {
logger.info("发送通知监听queue2");
try {
logger.info("发送通知监听queue2");
Object body = message.getPayload();
String json = body.toString();;
logger.info("推送 json:" + json);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@StreamListener(ReceviceQueueProcess.queue3)
public void queue3(Message message) {
logger.info("发送通知监听queue3");
try {
logger.info("发送通知监听queue3");
Object body = message.getPayload();
String json = body.toString();;
logger.info("推送json:" + json);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}