1 RabbitMQ知识点

  1.1 整体架构图

    消息生产者将消息投递到exchange中,exchange会以某种路由机制将生产者投递的消息路由到queue中,消息消费者再从queue中获取消息进行消费

 

 

  1.2 核心概念

    1.2.1 server

      又称Broker,接收客户端的连接,实现AMQP实体服务

    1.2.2 Connection

      连接,应用服务与Broker的网络连接(PS:和JDBC中的connection作用相似)

    1.2.3 Channel

      网络信道,几乎所有的操作都在Channel中进行,Channel是进行消息读写的通道。客户端可建立多个Channel,每个Channel代表一个会话。【PS:相当于JDBC的Statement】

    1.2.4 Message

      消息,服务器和应用程序之间传递的数据,由Properties和Body组成;Properties可以对消息进行修饰,比如消息的优先级、迟延等高级特性;Body则就是消息的内容。

    1.2.5 Virtual Host

      虚拟地址,用于进行逻辑隔离的最上层的消息路由。一个Virtual Host 里面可以有若干个Exchange 和 Queue,同一个Virtual Host里面不能有相同名称的Exchange或Queue;默认是 / , Virtual Host 通常用来进行项目划分,即:一个项目相关的exchange 和 queue 都在一个Virtual Host中。

     1.2.6 Exchange

      交换机,用于接收消息;根据路由键转发消息绑定的队列

    1.2.7 Binding

      Exchange 和 Queue 之间的虚拟连接,Binding 可以包含routing key。

    1.2.8 Routing Key

      一个路由规则,虚拟机可以用它来确定如何路由一个特定消息;就是exchange和queue的路由规则

    1.2.9 Queue

      也称Message Queue,消息队列,保存消息并将他们转发给消费者

   1.3 RabbitMQ消息的流转

    消息发布者会发送一个消息、exchange、routeing Key 到rabbtiMQ,他们先经过 Virtual Host , 再经过 Exchange, 再到Queue;消息消费者再到Queue中获取消息

 

2 Docker下载RabbiMQ

  2.1 Docker环境搭建

    请参见另外一篇博文:点击前往

    docker版本为:

  2.2 安装RabbitMQ

    2.2.1 拉取RabbitMQ镜像

docker pull rabbitmq:3.7.7-management

      技巧01:rabbitMQ的版本可以去官网进行查看

    2.2.2 查看拉取的镜像列表

    2.2.3 创建并启动rabbitMQ容器

docker run -d --hostname my-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3.7.7-management

      技巧01:-p 是将虚拟机的端口和docker中的端口做一个映射,这样访问虚拟机的端口就相当于访问了docker中的端口

      技巧02:5672是连接rabbtiMQ的端口,15672是连接rabbitMQ管理页面的端口

    2.2.4 查看启动的rabbitMQ容器

  2.3 连接rabbitMQ管理页面

    技巧01:直接在浏览器访问 虚拟机IP + rabbtiMQ管理页面端口 即可,例如:

http://192.168.233.135:15672/

    技巧02:第一次访问的时候需要输入用户名和用户密码,默认的用户名和用户密码是guest

    技巧03:访问成功后的界面如下

 

3 SpringBoot整合RabbitMQ之消费发布者

  3.1 创建一个SpringBoot项目

    引入相关依赖:web、amqp,其他的都是辅助依赖

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

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

<?xml version="1.0" encoding="UTF-8"?>
<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.xunyji</groupId>
    <artifactId>rabbitmq01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>rabbitmq01</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

pom.xml

  3.2 配置文件

# rabbitMQ基本配置
spring.rabbitmq.addresses=192.168.233.135:5672
spring.rabbitmq.username=guest
spring.activemq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000

server.servlet.path=/
server.port=8001


spring.http.encoding.charset=UTF-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.default-property-inclusion=non_null

application.properties

  3.3 发布消息

    依赖注入RabbitTemplate的实例并调用实例方法convertAndSend发布消息

    技巧01:在进行发布之前需要在rabbitMQ中设置响应的exchange和queue

    3.3.1 在RabbitMQ中添加exchange

      技巧01:添加完成后exchange列表中就会出现新增的exchange信息

    3.3.2 在RabbitMQ中添加Queue

      技巧01:添加完queue后就会在queue列表中看到新添加的queue

    3.3.3 设置exchange和queue的路由规则

      技巧01:exchange和queue都可以进行设置,只有一方设置了就可以啦

  3.4 测试

    向rabbitMQ发布一则消息

package com.xunyji.rabbitmq01.producer;

import com.xunyji.rabbitmq01.Rabbitmq01ApplicationTests;
import com.xunyji.rabbitmq01.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.UUID;

import static org.junit.Assert.*;

@Component
@Slf4j
public class OrderSenderTest extends Rabbitmq01ApplicationTests {

    @Autowired
    private OrderSender orderSender;

    @Test
    public void test01() throws Exception {

        Order order = Order.builder()
                .id("20180830")
                .name("测试订单01")
                .messageId(System.currentTimeMillis() + "$" + UUID.randomUUID().toString())
                .build();

        orderSender.sendOrder(order);


    }

}

View Code

    技巧01:发送成功后,queue中就会有有一条为被消费的消息;当消息消费者消费了过后才会从队列中剔除

 

4 SpringBoot整合RabbitMQ之消费订阅者

  4.1 创建一个SpringBoot项目

    引入必要依赖:web、ampq,其余都是辅助依赖

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

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

<?xml version="1.0" encoding="UTF-8"?>
<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.xunyji</groupId>
    <artifactId>rabbitmq02-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>rabbitmq02-consumer</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

pom.xml

  4.2 配置文件

#springboot整合mq的基本配置
spring.rabbitmq.addresses=192.168.233.135:5672
spring.rabbitmq.username=guest
spring.activemq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000

#springboot整合mq的消费配置
spring.rabbitmq.listener.simple.concurrency=5
spring.rabbitmq.listener.simple.max-concurrency=10
spring.rabbitmq.listener.simple.acknowledge-mode=manual
spring.rabbitmq.listener.simple.prefetch=1


server.servlet.path=/
server.port=8002


spring.http.encoding.charset=UTF-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.default-property-inclusion=non_null

View Code

  4.3 消费方法编写

    在消费方法上利用注解开启监听和指定监听那个队列即可

    技巧01:@RabbitHandler 和 @RabbitListener 注解可以自动到rabbitMQ中生成exchange和queue的绑定关系

    4.4 获取消息

      由于rabbitMQ中已经有一条消息待处理了,所以直接启动项目就可以完成消息的消费,消费后rabbitMQ中待消费的消息数量就为0啦

 

5 本博文源代码

  点击前往

    

 

 

版权声明:本文为NeverCtrl-C原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/NeverCtrl-C/p/9563339.html