码迷,mamicode.com
首页 > 编程语言 > 详细

springboot 微服务之集成RabbitMQ消息中间件

时间:2020-06-30 13:15:13      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:data   ica   学习   info   value   css   基础知识   group   original   

前言

一直没机会做spring生态圈的框架,公司选择的是一些小众的微服务,鉴于此考虑,丰富自己的技术栈,花了两天时间从网上各网站上学习了springboot一些基础知识。
本章只介绍springboot微服务集成RabbitMQ,用于通过消息中间件给其他微服务发送消息。

环境准备

  • IntelliJ IDEA
  • 前一章中搭建的微服务框架

开始集成

  1. pom.xml中增加依赖包


     
    技术图片
    依赖包.png
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
  1. 在application.yml中增加rabbit服务端信息:


     
    技术图片
    yml配置.png
spring:
  #...
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
  1. 增加rabbit的配置类,定义所需要绑定的queue,exchange,routingkey,binding等


     
    技术图片
    RabbitConfig.png
package com.example.demo;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类功能描述:<br>
 * Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线,保证数据能按照指定的方式进行传输,
 * Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。
 * Queue:消息的载体,每个消息都会被投到一个或多个队列。
 * Binding:绑定,它的作用就是把exchange和queue按照路由规则绑定起来.
 * Routing Key:路由关键字,exchange根据这个关键字进行消息投递。
 * vhost:虚拟主机,一个broker里可以有多个vhost,用作不同用户的权限分离。
 * Producer:消息生产者,就是投递消息的程序.
 * Consumer:消息消费者,就是接受消息的程序.
 * Channel:消息通道,在客户端的每个连接里,可建立多个channel.
 * <ul>
 * <li>类功能描述1<br>
 * <li>类功能描述2<br>
 * <li>类功能描述3<br>
 * </ul>
 * 修改记录:<br>
 * <ul>
 * <li>修改记录描述1<br>
 * <li>修改记录描述2<br>
 * <li>修改记录描述3<br>
 * </ul>
 *
 * @author xuefl
 * @version 5.0 since 2020-01-02
 */

此处定义了三种类型的queue,exchange,和routingkey,预先定义,并且将队列,绑定到指定Exchange上,定义其routingKey。并使用@Bean注解定义为实体

  1. 在demo下新增rabbitmq包,在其下新建MsgProducer和MsgConsumer类,定义消费者和生产者,用于消费队列和发送消息(此处为了自测,自己给自己发送消息,并由自己消费消息)


     
    技术图片
    消费者.png

     
    技术图片
    生产者.png
package com.example.demo.rabbitmq;
import com.example.demo.RabbitConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 类功能描述:<br>
 * <ul>
 * <li>类功能描述1<br>
 * <li>类功能描述2<br>
 * <li>类功能描述3<br>
 * </ul>
 * 修改记录:<br>
 * <ul>
 * <li>修改记录描述1<br>
 * <li>修改记录描述2<br>
 * <li>修改记录描述3<br>
 * </ul>
 *
 * @author xuefl
 * @version 5.0 since 2020-01-02
 */
package com.example.demo.rabbitmq;
import com.example.demo.RabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 类功能描述:<br>
 * <ul>
 * <li>类功能描述1<br>
 * <li>类功能描述2<br>
 * <li>类功能描述3<br>
 * </ul>
 * 修改记录:<br>
 * <ul>
 * <li>修改记录描述1<br>
 * <li>修改记录描述2<br>
 * <li>修改记录描述3<br>
 * </ul>
 *
 * @author xuefl
 * @version 5.0 since 2020-01-02
 */

消费者类中通过@RabbitListener和@RabbitHandler注解将一个方法定义为消息监听的方法,使用方法如下


 
技术图片
注解.png

@RabbitListener可以通过定义bindings={@QueueBinding},@QueueBinding可以通过赋值value=@Queue(value = RabbitConfig.FANOUT_QUEUE_NAME, durable = "true")定义出消息队列名,@QueueBinding也可以通过赋值exchange=@Exchange(value = RabbitConfig.TEST_FANOUT_EXCHANGE, type = "fanout")来定义当前方法所监听的Exchange,以及类型,类型默认是direct

  1. 定义controller,在controller下新建RabbitMQController类,定义几个接口


     
    技术图片
    RabbitMQController.png
package com.example.demo.controller;
import com.example.demo.rabbitmq.MsgProducer;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 类功能描述:<br>
 * <ul>
 * <li>类功能描述1<br>
 * <li>类功能描述2<br>
 * <li>类功能描述3<br>
 * </ul>
 * 修改记录:<br>
 * <ul>
 * <li>修改记录描述1<br>
 * <li>修改记录描述2<br>
 * <li>修改记录描述3<br>
 * </ul>
 *
 * @author xuefl
 * @version 5.0 since 2020-01-02
 */
@RequestMapping("/rabbit")
@Controller
@Slf4j
@Api(value = "SwaggerValue", tags={"RabbitMQController"},description = "swagger应用",  produces = MediaType.APPLICATION_JSON_VALUE)
public class RabbitMQController {

    @Autowired
    private MsgProducer msgProducer;

    @GetMapping(value = "/sendFanout")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public void sendMsg(){
        msgProducer.send2FanoutTestQueue("this is a test fanout message!");
    }

    @GetMapping(value = "/sendDirect")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public void sendDirectMsg(){
        msgProducer.send2DirectTestQueue("this is a test direct message!");
    }

    @GetMapping(value = "/sendDirectA")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public void sendTopicAMsg(){
        msgProducer.send2TopicTestAQueue("this is a test topic aaa message!");
    }

    @GetMapping(value = "/sendTopicB")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public void sendTopicBMsg(){
        msgProducer.send2TopicTestBQueue("this is a test topic bbb message!");
    }
}

此接口用于自测试,测试消息发送。



作者:xuefly3
链接:https://www.jianshu.com/p/8fe4bc1c584d
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

springboot 微服务之集成RabbitMQ消息中间件

标签:data   ica   学习   info   value   css   基础知识   group   original   

原文地址:https://www.cnblogs.com/go1188/p/13212812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!