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
英文地址,請注明出處:http://en.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 深度研究框斜體樣式架 私…