RabbitMQ的工作原理图:
Spring整合RibbitMQ
第一步:添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
第二步:配置application.yml,启动类
application.yml
server:
port: 44000
spring:
application:
name: test-rabbitmq-producer
rabbitmq: # 连接rabbitmq的参数
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtualHost: /
启动类
@SpringBootApplication
public class TestRabbitmqApplication {
public static void main(String[] args) {
SpringApplication.run(TestRabbitmqApplication.class,args);
}
}
第三步:定义RabbitConfig类,配置Exchange、Queue、及绑定交换机。
@Configuration
public class RabbitmqConfig {
// 队列
public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
// 交换机
public static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";
// 路由key
public static final String ROUTINGKEY_EMAIL="inform.#.email.#";
public static final String ROUTINGKEY_SMS="inform.#.sms.#";
//声明QUEUE_INFORM_EMAIL队列
@Bean(QUEUE_INFORM_EMAIL)
public Queue QUEUE_INFORM_EMAIL(){
return new Queue(QUEUE_INFORM_EMAIL);
}
//声明QUEUE_INFORM_SMS队列
@Bean(QUEUE_INFORM_SMS)
public Queue QUEUE_INFORM_SMS(){
return new Queue(QUEUE_INFORM_SMS);
}
//声明交换机
@Bean(EXCHANGE_TOPICS_INFORM)
public Exchange EXCHANGE_TOPICS_INFORM(){
return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
}
//ROUTINGKEY_EMAIL队列绑定交换机,指定routingKey
@Bean
public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
}
//ROUTINGKEY_SMS队列绑定交换机,指定routingKey
@Bean
public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
}
}
解释:
RabbitMQ常用工作模式
-
Work queues
— —默认交换机,无需定义
— —work queues的生产方是面向队列发送消息(底层使用默认 交换机)。
— —rabbit采用轮询的方式将消息是平均发送给消费者(也就是你一个我一个我) -
Publish/subscribe(发布/订阅模式)
— —需要定义交换机,对应交换机类型:fanout
— —publish/subscribe的生产方是面向交换机发送消息,还需要设置队列 和交换机的绑定
— — 一个交换机绑定的队列,监听她的消费者都能接受到消息。
— — 功能比Work queues强大,可实现Work queues的功能 -
Routing(路由模式)
— —需要定义交换机,对应交换机类型:direct
— —Routing模式要求队列在绑定交换机时要指定routingkey,消息会转发到符合routingkey的队列(相当于确定查询) -
Topics(通配符模式)------这里使用的就是Topics,因为功能最强大
— —需要定义交换机,对应交换机类型:topic
— —路由key可以这样定义:inform.#.email.#
— —Topic模式最强大,可以实现Routing、publish/subscirbe模式的功能。
测试
生产者:
@SpringBootTest
@RunWith(SpringRunner.class)
public class Producer05_topics_springboot {
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void testSendByTopics(){
String msg = "testSendByTopics";
rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_INFORM,"inform.email.sms",msg);
System.out.println("发送消息"+msg);
}
}
消费者
@Component
public class ReceiveHandler {
//监听email队列
@RabbitListener(queues = RabbitmqConfig.QUEUE_INFORM_EMAIL)
public void receive_email(String msg, Message message, Channel channel){
System.out.println(msg);
}
//监听sms队列
@RabbitListener(queues = RabbitmqConfig.QUEUE_INFORM_SMS)
public void receive_SMS(String msg, Message message, Channel channel){
System.out.println(msg);
}
}