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步骤:
-
在生产者和消费者的spring配置文件中加入上面的bean的配置
-
生产者在需要发送消息时通过rabbitTemplate.convertAndSend(交换机名称,路由key,消息)发送消息
-
消费者编写各消息队列对应的监听器类(监听器类需要实现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步骤:
-
在生产者模块和消费者模块的application.yml里配置好连接信息
-
在生产者模块里添加RabbitMQ的配置类(RabbitMQConfig)
-
生产者在需要发送消息的类里加入RabbitTemplate rabbitTemplate;属性,用@Autowired自动装配属性,
并使用rabbitTemplate.convertAndSend(交换机名称,路由key,消息)发送消息
-
在消费者模块里添加监听器