最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Netty結(jié)合Protobuf進(jìn)行編解碼的方法

 更新時(shí)間:2021年06月25日 17:34:34   作者:xiah_jp  
這篇文章主要介紹了Netty結(jié)合Protobuf進(jìn)行編解碼,通過(guò)文檔表述和代碼實(shí)例充分說(shuō)明了如何進(jìn)行使用和操作,需要的朋友可以參考下

 一般在使用netty時(shí),數(shù)據(jù)傳輸?shù)臅r(shí)候都會(huì)選擇對(duì)傳輸?shù)臄?shù)據(jù)進(jìn)行編解碼,編碼后的數(shù)據(jù)變小, 有利于在有限的帶寬下傳輸更多的數(shù)據(jù)。

由于java本身序列化的缺點(diǎn)較多(無(wú)法跨語(yǔ)言,序列化后的碼流太大,序列化的性能太低等),業(yè)界主流的編解碼框架主要有如下三個(gè):

  1. Google的Protobuf
  2. Facebook的Thrift
  3. JBoss的Marshalling

今天我們簡(jiǎn)單介紹一下Netty結(jié)合google的Protobuf框架進(jìn)行數(shù)據(jù)的編解碼。

1. 什么是Protobuf?

Protobuf全稱(chēng)是Google Protocol Buffers, 它是谷歌公司開(kāi)源的一個(gè)序列化框架。

它將數(shù)據(jù)結(jié)構(gòu)以.proto文件進(jìn)行描述,通過(guò)代碼生成工具可以生成對(duì)應(yīng)數(shù)據(jù)結(jié)構(gòu)的POJO對(duì)象和Protobuf相關(guān)的方法和屬性。

它的特點(diǎn)如下:

  1. 結(jié)構(gòu)化數(shù)據(jù)存儲(chǔ)格式
  2. 高效的編解碼性能
  3. 語(yǔ)言無(wú)關(guān)、平臺(tái)無(wú)關(guān)、擴(kuò)展性好
  4. 官方支持多個(gè)語(yǔ)言(java,c++,python,c#等)

2. 下載安裝

Protobuf已經(jīng)托管在github上,可以在release頁(yè)面下載,本案例使用的是v3.6(要下載后綴為-win32.zip的)。

下載后,將壓縮包進(jìn)行解壓。這里主要用到bin目錄下的protoc.exe。

3. 定義好proto文件

SubscribeReq.proto

// 區(qū)分不同的protobuf版本,必須有
syntax = "proto2";
 
package netty;
// 生成的目標(biāo)類(lèi)的包路徑
option java_package = "cn.ddlover.nettystudy.protobuf";
// 生成的目標(biāo)類(lèi)的名字
option java_outer_classname = "SubscribeReqProto";
 
message SubscribeReq{
    required int32 subReqID = 1;
    required string userName = 2;
    required string productName = 3;
    repeated string address = 4;
}

SubscribeResp.proto

syntax = "proto2";
package netty;
 
option java_package = "cn.ddlover.nettystudy.protobuf";
option java_outer_classname = "SubscribeRespProto";
 
message SubscribeResp{
    required int32 subReqID = 1;
    required int32 respCode = 2;
    required string desc = 3;
}

此時(shí)項(xiàng)目結(jié)構(gòu)如下

重點(diǎn)關(guān)注一下兩個(gè)proto文件的位置,是位于項(xiàng)目的根路徑下

4. 分別執(zhí)行以下命令

D:\xhb\protoc-3.6.1-win32\bin\protoc.exe --java_out=.\src\main\java SubscribeResp.proto 

D:\xhb\protoc-3.6.1-win32\bin\protoc.exe --java_out=.\src\main\java SubscribeReq.proto 

這里需要把protoc.exe的位置換成自己的, --java_out命令設(shè)置的是proto文件中java_package指令的父目錄。

5. 此時(shí)項(xiàng)目結(jié)構(gòu)如下

下面開(kāi)始代碼方面的開(kāi)發(fā),

1. 先貼一波maven的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>cn.ddlover</groupId>
    <artifactId>nettystudy</artifactId>
    <version>1.0-SNAPSHOT</version>
 
 
    <properties>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.33.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.6.1</version>
        </dependency>
 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
        </dependency>
    </dependencies>
</project>

2. 這里貼上完整代碼的項(xiàng)目結(jié)構(gòu)

3. SubReqServer.java

 
import cn.ddlover.nettystudy.handler.SubReqServerHandler;
import cn.ddlover.nettystudy.protobuf.SubscribeReqProto;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
 
/**
 * Protobuf版本圖書(shū)訂購(gòu)代碼
 */
public class SubReqServer {
    private static final int PORT = 8080;
 
    public static void main(String[] args) {
        bind(PORT);
    }
 
    private static void bind(int port) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
 
        ServerBootstrap b = new ServerBootstrap();
        try {
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 100)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            // 半包處理
                            socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                            socketChannel.pipeline().addLast(new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()));
                            socketChannel.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                            socketChannel.pipeline().addLast(new ProtobufEncoder());
                            socketChannel.pipeline().addLast(new SubReqServerHandler());
                        }
                    });
            ChannelFuture future = b.bind(port).sync();
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}

