MQ(仅供自己参考)

ops/2024/10/19 11:40:20/

同步通讯的优缺点:

优点:时效高,数据一致,过程简单

缺点:耦合度高。性能下降。CPU等待资源的浪费。级联失败。

2、异步通讯:异步调用常见的实现就是事件驱动模式

异步的优缺点:

优点:耦合度低 ,吞吐量提升 ,故障隔离(不在联机发生失败) ,流量削峰(Broker缓存事件,让其他服务慢慢来执行)

缺点:依赖于Broker的可靠性、安全性、吞吐量

架构复杂,没有明显的流程线,不好追踪管理

一般的项目都是使用同步通讯,因为需要拿到其他服务的返回结果,而异步通讯只是通知他有事情要做,而不需要他返回结果。

三、RabbitMq:

是一个开源的消息代理软件,广泛用于实现消息传递和异步处理。它基于消息队列的设计理念,允许不同的应用程序或服务之间进行通信,而无需它们直接相互依赖。

主要特点:

  1. 可靠性:RabbitMQ 提供消息确认机制,确保消息在处理过程中不会丢失。

  2. 灵活的路由:通过交换机和队列的配置,RabbitMQ 支持多种消息路由模式,包括点对点和发布/订阅。

  3. 多协议支持:支持多种消息协议,如 AMQP、STOMP 和 MQTT。

  4. 高可用性:通过集群和镜像队列功能,可以实现高可用性和负载均衡。

  5. 管理界面:提供用户友好的管理界面,便于监控和管理消息队列。

几种模式:

AMQP:

AMQP(Advanced Message Queuing Protocol)是一种开放标准的消息传递协议,旨在支持消息的可靠传递和异步通信。它允许不同的应用程序通过消息代理进行通信,无论这些应用程序使用的编程语言或平台如何。

AMQP 的主要特点包括:

  1. 消息队列:支持将消息存储在队列中,允许消费者异步处理消息。
  2. 发布/订阅模式:支持多种消息传递模式,包括点对点和发布/订阅。
  3. 可靠性:提供消息确认机制,确保消息不会丢失。
  4. 灵活性:支持不同的消息传递场景,适用于多种分布式系统。
  5. 跨平台:由于其开放性,AMQP 可以在不同的操作系统和语言之间进行互操

RabbitMQ的依赖,yml配置:

   <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>spring:rabbitmq:host: 192.168.136.128   #地址port: 5672                #端口username: root        #用户账号password: root        #用户密码virtual-host: /        

RabbitMQ:发送和接收:

发送

package org.example;import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class RabbitMQTest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void sendMessage(){rabbitTemplate.convertAndSend("simple.cc","hello,RabbitMq");}}

接收

package org.example.listener;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
@Slf4j
public class SimpleQueueListener {@RabbitListener(queues = "simple.cc")public void getMessage(String message){log.info(message);}
}

2、workqueue:

代码:workqueue有预取机制,当一个没有设置预取上限,那么两个消费者会均分消息,即使不能立即处理也会将消息拿到。当设置prefetch的值那么就会按这个上限那取一定数量的消息,将消息处理完成之后,再从队列中拿消息。

package org.example;import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
public class RabbitMQTest {@Autowiredprivate RabbitTemplate rabbitTemplate;@Testpublic void sendWorkQueue(){for (int i = 0; i < 50; i++) {rabbitTemplate.convertAndSend("simple.cc","hello,--"+ i);try {Thread.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}}}}package org.example.listener;import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;@Component
@Slf4j
public class SimpleQueueListener {//@RabbitListener(queues = "simple.cc")//public void getMessage(String message)//{//    log.info(message);//}@RabbitListener(queues = "simple.cc")public void getWorkQueueMessage(String message){log.info(message);try {Thread.sleep(20);} catch (InterruptedException e) {throw new RuntimeException(e);}}@RabbitListener(queues = "simple.cc")public void getWorkQueueMessages2(String message) {log.error(message);try {Thread.sleep(100);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}

3、发布订阅模式:

4、Fanout交换机:将消息路由到每一个队列,缺点是exchange只是转发消息,而不保存,如果路由的时候丢失消息那么消息就直接丢失了。

代码:

    @RabbitListener(queues = "fanout.queue1")public void getFanoutMessage(String message){log.info(message);}@RabbitListener(queues = "fanout.queue2")public void getFanoutMessage1(String message){log.info(message);}package org.example.config;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class FanoutConfig {@Beanpublic FanoutExchange fanoutExchange(){return new FanoutExchange("cc.fanout");}@Beanpublic Queue fanoutQueue1(){return new Queue("fanout.queue1");}@Beanpublic Queue fanoutQueue2(){return new Queue("fanout.queue2");}@Beanpublic Binding binding(Queue fanoutQueue1,FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);}@Beanpublic Binding binding2(Queue fanoutQueue2,FanoutExchange fanoutExchange){return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);}
}@Testpublic void sendFanoutMessage(){rabbitTemplate.convertAndSend("cc.fanout","","hello,fanout");}

5、Direct:可以通过key与相应的queue绑定,绑定之后相应的key只能发送到相应的queue上。当然一个queue可以绑定多个key,那么就可以实现广播(fanout)

