本文共 4351 字,大约阅读时间需要 14 分钟。
gRPC(Google Remote Procedure Call)是一种高性能的远程调用框架,基于Protocol Buffers(Protobuf)进行数据序列化。其独特之处在于支持多语言扩展、自动生成服务接口,并通过简单命令即可搭建完整的RPC运行环境。在以下文中,我们将通过一个具体实践案例,详细讲述gRPC的开发流程。
首先,我们创建一个基于Maven的项目GrpcAPI。项目结构如下:
GrpcAPI/ src/ main/ java/ com/ hansonwang99/ grpc/ api/ RPCDateServiceApi.proto
在项目的POM文件中添加gRPC相关依赖。以下是示例配置:
io.grpc grpc-all 1.12.0
此外,还需要添加 Protocol Buffers 的相关插件:
kr.motd.maven os-maven-plugin 1.4.1.Final org.xolstice.maven.plugins protobuf-maven-plugin 0.5.0 grpc-java com.google.protobuf:protoc:3.0.2:exe:${os.detected.classifier} io.grpc:protoc-gen-grpc-java:1.2.0:exe:${os.detected.classifier} compile compile-custom
在项目的RPCDateServiceApi.proto文件中定义服务接口。以下是示例内容:
syntax = "proto3";option java_package = "com.hansonwang99.grpc.api";option java_outer_classname = "RPCDateServiceApi";option java_multiple_files = true;package com.hansonwang99.grpc.api;service RPCDateService { rpc getDate (RPCDateRequest) returns (RPCDateResponse) {}}message RPCDateRequest { string userName = 1;}message RPCDateResponse { string serverDate = 1;} 执行命令mvn compile,在target/generated-sources目录下会生成Java代码 stub。
创建一个新的Maven项目Server,并在其POM中添加GrpcAPI依赖:
com.hansonwang99 GrpcAPI 1.0-SNAPSHOT compile
接下来,实现服务接口:
public class RPCDateServiceImpl extends RPCDateServiceGrpc.RPCDateServiceImplBase { @Override public void getDate(RPCDateRequest request, StreamObserver responseObserver) { Date now = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分"); String nowTime = simpleDateFormat.format(now); try { RPCDateResponse rpcDateResponse = RPCDateResponse.newBuilder() .setServerDate("Welcome " + request.getUserName() + ", " + nowTime) .build(); responseObserver.onNext(rpcDateResponse); responseObserver.onCompleted(); } catch (Exception e) { responseObserver.onError(e); } }} 最后,创建gRPC服务端启动类:
public class GRPCServer { private static final int port = 9999; public static void main(String[] args) throws Exception { ServerBuilder.forPort(port) .addService(new RPCDateServiceImpl()) .build() .start(); System.out.println("gRPC服务端启动成功, 端口=" + port); server.awaitTermination(); }} 创建一个新的Maven项目Client,并在其POM中添加GrpcAPI依赖。客户端启动类如下:
public class GRPCClient { private static final String host = "localhost"; private static final int serverPort = 9999; public static void main(String[] args) throws Exception { ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(host, serverPort) .usePlaintext() .build(); try { RPCDateServiceGrpc.RPCDateServiceBlockingStub rpcDateService = RPCDateServiceGrpc.newBlockingStub(managedChannel); RPCDateRequest rpcDateRequest = RPCDateRequest.newBuilder() .setUserName("hansonwang99") .build(); RPCDateResponse rpcDateResponse = rpcDateService.getDate(rpcDateRequest); System.out.println(rpcDateResponse.getServerDate()); } finally { managedChannel.shutdown(); } }} 通过上述步骤,我们已经完成了一个完整的gRPC开发流程。客户端可以通过远程调用服务端的getDate接口,获取服务器时间并显示在本地。
本文的实践代码已发布至码云(https://my.oschina.net/hansonwang99/blog/1815743)。如需了解更多关于容器化和微服务化的实践案例,请关注我的技术博客。
转载地址:http://ugqfk.baihongyu.com/