日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Netty内置处理器以及编解码器

發布時間:2025/3/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Netty内置处理器以及编解码器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、Netty已經為我們提供了常用協議的處理器,我們直接使用就可以

可以看到包括redis協議,http協議等主流協議的編碼解碼器,netty都為我們提供了。

二、代碼測試

  • 使用netty直接對redis進行操作

redis協議的詳細規定,這里不深入

@Slf4j public class SimpleRedisProtocol {public static void main(String[] args) {final byte[] LINE = {13, 10};NioEventLoopGroup worker = new NioEventLoopGroup();try {Bootstrap bootstrap = new Bootstrap();bootstrap.channel(NioSocketChannel.class);bootstrap.group(worker);bootstrap.handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) {ch.pipeline().addLast(new LoggingHandler());ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {@Overridepublic void channelActive(ChannelHandlerContext ctx) {ByteBuf buf = ctx.alloc().buffer();buf.writeBytes("*3".getBytes());buf.writeBytes(LINE);buf.writeBytes("$3".getBytes());buf.writeBytes(LINE);buf.writeBytes("set".getBytes());buf.writeBytes(LINE);buf.writeBytes("$4".getBytes());buf.writeBytes(LINE);buf.writeBytes("name".getBytes());buf.writeBytes(LINE);buf.writeBytes("$8".getBytes());buf.writeBytes(LINE);buf.writeBytes("zhangsan".getBytes());buf.writeBytes(LINE);ctx.writeAndFlush(buf);}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf buf = (ByteBuf) msg;System.out.println(buf.toString(Charset.defaultCharset()));}});}});ChannelFuture channelFuture = bootstrap.connect("localhost", 6379).sync();channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {log.error("client error", e);} finally {worker.shutdownGracefully();}} }


可以看到,接收到了來自redis服務端的響應,提示需要密碼驗證

  • http協議測試
/*** @author cristianoxiaoming@gmail.com* @version 1.0* @ClassName HttpProtocol* @Package MyNetty.Netty.protocol* @ProjectName javaStudy* @description Netty內置Http協議測試* @date 2021-09-24 23:40* @ProductName IntelliJ IDEA*/ @Slf4j public class HttpProtocol {public static void main(String[] args) {NioEventLoopGroup boss = new NioEventLoopGroup();NioEventLoopGroup worker = new NioEventLoopGroup();try {ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.channel(NioServerSocketChannel.class);serverBootstrap.group(boss, worker);serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));//HttpServerCodec已經包含編碼器和解碼器,會將http請求解析成兩部分(請求頭http request和請求行http content)ch.pipeline().addLast(new HttpServerCodec());//對上面編解碼器的結果進行處理,使用SimpleChannelInboundHandler,可以讓netty知道,我們只關心HttpRequests類型的消息,httpContent類型就不進行處理ch.pipeline().addLast(new SimpleChannelInboundHandler<HttpRequest>() {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {// 獲取請求log.debug(msg.uri());// 返回響應DefaultFullHttpResponse response =new DefaultFullHttpResponse(msg.protocolVersion(), HttpResponseStatus.OK);byte[] bytes = "<h1>Hello, world!</h1>".getBytes();//設置響應的內容長度,不然瀏覽器會不知道信息已經接收完整,而不斷刷新請求response.headers().setInt(CONTENT_LENGTH, bytes.length);response.content().writeBytes(bytes);// 寫回響應ctx.writeAndFlush(response);}});}});ChannelFuture channelFuture = serverBootstrap.bind(8080).sync();channelFuture.channel().closeFuture().sync();} catch (InterruptedException e) {log.error("server error", e);} finally {boss.shutdownGracefully();worker.shutdownGracefully();}} }


可以看到,使用netty可以非常簡單快速的編寫出一個http服務器,這就是netty的魅力啊!

總結

以上是生活随笔為你收集整理的Netty内置处理器以及编解码器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。