SpringBoot与rabbitmq的结合的示例

 更新时间:2018年03月26日 16:50:12   作者:数齐  
这篇文章主要介绍了SpringBoot与rabbitmq的结合的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

消息中间件对于我们系统之间的解耦合,消峰等都有极大的帮助。spring boot 也集成了此部分的内容,集成最为容易的是rabbitmq。今天我们就以rabbitmq为例说明。

老规矩,先看下pom

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

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

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

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>

AMQP,即Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。基于此协议的客户端与消息中间件可传递消息,并不受客户端/中间件不同产品,不同的开发语言等条件的限制,spring-boot-starter-amqp引入的就是rabbitmq。有个前提,你的机子上要首先先安装rabbitmq的server,然后执行 rabbitmq-server server就启动了。启动后,我们就可以配置我们的客户端程序了。首先看下我们的配置文件

spring.application.name: spirng-boot-rabbitmq

spring.rabbitmq.host: 127.0.0.1
spring.rabbitmq.port: 5672
spring.rabbitmq.username: guest
spring.rabbitmq.password: guest

配置了服务器的IP,端口,用户名,密码等基础信息,保证我们能连上服务器。

增加一个Rabbitmq的配置类

package com.shuqi;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

创建了一个名称叫做hello的队列,然后producer可以往hello的队列里放数据,consumer可以从hello的队列里消费数据。看下producer的处理程序

package com.shuqi.controller;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

  @Autowired
  private AmqpTemplate rabbitTemplate;

  @RequestMapping("/hello")
  public String hello(@RequestParam String name){
    rabbitTemplate.convertAndSend("hello","hello "+name);
    return "消息发送成功";
  }

}

通过controller生产消息,通过AmqpTemplate发送消息。有了生产者我们看下消费者

package com.shuqi.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "hello")
@Slf4j
public class HelloConsumer {

  @RabbitHandler
  public void process(String hello) {
    log.info("接收到的消息:message:{}",hello);
  }
}

@RabbitListener(queues = "hello") 表示是一个Rabbitmq的监听器,监听的队列名称是hello,说明数据可定会过来,数据过来了,通过 @RabbitHandler 修饰的方法来处理过来的数据。打印一下。下面我们启动项目看看效果。

在浏览器中输入 http://localhost:8080/hello?name=shuqi 看到下面的结果

看下控制台输出的日志

2018-03-25 16:24:32.752 INFO 4987 --- [cTaskExecutor-1] com.shuqi.consumer.HelloConsumer : 接收到的消息:message:hello shuqi

说明消息已经被consumer接收并处理掉了。大家可以把玩下。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java通过调用FFMPEG获取视频时长

    Java通过调用FFMPEG获取视频时长

    这篇文章主要为大家详细介绍了Java通过调用FFMPEG获取视频时长,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • JSON序列化Redis读取出错问题解决方案

    JSON序列化Redis读取出错问题解决方案

    这篇文章主要介绍了JSON序列化Redis读取出错问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Spring Boot 应用的热部署配置方法

    Spring Boot 应用的热部署配置方法

    热部署,简单来说,就是代码修改后不需重启项目就可自动加载出新的内容,这篇文章主要介绍了Spring Boot 应用的热部署配置 ,需要的朋友可以参考下
    2022-11-11
  • IDEA中Jar包的制作和使用方法

    IDEA中Jar包的制作和使用方法

    这篇文章主要介绍了IDEA的Jar包的制作和使用方法,文中补充介绍了idea的pom依赖配置后,不会自动下载jar包的解决办法,感兴趣的朋友跟随小编一起看看吧
    2025-04-04
  • java实现文件上传的详细步骤

    java实现文件上传的详细步骤

    文件上传是用户将本地文件通过Web页面提交到服务器的过程,涉及客户端、服务器端、上传表单等组件,在SpringBoot中,通过MultipartFile接口处理上传文件,并将其保存在服务器,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-10-10
  • springboot对接第三方微信授权及获取用户的头像和昵称等等

    springboot对接第三方微信授权及获取用户的头像和昵称等等

    这篇文章主要介绍了springboot对接第三方微信授权及获取用户的头像和昵称等等,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • SpringBoot 使用hibernate validator校验

    SpringBoot 使用hibernate validator校验

    这篇文章主要介绍了SpringBoot 使用hibernate validator校验,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • 解决@MapperScan和@Mapper共存之坑XxxMapper that could not be found.

    解决@MapperScan和@Mapper共存之坑XxxMapper that could not be fo

    这篇文章主要介绍了解决@MapperScan和@Mapper共存之坑XxxMapper that could not be found问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • MyBatis常见报错问题及解决方案

    MyBatis常见报错问题及解决方案

    这篇文章主要介绍了MyBatis常见报错问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-11-11
  • java非法字符‘\ufeff‘解决方法

    java非法字符‘\ufeff‘解决方法

    本文主要介绍了java非法字符‘\ufeff‘解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07

最新评论