搜索

第 7 章 协议的更改

download PDF

本节解释了网络协议中的更改。

7.1. Eclipse Vert.x gRPC 的更改

下面的部分论述了 Eclipse Vert.x gRPC 中的更改。

7.1.1. 新的 gRPC 编译器插件

在 Eclipse Vert.x 4 中,模块 protoc-gen-grpc-java 不再可用。这个模块是官方 gRPC 编译器的一个分叉。在较早版本的 Eclipse Vert.x 中,您必须使用这个 fork。此分叉由 Eclipse 项目维护。使用 fork 很复杂。

在以前的版本中,为了使用 gRPC,在 pom.xml 文件中添加以下详情。

  <!-- Vert.x 3.x -->
<plugin>
  <groupId>org.xolstice.maven.plugins</groupId>
  <artifactId>protobuf-maven-plugin</artifactId>
  <configuration>
    <protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}</protocArtifact>
    <pluginId>grpc-java</pluginId>
    <!-- NOTE: the gav coordinates point to the 3.x only compiler fork -->
    <pluginArtifact>io.vertx:protoc-gen-grpc-java:${vertx.grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
  </configuration>
  ...
</plugin>

在 Eclipse Vert.x 4 中,提供了一个新的 gRPC 编译器插件。此插件使用官方 gRPC 编译器而不是 fork。要使用新的 gRPC 插件,请在 pom.xml 文件中添加以下详情。

    <!-- Vert.x 4.x -->
<plugin>
  <groupId>org.xolstice.maven.plugins</groupId>
  <artifactId>protobuf-maven-plugin</artifactId>
  <configuration>
    <protocArtifact>com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier}</protocArtifact>
    <pluginId>grpc-java</pluginId>
    <!-- NOTE: the gav coordinates point to the official compiler -->
    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${vertx.grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
    <protocPlugins>
      <!-- NEW: a plugin is added to generate the Vert.x specific code -->
      <protocPlugin>
        <id>vertx-grpc-protoc-plugin</id>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-grpc-protoc-plugin</artifactId>
        <version>${vertx.version}</version>
        <mainClass>io.vertx.grpc.protoc.plugin.VertxGrpcGenerator</mainClass>
      </protocPlugin>
    </protocPlugins>
  </configuration>
  ...
</plugin>

7.1.2. 迁移生成的代码

在 Eclipse Vert.x 4 中,使用新的编译器。当使用新的 gRPC 插件时,生成的代码不会使用相同的源文件写入。这是因为编译器不允许在其基础类上生成自定义代码。插件必须生成一个具有不同名称的新类来保存代码。

在较早版本的 Eclipse Vert.x 中,旧的 gRPC 插件会在同一源文件中写入生成的代码。

例如,如果您有以下描述符:

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

在 Eclipse Vert.x 3.x 中,代码会在 GreeterGrpc 类中生成。

// 3.x
GreeterGrpc.GreeterVertxImplBase service =
  new GreeterGrpc.GreeterVertxImplBase() {
      ...
  }

在 Eclipse Vert.x 4 中,代码在 VertxGreeterGrpc 类中生成。

// 4.x
VertxGreeterGrpc.GreeterVertxImplBase service =
  new VertxGreeterGrpc.GreeterVertxImplBase() {
      ...
  }

7.1.3. gRPC API 支持将来的

在 Eclipse Vert.x 4 中,gRPC API 支持将来的版本。gRPC 插件会生成混杂的 API。这些 API 使用标准 Eclipse Vert.x 输入和输出参数,这有助于创建标准 Eclipse Vert.x 应用程序。

以下示例显示了在 Eclipse Vert.x 3.x 中使用 promise。

// 3.x
GreeterGrpc.GreeterVertxImplBase service =
  new GreeterGrpc.GreeterVertxImplBase() {
        @Override
        public void sayHello(HelloRequest request, Promise<HelloReply> future) {
          future.complete(
              HelloReply.newBuilder().setMessage(request.getName()).build());
        }
  }

以下示例显示了在 Eclipse Vert.x 4 中使用未来。

// 4.x
VertxGreeterGrpc.GreeterVertxImplBase service =
  new VertxGreeterGrpc.GreeterVertxImplBase() {
      @Override
      public Future<HelloReply> sayHello(HelloRequest request) {
        return Future.succeededFuture(
          HelloReply.newBuilder()
            .setMessage(request.getName())
            .build());
      }
  }
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.