4. SubReqServerHandler.java

import cn.ddlover.nettystudy.protobuf.SubscribeReqProto;
import cn.ddlover.nettystudy.protobuf.SubscribeRespProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
 
public class SubReqServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq)msg;
 
        if ("張三".equals(req.getUserName())) {
            System.out.println("Server accept clietn subscribe req : ["+req.toString()+"]");
            ctx.writeAndFlush(resp(req.getSubReqID()));
        }
    }
 
    private SubscribeRespProto.SubscribeResp resp(int subReqID) {
        SubscribeRespProto.SubscribeResp.Builder builder = SubscribeRespProto.SubscribeResp.newBuilder();
        builder.setSubReqID(subReqID);
        builder.setRespCode(0);
        builder.setDesc("netty書(shū)籍下單成功,3天后將會(huì)送到你的住處");
        return builder. build();
    }
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

5. SubReqClient.java

import cn.ddlover.nettystudy.handler.SubReqClientHandler;
import cn.ddlover.nettystudy.protobuf.SubscribeRespProto;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
 
public class SubReqClient {
    private static final String HOST = "localhost";
    private static final int PORT = 8080;
 
    public static void main(String[] args) {
        connect(HOST, PORT);
    }
 
    private static void connect(String host, int port) {
        EventLoopGroup group = new NioEventLoopGroup();
 
        Bootstrap b = new Bootstrap();
        try {
            b.group(group).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
 
                            // 半包處理
                            socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                            socketChannel.pipeline().addLast(new ProtobufDecoder(SubscribeRespProto.SubscribeResp.getDefaultInstance()));
                            socketChannel.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                            socketChannel.pipeline().addLast(new ProtobufEncoder());
                            socketChannel.pipeline().addLast(new SubReqClientHandler());
                        }
                    });
            ChannelFuture f = b.connect(host, port).sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
}

6. SubReqClientHandler.java

import cn.ddlover.nettystudy.protobuf.SubscribeReqProto;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
 
import java.util.ArrayList;
import java.util.List;
 
public class SubReqClientHandler extends ChannelInboundHandlerAdapter {
 
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i =0;i<10;i++) {
            ctx.write(subReq(i));
        }
        ctx.flush();
    }
 
    private SubscribeReqProto.SubscribeReq subReq(int i) {
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
        builder.setSubReqID(i);
        builder.setUserName("張三");
        builder.setProductName("Netty Book");
        List<String> address = new ArrayList<>();
        address.add("NanJing YuHuaTai");
        address.add("BeiJing LiuLiChang");
        address.add("ShenZhen HongShuLin");
        builder.addAllAddress(address);
        return builder.build();
    }
 
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("Receive server response : ["+ msg +"]");
    }
 
 
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}

