RabbitMQ整个SpringBoot

SpringBoot因其配置简单、快速开发,已经成为热门的开发之一

消息中间件的工作过程可以用生产者消费者模型来表示.即,生产者不断的向消息队列发送信息

而消费者从消息队列中消费信息.具体过程如下:

从上图可看出,对于消息队列来说,生产者,消息队列,消费者是最重要的三个概念

生产者发消息到消息队列中去,消费者监听指定的消息队列,并且当消息队列收到消息之后,

接收消息队列传来的消息,并且给予相应的处理.消息队列常用于分布式系统之间互相信息的传递.

使用SpringBoot进行整合RabbitMQ

1.pom文件的引入

这是操作RabbitMQ的starter必须要进行引入的

     <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

2.配置文件进行基础的配置

spring.rabbitmq.virtual-host=/user
spring.rabbitmq.port=5672
spring.rabbitmq.password=user
spring.rabbitmq.username=user
spring.rabbitmq.host=192.168.43.157

RabbitMQ的模式

1、direct模式

配置Queue(消息队列).那注意由于采用的是Direct模式,需要在配置Queue的时候,指定一个键

使其和交换机绑定.

DirectQueue.java
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class DirectQueue {//若队列不存在则进行创建队列   //返回的是队列名字@Beanpublic Queue queue(){return new Queue("direct_queue");}
}

消息生产者

Sender.java 
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class Sender {@Autowiredprivate AmqpTemplate amqpTemplate;public void send(){String msg = "direct_queue";User user = new User();user.setName("MrChegns");user.setAge(12);amqpTemplate.convertAndSend("direct_queue",user);}}

此时发送的消息是一个User类型的对象

对于发送对象需要实现序列化接口

User.java 
package com.cr.rabbitmqs.direct;
import java.io.Serializable;
public class User implements Serializable {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public User(String name, int age) {this.name = name;this.age = age;}public User() {}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +'}';}
}

消费者

Receive.java 
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class Receive {//对队列进行监听   //同时可以监听多个队列   @RabbitListener(queues = "direct_queue")public void listen(User msg){System.out.println(msg);}
}

测试:

 @Autowiredprivate  Sender sender;@Testpublic void test1(){sender.send();}

得到的结果i:

2、topic模式

首先我们看发送端,我们需要配置队列Queue,再配置交换机(Exchange)

再把队列按照相应的规则绑定到交换机上

Topic.java
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;@Configuration
public class Topic {//创建队列@Bean(name = "message")public Queue Aqueue(){return  new Queue("message.topic");}@Bean(name = "message1")public Queue BQueue(){return  new Queue("message.topics");}//交换机//若不存在则进行创建交换机
    @Beanpublic TopicExchange exchange(){return new TopicExchange("topic_exchange");}//交换机和队列进行绑定@BeanBinding bindingExchangeTopic(@Qualifier("message")Queue message,TopicExchange exchange){return BindingBuilder.bind(message).to(exchange).with("message.topic");}@BeanBinding bindingExchangeTopics(@Qualifier("message1")Queue message,TopicExchange exchange){return BindingBuilder.bind(message).to(exchange).with("message.#");}
}

消费者

Receive1.java 
import com.cr.rabbitmqs.direct.User;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class Receive1 {
@RabbitListener(queues = "message.topic")public void tes(User user){System.out.println( "user1111:" + user);}
}

Receive2.java 
import com.cr.rabbitmqs.direct.User;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
public class Receive2 {  @RabbitListener(queues = "message.topics")public void tes(User user){System.out.println("user222:" + user);}
}

消息生产者:

TopicSend.java 
import com.cr.rabbitmqs.direct.User;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class TopicSend {@Autowiredprivate AmqpTemplate amqpTemplate;//发送消息public void send(){User user = new User("name",12);amqpTemplate.convertSendAndReceive("topic_exchange","message.dev",user);}//发送消息public void send1(){
   User user = new User("name",12);
   amqpTemplate.convertSendAndReceive("topic_exchange","message.topic",user ); 
} }

在开发中这种模式的使用还是相对比较多的,此时测试的是两种方法

一个方法所有的队列都可以进行获取

一个方法只有一个队列可以获取到消息

测试:

    @Autowiredprivate TopicSend topicSend;@Testpublic  void ttt(){topicSend.send();}

测试:

    @Autowiredprivate TopicSend topicSend;@Testpublic  void ttt(){topicSend.send1();}

后台查看交换机和队列的绑定关系以机相关的路由键

3、fanout

那前面已经介绍过了,Fanout Exchange形式又叫广播形式,因此我们发送到路由器的消息会使

得绑定到该路由器的每一个Queue接收到消息,这个时候就算指定了Key,或者规则(即上文中

convertAndSend方法的参数2),也会被忽略!那么直接上代码,发送端配置如下:

Fanout.java

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class Fanout {//队列//如果队列不存在会自动创建队列@Beanpublic Queue queueA(){return new Queue("queueA");}
@Beanpublic Queue queueB(){return new Queue("queueB");}@Beanpublic Queue queueC(){return new Queue("queueC");}//交换机//如果交换机不存在会自动创建队列
    @Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange("fanoutExchange");}//将交换机和队列进行绑定
    @BeanBinding bindingExchangequeueA(Queue queueA,FanoutExchange fanoutExchange){return BindingBuilder.bind(queueA).to(fanoutExchange);}@BeanBinding bindingExchangequeueB(Queue queueB,FanoutExchange fanoutExchange){return BindingBuilder.bind(queueB).to(fanoutExchange);}@BeanBinding bindingExchangequeueC(Queue queueC,FanoutExchange fanoutExchange){return BindingBuilder.bind(queueC).to(fanoutExchange);}
}

