Netty核心技术九--TCP 粘包和拆包及解决方案

news2024/9/22 5:39:37

1. TCP 粘包和拆包基本介绍

  1. **TCP是面向连接的,面向流的,提供高可靠性服务。收发两端(客户端和服务器端)都要有一一成对的socket,因此,发送端为了将多个发给接收端的包,更有效的发给对方,使用了优化方法(Nagle算法),将多次间隔较小且数据量小的数据,合并成一个大的数据块,然后进行封包。**这样做虽然提高了效率,但是接收端就难于分辨出完整的数据包了,因为面向流的通信是无消息保护边界的

  2. 由于TCP无消息保护边界, 需要在接收端处理消息边界问题,也就是我们所说的粘包拆包问题

  3. TCP粘包、拆包图解

    image-20230708102428452

    假设客户端分别发送了两个数据包D1和D2给服务端,由于服务端一次读取到字节数是不定的,故可能存在以下四种情况:

    1. 服务端分两次读取到了两个独立的数据包,分别是D1和D2,没有粘包和拆包
    2. 服务端一次接受到了两个数据包,D1和D2粘合在一起,称之为TCP粘包
    3. 服务端分两次读取到了数据包,第一次读取到了完整的D1包和D2包的部分内容,第二次读取到了D2包的剩余内容,这称之TCP拆包
    4. 服务端分两次读取到了数据包,第一次读取到了D1包的部分内容D1_1,第二次读取到了D1包的剩余部分内容D1_2和完整的D2包。这称之TCP拆包

2. TCP 粘包和拆包现象示例1

实例需求:

在编写Netty 程序时,如果没有做处理,就会发生粘包和拆包的问题

2.1 MyClient

package site.zhourui.nioAndNetty.netty.tcp;



import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class MyClient {
    public static void main(String[] args)  throws  Exception{

        EventLoopGroup group = new NioEventLoopGroup();

        try {

            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group).channel(NioSocketChannel.class)
                    .handler(new MyClientInitializer()); //自定义一个初始化类

            ChannelFuture channelFuture = bootstrap.connect("localhost", 7000).sync();

            channelFuture.channel().closeFuture().sync();

        }finally {
            group.shutdownGracefully();
        }
    }
}

2.2 MyClientInitializer

只在Initializer加入了MyClientHandler处理业务

package site.zhourui.nioAndNetty.netty.tcp;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;


public class MyClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new MyClientHandler());
    }
}

2.3 MyClientHandler

  • 使用客户端发送10条数据 hello,server 编号
  • 回显客户端接收到消息
package site.zhourui.nioAndNetty.netty.tcp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.Charset;

public class MyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private int count;
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //使用客户端发送10条数据 hello,server 编号
        for(int i= 0; i< 10; ++i) {
            ByteBuf buffer = Unpooled.copiedBuffer("hello,server " + i, Charset.forName("utf-8"));
            ctx.writeAndFlush(buffer);
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        byte[] buffer = new byte[msg.readableBytes()];
        msg.readBytes(buffer);

        String message = new String(buffer, Charset.forName("utf-8"));
        System.out.println("客户端接收到消息=" + message);
        System.out.println("客户端接收消息数量=" + (++this.count));

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

2.4 MyServer

package site.zhourui.nioAndNetty.netty.tcp;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class MyServer {
    public static void main(String[] args) throws Exception{

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {

            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class).childHandler(new MyServerInitializer()); //自定义一个初始化类


            ChannelFuture channelFuture = serverBootstrap.bind(7000).sync();
            channelFuture.channel().closeFuture().sync();

        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
}

2.5 MyServerInitializer

只在Initializer加入了MyServerHandler处理业务

package site.zhourui.nioAndNetty.netty.tcp;



import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;


public class MyServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new MyServerHandler());
    }
}

2.6 MyServerHandler

  • 打印服务器接收到数据
  • 服务器回送数据给客户端, 回送一个随机id
package site.zhourui.nioAndNetty.netty.tcp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.Charset;
import java.util.UUID;

