rabbitmq基础教程(ui,java,springamqp)

简介: 本文提供了RabbitMQ的基础教程,包括如何使用UI创建队列和交换机、Java代码操作RabbitMQ、Spring AMQP进行消息发送和接收,以及如何使用不同的交换机类型(fanout、direct、topic)进行消息路由。

概述:安装看我上篇文章Docker安装rabbitmq

任务一

创建一个队列

这样创建两个队列

在amq.fanout交换机里面发送数据

模拟发送数据

发送消息,发现一下信息:

所以得出理论,消息发送是先到交换机,然后由交换机路由到消息队列

交换机是负责路由和转发消息的,并没有存储的功能。

绑定队列

同理绑定queue2

这时,再在交换机中发消息

查看结果:

数据隔离

在rabbitmq中有虚拟主机的概念。

第一步:新添用户

添加成功后,发现没有虚拟主机,也就是说,我用这个用户登录后,是不可以操作上面的数据的。

又因为,我是超级管理员,所以我能看到这些

所以只能看,不能操作。

第二步:创立自己的虚拟主机

第三步:选自己的虚拟主机

选好后就只能看自己的了。

用Java代码操作

官网:RabbitMQ Tutorials — RabbitMQ

可以看到,官网上有案例,我们大多情况下用的是SpringAmqp,所以也就不讲那么多java简单调用的事情了。

用Spring AMQP操作

第一步:在控制台里面创建一个simple.queue队列

第二步:编写代码

pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.cyl</groupId>
    <artifactId>test09</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test09</name>
    <description>test09</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.13.0</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>org.cyl.test09.Test09Application</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

配置mq服务端消息

spring:
  rabbitmq:
    host: 192.168.56.10
    port: 5672
    virtual-host: /cmall
    username: cmall
    password: 123456

发送方:

package org.cyl.test09.demos;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendMessageService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void testSimpleQueue(){
        String queueName="simple.queue";
        String message="hello,spring amqp!";
        rabbitTemplate.convertAndSend(queueName,message);
    }

    public void sendMessage(String exchange, String routingKey, Object message) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
    }
}

接收方:

package org.cyl.test09.demos;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class ReceiveMessageService {

    @RabbitListener(queues = "simple.queue")
    public void receiveMessage(String message) {
        System.out.println("接收到的消息: " + message);
    }
}

controller类:

package org.cyl.test09.demos.controller;

import org.cyl.test09.demos.ReceiveMessageService;
import org.cyl.test09.demos.SendMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    SendMessageService sendMsgservice;

    @Autowired
    ReceiveMessageService receiveMsgService;

    @GetMapping("/send")
    public String send(){
        sendMsgservice.testSimpleQueue();
        return "ok";
    }

}

展示结果:

Work模型

第一步:创建一个队列

第二步:编写代码

发送:

package org.cyl.test09.demos;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SendMessageService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void testSimpleQueue() throws InterruptedException {
        String queueName="work.queue";

        for (int i=1;i<50;i++){
            String message="hello,spring amqp!_"+i;
            rabbitTemplate.convertAndSend(queueName,message);
            Thread.sleep(20);
        }

    }

    public void sendMessage(String exchange, String routingKey, Object message) {
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
    }
}

接收:

package org.cyl.test09.demos;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class ReceiveMessageService {

    @RabbitListener(queues = "work.queue")
    public void receiveMessage1(String message) {
        System.out.println("消费者1接收到的消息: " + message);
    }
    @RabbitListener(queues = "work.queue")
    public void receiveMessage2(String message) {
        System.out.println("消费者2接收到的消息: " + message);
    }
}

结果展示:

消费者一和消费者二是轮询效果。

Fanout交换机

第一步:创建队列

第二步:创建交换机并绑定

第三步:编写代码

发送端:

    public void testFanout() {
        String exchangeName="cmall.fanout";
        String message="hello,spring everyone";
        rabbitTemplate.convertAndSend(exchangeName,null,message);
    }

