<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 一、概述 [ProtoBuf](https://github.com/protocolbuffers/protobuf)是一種數據格式,或者說序列化框架;與其他幾種主流數據格式比較,它有如下特點: * **JSON**:一般在web項目中廣泛使用,主要是由于瀏覽器的支持非常好,內部構建了與多函數來支持JSON。具有可讀性; * **XML**:在WebService中廣泛使用,但是過于冗余(畢竟是通過標簽進行標識)。也具有可讀性; * **ProtoBuf**:適合高性能,對響應速度有要求的數據傳輸場景。由于是二進制數據格式需要編碼和解碼。不具有可讀性。在官網上描述ProtoBuf比XML小3到10倍,快20到100倍; ## 二、快速入門 ### **核心原理** 核心上,通訊的產生者通過**proto文件**生成二進制傳送到接受端,接收端依據proto文件,解析出原始內容; ### **結構定義** ~~~protobuf message xxx { 字段規則 類型 名稱 = 字段編號; } ~~~ >[info] 規則: > // 字段規則:required -> 字段只能也必須出現 1 次 > // 字段規則:optional -> 字段可出現 0 次或1次 > // 字段規則:repeated -> 字段可出現任意多次(包括 0) > // 數據類型:int32、int64、sint32、sint64、string、32-bit .... > // 字段編號:0 ~ 536870911(除去 19000 到 19999 之間的數字) > | .proto Type | 說明 | C++ Type | Java Type | Python Type\[2\] | Go Type | | --- | :-- | :-- | :-- | :-- | :-- | | double | ? | double | double | float | float64 | | float | ? | float | float | float | float32 | | int32 | 使用可變長度編碼。對負數進行編碼時比較低效 – 如果你的字段要使用負數值,請使用sint32來代替。 | int32 | int | int | int | | int64 | 使用可變長度編碼。對負數進行編碼時比較低效 – 如果你的字段要使用負數值,請使用sint64來代替。 | int64 | long | int/long\[3\] | int64 | | uint32 | 使用可變長度編碼 | uint32 | int\[1\] | int/long\[3\] | uint32 | | uint64 | 使用可變長度編碼 | uint64 | long\[1\] | int/long\[3\] | uint64 | ### **基于protoc.exe把proto文件轉為java數據結構文件** 舉例: 一個簡單的proto文件: ``` syntax = "proto3"; package org.ray.framework.sample; import "google/protobuf/timestamp.proto"; option java_multiple_files = true; option java_package = "org.ray.framework.sample"; option java_outer_classname = "AddressBookProto"; // [START messages] message Person { string name = 1; int32 id = 2; // Unique ID number for this person. string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { string number = 1; PhoneType type = 2; } repeated PhoneNumber phones = 4; google.protobuf.Timestamp last_updated = 5; } // Our address book file is just one of these. message AddressBook { repeated Person people = 1; } // [END messages] ``` ``` protoc --java_out=./javasrc ./AddressBook.proto ``` ![](https://img.kancloud.cn/6b/e3/6be3895e25cc3c7c567eb7b2da15e25e_677x457.png) 可以看到,javasrc目錄下,生成了對應的文件; ![](https://img.kancloud.cn/0c/e1/0ce1d2c3a36ef5ae8a543f39b785d587_804x287.png) ### **使用java文件** 將生成的java文件,納入java工程,基于protobuf-java; ![](https://img.kancloud.cn/7c/0f/7c0fa7a932e41af19a334e459d6b2347_1366x736.png) 需先確保導入了protobuf-java類庫; ``` <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>3.17.3</version> </dependency> ``` 簡單的使用,將java對象轉為protoc報文: ``` public static void main(String... args) { org.ray.framework.sample.Person.Builder builder = Person.newBuilder().setEmail("a").setId(1).setName("ok"); byte[] messageBody = builder.build().toByteArray(); System.out.println("java對象轉protoprof:"+new String(messageBody)); // Person person = Person.parseFrom(messageBody); System.out.println("protoprof轉java對象:"+person.toString()); } ``` 效果: ![](https://img.kancloud.cn/5b/57/5b572c8bc5285949b5d7a8daae3715ab_1366x736.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看