SpringBoot整合RabbitMQ

SpringBoot整合RabbitMQ

Spring整合RabbitMQ

spring整合RabbitMQ时,是将交换机、队列看作一个个bean,我们需要将交换机和队列在spring的配置文件里面定义。

这里我们可以用rabbitmq提供的标签来简化bean的定义。

生产者spring配置文件beans标签内部:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"/>
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--定义持久化队列,不存在则自动创建;不绑定到交换机则绑定到默认交换机
    默认交换机类型为direct,名字为:"",路由键为队列的名称
    -->
    <rabbit:queue id="queue1" name="queue1" auto-declare="true"/>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~广播;所有队列都能收到消息~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="fanout_queue_1" name="fanout_queue_1" auto-declare="true"/>

    <!--定义广播交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="fanout_queue_2" name="fanout_queue_2" auto-declare="true"/>

    <!--定义广播类型交换机;并绑定上述两个队列-->
    <rabbit:fanout-exchange id="fanout_exchange" name="fanout_exchange" auto-declare="true">
        <rabbit:bindings>
            <rabbit:binding queue="fanout_queue_1"/>
            <rabbit:binding queue="fanout_queue_2"/>
        </rabbit:bindings>
    </rabbit:fanout-exchange>
    
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~路由;指定队列能收到消息 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--定义路由交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="direct_queue" name="direct_queue" auto-declare="true"/>

    <!--定义路由类型交换机;并绑定上述队列-->
    <rabbit:direct-exchange id="direct_exchange" name="direct_exchange" auto-declare="true">
        <rabbit:bindings>
            <rabbit:binding key="xxx" queue="direct_queue"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~通配符;*匹配一个单词,#匹配多个单词 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!--定义通配符交换机中的持久化队列,不存在则自动创建-->
    <rabbit:queue id="topic_queue" name="topic_queue" auto-declare="true"/>

    <!--定义通配符类型交换机;并绑定上述队列-->
    <rabbit:topic-exchange id="topic_exchange" name="topic_exchange" auto-declare="true">
        <rabbit:bindings>
            <rabbit:binding pattern="*.*" queue="topic_queue"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>

消费者spring配置文件beans标签内部:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"/>

    <bean id="queueListener" class="com.xxx.rabbitmq.listener.QueueListener"/>
    <bean id="fanoutListener1" class="com.xxx.rabbitmq.listener.FanoutListener1"/>
    <bean id="fanoutListener2" class="com.xxx.rabbitmq.listener.FanoutListener2"/>
    <bean id="directListener" class="com.xxx.rabbitmq.listener.DirectListener"/>
    <bean id="topicListener" class="com.xxx.rabbitmq.listener.TopicListenerStar"/>

    <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
        <rabbit:listener ref="queueListener" queue-names="queue"/>
        <rabbit:listener ref="fanoutListener1" queue-names="fanout_queue_1"/>
        <rabbit:listener ref="fanoutListener2" queue-names="fanout_queue_2"/>
        <rabbit:listener ref="directListener" queue-names="direct_queue"/>
        <rabbit:listener ref="topicListener" queue-names="topic_queue"/>
    </rabbit:listener-container>
</beans>

spring整合RabbitMQ步骤:

  1. 在生产者和消费者的spring配置文件中加入上面的bean的配置

  2. 生产者在需要发送消息时通过rabbitTemplate.convertAndSend(交换机名称,路由key,消息)发送消息

  3. 消费者编写各消息队列对应的监听器类(监听器类需要实现MessageListener接口,并重写onMessage()方法)

    在onMessage()方法内做回调函数的操作

SpringBoot整合RabbitMQ

而在SpringBoot中使用RabbitMQ,无非就是将以上的connectionFactory的配置写在yml配置文件中,编写RabbitMQ的配置类,在配置类里面定义交换机和队列的bean

application.yml文件中的连接配置:

spring:
  rabbitmq:
    host: 192.168.67.101
    port: 5672
    username: guest
    password: guest
    virtual-host: /lin

生产者RabbitMQ的配置类:

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    public static final String TOPIC_EXCHANGE = "topic_exchange";
    public static final String TOPIC_QUEUE1 = "topic_queue1";

    //交换机
    @Bean("topicExchange")
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();
    }

    //队列
    @Bean("topicQueue1")
    public Queue topicQueue1(){
        return QueueBuilder.durable(TOPIC_QUEUE1).build();
    }

    //绑定交互机与队列
    @Bean
    public Binding bindTopicQueue1ToTopicExchange(@Qualifier("topicQueue1") Queue queue,@Qualifier("topicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("*.*").noargs();
    }
}

消费者RabbitMQ的监听器:

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQListener {

    @RabbitListener(queues = "topic_queue1")
    public void listenertopicQueue1(Message message){
        System.out.println(new String(message.getBody()));
    }
}

springboot整合RabbitMQ步骤:

  1. 在生产者模块和消费者模块的application.yml里配置好连接信息

  2. 在生产者模块里添加RabbitMQ的配置类(RabbitMQConfig)

  3. 生产者在需要发送消息的类里加入RabbitTemplate rabbitTemplate;属性,用@Autowired自动装配属性,

    并使用rabbitTemplate.convertAndSend(交换机名称,路由key,消息)发送消息

  4. 在消费者模块里添加监听器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值