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

2024-01-18 03:20

本文主要是介绍rabbitmq基础教程(ui,java,springamqp),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述:安装看我上篇文章Docker安装rabbitmq-CSDN博客

任务一

创建一个队列

这样创建两个队列

在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.10port: 5672virtual-host: /cmallusername: cmallpassword: 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 {@Autowiredprivate 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 {@AutowiredSendMessageService sendMsgservice;@AutowiredReceiveMessageService 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 {@Autowiredprivate 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。

好了,基础讲完。

这篇关于rabbitmq基础教程(ui,java,springamqp)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/617967

相关文章

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Java中如何正确的停掉线程

《Java中如何正确的停掉线程》Java通过interrupt()通知线程停止而非强制,确保线程自主处理中断,避免数据损坏,线程池的shutdown()等待任务完成,shutdownNow()强制中断... 目录为什么不强制停止为什么 Java 不提供强制停止线程的能力呢?如何用interrupt停止线程s

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映