springAMQP消息模型
WorkQueue模型
一个生产者,多个消费者
存在生产预取机制
举例:生产者生产50条消息,消费者A和消费者B各自拿到25条;
那么假如A处理效率高,B处理效率低,那么整体效率会收到处理慢的影响;
解决
prefetch参数值设置为1,每次预期只能拿到一个;
那么处理效率高的拿到的消息多,能者多劳;
spring:
rabbitmq:
host: # 主机名
port: 5672 # 端口
virtual-host: / # 虚拟主机
username: # 用户名
password: # 密码
listener:
simple:
prefetch: 1 #每次只能获取一条,能力强的多消费
发布订阅模型
FanoutExchange
发布订阅模型包含交换机、队列
交换机需要绑定队列
我们在java中声明交换机、队列及绑定;
消费者
@Configuration
public class FanoutConfig {
//itcast.fanout
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("itcast.fanout");
}
//fanout.queue1
@Bean
public Queue fanoutQueue1(){
return new Queue("fanout.queue1");
}
//绑定queue1到fanout
@Bean
public Binding fanoutBindQueue1(Queue fanoutQueue1,FanoutExchange fanoutExchange){
return BindingBuilder
.bind(fanoutQueue1)
.to(fanoutExchange);
}
//fanout.queue2
@Bean
public Queue fanoutQueue2(){
return new Queue("fanout.queue2");
}
//绑定queue2到fanout
@Bean
public Binding fanoutBindQueue2(Queue fanoutQueue2,FanoutExchange fanoutExchange){
return BindingBuilder
.bind(fanoutQueue2)
.to(fanoutExchange);
}
生产者
生产者指定交换机和消息;
//fanout
@Test
public void testFanoutExchange()throws InterruptedException{
String exchangeName = "itcast.fanout";//交换机name
String msg = "hello,every~queue";
rabbitTemplate.convertAndSend(exchangeName,"",msg);
}
DirectExchage
相较于FanoutExchange,这里添加了Routingkey,我们将消息路由到指定的routingKey的消息
消费者
我们的RabbitListener中的key如果包含routingKey,那么就能收到消息
//DirectExchange
@RabbitListener(bindings=@QueueBinding(
value = @Queue(name = "direct.queue1"),
exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
key = {"red","blue"}
))
public void listenerDirectorQueue1(String msg){
System.out.println("消费者接收到direct.queue的消息【"+msg+"】");
}
@RabbitListener(bindings=@QueueBinding(
value = @Queue(name = "direct.queue2"),
exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
key = {"red","yellow"}
))
public void listenerDirectorQueue2(String msg){
System.out.println("消费者接收direct.queue的消息【"+msg+"】");
}
生产者
生产者发送消息时指定routingKey、交换机、消息;
@Test
public void testDirect()throws InterruptedException{
String exchangeName = "itcast.direct";//交换机name
String routingKey="red";
String msg = "hello,every~queue";
rabbitTemplate.convertAndSend(exchangeName,routingKey,msg);
}
TopicExchange
TopicExchage相较于DirectExchage的区别在于指定key时更加灵活,可以使用通配符;
比如news.#可以是news.china也可以是news.japan
#.weather可以是china.weather或者japan.weather
消费者
//TopicExchange
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name="topic.queue1"),
exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
key = "#.weather"
))
public void listenTopicQueue1(String msg){
System.out.println("今天天气不错"+msg);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name="topic.queue2"),
exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
key = "news.#"
))
public void listenTopicQueue2(String msg){
System.out.println("今天新闻不错"+msg);
}
生产者
//topic
@Test
public void testTopic()throws InterruptedException{
String exchangeName = "itcast.topic";//交换机name
String routingKey="my.weather";
String msg = "hello,every~queue";
rabbitTemplate.convertAndSend(exchangeName,routingKey,msg);
}