目录
目录
1、基本收发
引入spring-boot-starter-amqp依赖
ymal文件写rabbitmq服务器信息
使用RabbitTemplate工具类
消息发送:
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
void testSendMessage2Queue(){
String queueName = "simple.queue";
String msg = "hello";
rabbitTemplate.convertAndSend(queueName,msg);
}
消息接收:使用注解@RabbitListener
@Slf4j
@Component
public class MqListen {
@RabbitListener(queues = "simple.queue")
public void listenSimpleQueue(String msg){
System.out.println("消费者收到消息:"+msg);
}
}
2、Work Queues
增加prefetch参数
修改后可以让处理快的消费者处理更多信息
@RabbitListener(queues = "work.queue")
public void listenWorkQueue1(String msg) throws InterruptedException {
System.out.println("消费者1收到消息:"+msg);
Thread.sleep(20);
}
@RabbitListener(queues = "work.queue")
public void listenWorkQueue2(String msg) throws InterruptedException {
System.err.println("消费者2收到消息:"+msg);
Thread.sleep(100);
}
Work模型的使用
- 多个消费者绑定到一个队列,可以加快消息处理速度
- 同一条消息只会被一个消费者处理
- 通过设置prefetch来控制消费者预取的消息数量,处理完一条再处理下一条,实现能者多劳
3、Fanout(广播)交换机
将接收到的消息广播到每一个queue
向交换机发送消息
@Test
void testSendFanout() throws InterruptedException {
String exchangeName = "hmall.fanout";
String msg = "test";
rabbitTemplate.convertAndSend(exchangeName,null,msg);
}
4、Direct(定向)交换机
bindingkey相同则广播
绑定时指定key
@Test
void testSendDirect() throws InterruptedException {
String exchangeName = "hmall.direct";
String msgRed = "red";
String msgBlue = "blue";
String msgYellow = "yellow";
rabbitTemplate.convertAndSend(exchangeName,"red",msgRed);
rabbitTemplate.convertAndSend(exchangeName,"blue",msgBlue);
rabbitTemplate.convertAndSend(exchangeName,"yellow",msgYellow);
}
5、Topic(话题)交换机
@Test
void testSendTopic() throws InterruptedException {
String exchangeName = "hmall.topic";
String msgchina = "china message";
String msgnews = "news";
String msgYellow = "wrong";
rabbitTemplate.convertAndSend(exchangeName,"china.message",msgchina);
rabbitTemplate.convertAndSend(exchangeName,"japan.news",msgnews);
rabbitTemplate.convertAndSend(exchangeName,"japan.message",msgYellow);
}
6、声明队列和交换机——方法一
@Configuration
public class FanoutConfiguration {
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("hmall.fanout2");
}
@Bean
public Queue fanoutQueen3(){
return new Queue("fanout.queue3");
}
@Bean
public Queue fanoutQueen4(){
return new Queue("fanout.queue4");
}
@Bean
public Binding fanoutBinding3(){
return BindingBuilder.bind(fanoutQueen3()).to(fanoutExchange());
}
@Bean
public Binding fanoutBinding4(){
return BindingBuilder.bind(fanoutQueen4()).to(fanoutExchange());
}
7、声明队列和交换机——方法二
基于注解
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct.queue1",durable = "true"),
exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
key = {"red","blue"}
))
public void listenDirectQueue1(String msg) throws InterruptedException {
System.out.println("消费者1收到direct.queue1消息:"+msg);
//Thread.sleep(20);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct.queue2",durable = "true"),
exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
key = {"red","yellow"}
))
public void listenDirectQueue2(String msg) throws InterruptedException {
System.out.println("消费者1收到direct.queue2消息:"+msg);
//Thread.sleep(20);
}
8、消息转换器
json引入依赖 实现消息转换器
<!-- Jackson依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.13.5</version>
</dependency>
@Bean
public MessageConverter jacksonMessageConverter() {
return new Jackson2JsonMessageConverter();
}