Google gRPC 编写的注意事项

x33g5p2x  于2022-06-06 转载在 其他  
字(5.5k)|赞(0)|评价(0)|浏览(264)

一 POM 文件准备

pom 文件可从官方提供的 example 中的 pom 文件中提取。官方提供如下。

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.grpc</groupId>
  <artifactId>examples</artifactId>
  <packaging>jar</packaging>
  <!-- Feel free to delete the comment at the end of these lines. It is just
       for safely updating the version in our release process. -->
  <version>1.15.0</version><!-- CURRENT_GRPC_VERSION -->
  <name>examples</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <grpc.version>1.15.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
    <protobuf.version>3.5.1</protobuf.version>
    <protoc.version>3.5.1-1</protoc.version>
    <netty.tcnative.version>2.0.7.Final</netty.tcnative.version>
    <!-- required for jdk9 -->
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-netty-shaded</artifactId>
      <version>${grpc.version}</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-protobuf</artifactId>
      <version>${grpc.version}</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-stub</artifactId>
      <version>${grpc.version}</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-alts</artifactId>
      <version>${grpc.version}</version>
    </dependency>
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-testing</artifactId>
      <version>${grpc.version}</version>
      <scope>test</scope>
    </dependency>

    <!-- Used in HelloWorldServerTls -->
    <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-netty</artifactId>
      <version>${grpc.version}</version>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-tcnative-boringssl-static</artifactId>
      <version>${netty.tcnative.version}</version>
    </dependency>

    <dependency>
      <groupId>com.google.api.grpc</groupId>
      <artifactId>proto-google-common-protos</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java-util</artifactId>
      <version>${protobuf.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>1.9.5</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <extensions>
      <extension>
        <groupId>kr.motd.maven</groupId>
        <artifactId>os-maven-plugin</artifactId>
        <version>1.5.0.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:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
          <pluginId>grpc-java</pluginId>
          <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>compile-custom</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.4.1</version>
        <executions>
          <execution>
            <id>enforce</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireUpperBoundDeps/>
              </rules>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

从这份文件中,我们可以获得以下两点。

1 gRPC 实际使用了 Netty

实际上,gRPC 可以使用3种技术来实现数据的网络传输:Netty、OKHttp 和 inProcess。

2 gRPC 对 Protobuf 做了进一步改进

Protobuf 使用 protoc.exe 对消息进行编码和解码,但不能通过网络传输数据;gRPC 为了弥补这个缺陷,提供了一个自己编译插件 generateProto。

如果使用 Maven 工具进行构建,可以参考 https://github.com/grpc/grpc-java 安装 generateProto

二 代码实现方式

在 gRPC 中,客户端与服务端在通信时,各自都有两种实现方式。一种是使用 Request 对象发送请求,再用 Response 对象返回响应;二是发送请求和做出响应都使用 Stream 对象,因此,客户端在和服务端进行双向交互时,有如下四种实现方式。

1 客户端向服务端发送一个 Request 对象,服务端接收并处理后,再给客户端响应一个 Response 对象。

2 客户端向服务端发送一个 Request 对象,服务端接收并处理后,再通过一个 Stream 对象响应客户端。

3 客户端向服务端发送一个 Stream 对象,服务端接收并处理后,再给客户端响应一个 Response 对象。

4 客户端向服务端发送一个 Stream 对象,服务端接收并处理后,再通过一个 Stream 对象响应客户端。

在具体实现时,还需要注意一下两点。

1 在 gRPC 中,上述 Request 对象和 Response 对象都必须是在 .proto 文件中定义的 message,而不能是普通的数据类型。

2 客户端如果以 Stream 方式发出请求,则此请求是异步的。

三 关于 proto 文件

gRPC 文件所有的 proto 文件必须存放在项目的 src/main/proto 或 src/test/proto 目录下。

四 gRPC 一般需要编写下面四类文件

1 proto 文件定义数据结构和接口

2 编写接口实现类

3 编写服务端代码

4 编写客户端代码

相关文章