当前位置: 首页 > 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.mrgr.cn/news/22943.html

相关文章:

  • table标签里不能包含div标签?居然因为它!!!
  • Error mongodb connect: 使用Mongoose连不上mongodb官方数据库
  • 阿里云服务器K8S安装教程
  • 重磅发布!《人工智能安全治理框架》1.0版来了
  • 深圳MES系统在制造业的应用与发展
  • 三十四、模型绑定与验证
  • RedisTemplate操作Redis
  • C++复习day05
  • Python列表浅拷贝的陷阱与破解之道
  • 29个横幅广告及详细点评,帮您优化广告效果
  • 苹果账号登录后端验证两种方式 python2
  • Qt工程使用MQTT-C库与mqtt服务器数据通信
  • 【C++11 ——— 右值引用和移动语义】
  • 久久派搭建风电系统网站(基于mariadb数据库)
  • PVE动态核显直通
  • 一个数组向左移动i位(学会分析问题,找出规律,不要小看任何一个小程序;小程序都是实现大的功能的基础--体现问题分解的思想)
  • Python数据分析高频面试题及答案
  • 使用gdb跟踪调试linux内核
  • 仕考网:公务员国考考什么?
  • 三维点云骨架提取(以树木为例 python 代码)