1. 程式人生 > >Google Protocol Buffer 的使用(一)

Google Protocol Buffer 的使用(一)

一、什麼是Google Protocol Buffer
下面是官網給的解釋:
Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. – think XML, but smaller, faster, and simpler.
協議緩衝區是一種和語言無關、平臺無關的可擴充套件機制,用於序列化結構化的資料。相比於xml,它更小,更快,更簡單。
資料緩衝區常用語通訊協議和資料儲存。
下面兩個網站是別人做的效率測試實驗:
https://code.google.com/archive/p/thrift-protobuf-compare/wikis/Benchmarking.wiki


https://github.com/eishay/jvm-serializers/wiki

二、環境安裝(Intellij IDEA外掛安裝)
Intellij中的“File”-->"Settings"-->"Plugins" 中搜索Protobuf外掛安裝,重啟生效。重啟後.proto檔案會高亮顯示。安裝如下圖:

三、建立Protocol Buffer檔案
Protocol Buffer檔案是.proto檔案。穿件檔案xxx.proto放在名為proto的檔案加下,例項:

syntax = "proto3";

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

四、java建立maven工程
pom檔案:

<?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">
    <parent>
        <artifactId>google</artifactId>
        <groupId>test.tom</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>protobuf</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <grpc.version>1.6.1</grpc.version>
        <protobuf.version>3.4.0</protobuf.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty</artifactId>
            <version>${grpc.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>${grpc.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>${grpc.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>${protobuf.version}</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.2.Final</version>
        </dependency>
        <!-- 引入log4j2  -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.6.1</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>protobuf</finalName>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.4.1.Final</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.1</version>
                <configuration>
                    <protocArtifact>
                        com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

五、編譯生成程式碼
通過Maven外掛protobuf雙擊compile編譯生成程式碼。生在程式碼預設在target中,拷貝到你的工程中即可使用。

參考:
Google Protocol Buffer的原始碼地址: https://github.com/protocolbuffers/protobuf
Google Protocol Buffer的官方文件: https://developers.google.com/protocol-buffers/