代码:

   @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),exchange = @Exchange(name = "cc.direct",type = ExchangeTypes.DIRECT),key = {"blue","red"}))public void getDirectMessage1(String message){log.info(message);}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),exchange = @Exchange(name = "cc.direct",type = ExchangeTypes.DIRECT),key = {"yellow","red"}))public void getDirectMessage2(String message){log.info(message);}@Testpublic void sendFanoutMessage(){rabbitTemplate.convertAndSend("cc.direct","red","hello,direct");rabbitTemplate.convertAndSend("cc.direct","blue","hello,direct,blue");rabbitTemplate.convertAndSend("cc.direct","yellow","hello,direct,yellow");}

6、Topic:通过通配符来匹配某一类消息,通过key的匹配来选择不需要的某一类消息

代码:

    @Testpublic void sendTopicMessage(){rabbitTemplate.convertAndSend("cc.topic","china.cc","hello,china,cc");rabbitTemplate.convertAndSend("cc.topic","china.news","china,weather");rabbitTemplate.convertAndSend("cc.topic","japan.news","japan,weather");}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),exchange = @Exchange(name = "cc.topic",type = ExchangeTypes.TOPIC),key = "china.#"))public void getTopicMessage1(String message){log.info(message);}@RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),exchange = @Exchange(name = "cc.topic",type = ExchangeTypes.TOPIC),key = "*.news"))public void getTopicMessage2(String message){log.info(message);}

7、SpringAMQP:消息转换器,默认使用jdk的序列化

引依赖:

<!--json--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.14.0</version></dependency>

添加配置,覆盖原本的序列化方式:

 @Beanpublic MessageConverter messageConverter(){return new Jackson2JsonMessageConverter();}


http://www.ppmy.cn/ops/122383.html

相关文章

1.Python 引入(字面量、注释、变量、数据类型、数据类型转换、标识符、运算符、字符串扩展)

一、字面量 1、基本介绍 在代码中&#xff0c;被写直接下来的、不需要通过变量存储的值&#xff0c;称之为字面量 2、常用值类型 类型说明数字&#xff08;Number&#xff09;整数&#xff08;int&#xff09;&#xff0c;例如&#xff1a;10、-10浮点数&#xff08;float&…

【黑马点评】 使用RabbitMQ实现消息队列——1.Docker与RabbitMQ环境安装

黑马点评中&#xff0c;使用基于Redis的Stream实现消息队列&#xff0c;但是Strema已经不太常用。在此修改为使用RabbitMQ实现消息队列。主要包括RabbitMQ的环境准备&#xff08;Docker的下载与安装&#xff09;以及如何修改黑马点评中的代码。 【黑马点评】使用RabbitMQ实现消…

Unity实战案例全解析:RTS游戏的框选和阵型功能(5)阵型功能 优化

前篇&#xff1a;Unity实战案例全解析&#xff1a;RTS游戏的框选和阵型功能&#xff08;4&#xff09;阵型功能-CSDN博客 本案例来源于unity唐老狮&#xff0c;有兴趣的小伙伴可以去泰克在线观看该课程 我只是对重要功能进行分析和做出笔记分享&#xff0c;并未无师自通&#x…

IDEA创建、导入、删除maven项目

全局配置&#xff1a; 1.File->Close Project 2.Customize->All settings 3. Apply 4.选择JRE版本->Apply 5.选择字节码版本->Apply->OK 全局配置结束 创建maven项目&#xff1a; 1.File->New->Module 2.Build system选择Maven GroupId&#xff1a…

PostgreSQL 任意命令执行漏洞(CVE-2019-9193)

记一次授权攻击通过PostgreSql弱口令拿到服务器权限的事件。 使用靶机复现攻击过程。 过程 在信息收集过程中&#xff0c;获取到在公网服务器上开启了5432端口&#xff0c;尝试进行暴破&#xff0c;获取到数据库名为默认postgres&#xff0c;密码为1 随后连接进PostgreSql …

【多线程】详解 CAS 机制

&#x1f970;&#x1f970;&#x1f970;来都来了&#xff0c;不妨点个关注叭&#xff01; &#x1f449;博客主页&#xff1a;欢迎各位大佬!&#x1f448; 文章目录 1. CAS 是什么1.1 CAS 具体步骤1.2 CAS 伪代码 2. CAS 的应用2.1 实现原子类2.1.1 AtomInteger 类2.1.2 伪代…

初阶C语言-指针

1.指针是什么&#xff1f; 理解指针的两个要点&#xff1a; 1.指针是内存中一个最小单元的编号&#xff0c;也就是地址 2.口头语中说的指针&#xff0c;通常是指指针变量&#xff0c;是用来存放内存地址的变量 总结&#xff1a;指针就是地址&#xff0c;口语中说的指针通常是指…

算法6:模拟运算

文章目录 z字形变幻外观数列数青蛙 题目均来自于力扣 z字形变幻 class Solution { public:string convert(string s, int numRows) {int n s.size();if(n < numRows || numRows 1) return s;int d 2 * numRows - 2;string res;for(int j 0; j < n; j d){res s[j]; …