Netty介绍和基本代码演示

什么是Netty?

Netty是一个基于Java NIO的异步事件驱动的网络应用框架,主要用于快速开发高性能、高可靠性的网络服务器和客户端程序。它简化了网络编程的复杂性,提供了丰富的协议支持,被广泛应用于各种高性能网络应用中。

为什么选择Netty?

  1. 高性能:基于NIO,支持异步非阻塞I/O
  2. 高并发:能够处理大量并发连接
  3. 易用性:提供了简洁的API,降低了网络编程的复杂度
  4. 可扩展性:模块化设计,易于扩展和定制
  5. 稳定性:经过大量生产环境验证

核心概念

Channel(通道)

Channel是Netty网络操作的基础,代表一个到实体的连接,如硬件设备、文件、网络套接字等。

EventLoop(事件循环)

EventLoop负责处理Channel上的I/O操作,一个EventLoop可以处理多个Channel。

ChannelHandler(通道处理器)

ChannelHandler处理I/O事件,如连接建立、数据读取、数据写入等。

Pipeline(管道)

Pipeline是ChannelHandler的容器,定义了ChannelHandler的处理顺序。

基本代码演示

1. Maven依赖配置
<dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.94.Final</version>
</dependency>
2. 简单的Echo服务器
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;public class EchoServer {private final int port;public EchoServer(int port) {this.port = port;}public void start() throws Exception {// 创建boss线程组,用于接收连接EventLoopGroup bossGroup = new NioEventLoopGroup(1);// 创建worker线程组,用于处理连接EventLoopGroup workerGroup = new NioEventLoopGroup();try {// 创建服务器启动引导类ServerBootstrap bootstrap = new ServerBootstrap();bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();// 添加字符串编解码器pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());// 添加自定义处理器pipeline.addLast(new EchoServerHandler());}}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);// 绑定端口并启动服务器ChannelFuture future = bootstrap.bind(port).sync();System.out.println("Echo服务器启动成功,监听端口: " + port);// 等待服务器关闭future.channel().closeFuture().sync();} finally {// 优雅关闭线程组bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {new EchoServer(8080).start();}
}
3. 服务器处理器
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;public class EchoServerHandler extends ChannelInboundHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {String message = (String) msg;System.out.println("服务器收到消息: " + message);// 回显消息给客户端ctx.writeAndFlush("服务器回复: " + message + "\n");}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println("客户端连接: " + ctx.channel().remoteAddress());}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {System.out.println("客户端断开连接: " + ctx.channel().remoteAddress());}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}
}
4. 客户端实现
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;import java.util.Scanner;public class EchoClient {private final String host;private final int port;public EchoClient(String host, int port) {this.host = host;this.port = port;}public void start() throws Exception {EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());pipeline.addLast(new EchoClientHandler());}});// 连接服务器ChannelFuture future = bootstrap.connect(host, port).sync();Channel channel = future.channel();System.out.println("连接到服务器: " + host + ":" + port);// 从控制台读取输入并发送Scanner scanner = new Scanner(System.in);while (scanner.hasNextLine()) {String line = scanner.nextLine();if ("quit".equals(line)) {break;}channel.writeAndFlush(line + "\n");}// 关闭连接channel.closeFuture().sync();} finally {group.shutdownGracefully();}}public static void main(String[] args) throws Exception {new EchoClient("localhost", 8080).start();}
}
5. 客户端处理器
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;public class EchoClientHandler extends ChannelInboundHandlerAdapter {@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {String message = (String) msg;System.out.println("客户端收到消息: " + message);}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println("客户端连接成功");}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {cause.printStackTrace();ctx.close();}
}

运行步骤

  1. 首先运行 EchoServer 类启动服务器
  2. 然后运行 EchoClient 类启动客户端
  3. 在客户端控制台输入消息,服务器会回显消息

运行EchoServer 输出:

