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模式,亲自实测能用!!!的更多相关文章

  1. 干货!基于SpringBoot的RabbitMQ多种模式队列实战

    目录 环境准备 安装RabbitMQ 依赖 连接配置 五种队列模式实现 1 点对点的队列 2 工作队列模式Work Queue 3 路由模式Routing 4 发布/订阅模式Publish/Subsc ...

  2. SpringBoot之RabbitMQ的使用

    一 .RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件,消息中间件的工作过程可以用生产者消费者模型来表示.即,生产者不断的向消息队列发 ...

  3. springboot学习笔记-6 springboot整合RabbitMQ

    一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...

  4. SpringBoot整合RabbitMQ实现六种工作模式

    RabbitMQ主要有六种种工作模式,本文整合SpringBoot分别介绍工作模式的实现. 前提概念 生产者 消息生产者或者发送者,使用P表示: 队列 消息从生产端发送到消费端,一定要通过队列转发,使 ...

  5. RabbitMQ入门-Topic模式

    上篇<RabbitMQ入门-Routing直连模式>我们介绍了可以定向发送消息,并可以根据自定义规则派发消息.看起来,这个Routing模式已经算灵活的了,但是,这还不够,我们还有更加多样 ...

  6. spring boot整合RabbitMQ(Topic模式)

    1.Topic交换器介绍 Topic Exchange 转发消息主要是根据通配符. 在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发 ...

  7. demo rabbitmq topic(主题模式),fanout(广播模式),轮询分发,确认接收Ack处理

    //durable = true 代表持久化 交换机和队列都要为true ,持久代表服务重启,没有处理的消息依然存在 //topic 根据不同的routkey 发送和接收信息 //fanout 广播模 ...

  8. RabbitMQ广播:topic模式

    topic模式跟direct差不多,只是把type改一下就行. direct是把固定的routing_key跟queue绑定,topic是把模糊的routing_key跟queue绑定 原理图: 发布 ...

  9. SpringBoot集成RabbitMQ

    官方说明:http://www.rabbitmq.com/getstarted.html 什么是MQ? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ ...

随机推荐

  1. 基于thymeleaf实现简单登录

    1.引入thymeleaf.静态资源等依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...

  2. Fiddler抓取https原理

    首先fiddler截获客户端浏览器发送给服务器的https请求, 此时还未建立握手.第一步, fiddler向服务器发送请求进行握手, 获取到服务器的CA证书, 用根证书公钥进行解密, 验证服务器数据 ...

  3. 关于Mybatis中mapper.xml的传入参数简单技巧

    由于在做项目的时候,我看见同事使用的传入参数类型各式各样,感觉没规律可言,闲暇的时候我就自己搭建了项目做了一些传入参数的测试(当然其实更好的方式是看源码,但是博主能力有限,毕竟入行没多久,看起来很吃力 ...

  4. JavaScript作用域、作用域链 学习随笔

    (本文是这些知识点的自我理解.写之余从头回顾,加深理解.取得更多收获之用.) 作用域(scope) 程序设计概念,通常来说,一段程序代码中所用到的名字(JS叫标识符(如变量名.函数名.属性名.参数.. ...

  5. 简单记录一次注入到shell

    0x00 前言 帮朋友之前拿的一个站,有点久了没有完整截图,简单记录一下. 0x01 基础信息 操作系统:win 集成环境:phpstudy 端口开放:82,3306,3389 有phpmyadmin ...

  6. Jerry带您了解Restful ABAP Programming模型系列之三:云端ABAP应用调试

    Jerry的Restful ABAP Programming模型介绍系列的前两篇文章: 30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用 Jerry带 ...

  7. Android笔记(二十九) Android中的异步更新(一) Android中的线程

    Java中的线程 1. 线程的两种实现方式 ①继承Thread类 ②实现Runnable接口 两者区别在于,Thread这个类的对象,代表的是一个线程,而Runnable的对象,代表的是线程体(也就是 ...

  8. CSS之属相相关

    一.padding与margin padding:就是内边距的意思,它是边框到内容之间的距离 另外padding的区域是有背景颜色的.并且背景颜色和内容的颜色一样.也就是说background-col ...

  9. Qt中C++与QML交互

    ###main.c部分int main(int argc, char *argv[]){    QString info1 = "xxxxxxxxxxx";    QString ...

  10. k8s的网络

    K8S的网络中主要存在4种类型的通信:   ①同一Pod内的容器间通信 ②各个Pod彼此间的通信 ③Pod和Service间的通信 ④集群外部流量和Service之间的通信   K8S为Pod和Ser ...