欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot与rabbitmq的结合的示例

程序员文章站 2023-11-17 16:40:07
消息中间件对于我们系统之间的解耦合,消峰等都有极大的帮助。spring boot 也集成了此部分的内容,集成最为容易的是rabbitmq。今天我们就以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 看到下面的结果

SpringBoot与rabbitmq的结合的示例

看下控制台输出的日志

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

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

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