Google gRPC 编写的注意事项

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

一 POM 文件准备

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

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>io.grpc</groupId>
  5. <artifactId>examples</artifactId>
  6. <packaging>jar</packaging>
  7. <!-- Feel free to delete the comment at the end of these lines. It is just
  8. for safely updating the version in our release process. -->
  9. <version>1.15.0</version><!-- CURRENT_GRPC_VERSION -->
  10. <name>examples</name>
  11. <url>http://maven.apache.org</url>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <grpc.version>1.15.0</grpc.version><!-- CURRENT_GRPC_VERSION -->
  15. <protobuf.version>3.5.1</protobuf.version>
  16. <protoc.version>3.5.1-1</protoc.version>
  17. <netty.tcnative.version>2.0.7.Final</netty.tcnative.version>
  18. <!-- required for jdk9 -->
  19. <maven.compiler.source>1.7</maven.compiler.source>
  20. <maven.compiler.target>1.7</maven.compiler.target>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>io.grpc</groupId>
  25. <artifactId>grpc-netty-shaded</artifactId>
  26. <version>${grpc.version}</version>
  27. </dependency>
  28. <dependency>
  29. <groupId>io.grpc</groupId>
  30. <artifactId>grpc-protobuf</artifactId>
  31. <version>${grpc.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>io.grpc</groupId>
  35. <artifactId>grpc-stub</artifactId>
  36. <version>${grpc.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>io.grpc</groupId>
  40. <artifactId>grpc-alts</artifactId>
  41. <version>${grpc.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>io.grpc</groupId>
  45. <artifactId>grpc-testing</artifactId>
  46. <version>${grpc.version}</version>
  47. <scope>test</scope>
  48. </dependency>
  49. <!-- Used in HelloWorldServerTls -->
  50. <dependency>
  51. <groupId>io.grpc</groupId>
  52. <artifactId>grpc-netty</artifactId>
  53. <version>${grpc.version}</version>
  54. </dependency>
  55. <dependency>
  56. <groupId>io.netty</groupId>
  57. <artifactId>netty-tcnative-boringssl-static</artifactId>
  58. <version>${netty.tcnative.version}</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>com.google.api.grpc</groupId>
  62. <artifactId>proto-google-common-protos</artifactId>
  63. <version>1.0.0</version>
  64. </dependency>
  65. <dependency>
  66. <groupId>com.google.protobuf</groupId>
  67. <artifactId>protobuf-java-util</artifactId>
  68. <version>${protobuf.version}</version>
  69. </dependency>
  70. <dependency>
  71. <groupId>junit</groupId>
  72. <artifactId>junit</artifactId>
  73. <version>4.12</version>
  74. <scope>test</scope>
  75. </dependency>
  76. <dependency>
  77. <groupId>org.mockito</groupId>
  78. <artifactId>mockito-core</artifactId>
  79. <version>1.9.5</version>
  80. <scope>test</scope>
  81. </dependency>
  82. </dependencies>
  83. <build>
  84. <extensions>
  85. <extension>
  86. <groupId>kr.motd.maven</groupId>
  87. <artifactId>os-maven-plugin</artifactId>
  88. <version>1.5.0.Final</version>
  89. </extension>
  90. </extensions>
  91. <plugins>
  92. <plugin>
  93. <groupId>org.xolstice.maven.plugins</groupId>
  94. <artifactId>protobuf-maven-plugin</artifactId>
  95. <version>0.5.1</version>
  96. <configuration>
  97. <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
  98. <pluginId>grpc-java</pluginId>
  99. <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
  100. </configuration>
  101. <executions>
  102. <execution>
  103. <goals>
  104. <goal>compile</goal>
  105. <goal>compile-custom</goal>
  106. </goals>
  107. </execution>
  108. </executions>
  109. </plugin>
  110. <plugin>
  111. <groupId>org.apache.maven.plugins</groupId>
  112. <artifactId>maven-enforcer-plugin</artifactId>
  113. <version>1.4.1</version>
  114. <executions>
  115. <execution>
  116. <id>enforce</id>
  117. <goals>
  118. <goal>enforce</goal>
  119. </goals>
  120. <configuration>
  121. <rules>
  122. <requireUpperBoundDeps/>
  123. </rules>
  124. </configuration>
  125. </execution>
  126. </executions>
  127. </plugin>
  128. </plugins>
  129. </build>
  130. </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 编写客户端代码

相关文章