springboot2.0应用:springboot2.0整合rabbitmq(代码片段)

yjtz yjtz     2022-12-03     234

关键词:

如何整合RabbitMQ

1、添加spring-boot-starter-amqp

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

2、添加配置

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.dynamic=true
spring.rabbitmq.cache.connection.mode=channel

3、注入队列

@Configuration
public class RabbitConfig 
    @Bean
    public Queue Queue() 
        return new Queue("hello");
    

4、创建操作数据的Repository对象

interface CityRepository extends Repository<City, Long> 

    Page<City> findAll(Pageable pageable);

    Page<City> findByNameContainingAndCountryContainingAllIgnoringCase(String name,
            String country, Pageable pageable);

    City findByNameAndCountryAllIgnoringCase(String name, String country);

5、创建消费者

@Component
public class RabbitConsumer 
    @RabbitHandler
    @RabbitListener(queues = "hello")
    public void process(@Payload String foo) 
        System.out.println(new Date() + ": " + foo);
    

6、启动主类

@SpringBootApplication
@EnableScheduling
public class AmqpApplication 
    public static void main(String[] args) 
        SpringApplication.run(AmqpApplication.class, args);
    

控制台输出:

Sun Sep 30 16:30:35 CST 2018: hello

到此,一个简单的SpringBoot2.0集成RabbitMQ就完成了。
熟悉RabbitMQ的小伙伴们应该知道,RabbitMQ在一般的队列基础上,增加了ExChange的概念。ExChange有四种类型:Direct, Topic, Headers and Fanout。其中Headers实际很少使用,Direct较为简单。接下来将详细介绍如何使用topic和Fanout。

Topic Exchange

1、配置Topic规则

@Configuration
public class TopicRabbitConfig 

    @Bean
    public Queue queueMessage1() 
        return new Queue(MQConst.TOPIC_QUEUENAME1);
    

    @Bean
    public Queue queueMessage2() 
        return new Queue(MQConst.TOPIC_QUEUENAME2);
    

    @Bean
    TopicExchange exchange() 
        return new TopicExchange(MQConst.TOPIC_EXCHANGE);
    

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage1, TopicExchange exchange) 
        // 将队列1绑定到名为topicKey.A的routingKey
        return BindingBuilder.bind(queueMessage1).to(exchange).with(MQConst.TOPIC_KEY1);
    

    @Bean
    Binding bindingExchangeMessages(Queue queueMessage2, TopicExchange exchange) 
        // 将队列2绑定到所有topicKey.开头的routingKey
        return BindingBuilder.bind(queueMessage2).to(exchange).with(MQConst.TOPIC_KEYS);
    

2、配置消费者

@Component
public class TopicConsumer 

    @RabbitListener(queues = MQConst.TOPIC_QUEUENAME1)
    @RabbitHandler
    public void process1(String message) 
        System.out.println("queue:topic.message1,message:" + message);
    

    @RabbitListener(queues = MQConst.TOPIC_QUEUENAME2)
    @RabbitHandler
    public void process2(String message) 
        System.out.println("queue:topic.message2,message:" + message);
    

3、生产消息

在Producer类中添加:

        // Topic
        rabbitTemplate.convertAndSend(MQConst.TOPIC_EXCHANGE, MQConst.TOPIC_KEYS, "from keys");
        rabbitTemplate.convertAndSend(MQConst.TOPIC_EXCHANGE, MQConst.TOPIC_KEY1, "from key1");

再次启动主类,控制台输出:

queue:topic.message2,message:from keys
queue:topic.message1,message:from key1
queue:topic.message2,message:from key1

Fanout Exchange

1、配置Fanout规则

@Configuration
public class FanoutRabbitConfig 
    @Bean
    public Queue MessageA() 
        return new Queue(MQConst.FANOUT_QUEUENAME1);
    

    @Bean
    public Queue MessageB() 
        return new Queue(MQConst.FANOUT_QUEUENAME2);
    

    @Bean
    FanoutExchange fanoutExchange() 
        return new FanoutExchange(MQConst.FANOUT_EXCHANGE);
    

    @Bean
    Binding bindingExchangeA(Queue MessageA, FanoutExchange fanoutExchange) 
        return BindingBuilder.bind(MessageA).to(fanoutExchange);
    

    @Bean
    Binding bindingExchangeB(Queue MessageB, FanoutExchange fanoutExchange) 
        return BindingBuilder.bind(MessageB).to(fanoutExchange);
    

2.配置消费者

@Component
public class FanoutConsumer 

    @RabbitListener(queues = MQConst.FANOUT_QUEUENAME1)
    @RabbitHandler
    public void process1(String message) 
        System.out.println("queue:fanout.message1,message:" + message);
    

    @RabbitListener(queues = MQConst.FANOUT_QUEUENAME2)
    @RabbitHandler
    public void process2(String message) 
        System.out.println("queue:fanout.message2,message:" + message);
    

3、生产消息

在Producer类中添加:

        // FanOut
        rabbitTemplate.convertAndSend(MQConst.FANOUT_EXCHANGE, "", "fanout"); 

再次启动主类,控制台输出:

queue:fanout.message2,message:fanout
queue:fanout.message1,message:fanout

源码地址:GitHub


本篇到此结束,如果读完觉得有收获的话,欢迎点赞、关注、加公众号【贰级天災】,查阅更多精彩历史!!!

技术图片

使用docker构建部署运行springboot应用《springboot2.0极简教程》

使用Docker构建部署运行SpringBoot应用《SpringBoot2.0极简教程》image.pngimage.pngimage.pngimage.png。。。image.pngimage.pngimage.png。。。image.pngimage.pngimage.pngimage.png 查看详情

