grpc整理
文章目录
proto文件(java)
syntax = "proto3";
option java_multiple_files = false; //是否多个文件
option java_package = "com.example.grpcs"; //包名
option java_outer_classname = "HelloWorldProto"; //生成的bean类
//option objc_class_prefix = "HLW";
package helloworld; //如果没有java_package 指定,则会根据这个生成包名,但这个包名一般不推荐使用反域名的格式,因此建议使用java_package
//这样声明后将会生成一个叫GreeterGrpc的类(Grpc表示后缀)
//这个生成类中包含一个有ImplBase后缀的抽象类,在这里就是GreeterImplBase,服务端需要继承这个类并实现接口
service Greeter {
}
生成代码核心内容
grpc服务Stub方法签名类型
- 生成代码中有一个带有后缀ImplBase后缀的抽象类,类中方法的签名取决于它处理的RPC类型,总共有4种类型
Unary
- 一个请求对象对应一个返回对象
public void unaryExample( RequestType request, StreamObserver<ResponseType> responseObserver)
Server-streaming
- 一个请求对象,服务端可以传回多个结果对象
与Unary是一样的public void serverStreamingExample( RequestType request, StreamObserver<ResponseType> responseObserver)
Client-streaming
- 客户端传入多个请求对象,服务端返回一个响应结果
public StreamObserver<RequestType> clientStreamingExample( StreamObserver<ResponseType> responseObserver)
Bidirectional-streaming
- 结合客户端流式rpc和服务端流式rpc,可以传入多个对象,返回多个响应对象
与Client-streaming是一样的public StreamObserver<RequestType> bidirectionalStreamingExample( StreamObserver<ResponseType> responseObserver)
grpc客户端Stub
- 生成的代码中包含被客户端调用的由服务定义的方法,每一个stub都绑定一个Channel,通过Channel发送服务端
- 生成代码中包含三种类型的stub,分别是 asynchronous(异步)、 blocking(阻塞)、 future,每一种类型都有对应的生成代码,比如ServiceNameStub,ServiceNameBlockingStub,ServiceNameFutureStub,其中ServiceName为proto文件中定义的服务名
Asynchronous Stub
- 通过ServiceNameGrpc.newStub(Channel channel)可以实例化一个异步stub
- Asynchronous Stub支持Unary,Server-streaming,Client-streaming,Bidirectional-streaming4种
Blocking Stub
- 通过ServiceNameGrpc.newBlockingStub(Channel channel)可以实例化一个同步stub
- Asynchronous Stub支持Unary和Server-streaming两种:
- Unary:public ResponseType unaryExample(RequestType request)
- Server-streaming:public Iterator serverStreamingExample(RequestType request)
Future Stub
- 通过ServiceNameGrpc.newFutureStub(Channel channel)可以实例化一个future stub
- Future Stub只支持Unary一种
- Unary:public ListenableFuture unaryExample(RequestType request)
其中关于ListenableFuture的使用请见文末的参考
Asynchronous 与Future 的区别
- Future最适合的场景就是一个大任务需要多个小任务,只有小任务都完成了大任务才能执行。以泡茶为例,要泡茶需要烧水和洗茶杯等小任务,烧水和洗茶杯都可以同时执行,但最后要泡茶必须等烧水和洗茶杯都执行完了才能进行。
- 而Asynchronous 适合的场景就是多个任务之间并没有顺序关系,都是独立的任务。
gradle
- 以下是一个典型的gradle配置
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
}
dependencies {
// ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
// gradle versions
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.2.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.4.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
demo
https://github.com/huweijian5/grpc-demo
参考
grpc /
https://grpc.io/docs/tutorials/basic/java.html
grpc / Java Generated Code Reference
https://grpc.io/docs/reference/java/generated-code.html
grpc的四种服务类型 - resentment - 博客园
http://www.cnblogs.com/resentment/p/6792029.html
多线程学习-ListenableFuture使用介绍以及示例 - qq_40074764的博客 - CSDN博客
https://blog.csdn.net/qq_40074764/article/details/79655306
同步调用,异步回调和 Future 模式 - 流的博客 - CSDN博客
https://blog.csdn.net/qq_35688140/article/details/83115723