public class MyServerHandler extends SimpleChannelInboundHandler<ByteBuf>{
    private int count;

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {

        byte[] buffer = new byte[msg.readableBytes()];
        msg.readBytes(buffer);

        //将buffer转成字符串
        String message = new String(buffer, Charset.forName("utf-8"));

        System.out.println("服务器接收到数据 " + message);
        System.out.println("服务器接收到消息量=" + (++this.count));

        //服务器回送数据给客户端, 回送一个随机id ,
        ByteBuf responseByteBuf = Unpooled.copiedBuffer(UUID.randomUUID().toString() + " ", Charset.forName("utf-8"));
        ctx.writeAndFlush(responseByteBuf);

    }
}

2.7 测试

  1. 启动MyServer

    image-20230708105325765

  2. 启动一个MyClient

    image-20230708105358618

    image-20230708105413184

  3. 再启动一个MyClient

    客户端收到了4个服务端回复的消息

    image-20230708105538832

    服务端发送4次数据,但是

    image-20230708105506907

服务端发送的10个数据被分4次发送,有粘包情况,并且每次执行客户端被划分的个数都不一样

3. TCP 粘包和拆包解决方案

  1. 使用自定义协议 + 编解码器 来解决
  2. 关键就是要解决 服务器端每次读取数据长度的问题, 这个问题解决,就不会出现服务器多读或少读数据的问题,从而避免的TCP 粘包、拆包。

3.1 自定义协议 + 编解码器 解决TCP 粘包、拆包 示例2

实例要求:

image-20230708111517718

  1. 要求客户端发送 5 个 Message 对象, 客户端每次发送一个Message对象
  2. 服务器端每次接收一个Message, 分5次进行解码,每读取到一个Message,会回复一个Message 对象 给客户端.

示例1的代码基础上做扩充

3.1.1 自定义协议 MessageProtocol

  • len:每个数据包的长度
  • content:每个数据包的内容
package site.zhourui.nioAndNetty.netty.protocoltcp;

//协议包
public class MessageProtocol {
    private int len; //关键
    private byte[] content;

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }
}

3.1.2 对应协议的编码器MyMessageEncoder

  1. extends MessageToByteEncoder<MessageProtocol>,因为编码器接收到的数据可以确定为MessageProtocol
  2. 重写encode将我们的数据封装为MessageProtocol协议对象
package site.zhourui.nioAndNetty.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class MyMessageEncoder extends MessageToByteEncoder<MessageProtocol> {
    @Override
    protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
        System.out.println("MyMessageEncoder encode 方法被调用");
        out.writeInt(msg.getLen());
        out.writeBytes(msg.getContent());
    }
}

3.1.3 对应协议的解码器MyMessageDecoder

  • ReplayingDecoder<Void>:Void代表不需要状态管理(由ByteToMessageDecoder来帮我们自动识别并管理)
  • 将得到二进制字节码-> MessageProtocol 数据包(对象)
  • 封装成 MessageProtocol 对象,放入 out, 传递下一个handler业务处理
package site.zhourui.nioAndNetty.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

public class MyMessageDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyMessageDecoder decode 被调用");
        //需要将得到二进制字节码-> MessageProtocol 数据包(对象)
        int length = in.readInt();

        byte[] content = new byte[length];
        in.readBytes(content);

        //封装成 MessageProtocol 对象,放入 out, 传递下一个handler业务处理
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(length);
        messageProtocol.setContent(content);

        out.add(messageProtocol);

    }
}

3.1.4 MyClient和MyServer没有变动

3.1.6 MyClientInitializer

在客户端业务handler之前加入了协议编码器和协议解码器

package site.zhourui.nioAndNetty.netty.protocoltcp;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;


public class MyClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new MyMessageEncoder()); //加入编码器
        pipeline.addLast(new MyMessageDecoder());//加入解码器
        pipeline.addLast(new MyClientHandler());
    }
}

3.1.7 MyServerInitializer

在服务端业务handler之前加入了协议编码器和协议解码器

package site.zhourui.nioAndNetty.netty.protocoltcp;



import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;


public class MyServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new MyMessageEncoder()); //加入编码器
        pipeline.addLast(new MyMessageDecoder());//加入解码器
        pipeline.addLast(new MyServerHandler());
    }
}