2018最新springboot2.0教程(零基础入门)

一、零基础快速入门SpringBoot2.01、SpringBoot2.x课程全套介绍和高手系列知识点简介:介绍SpringBoot2.x课程大纲章节java基础,jdk环境,maven基础2、SpringBoot2.x依赖环境和版本新特性说明简介:讲解新版本依赖环境和springboot2新特性概述3... 查看详情

springboot2.0-为用户配置

我有一个应用程序(springboot2),客户希望编辑整数值,它表示AutoGrowCollectionLimit的最大值。默认情况下,此值(根据springdocs)设置为256,这对我们的目的来说还不够。设置属性的代码:@InitBinderpublicvoidinitBinder(WebDataBinderbinder){bi... 查看详情

springboot2.0.0.release中的serverrequest

我最近搬了我的应用程序。从SpringBoot1到SpringBoot2(2.0.0.RELEASE)。<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2 查看详情

springboot2.0源码分析:整合activemq分析(代码片段)

SpringBoot具体整合ActiveMQ可参考:SpringBoot2.0应用(二):SpringBoot2.0整合ActiveMQActiveMQ自动注入当项目中存在javax.jms.Message和org.springframework.jms.core.JmsTemplate着两个类时,SpringBoot将ActiveMQ需要使用到的对象注册为Bean,供项目注入使用... 查看详情

springboot2.0深度实践之核心技术篇

第1章系列总览总览SpringBoot2.0深度实践系列课程的整体议程,包括SpringBoot三大核心特性(组件自动装配、嵌入式Web容?、生产准备特性)、Web应用(传统Servlet、SpringWebMVC、SpringWebFlux)、数据相关(JDBC、JPA、事务)、功能扩展(Sp... 查看详情

零基础快速入门springboot2.0教程

一、SpringBoot2.x使用Dev-tool热部署简介:介绍什么是热部署,使用springboot结合dev-tool工具,快速加载启动应用官方地址:https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#using-boot-devtools核心依赖包:<dependency 查看详情

springboot2.0集成fastdfs

SpringBoot2.0集成FastDFS前两篇整体上介绍了通过Nginx和FastDFS的整合来实现文件服务器。但是,在实际开发中对图片或文件的操作都是通过应用程序来完成的,因此,本篇将介绍SpringBoot整合FastDFS客户端来实现对图片/文件服务器的访... 查看详情

springboot2.0之监控管理

Springboot监控中心: 针对微服务的服务状态,服务器的内存变化(内存、线程、日志管理等)检测服务配置连接地址是否有用(有些懒加载的情况下,用的时候发现卧槽不能用)模拟访问,懒加载。统计有多少个bean(Spring容器... 查看详情

springboot2.0:重磅springboot2.0权威发布

就在昨天SpringBoot2.0.0.RELEASE正式发布,今天早上在发布SpringBoot2.0的时候还出现一个小插曲,将SpringBoot2.0同步到Maven仓库的时候出现了错误,然后SpringBoot官方又赶紧把GitHub上发布的v2.0.0.RELEASE版本进行了撤回。到了下午将问题修复... 查看详情

springboot3.0正式发布,王炸!!

...SpringBoot3.0这是一个重大的主版本更新,距离上一代的SpringBoot2.0的发布已经过去4年多了,SpringBoo 查看详情

springboot2:springboot2.0新特性

SpringBoot2(一):SpringBoot2.0新特性SpringBoot依赖于Spring,而SpringCloud又依赖于SpringBoot,因此SpringBoot2.0的发布正式整合了Spring5.0的很多特性,同样后面SpringCloud最新版本的发布也需要整合最新的SpringBoot2.0内容。一、新版本特性1,基于J... 查看详情

springboot2.0.2教程-目录

SpringBoot2.0.2教程-HelloWorld-01 SpringBoot2.0.2教程-HelloWorld之intellijidea创建web项目-02 SpringBoot2.0.2教程-配置文件application.properties-03 SpringBoot2.0.2教程-日志管理-04  SpringBoot2.0.2教 查看详情

springboot2.0防止xss攻击

一:什么是XSSXSS攻击全称跨站脚本攻击,是一种在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中。你可以自己做个简单尝试: 1.在任何一个表单内,你输入一段简单的js代码:<s... 查看详情

springboot2.0之热部署原理

所谓的热部署:比如项目的热部署,就是在应用程序在不停止的情况下,实现新的部署原理:实用类加载器(classloader重新读取字节码文件到jvm内存)如何纯手写一个热部署功能: 1、监听class文件是否发生改变 版本号、... 查看详情

微服务springboot2.0:配置文件解析

properties我用了好多年,你却让我用yml,这是什么鬼——Java面试必修引言上一篇介绍了SpringBoot的轻松入门项目构建,对SpringBoot的特性有了初步了解。但如果要想玩得很熟练的话就请看接下来的文章,这样有助于后续我们快速的... 查看详情

springboot2.0(一):springboot2.0尝鲜-动态banner

SpringBoot2.0提供了很多新特性,其中就有一个小彩蛋:动态Banner,今天我们就先拿这个来尝尝鲜SpringBoot更换Banner我们先来回顾一下在SpringBoot1.0中如何更换启动Banner,其实都很简单,只需要在src/main/resources路径下新建一个banner.txt... 查看详情

springboot2.0:dockercompose+springboot+nginx+mysql实践

SpringBoot案例首先我们先准备一个SpringBoot使用Mysql的小场景,我们做这样一个示例,使用SpringBoot做一个Web应用,提供一个按照IP地址统计访问次数的方法,每次请求时将统计数据存入Mysql并展示到页面中。配置信息依赖包<dependen... 查看详情