消费者:

FanoutReceive.java

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
//监听器
@RabbitListener(queues = "queueA")
public class FanoutReceive {//监听的方法@RabbitHandlerpublic void listen(String  msg){System.out.println("queueA" + msg);}}

FanoutSender.java

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class FanoutSender {    @Autowiredprivate AmqpTemplate amqpTemplate;//发送消息public void send(){String  msg = "test fanout....";//发送消息:参数依次是  交换机名字--路由键(此时设置路由键没有作用)--消息amqpTemplate.convertAndSend("fanoutExchange","",msg);}
}

测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class BpptandrabbitmqApplicationTests {//测试fanout
    @Autowiredprivate FanoutSender fanoutSender;@Testpublic void fanout() {fanoutSender.send();}}

此时3个队列都能接收到消息

交换机、队列以及路由键

转载于:https://www.cnblogs.com/Mrchengs/p/10539003.html

10-RabbitMQ-整合SpringBoot相关推荐

  1. [RabbitMQ]整合SpringBoot

    整合SpringBoot 创建项目 引入依赖 <dependencies><!--RabbitMQ 依赖--><dependency><groupId> ...

  2. RabbitMQ整合SpringBoot(web)

    一.index.html <!DOCTYPE html> <html lang="en"> <head><meta charset=&qu ...

  3. SpringBoot整合 ActiveMQ、SpringBoot整合RabbitMQ、SpringBoot整合Kafka

    1.概念:SpringBoot 整合消息服务2.具体内容对于异步消息组件在实际的应用之中会有两类:· JMS:代表作就是 ActiveMQ,但是其性能不高,因为其是用 java 程序实现的:· AMQ ...

  4. RabbitMQ教程_5 整合SpringBoot

    https://gitee.com/fakerlove/rabbitmq 文章目录 5. 整合SpringBoot 5.1 helloword 模型 引入依赖 创建生产者 创建消费者 目录结构 5.2 ...

  5. rabbitmq消息队列入门到整合springboot(篇幅较长内容详细)

    1.安装rabbitmq服务器 我们选择在linux下安装 安装的前提需要在虚拟机下安装docker docker pull rabbitmq:management(拉去镜像) docker run ...

  6. springboot+rabbitMq整合开发实战一

    springboot+rabbitMq整合开发实战一 消息队列mq相信诸位都听过,甚至还用的相当娴熟,我也是近来才接触,个人感觉相当不错,特别是在业务模块的开发中进行异步解耦有很大的作用.这篇博文主要 ...

  7. 本地缓存Caffeine详解+整合SpringBoot的@EnableCaching

    目录 前言: Caffeine详解 加载策略 同步 异步,即多线程加载 回收策略 回收策略-数量 回收策略-权重 回收策略-时间 回收策略-软引用/弱引用 移除监听 统计 整合SpringBoot @ ...

  8. 使用Gradle整合SpringBoot+Vue.js-开发调试与打包

    为什么80%的码农都做不了架构师?>>>    非常感谢两位作者: kevinz分享的文章<springboot+gradle+vue+webpack 组合使用> 首席卖 ...

  9. java kafka client_Kafka Java Client基本使用及整合SpringBoot

    kafka-clients 添加依赖 org.apache.kafka kafka-clients 2.5.0 消费者 Consumer 代码上总体可以分为三部分:消费者的配置消费者的配置在 org. ...

  10. mall整合SpringBoot+MyBatis搭建基本骨架

    本文主要讲解mall整合SpringBoot+MyBatis搭建基本骨架,以商品品牌为例实现基本的CRUD操作及通过PageHelper实现分页查询. mysql数据库环境搭建 下载并安装mysql5 ...

最新文章

  1. JavaWeb--过滤器
  2. 记忆网络RNN、LSTM与GRU
  3. python tkinter 下拉框_python中tkinter入门之Menu创建顶级菜单、下拉菜单和弹出菜单。...
  4. 在浙学计算机基础2020答案,浙江大学2020年硕士研究生复试分数线的基本要求
  5. 6月第2周回顾:雅虎收购案谈崩 中国***成焦点
  6. [react-router] 请你说说react的路由是什么?
  7. 飞鸽转载异步操作(二)
  8. python入门教程 非常详细-Python编程入门教程:从入门到高级,非常详细
  9. 触发器和存储过程的使用
  10. excel下拉公式保持一些参数不变
  11. altium怎么锁定_在AD软件中的锁定与解锁命令应该如何使用?
  12. 心理学与生活《感知与记忆》
  13. 写出一个类People,并由该类做基类派生出子类Employee和Teacher
  14. Java基于局域网(LAN)的聊天室软件-内附源码
  15. 【复现笔记】Iterative Corresponding Geometry
  16. ACPI Spec Chapter 10 Power Source And Power Meter Devices
  17. Linux基础:文件类型
  18. PPPOE开机自动拨号
  19. HTML相玲选择器,CSS 相邻元素选择器
  20. JS 特性:可选链(?.)

热门文章

  1. php 加载慢,PHP版网站缓存加快打开速度的方法分享
  2. linux ftp mysql_linux搭建ftp服务——未连接mysql数据库的做法
  3. 盈建科弹性板6计算_YJK参数设置详细解析
  4. tomcat连接oracle非常慢,关于myEclipse中tomcat 6.0启动慢的有关问题
  5. 20200802:力扣200周周赛题解
  6. 20190824:(leetcode习题)报数
  7. jquery中checkbox赋值
  8. EasyUI中combotree 研究
  9. 如何将excel里的数据批量导入ACCESS,要用vb代码?
  10. 海外同行首次大规模声援996.ICU,微软和GitHub员工签署联名信,一夜4700星