09:53:01.748 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
09:53:01.755 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 16
09:53:01.780 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
09:53:01.780 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
09:53:01.820 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
09:53:01.821 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
09:53:01.822 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
09:53:01.823 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
09:53:01.823 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.storeFence: available
09:53:01.824 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
09:53:01.824 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
09:53:01.825 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
09:53:01.825 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
09:53:01.825 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
09:53:01.825 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
09:53:01.826 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\H-ZHON~1\AppData\Local\Temp (java.io.tmpdir)
09:53:01.826 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
09:53:01.827 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
09:53:01.830 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 3678404608 bytes
09:53:01.830 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
09:53:01.832 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
09:53:01.832 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
09:53:01.833 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
09:53:01.833 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
09:53:01.849 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
09:53:02.186 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 2552 (auto-detected)
09:53:02.187 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
09:53:02.187 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
09:53:02.489 [main] DEBUG io.netty.util.NetUtilInitializations - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
09:53:02.490 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
09:53:02.744 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:ff:86:ff:fe:50:58:66 (auto-detected)
09:53:02.760 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
09:53:02.760 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 16
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 16
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 9
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 4194304
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimIntervalMillis: 0
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: false
09:53:02.786 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
09:53:02.796 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
09:53:02.796 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
09:53:02.796 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
Echo服务器启动成功,监听端口: 8080

运行 EchoClient,控制台输入消息, 输出:

