SpringBoot整合RabbitMQ

Posted 鼓捣猫腻

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合RabbitMQ相关的知识,希望对你有一定的参考价值。

一、创建项目并导入依赖

   

<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>

   

二、相关配置和代码

   

2.1)application.properties

   

spring.rabbitmq.host=192.168.21.136

spring.rabbitmq.username=guest

spring.rabbitmq.password=guest

spring.rabbitmq.port=5672

   

2.2)RabbitMQ消息路由四种策略

   

不了解RabbitMQ的强烈建议去看一下理论https://blog.csdn.net/qq_35953966/article/details/104054306

   

Direct和Topic类型的Exchange依赖routingkey与bindingkey的匹配规则来路由消息

Fanout和Headers类型的Exchange依赖routingkey与bindingkey的匹配规则来路由消息

   

2.2.1)Direct策略

   

2.2.1.1)创建RabbitDirectConfig配置类

该类主要是将Queue和Exchange绑定在一起,让Exchange根据消息生产者的routingkey和消息消费者的routingkey匹配,再将消息发到对应消息消费者那里

   

@Configuration

public class RabbitDirectConfig{

public final static String DIRECTNAME="fern_direct";

   

@Bean

Queue queue(){

//队列,存放着消息生成者发来的消息

return newQueue("queue");

}

   

@Bean

DirectExchange directExchange(){

//durable是重启后是否依然有效,autoDelete长期没有使用是否删除掉

return new DirectExchange(DIRECTNAME,true,false);

   

}

   

@Bean

Binding binding(){

//queue|队列和directExchange绑定在一起

return BindingBuilder.bind(queue()).to(directExchange()).with("direct");

}

   

}

   

2.2.1.2)创建DirectReceiver类

   

@Component

public class DirectReceiver{

@RabbitListener(queues="queue")

public void receive1(String msg){

System.out.println("handler1>>>>>>>>>>>>>>"+msg);

}

   

}

   

2.2.1.3)测试

   

   

   

备注:Direct还有一种写法可以省略掉Exchange与Queue的绑定,只限Direct

   

   

   

   

   

2.2.2)Fanout策略

   

2.2.2.1)创建RabbitFanoutConfig

   

@Configuration

publicclassRabbitFanoutConfig{

public final static String FANOUTNAME="fern_fanout";

   

@Bean

Queue queueOne(){

return new Queue("queue_one");

}

@Bean

Queue queueTwo(){

return new Queue("queue_two");

}

   

@Bean

FanoutExchange fanoutExchange(){

return new FanoutExchange(FANOUTNAME,true,false);

}

   

@Bean

Binding bindingOne(){

return BindingBuilder.bind(queueOne()).to(fanoutExchange());

}

   

@Bean

Binding bindingTwo(){

return BindingBuilder.bind(queueTwo()).to(fanoutExchange());

}

}

   

2.2.2.2)创建FanoutReceiver

   

@Component

public class FanoutReceiver{

@RabbitListener(queues="queue_one")

public void receive1(String msg){

System.out.println("FanoutReceiver:receive1>>>>>>>>>"+msg);

}

@RabbitListener(queues="queue_two")

public void receive2(String msg){

System.out.println("FanoutReceiver:receive2>>>>>>>>>"+msg);

}

}

   

2.2.2.3)测试

   

   

   

2.2.3)Topic策略

   

2.2.3.1)创建RabbitTopicConfig

   

@Configuration

public class RabbitTopicConfig{

public final static String TOPICNAME="fern_topic";

@Bean

Queue queue_xiaomi(){

return new Queue("queue_xiaomi");

}

@Bean

Queue queue_huawei(){

return newQueue("queue_huawei");

}

@Bean

Queue queue_iphone(){

return newQueue("queue_iphone");

}

   

@Bean

TopicExchange topicExchange(){

return newTopicExchange(TOPICNAME,true,false);

}

   

@Bean

Binding binding_xiaomi(){

return BindingBuilder.bind(queue_xiaomi()).to(topicExchange()).with("xiaomi.*.*");

}

@Bean

Binding binding_huawei(){

return BindingBuilder.bind(queue_huawei()).to(topicExchange()).with("huawei.#");

}

@Bean

Binding binding_iphone(){

return BindingBuilder.bind(queue_iphone()).to(topicExchange()).with("#.iphone.#");

}

}

   

这里不懂得赶快去认真翻翻上面得链接吧

   

2.2.3.2)创建TopicReceiver

   

@Component

public class TopicReceiver{

@RabbitListener(queues="queue_xiaomi")

public void receive1(String msg){

System.out.println("小米:"+msg);

}

@RabbitListener(queues="queue_huawei")

public void receive2(String msg){

System.out.println("华为:"+msg);

}

@RabbitListener(queues="queue_iphone")

public void receive3(String msg){

System.out.println("苹果:"+msg);

}

}

   

2.2.3.3)Test

   

   

   

   

[SpringBoot系列]SpringBoot如何整合SSMP

springboot怎么整合activiti

SpringBoot完成SSM整合之SpringBoot整合junit

springboot整合jedis

SpringBoot 整合其他框架 -- SpringBoot整合Mybatis

SpringBoot 整合其他框架 -- SpringBoot整合Junit