springboot使用rabbitmq-Topic模式,亲自实测能用!!!
0.项目目录截图
=====================================================================
springboot的版本:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
==========================================================================================================================================
引入jar包:
<!-- springboot整合rabbitMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
========================================================================================================================
使用junit5做测试.
========================================================================================================================
1.application.yml配置文件
#SpringBoot整合RabbitMQ
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#connection-timeout: 15000
2.RabbitMqConfig.java
package com.nantian.rabbitmqtopic;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by Administrator on 2018/4/10.
* 1. yaml
* 2. RabbitMqConfig
* a 创建queue
* b. 创建交换机TopicExchange
* c. 根据绑定规则将队列绑定到相应的交换机上
* 3. sender
* 4. test
* 5. receiver
*/
@Configuration
public class RabbitMqConfig {
// a 创建queue
@Bean(name = "message") // 指定该参数名是message 见下面Qualifier("message")
public Queue queueMessage(){
return new Queue("topic.message"); // topic.message 是rounting-key,匹配规则
}
@Bean(name = "messages")
public Queue queueMessages(){
return new Queue("topic.messages");
}
// b. 创建交换机TopicExchange
@Bean
public TopicExchange exchange(){
return new TopicExchange("topicExchange");
}
/*
c. 根据绑定规则将队列绑定到相应的交换机上(bindingKey)--Exchange Queues
*/
@Bean
public Binding bindingExchangeMessage(@Qualifier("message") Queue queueMessage,TopicExchange exchange){
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
}
@Bean
/**
* 将队列"messages" 绑定到交换机上,绑定规则是 topic.messages
*
*/
public Binding bindingExchangeMessages(@Qualifier("messages")Queue queueMessages,TopicExchange exchange){
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
}
}
3.发送者:TopicSender.java
package com.nantian.rabbitmqtopic;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2018/4/10.
*/
@Component
public class TopicSender {
@Autowired
private AmqpTemplate amqpTemplate;
/**
* 交换机 rountingkey 发送的内容 发送消息到相应的Exchange
* 向名称为 topicExchange 的交换机 发送满足routingkey规则为 "topic.messages " 的内容为 "我是发送消息的内容" 的消息
*/
public void sendMessage(){
this.amqpTemplate.convertAndSend("topicExchange","topic.messages","我是发送消息的内容! ");
}
/**
* 消息一
*/
public void send1() {
String context = "hi, i am message 1";
System.out.println("Sender : " + context);
this.amqpTemplate.convertAndSend("topicExchange", "topic.message", context);
}
/**
* 消息2
*/
public void send2() {
String context = "hi, i am messages 2";
System.out.println("Sender : " + context);
this.amqpTemplate.convertAndSend("topicExchange", "topic.messages", context);
}
}
4.接受者:TopicReceiver.java
package com.nantian.rabbitmqtopic;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2018/4/10.
*/
@Component
public class TopicReceiver {
@RabbitListener(queues ="topic.message" )
public void receiveMessage1(String str){
System.out.println("赵云1:我是监听topic.message的,仅满足topic.message的过来 , "+str);
}
@RabbitListener(queues ="topic.messages" )
public void receiveMessage2(String str){
System.out.println("关羽2:我是监听topic.# 的,满足 topic.# 的都过来 , "+str);
}
}
5.测试类:TestTopicRabbitMq.java
package com.nantian;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.nantian.rabbitmqtopic.TopicSender;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestTopicRabbit {
@Autowired
private TopicSender topicSender;
@Test
public void contextLoads() {
topicSender.sendMessage();
}
/**
* 测试消息一
*/
@Test
public void send1() {
topicSender.send1();
}
/**
* 测试消息二
*/
@Test
public void send2() {
topicSender.send2();
}
}
springboot使用rabbitmq-Topic模式,亲自实测能用!!!的更多相关文章
- 干货!基于SpringBoot的RabbitMQ多种模式队列实战
目录 环境准备 安装RabbitMQ 依赖 连接配置 五种队列模式实现 1 点对点的队列 2 工作队列模式Work Queue 3 路由模式Routing 4 发布/订阅模式Publish/Subsc ...
- SpringBoot之RabbitMQ的使用
一 .RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件,消息中间件的工作过程可以用生产者消费者模型来表示.即,生产者不断的向消息队列发 ...
- springboot学习笔记-6 springboot整合RabbitMQ
一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...
- SpringBoot整合RabbitMQ实现六种工作模式
RabbitMQ主要有六种种工作模式,本文整合SpringBoot分别介绍工作模式的实现. 前提概念 生产者 消息生产者或者发送者,使用P表示: 队列 消息从生产端发送到消费端,一定要通过队列转发,使 ...
- RabbitMQ入门-Topic模式
上篇<RabbitMQ入门-Routing直连模式>我们介绍了可以定向发送消息,并可以根据自定义规则派发消息.看起来,这个Routing模式已经算灵活的了,但是,这还不够,我们还有更加多样 ...
- spring boot整合RabbitMQ(Topic模式)
1.Topic交换器介绍 Topic Exchange 转发消息主要是根据通配符. 在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发 ...
- demo rabbitmq topic(主题模式),fanout(广播模式),轮询分发,确认接收Ack处理
//durable = true 代表持久化 交换机和队列都要为true ,持久代表服务重启,没有处理的消息依然存在 //topic 根据不同的routkey 发送和接收信息 //fanout 广播模 ...
- RabbitMQ广播:topic模式
topic模式跟direct差不多,只是把type改一下就行. direct是把固定的routing_key跟queue绑定,topic是把模糊的routing_key跟queue绑定 原理图: 发布 ...
- SpringBoot集成RabbitMQ
官方说明:http://www.rabbitmq.com/getstarted.html 什么是MQ? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ ...
随机推荐
- python 之 面向对象(多态性、装饰器方法 内置函数补充)
7.6 多态性 1 什么是多态性 多态指的是同一种事物多种形态,在程序中用继承可以表现出多态.多态性:可以在不用考虑对象具体类型的前提下而直接使用对象下的方法 2.为什要用多态 用基类创建一套统一的规 ...
- Vector、ArrayList异同和HTTP请求异同的概括和区别
今天我所记录的是两个异同的概括: HTTP: 同步请求:提交请求->等待服务器处理->处理完毕返回给客户端 这个期间客户端浏览器只能处于等待状态,得到回应才可以执行下一步操作. 异步请求 ...
- session和cookie有什么区别?
1.存储位置不同 cookie的数据信息存放在客户端浏览器上. session的数据信息存放在服务器上. 2.存储容量不同 单个cookie保存的数据<=4KB,一个站点最多保存20个Cooki ...
- 玩转【Mock.js】,前端也能跑的很溜
现在开发已经是前后端分离了,前端和后端可以同时进行开发,互不影响,但是有些时候后端开发的接口慢于前端,导致前端需要等待后端的接口完成才能完成前后端对接,为了解决这个痛点,出现了模拟接口数据的方案,目前 ...
- C# 用Redis实现的分布式锁
Redis实现分布式锁(悲观锁/乐观锁) 对锁的概念和应用场景在此就不阐述了,网上搜索有很多解释,只是我搜索到的使用C#利用Redis的SetNX命令实现的锁虽然能用,但是都不太适合我需要的场景. 基 ...
- Springboot笔记01——Springboot简介
一.什么是微服务 在了解Springboot之前,首先我们需要了解一下什么是微服务. 微服务是一种架构风格(服务微化),是martin fowler在2014年提出来的.微服务简单地说就是:一个应用应 ...
- JAVA 插入注解处理器
JDK1.5后,Java语言提供了对注解(Annotation)的支持 JDK1.6中提供一组插件式注解处理器的标准API,可以实现API自定义注解处理器,干涉编译器的行为. 在这里,注解处理器可以看 ...
- 弹性布局flex 介绍
摘自:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模 ...
- FICO-错误日志集
1.F-02报错 System error in routine FI_TAX_CHK_PRICING_DATA error code 13 function builder TAX2 程序 FI_T ...
- win10 下的YOLO v3 的编译与使用
部署环境:win10 +CUDA 10.0 + vs2017 + opencv 3.4.0 代码版本是 https://github.com/AlexeyAB/darknet 1.初始准备 (1)下 ...