一、grpc-contract
還是按老套路,把服務涉及的對象定義、接口定義抽象出來,下面是項目結構圖:
pom.xml的内容如下:
View Code
demo_service_dto.proto内容如下:
注:grpc要求protobuf必須使用3.0以上版本
demo_service.proto内容如下:
mvn install 後,會自動在target下生成相應的java class類
二、grpc-server
pom.xml檔案如下:
先對服務接口提供實作:
grpc的server端是基于Netty的(當然還有OKHttp的實作,詳情見github項目首頁),下面是server端的代碼:
三、grpc-client
pom.xml内容:
Client端代碼:
在筆記本測試的結果:
Will try to ping a ...
ping: pong => a
rgpc 100000 次NettyServer調用,耗時:36409毫秒,平均2746次/秒
基本上在每秒3k次的數量級,相對thrift(1w+)、avro(5k+)來講,目前的差距還是很明顯的,但是新事物成長總是需要時間,再給google一段時間,相信以後會讓大家感到驚豔的。
在序列化方面,也做了一個跟之前thrift、avro類似的測試:
輸出:
Protobuf 3.0 二進制序列後的byte數組長度:4
1 - 1
在2進制序列化後的大小方面,protobuf 3大體跟thrift的TCompactProtocal(大小5)接近,比avro(大小2)略差。
參考文章:
<a href="http://www.useopen.net/blog/2015/rpc-performance.html" target="_blank">RPC架構性能基本比較測試</a>
<a href="http://www.cnblogs.com/fxjwind/archive/2013/05/14/3078041.html" target="_blank">Schema evolution in Avro, Protocol Buffers and Thrift</a>
<a href="https://github.com/eishay/jvm-serializers" target="_blank">jvm-serializers</a>
<a href="http://www.cnblogs.com/fxjwind/archive/2013/05/16/3082219.html" target="_blank">RPC綜述 - PB, Thrift, Avro</a>