介绍
Vert.x gRPC 模块,它将 Google gRPC 的编程风格与 Vert.x 风格保持一致. gRPC 是一个高性能、开源、通用的RPC框架,是一种进程间通讯技术,由Google推出,基于HTTP2协议标准设计开发,默认采用Protocol Buffers数据序列化协议,支持多种开发语言。gRPC提供了一种简单的方法来精确的定义服务,并且为客户端和服务端自动生成可靠的功能库。 在gRPC客户端可以直接调用不同服务器上的远程程序,使用姿势看起来就像调用本地程序一样,轻松的实现远端服务的连接、调用、操作和调试,很容易去构建分布式应用和服务。和很多RPC系统一样,服务端负责实现定义好的接口并处理客户端的请求, 客户端根据接口描述直接调用需要的服务。客户端和服务端可以分别使用gRPC支持的不同语言实现。
1. maven项目依赖
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-grpc</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.lance.common</groupId>
<artifactId>vertx-common-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2.vertx启动公用类
public class StartApplication {
public static void runClustered(Class<? extends AbstractVerticle> clazz) {
run(clazz, new VertxOptions(), null, true);
}
public static void run(Class<? extends AbstractVerticle> clazz) {
run(clazz, new VertxOptions(), null, false);
}
public static void run(Class<? extends AbstractVerticle> clazz, DeploymentOptions options) {
run(clazz, new VertxOptions(), options, false);
}
public static void run(Class<? extends AbstractVerticle> clazz, VertxOptions options, DeploymentOptions deploymentOptions, boolean clustered) {
run(clazz.getName(), options, deploymentOptions, clustered);
}
public static void run(String className, VertxOptions options, DeploymentOptions deploymentOptions, boolean clustered) {
if (options == null) {
// Default parameter
options = new VertxOptions();
}
Consumer<Vertx> runner = vertx -> {
try {
if (deploymentOptions != null) {
vertx.deployVerticle(className, deploymentOptions);
} else {
vertx.deployVerticle(className);
}
} catch (Throwable t) {
log.error("Vertx deploy fail: ", t);
}
};
if (clustered) {
Vertx.clusteredVertx(options, res -> {
if (res.succeeded()) {
Vertx vertx = res.result();
runner.accept(vertx);
} else {
res.cause().printStackTrace();
}
});
} else {
Vertx vertx = Vertx.vertx(options);
runner.accept(vertx);
}
}
}
3.Grpc 服务端
public class HelloServer extends AbstractVerticle {
public static void main(String[] args) {
StartApplication.run(HelloServer.class);
}
@Override
public void start() {
// Create the server
VertxServer server = VertxServerBuilder.forAddress(vertx, RpcConst.HOST, RpcConst.PORT)
.addService(new VertxGreeterGrpc.GreeterVertxImplBase() {
@Override
public Future<Hello.HelloReply> sayHello(Hello.HelloRequest request) {
log.info("Hello {}", request.getName());
String message = "Thanks " + request.getName();
return Future.succeededFuture(Hello.HelloReply.newBuilder().setMessage(message).build());
}
})
.build();
// start the server
server.start(ar -> {
if (ar.failed()) {
log.error("Grpc server start fail: ", ar.cause());
} else {
log.info("Grpc server[port: {}] start success.", server.getPort());
}
});
}
}
4.Grpc客户端
public class HelloClient extends AbstractVerticle {
public static void main(String[] args) {
StartApplication.run(HelloClient.class);
}
@Override
public void start() {
// Create the channel
ManagedChannel channel = VertxChannelBuilder
.forAddress(vertx, RpcConst.HOST, RpcConst.PORT)
.usePlaintext()
.build();
VertxGreeterGrpc.GreeterVertxStub stub = VertxGreeterGrpc.newVertxStub(channel);
Hello.HelloRequest request = Hello.HelloRequest.newBuilder().setName("Jim Green").build();
stub.sayHello(request).onComplete(asyncResponse -> {
if (asyncResponse.succeeded()) {
log.info("Got the server response: {}", asyncResponse.result().getMessage());
} else {
log.error("Could not reach server fail: ", asyncResponse.cause());
}
});
}
}
5.Proto文件
syntax = "proto3";
option java_multiple_files = false;
option java_outer_classname = "Hello";
package com.lance.grpc.hello.gen;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
6.日志
2022-02-13 20:22:19.301 INFO 21 --- [ntloop-thread-1] com.lance.grpc.hello.HelloClient ---[ 38] : Got the server response: Thanks .Jim Green
2022-02-13 20:21:42.852 INFO 21 --- [ntloop-thread-1] com.lance.grpc.hello.HelloServer ---[ 45] : Grpc server[port: 18006] start success.
2022-02-13 20:22:19.254 INFO 21 --- [ntloop-thread-1] com.lance.grpc.hello.HelloServer ---[ 33] : Hello Jim Green
7.项目完整地址
Vertx之Grpc消息发送 Github 地址
Vertx之Grpc消息发送 Gitee 地址
|