在上一篇随笔中我们认识并安装了RabbitMQ,接下来我们来看下怎么在Spring Boot 应用中整合RabbitMQ。

先给出最终目录结构:

搭建步骤如下:

  1. 新建maven工程amqp
  2. 修改pom文件,引入spring-boot-starter-amqp和spring-boot-starter-test
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sam</groupId>
    <artifactId>amqp</artifactId>
    <version>0.0.1-SNAPSHOT</version> <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.RELEASE</version>
    </parent> <properties>
    <javaVersion>1.8</javaVersion>
    </properties>
    <dependencies>
    <!-- 引入amqp依赖,它能很好的支持RabbitMQ -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <!-- 引入test依赖,这次需要用到JUnit -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    </dependencies>
    </project>
  3. 新建application.properties配置文件,主要就是配置下连接RabbitMQ的信息:
    spring.application.name=rabbitmq-hello
    #config rabbitmq info
    spring.rabbitmq.host=localhost
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest
  4. 新建启动类,这里没什么特殊的,就是普通的spring boot启动类
    /**
    * 这里没什么特殊的地方,就是普通的spring boot 配置
    *
    */
    @SpringBootApplication
    public class RabbitMQApp { public static void main(String[] args) {
    SpringApplication.run(RabbitMQApp.class, args);
    }
    }
  5. 创建生产者类,通过AmqpTemplate实现消息的发送,AmqpTemplate接口定义了一套针对AMQP协议的基础操作。在Spring Boot中会根据配置来注入具体的实现。这里我们会产生一个字符串,并发送到名为hello的队列中。
    @Component
    public class Sender { @Autowired
    AmqpTemplate rabbitmqTemplate; /**
    * 发送消息
    */
    public void send() {
    String content = "Sender says:" + "'hello, I'm sender'";
    System.out.println(content);
    rabbitmqTemplate.convertAndSend("hello", content);
    }
    }
  6. 创建消费者类,需要用到@RabbitListener来定义对hello队列的监听,并用@RabbitHandler注解来指定对消息处理的方法。我们这里实现了对hello队列的消费。
    /**
    * 通过@RabbitListener对hello队列进行监听
    *
    */
    @Component
    @RabbitListener(queues="hello")
    public class Receiver { /**
    * 通过@RabbitHandler声明的方法,对hello队列中的消息进行处理
    */
    @RabbitHandler
    public void receiver(String str) {
    System.out.println("Receiver says:[" + str + "]");
    }
    }
  7. 编写RabbitMQ的配置类,配置类可以配置队列、交换器、路由等高级信息。我们这里为了简单,只配置队列,其他的采用默认配置。
    /**
    * rabbitmq配置类,
    * 为了简单,我们这里只配置了Queue
    * 至于exchanges、brokers等用的默认配置
    *
    */
    @Configuration
    public class RabbitConfig { @Bean
    public Queue helloQueue() {
    return new Queue("hello");
    } }
  8. 编写测试类,用来调用消息生产者
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes=RabbitMQApp.class)
    public class HelloTest { @Autowired
    private Sender sender; /**
    * 调用生产者进行消息发送
    */
    @Test
    public void hello() throws Exception{
    sender.send();
    }
    }
  9. 运行启动类,启动后控制台会有下面的提示内容:
  10. 执行测试类,在测试类的控制台会打印我们打的log内容

  

  

  切换到amqp应用的控制台,能看到打印:

  

  

  在管理页面中我们能看到Connections和Channels中包含了当前连接的条目:

在整个生产和消费的过程中,生产和消费是一个异步操作,这是分布式系统中要使用消息代理的重要原因。

RabbitMQ入门:在Spring Boot 应用中整合RabbitMQ的更多相关文章

  1. Spring Boot入门 and Spring Boot与ActiveMQ整合

    1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...

  2. Kafka 入门和 Spring Boot 集成

    目录 Kafka 入门和 Spring Boot 集成 标签:博客 概述 应用场景 基本概念 基本结构 和Spring Boot 集成 集成概述 集成环境 kafka 环境搭建 Spring Boot ...

  3. spring boot / cloud (九) 使用rabbitmq消息中间件

    spring boot / cloud (九) 使用rabbitmq消息中间件 前言 rabbitmq介绍: RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.它可以用于大型软件系统 ...

  4. Spring Boot 2.x整合Redis

    最近在学习Spring Boot 2.x整合Redis,在这里和大家分享一下,希望对大家有帮助. Redis是什么 Redis 是开源免费高性能的key-value数据库.有以下的优势(源于Redis ...

  5. spring boot 2.0 整合 elasticsearch6.5.3,spring boot 2.0 整合 elasticsearch NoNodeAvailableException

    原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException 原文说的有点问题,下面贴出我的配置: 原码云项目地址:https://gi ...

  6. Spring Boot和Dubbo整合

    provider端 POM依赖 <dependencies> <dependency> <groupId>org.springframework.boot</ ...

  7. 转-Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合

    Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合 http://blog.csdn.net/balabalayi/article/detai ...

  8. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  9. Spring Boot项目中使用Mockito

    本文首发于个人网站:Spring Boot项目中使用Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试 ...

随机推荐

  1. POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)

    传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submi ...

  2. HDU 1059(多重背包加二进制优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1059 Dividing Time Limit: 2000/1000 MS (Java/Others)    Me ...

  3. sharepoint 配置个人网站容量

    we have a SharePoint 2013 Standard edition implementation and 80 users. We are now starting to use M ...

  4. Python 学习笔记(十三)Python函数(一)

    函数基础 函数:函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print().可以自己创建函数,这 ...

  5. Angular7教程-05-搭建项目环境

    1. 本节说明 本节以及后面的内容我们将会通过搭建一个简单的博客程序来对angular进行介绍,项目使用前端框架是bootstrap.版本v3.3.7,另外需要安装jquery.关于bootstrap ...

  6. c++高精度计算(加法)

    本文提供给刚入坑的新手 关于高精度的计算网上百度一下可以了解到许多 今天我分享的只是一些自己的心得,更详细的可以去看原博主的原创文章(https://blog.csdn.net/fanyun_01/a ...

  7. vue-cli 项目安装失败 tunneling socket could not be established, cause=connect ECONNREFUSED

    1.安装vue-cli npm install vue-cli -g 2.初始化项目 vue init webpack project 此时报错:vue-cli · Failed to downloa ...

  8. Redis 之武林大会 - 哨兵(Sentinel)

    前言 Redis在出从复制的模式下,一旦主节点由于故障不能提供服务,需要人工降从节点晋升为主节点,同时还要通知应用方更新主节点的地址,在很多应用场景下,这样的故障处理方式是无法被接受的.不过幸运的是R ...

  9. Redis学习笔记(二)

    解读Retwis官网例子 Redis需要考虑需要哪些keys以及对应的value使用合适的数据类型进行存储.在retwis例子中,我们需要users,user的粉丝列表, user的关注用户列表等等. ...

  10. ThinkPHP5.1 前置操作说明

    可以为某个或者某些操作指定前置执行的操作方法,设置 beforeActionList属性可以指定某个方法为其他方法的前置操作,数组键名为需要调用的前置方法名,无值的话为当前控制器下所有方法的前置方法. ...