代碼部分到此就結(jié)束了,然后分別運(yùn)行SubReqServerSubReqClient, 就可以發(fā)現(xiàn)運(yùn)行成功了。

這里要注意的事,調(diào)用toString的時(shí)候,中文在控制臺(tái)打印的是字節(jié),而調(diào)用對(duì)應(yīng)屬性的getter方法的時(shí)候,是可以做中文的判斷的,顯然這里protobuf的toString沒(méi)有被修改好呀。

當(dāng)然,我們也可以發(fā)現(xiàn),netty本身已經(jīng)封裝好了對(duì)谷歌的protobuf的支持。Netty還是很強(qiáng)大的。

到此這篇關(guān)于Netty結(jié)合Protobuf進(jìn)行編解碼的文章就介紹到這了,更多相關(guān)Netty結(jié)合Protobuf編解碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java開(kāi)源診斷工具Arthas使用方法詳解

    Java開(kāi)源診斷工具Arthas使用方法詳解

    這篇文章主要介紹了Java開(kāi)源診斷工具Arthas使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Java組件commons fileupload實(shí)現(xiàn)文件上傳功能

    Java組件commons fileupload實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Java組件commons fileupload實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • SpringBoot啟動(dòng)過(guò)程逐步分析講解

    SpringBoot啟動(dòng)過(guò)程逐步分析講解

    這篇文章主要介紹了SpringBoot啟動(dòng)過(guò)程的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2023-01-01
  • Spring加載配置和讀取多個(gè)Properties文件的講解

    Spring加載配置和讀取多個(gè)Properties文件的講解

    今天小編就為大家分享一篇關(guān)于Spring加載配置和讀取多個(gè)Properties文件的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • springBoo3.0集成knife4j4.1.0的詳細(xì)教程(swagger3)

    springBoo3.0集成knife4j4.1.0的詳細(xì)教程(swagger3)

    這篇文章主要介紹了springBoo3.0集成knife4j4.1.0的詳細(xì)教程(swagger3),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • 基于SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式

    基于SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式

    這篇文章主要介紹了SpringBoot應(yīng)用監(jiān)控Actuator安全隱患及解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • spring boot整合Shiro實(shí)現(xiàn)單點(diǎn)登錄的示例代碼

    spring boot整合Shiro實(shí)現(xiàn)單點(diǎn)登錄的示例代碼

    本篇文章主要介紹了spring boot整合Shiro實(shí)現(xiàn)單點(diǎn)登錄的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • JVM常用垃圾收集器及GC算法解讀

    JVM常用垃圾收集器及GC算法解讀

    這篇文章主要介紹了JVM常用垃圾收集器及GC算法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 使用mybatis攔截器處理敏感字段

    使用mybatis攔截器處理敏感字段

    這篇文章主要介紹了mybatis攔截器處理敏感字段方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot中對(duì)SpringMVC的自動(dòng)配置詳解

    SpringBoot中對(duì)SpringMVC的自動(dòng)配置詳解

    這篇文章主要介紹了SpringBoot中的SpringMVC自動(dòng)配置詳解,Spring MVC自動(dòng)配置是Spring Boot提供的一種特性,它可以自動(dòng)配置Spring MVC的相關(guān)組件,簡(jiǎn)化了開(kāi)發(fā)人員的配置工作,需要的朋友可以參考下
    2023-10-10

最新評(píng)論

阳江市| 弋阳县| 尚志市| 安庆市| 辽阳县| 汾阳市| 正镶白旗| 无锡市| 大悟县| 正安县| 景洪市| 潼关县| 新源县| 嘉义县| 通海县| 梁平县| 德化县| 廊坊市| 安庆市| 孟村| 渑池县| 满洲里市| 手游| 赫章县| 怀宁县| 固原市| 翁牛特旗| 延庆县| 诸城市| 普洱| 武宁县| 武威市| 永城市| 子长县| 板桥市| 海宁市| 区。| 汾西县| 南汇区| 远安县| 盐亭县|