3.1.8 MyClientHandler

  • channelActive:channel激活的时候发送5条数据,将数据封装为MessageProtocol协议对象(这个对象会给到MyClientHandler)
  • 打印出服务器端发送的内容
package site.zhourui.nioAndNetty.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.Charset;

public class MyClientHandler extends SimpleChannelInboundHandler<MessageProtocol> {

    private int count;
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //使用客户端发送10条数据 "今天天气冷,吃火锅" 编号

        for(int i = 0; i< 5; i++) {
            String mes = "今天天气冷,吃火锅";
            byte[] content = mes.getBytes(Charset.forName("utf-8"));
            int length = mes.getBytes(Charset.forName("utf-8")).length;

            //创建协议包对象
            MessageProtocol messageProtocol = new MessageProtocol();
            messageProtocol.setLen(length);
            messageProtocol.setContent(content);
            ctx.writeAndFlush(messageProtocol);

        }

    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {

        int len = msg.getLen();
        byte[] content = msg.getContent();

        System.out.println("客户端接收到消息如下");
        System.out.println("长度=" + len);
        System.out.println("内容=" + new String(content, Charset.forName("utf-8")));
        System.out.println("客户端接收消息数量=" + (++this.count));

        System.out.println("" );


    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("异常消息=" + cause.getMessage());
        ctx.close();
    }
}

3.1.9 MyServerHandler

  • 打印客户端发送内容
  • 每接收一次消息就回复客户端一个messageProtocol消息
package site.zhourui.nioAndNetty.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.Charset;
import java.util.UUID;

public class MyServerHandler extends SimpleChannelInboundHandler<MessageProtocol>{
    private int count;

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        //cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {

        //接收到数据,并处理
        int len = msg.getLen();
        byte[] content = msg.getContent();

        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("服务器接收到信息如下");
        System.out.println("长度=" + len);
        System.out.println("内容=" + new String(content, Charset.forName("utf-8")));

        System.out.println("服务器接收到消息包数量=" + (++this.count));

        //回复消息

        String responseContent = UUID.randomUUID().toString();
        int responseLen = responseContent.getBytes("utf-8").length;
        byte[]  responseContent2 = responseContent.getBytes("utf-8");
        //构建一个协议包
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setLen(responseLen);
        messageProtocol.setContent(responseContent2);

        ctx.writeAndFlush(messageProtocol);


    }
}

3.1.10 测试

  1. 启动服务端

    image-20230708134722220

  2. 启动一个客户端

    1. 客户端发送了5个协议数据
    2. 服务端也收到5个协议数据
    3. 服务端在回送5个协议数据
    4. 客户端接收到5个协议数据

    image-20230708134812094

    image-20230708134835031

3.2 个人总结

个人总结

  1. 首先必须要熟悉handler调用链是怎么只是(即encode和decode方法在什么时候执行拿到的是什么数据需要传出给下一个handler的数据应该是什么类型)
  2. 客户端启动首先执行客户端的MyClientHandler的channelActive方法将数据封装为messageProtocol类型的协议对象
  3. 因为是入站那么handler调用链inbound方法的实现类即MyMessageEncoder的encode方法将协议对象转换为字节对象来进行网络传输
  4. 服务端因为是出站那么handler调用链outbound方法的实现类MyMessageDecoder的decode方法将字节对象封装为协议对象,然后将该对象放入out集合
  5. 服务端的MyMessageDecoder的下一个handler即MyServerHandler会拿到out,然后进行业务处理(这里就是打印并返回一个数据并封装为协议对象,因为是5个那么就会执行5次)
  6. 因为是入站那么handler调用链inbound方法的实现类即MyMessageEncoder的encode方法将协议对象转换为字节对象来进行网络传输
  7. 客户端因为是出站那么handler调用链outbound方法的实现类MyMessageDecoder的decode方法将字节对象封装为协议对象,然后将该对象放入out集合
  8. 服务端的MyMessageDecoder的下一个handler即MyClientHandler会拿到out,然后进行业务处理(这里就是打印因为是5个那么就会执行5次)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/733059.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

