1:首先引入jar包:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2:配置RabbitMqConfig类:包含三种方式,分别为direct、topic、fanout,还有个heards基本不用,所以不演示了。
这三种模式的区别是:direct是RabbitMq默认的模式,也是最简单的一种,只会去匹配符合name的Queue;
topic是需要配置Exchange交换器,并将队列与交换器绑定。一个Exchange可对应多个Queue。
fanou是利用路由广播,将多个Queue绑定到路由广播上,在接收消息时,只需要匹配对应的队列名称就可以了。
package com.springboot.config;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author lize
* @Title: RabbitMqConfig
* @ProjectName SpringBoot
* @Description: TODO
* @date 2019-04-01 17:21
*/
@Configuration
public class RabbitMqConfig {
/**direct*/
@Bean
public Queue queue(){
return new Queue("queue");
}
/**topic*/
@Bean(name="message")
public Queue queueMessage() {
return new Queue("topic.message");
}
@Bean(name="messages")
public Queue queueMessages(){
return new Queue("topic.messages");
}
@Bean
public TopicExchange exchange(){
return new To