Gprc | TCP | 基于HTTP2.0的协议 | 二进制传输 |
Http远程调用 | HTTP | 基于HTTP1.1 | json |
HTTP远程调用:
传统的服务之间调用常用的方式:
RestfulAPI(接口规范)
HTTP网络传输协议
Json数据序列化与反序列化
传统RestfulAPI如下↓:
//RestController=reposebody+controller
//@reponsebody就是返回JSON
@RestController
@RequestMapping(value = "/help")
public interface HelpController {
@RequestMapping(value = "/detail", method = RequestMethod.POST)
ApiOut getDetail(
@RequestHeader(value = "token", required = true) String uu,
@Validated @RequestBody ApiIn apiInDto, BindingResult result)
throws SystemException, ApplicationException, BusinessException;
}
Rpc(远程方法调用):
GRPC
是由 google
开发的一个高性能、通用的开源RPC
框架
基于HTTP/2
协议标准(二进制格式传输协议,HTTP/1X是文本传输
)而设计,支持多语言。
GRPC
使用Protocol Buffers
作为序列化协议.相比于Json传输数据更快。
①:使用protocol buffer接口定义我们的服务方法↓
syntax = "proto3";
package hello.exp;
option java_package = "com.protobuf.hello.exp";
service HelloService{
rpc takingHello (Request) returns (Response) {
option (google.api.http) = {
get: "/grpctest/sayhello"
};
option (validation.method) = {};
}
message Request{
String name;
String code;
}
message Response{
String message = 1;
}
}
②:
服务端代码
@GrpcService
public class MarginPositionService extends MarginPositionServiceGrpc.MarginPositionServiceImplBase{
@Override
public void takingHello (Request request,
StreamObserver<Response> responseObserver){
responseObserver.onNext(request);
responseObserver.onCompleted();
}
}