当前位置: 首页 > news >正文

RabbitMQ创建交换机和队列——配置类 注解

交换机的类型

Fanout:广播,将消息交给所有绑定到交换机的队列。

Direct:订阅,基于RoutingKey(路由key)发送给订阅了消息的队列。

Topic:通配符订阅,与Direct类似,只不过RoutingKey可以使用通配符(# (一个或多个单词)和 * (一个单词))。

Headers:头匹配,基于MQ的消息头匹配,用的较少。

准备

导入依赖:

        <!--AMQP依赖,包含RabbitMQ--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><!--单元测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency>

配置文件:

spring:rabbitmq:host: ****** # 你的虚拟机/服务器IPport: 5672 # 端口virtual-host: **** # mq虚拟主机username: *** # 用户名password: *** # 密码

这里用direct类型的交换机举例:

基于配置类

步骤一:配置类中创建交换机和队列的Bean,并设置绑定关系,设置routingKey为sdg

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class FanoutConfiguration {@Beanpublic DirectExchange directExchange() {return new DirectExchange("exchange01.direct");//或者return ExchangeBuilder.directExchange("exchange01.direct").build();}@Beanpublic Queue queue() {return new Queue("direct.queue01");}@Beanpublic Binding bind01(DirectExchange directExchange, Queue queue){return BindingBuilder.bind(queue).to(directExchange).with("sdg");}
}

步骤二:发送者发送消息

    @Testpublic void test5() {String exchange = "exchange01.direct";String message = "Hello World!";rabbitTemplate.convertAndSend(exchange,"sdg",message);}

 步骤三:消费者消费消息

package com.itheima.consumer.mq;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Slf4j
@Component
public class SpringRabbitListener {@RabbitListener(queues = "direct.queue01")public void queue01Listener(String msg) {System.out.println("收到消息: "+msg);}
}

 direct模式由于要绑定多个KEY,每一个Key都要编写一个binding,会非常麻烦,基于配置类适用与简单的情况,所以我们就可以基于注解来声明交换机、队列和绑定关系

基于注解

消费者:

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class SpringRabbitListener {@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue01"),exchange = @Exchange(name = "htsdg.direct",type = ExchangeTypes.DIRECT),key = {"sdg","ht"}))//默认类型为directpublic void queue01Listener(String msg) {System.out.println("收到消息: "+msg);}
}

再来一个topic类型的:

发送者:

    @Testpublic void test5() {String exchange = "htsdg.direct";String message = "Hello World!";rabbitTemplate.convertAndSend(exchange,"china.qianXueSen",message);}

消费者:

@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),exchange = @Exchange(name = "htsdg.topic", type = ExchangeTypes.TOPIC),key = "china.#"
))
public void listenTopicQueue1(String msg){System.out.println("消费者1接收到topic.queue1的消息:【" + msg + "】");
}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),exchange = @Exchange(name = "htsdg.topic", type = ExchangeTypes.TOPIC),key = "#.news"
))
public void listenTopicQueue2(String msg){System.out.println("消费者2接收到topic.queue2的消息:【" + msg + "】");
}

http://www.lryc.cn/news/437415.html

相关文章:

  • proteus+51单片机+AD/DA学习5
  • 【Python机器学习】长短期记忆网络(LSTM)
  • 【Go】使用Goland创建第一个Go项目
  • STM32学习笔记(一、使用DAP仿真器下载程序)
  • 储能运维管理云平台解决方案EMS能量管理系统
  • 网络药理学:16、速通流程版
  • P2515 [HAOI2010] 软件安装
  • 51单片机快速入门之定时器和计数器
  • 【计算机网络 - 基础问题】每日 3 题(一)
  • Unity全面取消Runtime费用 安装游戏不再收版费
  • IDEA测试类启动报 “java: 常量字符串过长” 解决办法
  • 计算机科学基础 -- 访存单元
  • Linux压缩、解压缩、查看压缩内容详解使用(tar、gzip、bzip2、xz、jar、war、aar)
  • StreamReader 和 StreamWriter提供自动处理字符编码的功能
  • Gitlab备份、迁移、恢复和升级(Gitlab Backup, migration, recovery, and upgrade)
  • MySQL:INSERT command denied to user
  • 【Android安全】Ubuntu 16.04安装GDB和GEF
  • ISO 21434与网络安全管理系统(CSMS)的协同作用
  • Vue 67 vuex 四个map方法的使用
  • Unity自带脚本之GameObject脚本
  • 软件测试面试题-自测
  • 深度学习-神经网络
  • Redis - 集群篇 - 集群模式
  • Robot Operating System——线速度和角速度
  • 量化投资策略_因子打分选股的案例实现
  • 架构师知识梳理(七):软件工程-工程管理与开发模型
  • bp的模块被隐藏了
  • C++学习笔记(21)
  • Ubuntu系统入门指南:常用命令详解
  • keep-alive缓存不了iframe