第7章 プロトコルの変更点
本セクションでは、ネットワークプロトコルの変更点を説明します。
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 では、このフォークを使用して作業する必要がありました。このフォークは Eclipse プロジェクトにより維持されます。フォークの使用は複雑です。
以前のリリースでは、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>
<!-- 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 コンパイラーを使用します。新しい 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>
<!-- 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) {} }
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
Eclipse Vert.x 3.x では、コードは GreeterGrpc
クラスに生成されます。
// 3.x GreeterGrpc.GreeterVertxImplBase service = new GreeterGrpc.GreeterVertxImplBase() { ... }
// 3.x
GreeterGrpc.GreeterVertxImplBase service =
new GreeterGrpc.GreeterVertxImplBase() {
...
}
Eclipse Vert.x 4 では、コードは VertxGreeterGrpc
クラスに生成されます。
// 4.x VertxGreeterGrpc.GreeterVertxImplBase service = new VertxGreeterGrpc.GreeterVertxImplBase() { ... }
// 4.x
VertxGreeterGrpc.GreeterVertxImplBase service =
new VertxGreeterGrpc.GreeterVertxImplBase() {
...
}
7.1.3. gRPC API による future のサポート
Eclipse Vert.x 4 では、gRPC API は future をサポートします。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()); } }
// 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 での future の使用を示しています。
// 4.x VertxGreeterGrpc.GreeterVertxImplBase service = new VertxGreeterGrpc.GreeterVertxImplBase() { @Override public Future<HelloReply> sayHello(HelloRequest request) { return Future.succeededFuture( HelloReply.newBuilder() .setMessage(request.getName()) .build()); } }
// 4.x
VertxGreeterGrpc.GreeterVertxImplBase service =
new VertxGreeterGrpc.GreeterVertxImplBase() {
@Override
public Future<HelloReply> sayHello(HelloRequest request) {
return Future.succeededFuture(
HelloReply.newBuilder()
.setMessage(request.getName())
.build());
}
}