springMvc-spring-cloud-Stream通过rabbitMq交互

本文详细介绍了如何使用Spring框架整合RabbitMQ,包括生产者和消费者的配置过程。从安装RabbitMQ开始,到Spring MVC项目和Spring Cloud Stream项目的接入,提供了完整的XML配置和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springMvc-spring-cloud-Stream通过rabbitMq交互

这里就不赘述了,网上一搜一大把

rabbitMq安装

首先需要装rabbitmq,这里请参考官网资料。这里只给出Windows和linux 的rpm方式的安装链接。 Windows链接linuxRPM链接

生产者(springMVC)项目接入rabbitmq

  1. 新建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 连接工厂

  1. web.xml配置;
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-rabbitmq.xml
        </param-value>
  </context-param>
  1. 生产者代码
   @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

  1. 配置文件 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: /
  1. 自定义通道
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();
}

  1. 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();
		}
	}	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜雪天晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值