OpenCV 入门教程:图像的基本操作和处理

OpenCV 入门教程&#xff1a;图像的基本操作和处理 导语一、图像的基本操作1.1 获取图像的大小1.2 访问图像的像素1.3 修改图像的像素值 二、图像的基本处理2.1 图像的灰度化2.2 图像的平滑处理2.3 图像的边缘检测 总结 导语 在计算机视觉和图像处理领域&#xff0c;对图像进行…

Spring Boot 中的 CompletableFuture 类是什么,如何使用?

Spring Boot 中的 CompletableFuture 类是什么&#xff0c;如何使用&#xff1f; 介绍 在开发企业级应用程序时&#xff0c;我们经常需要异步执行任务。异步执行任务可以提高应用程序的性能和响应能力。在 Java 8 中&#xff0c;引入了 CompletableFuture 类&#xff0c;它提…

zabbix 监控 windows 系统、java应用、SNMP

目录 一、部署 zabbix 监控 windows系统 1.下载 Windows 客户端 Zabbix agent 2 2.安装客户端&#xff0c;在监控的windows主机上配置 3.在服务端 Web 页面添加主机&#xff0c;关联模板 二、部署 zabbix 监控 Java应用 1.客户端开启 java jmxremote 远程监控功能 1.1配置…

finalshell上传文件到虚拟机一直失败

目录 1.首先看一下你的虚拟机的可用空间是否足够 2.查看是否是root用户 1.首先看一下你的虚拟机的可用空间是否足够 在finalshell查看即可 如果空间不够则将虚拟机关机 &#xff0c;右键虚拟机找到设置&#xff0c;找到硬盘 &#xff08;我这里演示的是VMwareFusion&#xff…

Linux--冯诺依曼体系结构

【Linux】冯诺依曼体系结构、操作系统及进程概念_linux io 冯诺依曼_平凡的人1的博客-CSDN博客 存储器指的是内存还是磁盘&#xff1f; 内存 输入设备&#xff1a;键盘、摄像头、话筒、磁盘、网卡... 输出设备&#xff1a;显示器、音响、磁盘、网卡... CPU: 运算器&#x…

Jetpack compose——深入了解Diffing

Diffing是什么 "Diffing" 是 Jetpack Compose 中用于优化性能的一种技术。它的工作原理是比较新旧 UI 树&#xff0c;并只更新实际发生变化的部分。这意味着即使你的应用有大量的 UI&#xff0c;Compose 也能保持高效的性能。 当 Composable 函数被重新调用&#x…

医学图像增强系统的设计_kaic

目录 1绪论 1.1课题背景 1.2医学图像增强以及相关理论的现状2 1.3本文内容安排 2图像增强技术 2.1空域增强方法 2.1.1空域点运算增强方法 2.1.2空域滤波增强方法 2.2频域增强算法 2.2.1低通滤波 2.2.2高通滤波 2.2.3同态滤波 2.3本章小结 3医学图像增强算法 3.1医学图像的特点 …

Unity跑酷小游戏-警察捉小偷

Unity跑酷小游戏-警察捉小偷 WRPUltimate3DEndlessRunnerKit2017 采用Unity2017版本运行 NGUI版本较旧&#xff0c;需要更新NGUI的版本或者换成UGUI Assets/NGUI/Scripts/UI/UIAnchor.cs(73,53): error CS0619: UnityEngine.RuntimePlatform.WindowsWebPlayer is obsolete:…

【观察】新五丰联合华为“躬身实践”,推动猪场实现智慧化跨越升级

中国是全球的生猪生产和消费大国&#xff0c;生猪存栏量、出栏量以及猪肉产量均居世界第一。不仅如此&#xff0c;我国的人口数量和饮食结构还决定了猪肉在国内肉类消费中具有“不可撼动”的地位&#xff0c;可以说猪肉的供应与国计民生息息相关。 数据显示&#xff0c;2022年中…

MySQL外键约束使用案例

MySQL外键约束使用 语法:FOREIGN KEY (外键列名)REFERENCES 主表(参照列)案例 创建课程表和班级表 创建学生表

