RabbitMQ整合SpringBoot
pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
yml
spring:
rabbitmq:
username: username
password: password
host: 127.0.0.1
port: 5672
virtual-host: /
Code
Configuration
public class RabbitMQConstants {
public static final String QUEUE = "queue";
public static final String ROUTE = "route";
public static final String PUBSUB = "pubsub1";
public static final String TOPIC = "topic";
public static final String TOPIC1 = "topic1";
public static final String DIRECT_EXCHANGE = "direct_exchange";
public static final String ROUTE_EXCHANGE = "route_exchange";
public static final String PUBSUB_EXCHANGE = "pubsub_exchange";
public static final String TOPIC_EXCHANGE = "topic_exchange";
public static final String QUEUE_ROUTING_KEY = "queue_routeing_key";
public static final String ROUTE_ROUTING_KEY = "route_routeing_key";
public static final String TOPIC_ROUTING_KEY = "topic_routeing_key.*";
public static final String TOPIC_ROUTING_KEY1 = "topic_routeing_key.A.*";
public static final String TOPIC_ROUTING_KEY_TEST = "topic_routeing_key.A.B";
}
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queue(){
return new Queue(RabbitMQConstants.QUEUE);
}
@Bean
public Queue pubsub(){
return new Queue(RabbitMQConstants.PUBSUB);
}
@Bean
public Queue route(){
return new Queue(RabbitMQConstants.ROUTE);
}
@Bean
public Queue topic(){
return new Queue(RabbitMQConstants.TOPIC);
}
@Bean
public Queue topic1(){
return new Queue(RabbitMQConstants.TOPIC1);
}
@Bean
public Exchange directExchange(){
return new DirectExchange(RabbitMQConstants.DIRECT_EXCHANGE);
}
@Bean
public Exchange pubsubExchange(){
return new FanoutExchange(RabbitMQConstants.PUBSUB_EXCHANGE);
}
@Bean
public Exchange routeExchange(){
return new DirectExchange(RabbitMQConstants.ROUTE_EXCHANGE);
}
@Bean
public Exchange topicExchange(){
return new TopicExchange(RabbitMQConstants.TOPIC_EXCHANGE);
}
@Bean
public Binding queueBinding(){
return new Binding(RabbitMQConstants.QUEUE,
Binding.DestinationType.QUEUE,
RabbitMQConstants.DIRECT_EXCHANGE,
RabbitMQConstants.QUEUE_ROUTING_KEY,
null);
}
@Bean
public Binding pubsubBinding(){
return new Binding(RabbitMQConstants.PUBSUB,
Binding.DestinationType.QUEUE,
RabbitMQConstants.PUBSUB_EXCHANGE,
"",
null);
}
@Bean
public Binding routeBinding(){
return new Binding(RabbitMQConstants.ROUTE,
Binding.DestinationType.QUEUE,
RabbitMQConstants.ROUTE_EXCHANGE,
RabbitMQConstants.ROUTE_ROUTING_KEY,
null);
}
@Bean
public Binding topic1Binding(){
return new Binding(RabbitMQConstants.TOPIC,
Binding.DestinationType.QUEUE,
RabbitMQConstants.TOPIC_EXCHANGE,
RabbitMQConstants.TOPIC_ROUTING_KEY,
null);
}
@Bean
public Binding topic2Binding(){
return new Binding(RabbitMQConstants.TOPIC1,
Binding.DestinationType.QUEUE,
RabbitMQConstants.TOPIC_EXCHANGE,
RabbitMQConstants.TOPIC_ROUTING_KEY1,
null);
}
}
listener
@Component
public class RabbitMQListener {
@RabbitListener(queues = RabbitMQConstants.QUEUE)
public void queue1(String message){
System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.queue1 -> " + message);
}
@RabbitListener(queues = RabbitMQConstants.QUEUE)
public void queue2(String message){
System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.queue2 -> " + message);
}
@RabbitListener(queues = RabbitMQConstants.PUBSUB)
public void pubsub1(String message){
System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.pubsub1 -> " + message);
}
@RabbitListener(queues = RabbitMQConstants.ROUTE)
public void route1(String message){
System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.route1 -> " + message);
}
@RabbitListener(queues = RabbitMQConstants.ROUTE)
public void route2(String message){
System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.route2 -> " + message);
}
@RabbitListener(queues = RabbitMQConstants.TOPIC)
public void topic1(String message){
System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.topic1 -> " + message);
}
@RabbitListener(queues = RabbitMQConstants.TOPIC1)
public void topic2(String message){
System.err.println("com.free.fly.rabbitmq.boot.listeners.RabbitMQListener.topic2 -> " + message);
}
}
controller
@RestController
@RequestMapping("rabbit")
public class RabbitMQController {
@Autowired
private AmqpTemplate amqpTemplate;
@GetMapping("queue")
public void queue(String message){
amqpTemplate.convertAndSend(RabbitMQConstants.DIRECT_EXCHANGE,RabbitMQConstants.QUEUE_ROUTING_KEY,message);
}
@GetMapping("pubsub")
public void pubsub(String message){
amqpTemplate.convertAndSend(RabbitMQConstants.PUBSUB_EXCHANGE,null, message);
}
@GetMapping("route")
public void route(String message){
amqpTemplate.convertAndSend(RabbitMQConstants.ROUTE_EXCHANGE, RabbitMQConstants.ROUTE_ROUTING_KEY,message);
}
@GetMapping("topic")
public void topic(String message){
amqpTemplate.convertAndSend(RabbitMQConstants.TOPIC_EXCHANGE, RabbitMQConstants.TOPIC_ROUTING_KEY_TEST,message);
}
}