接收端:

   @RabbitListener(queues = "fanout.queue1")
    public void receiveMessage3(String message) {
        System.out.println("消费者1接收到的消息: " + message);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void receiveMessage4(String message) {
        System.out.println("消费者2接收到的消息: " + message);
    }

展示结果:

私发给不同的人:Direct交换机

第一步:创建两个队列

第二步:声明交换机并绑定

第三步:编写代码

接收方:

   @RabbitListener(queues = "direct.queue1")
    public void receiveMessage5(String message) {
        System.out.println("消费者1接收到的消息: " + message);
    }
    @RabbitListener(queues = "direct.queue2")
    public void receiveMessage6(String message) {
        System.out.println("消费者2接收到的消息: " + message);
    }

发送方:

    public void testDirect1() {
        String exchangeName="cmall.fanout";
        String message="hello,spring everyone";
        rabbitTemplate.convertAndSend(exchangeName,"red",message);
    }

    public void testDirect2() {
        String exchangeName="cmall.fanout";
        String message="hello,spring blue";
        rabbitTemplate.convertAndSend(exchangeName,"blue",message);
    }

    public void testDirect3() {
        String exchangeName="cmall.fanout";
        String message="hello,spring yellow";
        rabbitTemplate.convertAndSend(exchangeName,"yellow",message);
    }

Topic交换机

这个示例代码就懒得写了。

声明交换机和队列1

绑定队列到哪个交换机里面。

一般建立关系都是在消费者这边的。

声明交换机和队列2

基于注解式声明队列和交换机。

消息转换器

字节码可变,会有安全问题。

搞完以上东西,代码不用变,在发一次,即可为json。

好了,基础讲完。

相关实践学习
消息队列RocketMQ版:基础消息收发功能体验
本实验场景介绍消息队列RocketMQ版的基础消息收发功能,涵盖实例创建、Topic、Group资源创建以及消息收发体验等基础功能模块。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
目录
相关文章
|
2天前
|
存储 前端开发 Java
Java后端如何进行文件上传和下载 —— 本地版(文末配绝对能用的源码,超详细,超好用,一看就懂,博主在线解答) 文件如何预览和下载?(超简单教程)
本文详细介绍了在Java后端进行文件上传和下载的实现方法,包括文件上传保存到本地的完整流程、文件下载的代码实现,以及如何处理文件预览、下载大小限制和运行失败的问题,并提供了完整的代码示例。
38 1
|
22天前
|
Java API
Java时间戳教程
本文详细介绍Java中时间戳的处理方法,包括获取当前时间戳、使用`java.time`包、时间戳与日期的相互转换及格式化等。示例代码展示了如何利用`System.currentTimeMillis()`和`java.time.Instant`获取时间戳,以及如何通过`Date`和`ZonedDateTime`进行日期转换和时区处理。随着Java 8引入的`java.time`包,日期时间操作变得更加强大和便捷,推荐在新项目中优先采用。
|
1天前
|
Oracle IDE Java
IDEA安装教程配置java环境(超详细)
IDEA安装教程配置java环境(超详细)
|
2天前
|
JSON Java Maven
实现Java Spring Boot FCM推送教程
详细介绍实现Java Spring Boot FCM推送教程
14 0
|
27天前
|
消息中间件 存储 Java
SpringCloud基础4——RabbitMQ和SpringAMQP
消息队列MQ、RabbitMQ、SpringAMQP高级消息队列协议、发布/订阅模型、fanout、direct、topic模式
SpringCloud基础4——RabbitMQ和SpringAMQP
|
2月前
|
存储 搜索推荐 Java
探索安卓开发中的自定义视图:打造个性化UI组件Java中的异常处理:从基础到高级
【8月更文挑战第29天】在安卓应用的海洋中,一个独特的用户界面(UI)能让应用脱颖而出。自定义视图是实现这一目标的强大工具。本文将通过一个简单的自定义计数器视图示例,展示如何从零开始创建一个具有独特风格和功能的安卓UI组件,并讨论在此过程中涉及的设计原则、性能优化和兼容性问题。准备好让你的应用与众不同了吗?让我们开始吧!
|
15天前
|
消息中间件 缓存 Java
RocketMQ的JAVA落地实战
RocketMQ作为一款高性能、高可靠、高实时、分布式特点的消息中间件,其核心作用主要体现在异步处理、削峰填谷以及系统解耦三个方面。
67 0
|
5月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
|
5月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
|
5月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