Linux —— Gitee

目录 一&#xff0c;介绍 二&#xff0c;使用 一&#xff0c;介绍 用于代码托管、版本控制、多人协助等&#xff1b; Gitee是开源中国&#xff08;OSChina&#xff09;推出的基于Git的代码托管服务&#xff1b;深圳市奥思网络科技有限公司&#xff1b; 二&#xff0c;使用 网…

tidb之旅——资源管控

作者&#xff1a; 有猫万事足 原文来源&#xff1a; https://tidb.net/blog/26695303 前言 在我的设想里面&#xff0c;我应该不会这么早用到这个特性&#xff0c;原因很简单&#xff0c;整个TiDB集群根本不涉及多租户的使用场景。 应该说目前TiDB集群中的用户就2个&#x…

Mobaxterm远程桌面连接Linux

有很多远程桌面软件&#xff0c;如FastX&#xff0c;MSTSC&#xff0c;还有通过VNC、RDP协议走的。Mobaxterm作为极其优秀的软件&#xff0c;也可以这么干。但不知道为什么&#xff0c;总是设置不好&#xff08;可能是linux服务器端没设置好&#xff09;。下面记载一种方法&…

百度网盘删除“我的应用数据”文件夹

方法一&#xff1a;电脑端 工具链接&#xff0c; BaiduPCS-Go-3.6.8-windows-86.zip - 蓝奏云 电脑端下载解压运行&#xff0c;弹出浏览器窗口和命令行&#xff0c;在浏览器中输入百度网盘账号密码&#xff0c;登录。 之后会需要输入验证码&#xff0c;之后使用手机号或者邮…

Mysql查询

Mysql查询 一.DQL基础查询1.语法2.特点3.查询结果处理 二.单行函数(1)字符函数(2)逻辑处理(3)数学函数(4)日期函数 三.分组函数四.条件查询五.比较六.模糊查询七.UNION和UNION ALL(1)UNION(2)UNION ALL 八.排序九.数量限制十.分组查询 一.DQL基础查询 DQL&#xff08;Data Que…

【Java遇错】Error: failed to initialize Sentinel CommandCenterLog

问题描述&#xff1a; 引入sentinel的相关依赖之后&#xff0c;启动项目服务&#xff0c;发现如下错误 Error: failed to initialize Sentinel CommandCenterLog java.lang.NoClassDefFoundError: com/alibaba/csp/sentinel/log/LoggerSpiProviderat com.alibaba.csp.sentin…

【openGauss数据库】--运维指南04--数据导入

【openGauss数据库】--运维指南04--数据导入 &#x1f53b; 一、openGauss导入数据&#x1f530; 1.1 概述&#x1f530; 1.2 INSERT语句写入数据&#x1f530; 1.3 gsql元命令导入数据&#x1f530; 1.4 使用gs_restore命令、gsql命令导入数据&#xff08;主要&#xff09; &a…

[毕业设计baseline]tkinter+flask的毕业设计开发baseline

一.前言 最近开发了一个结合了tkinter和flask框架的GUI页面服务器。目前可以想到的开发方向有。 1.基于python的局域网聊天系统。 2.服务器管理系统。 3.网络安全防御系统。 接下来就来介绍一下这个框架以及开发方向的详细思路。如果计算机专业的本科毕业生感兴趣可以用pyt…

35.RocketMQ之Broker端消息存储文件详解

highlight: arduino-light Broker端文件详解 dubbo的核心是spi&#xff0c;看懂了spi那么dubbo基本上也懂了。对于rmq来说&#xff0c;它的核心是broker&#xff0c;而broker的核心是commitlog、consumequeue、indexfile&#xff0c;而这些文件对应的最终都是MappedFile&#x…

使用OpenCV在图像上绘制质心

这段代码中已经实现了在图像上绘制质心的功能。质心,也称为重心,是物体质量分布的几何中心,可以通过物体质量和位置的加权平均来求得。 在这个程序中,图像的质心(重心)是通过计算像素强度(可以被看作是“质量”)的加权平均位置得到的。图像上每一个像素都有一个位置(…