09:55:09.541 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
09:55:09.547 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 16
09:55:09.568 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
09:55:09.568 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
09:55:09.597 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
09:55:09.597 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
09:55:09.599 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
09:55:09.599 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
09:55:09.600 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.storeFence: available
09:55:09.600 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
09:55:09.601 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
09:55:09.601 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
09:55:09.601 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
09:55:09.601 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
09:55:09.601 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
09:55:09.602 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\H-ZHON~1\AppData\Local\Temp (java.io.tmpdir)
09:55:09.602 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
09:55:09.603 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
09:55:09.605 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 3678404608 bytes
09:55:09.605 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
09:55:09.606 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
09:55:09.607 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
09:55:09.607 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
09:55:09.608 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
09:55:09.618 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
09:55:10.051 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 21460 (auto-detected)
09:55:10.054 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
09:55:10.054 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
09:55:10.475 [main] DEBUG io.netty.util.NetUtilInitializations - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
09:55:10.477 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
09:55:10.773 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:ff:86:ff:fe:50:58:66 (auto-detected)
09:55:10.786 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
09:55:10.787 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 16
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 16
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 9
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 4194304
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimIntervalMillis: 0
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: false
09:55:10.813 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
09:55:10.821 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
09:55:10.821 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
09:55:10.822 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
连接到服务器: localhost:8080
客户端连接成功09:56:56.659 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
09:56:56.659 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
09:56:56.659 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.chunkSize: 32
09:56:56.659 [main] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.blocking: false
09:56:56.672 [nioEventLoopGroup-2-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
09:56:56.672 [nioEventLoopGroup-2-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
09:56:56.673 [nioEventLoopGroup-2-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@692bca4c
客户端收到消息: 服务器回复: hello
客户端收到消息: 服务器回复: hellotestNettyServer
客户端收到消息: 服务器回复: testNettyServer

代码解析

服务器端关键点:
  1. EventLoopGroup:创建两个线程组,bossGroup负责接收连接,workerGroup负责处理连接
  1. ServerBootstrap:服务器启动引导类,配置各种参数
  1. ChannelInitializer:初始化Channel,添加编解码器和处理器
  1. Pipeline:处理器链,定义了消息处理的顺序
客户端关键点:
  1. Bootstrap:客户端启动引导类
  1. 连接建立:通过connect方法连接到服务器
  1. 消息发送:通过Channel发送消息

实际应用建议

  1. 合理配置线程池大小:根据CPU核心数和业务特点调整

感谢阅读

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

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

相关文章

[BrowserOS] Nxtscape浏览器核心 | 浏览器状态管理 | 浏览器交互层

第三章&#xff1a;Nxtscape浏览器核心 欢迎回来&#xff01; 在前两章中&#xff0c;我们了解了名为专用AI代理的专家团队及其管理者AI代理协调器&#xff0c;它们协同解析需求并规划执行步骤。 但这些代理与协调器实际运行的平台是什么&#xff1f;答案正是本章的核心——…

时序数据库处理的时序数据独特特性解析

时序数据&#xff08;Time-Series Data&#xff09;作为大数据时代增长最快的数据类型之一&#xff0c;正在物联网、金融科技、工业监控等领域产生爆炸式增长。与传统数据相比&#xff0c;时序数据具有一系列独特特性&#xff0c;这些特性直接影响了时序数据库&#xff08;Time…

uniapp各端通过webview实现互相通信

目前网上&#xff0c;包括官方文档针对uniapp的webview的内容都是基于vue2的&#xff0c;此文章基于vue3的composition API方式网页对网页 由于uniapp中的webview只支持引入h5页面&#xff0c;不支持互相通信&#xff0c;所以要条件编译&#xff0c;用iframe导入页面&#xf…

【Vue】tailwindcss + ant-design-vue + vue-cropper 图片裁剪功能(解决遇到的坑)

1.安装 vue-cropper pnpm add vue-cropper1.1.12.使用 vue-cropper <template><div class"user-info-head" click"editCropper()"><img :src"options.img" title"点击上传头像" class"img-circle" /><…

【Java】【力扣】101.对称二叉树

思路递归大问题&#xff1a;对比 左 右 是否对称参数 左和右todo 先凑合看代码/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val val; }* …

前端 oidc-client 静默刷新一直提示:Error: Frame window timed out 问题分析与解决方案

引言 在现代前端开发中&#xff0c;OAuth 2.0 和 OpenID Connect (OIDC) 协议已成为身份验证和授权的标准解决方案。oidc-client-js 是一个流行的 JavaScript 库&#xff0c;用于在前端应用中实现 OIDC 协议。其中&#xff0c;静默刷新&#xff08;Silent Renew&#xff09;是一…

DAY02:【ML 第一弹】KNN算法

一、算法简介 1.1 算法思想 如果一个样本在特征空间中的 k 个最相似的样本中的大多数属于某一个类别&#xff0c;则该样本也属于这个类别。 1.2 样本相似性 样本都是属于一个任务数据集的&#xff0c;样本距离越近则越相似。 二维平面上点的欧氏距离 二维平面上点 a(x1,y1)a(x_…

wpf 实现窗口点击关闭按钮时 ​​隐藏​​ 而不是真正关闭,并且只有当 ​​父窗口关闭时才真正退出​​ 、父子窗口顺序控制与资源安全释放​

文章目录实现方法**方法 &#xff1a;重写 OnClosing 方法****子窗口&#xff08;SettingView&#xff09;代码****父窗口&#xff08;MainWindow&#xff09;代码****关键点****适用场景**为什么if (Owner null || !Owner.IsLoaded)能够判断父窗口已经关闭**1. Owner null 检…

硬件设计学习DAY4——电源完整性设计:从概念到实战

每日更新教程&#xff0c;评论区答疑解惑&#xff0c;小白也能变大神&#xff01;" 目录 一.电源完整性 1.1电源完整性的核心概念 1.2电源完整性的三个关键目标 1.3地弹现象的通俗解释 1.4总结要点 二.电源分配网络&#xff08;PDN&#xff09;的作用 电源与GND网络…

QT跨平台应用程序开发框架(8)—— 多元素控件

目录 一&#xff0c;关于多元素控件 二&#xff0c;QListWidget 2.1 主要方法 2.2 实现新增删除 三&#xff0c;Table Widget 3.1 主要方法 3.2 代码演示 四&#xff0c;Tree Widget 4.1 主要方法 4.2 代码演示 一&#xff0c;关于多元素控件 多元素控件就是一个控件里面包含了…

【React Native】环境变量和封装 fetch

环境变量和封装fetch 环境变量 一般做开发&#xff0c;都会将接口地址配置到环境变量里。在Expo建的项目里&#xff0c;也可以使用环境变量。 在项目根目录新建一个.env文件&#xff0c;里面添加上&#xff1a; EXPO_PUBLIC_API_URLhttp://localhost:3000如果你用手机真机等…

Linux 基础命令详解:从入门到实践(1)

Linux 基础命令详解&#xff1a;从入门到实践&#xff08;1&#xff09; 前言 在 Linux 操作系统中&#xff0c;命令行是高效管理系统、操作文件的核心工具。无论是开发者、运维工程师还是Linux爱好者&#xff0c;掌握基础命令都是入门的第一步。本文将围绕Linux命令的结构和常…

基于 SpringBoot+VueJS 的私人牙科诊所管理系统设计与实现

基于 SpringBootVueJS 的私人牙科诊所管理系统设计与实现摘要随着人们对口腔健康重视程度的不断提高&#xff0c;私人牙科诊所的数量日益增多&#xff0c;对诊所管理的信息化需求也越来越迫切。本文设计并实现了一个基于 SpringBoot 和 VueJS 的私人牙科诊所管理系统&#xff0…

华为云Flexus+DeepSeek征文|体验华为云ModelArts快速搭建Dify-LLM应用开发平台并创建天气预报大模型

华为云FlexusDeepSeek征文&#xff5c;体验华为云ModelArts快速搭建Dify-LLM应用开发平台并创建天气预报大模型 什么是华为云ModelArts 华为云ModelArts ModelArts是华为云提供的全流程AI开发平台&#xff0c;覆盖从数据准备到模型部署的全生命周期管理&#xff0c;帮助企业和开…

Mysql系列--0、数据库基础

目录 一、概念 1.1什么是数据库 1.2什么是mysql 1.3登录mysql 1.4主流数据库 二、Mysql与数据库 三、Mysql架构 四、SQL分类 五、存储引擎 5.1概念 5.2查看引擎 5.3存储引擎对比 一、概念 1.1什么是数据库 由于文件保存数据存在文件的安全性问题 文件不利于数据查询和管理…

深度学习和神经网络的介绍

一.前言本期不涉及任何代码&#xff0c;本专栏刚开始和大家介绍了一下机器学习&#xff0c;而本期就是大家介绍一下深度学习还有神经网络&#xff0c;作为一个了解就好。二.深度学习2.1 什么是深度学习&#xff1f;在介绍深度学习之前&#xff0c;我们先看下⼈⼯智能&#xff0…

AI驱动的软件工程(下):AI辅助的质检与交付

&#x1f4da; 系列文章导航 AI驱动的软件工程&#xff08;上&#xff09;&#xff1a;人机协同的设计与建模 AI驱动的软件工程&#xff08;中&#xff09;&#xff1a;文档驱动的编码与执行 AI驱动的软件工程&#xff08;下&#xff09;&#xff1a;AI辅助的质检与交付 大家好…

【WRFDA实操第一期】服务器中安装 WRFPLUS 和 WRFDA

目录在服务器上下载并解压 WRF v4.6.1编译 WRFDA 及相关库安装和配置所需库安装 WRFPLUS 和 WRFDA 以运行 4DVAR 数据同化一、安装 WRFPLUS&#xff08;适用于 WRF v4.0 及以上版本&#xff09;二、安装 WRFDA&#xff08;用于 4DVAR&#xff09;WRFDA 和 WRFPLUS 的安装说明另…

【机器学习【6】】数据理解:数据导入、数据审查与数据可视化方法论

文章目录一、机器学习数据导入1、 Pandas&#xff1a;机器学习数据导入的最佳选择2、与其他方法的差异二、机器学习数据理解的系统化方法论1、数据审查方法论&#xff1a;六维数据画像技术维度1&#xff1a;数据结构审查维度2&#xff1a;数据质量检查维度3&#xff1a;目标变量…

AI炼丹日志-30-新发布【1T 万亿】参数量大模型!Kimi‑K2开源大模型解读与实践

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; AI炼丹日志-29 - 字节跳动 DeerFlow 深度研究框斜体样式架 私…