标签:font ret dep queue mat byte 直接 let print
? ?
<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
? ?
? ?
? ?
? ?
2.2.4)Headers策略(用的少)
? ?
@Configuration
public class RabbitHeaderConfig{
public final static String HEADERSNAME="fern_header";
? ?
@Bean
Queue queueName(){
return new Queue("queue_name");
}
? ?
@Bean
Queue queueAge(){
return new Queue("queue_age");
}
? ?
@Bean
HeadersExchange headersExchange(){
return new HeadersExchange(HEADERSNAME,true,false);
}
? ?
@Bean
Binding bindingName(){
Map<String,Object> map = new HashMap<>();
map.put("name","哈哈哈");
map.put("name","嘤嘤嘤");
//whereAny()map集合中匹配到一对key-value就过
//还有WhereAll()全部匹配到才过
return BindingBuilder.bind(queueName()).to(headersExchange()).whereAny(map).match();
}
? ?
@Bean
Binding bindingAge(){
//returnBindingBuilder.bind(queueAge()).to(headersExchange()).where("age").exists();这个匹配到age就直接通过
return BindingBuilder.bind(queueAge()).to(headersExchange()).where("age").matches("呜呜呜");//这种则是key-value同时匹配到
}
}
? ?
2.2.5)创建HeaderReceiver
? ?
@Component
public class HeaderReceiver{
@RabbitListener(queues="queue_name")
public void receive1(byte[]msg){
System.out.println(newString(msg,0,msg.length));
}
@RabbitListener(queues="queue_age")
public void receive2(byte[]msg){
System.out.println(newString(msg,0,msg.length));
}
}
? ?
2.2.6)测试
? ?
? ?
? ?
标签:font ret dep queue mat byte 直接 let print
原文地址:https://www.cnblogs.com/fernfei/p/12219052.html