Jprotobuf-rpc-socket
Jprotobuf-rpc-socket
Protobuf RPC是一種基于TCP協議的二進制RPC通信協議。它以Protobuf作為基本的數據交換格式,并基于Protobuf內置的RPC Service形式,規定了通信雙方之間的數據交換協議,以實現完整的RPC調用。
關聯項目:https://github.com/jhunters/jprotobuf
協議規范
使用示例
環境要求:JDK 6+
Qucik Start
Jprotobuf-rpc-socket基于JProtobuf基礎上開發,可幫助大家開發中省去編寫Google Protobuf的IDL描述文件的過程。
客戶端開發
1.EchoService功用實現
EchoService 提供一個echo方法 ,參數對象EchoInfo只有一個message屬性。下面是EchoInfo對象定義
public class EchoInfo {@Protobuf public String message;
}</pre>
注解方式的定義可以極大簡化大家的工作量,上面等同于下面的IDEL配置
package pkg;option java_package = "com.baidu.bjf.remoting.protobuf.rpc";
//這里聲明輸出的java的類名
option java_outer_classname = "EchoInfo";message InterClassName {
required string message = 1; } </pre></code>2.定義EchoService接口
public interface EchoService {/** * To define a RPC client method. <br> * serviceName is "echoService" * methodName is use default method name "echo" * onceTalkTimeout is 200 milliseconds * * @param info * @return */ @ProtobufPRC(serviceName = "echoService", onceTalkTimeout = 200) EchoInfo echo(EchoInfo info);
}</pre>
RPC的方法必須要指定@ProtobufRPC注解. serviceName與methodName要與服務端保持一致。這里未指定methodName,則使用方法的名稱 "echo"
3.創建RPC Client進行訪問
RpcClient rpcClient = new RpcClient(); // 創建EchoService代理 ProtobufRpcProxy<EchoService> pbrpcProxy = new ProtobufRpcProxy<EchoService>(rpcClient, EchoService.class); pbrpcProxy.setPort(1031); // 動態生成代理實例 EchoService echoService = pbrpcProxy.proxy(); EchoInfo request = new EchoInfo(); request.message = "hello"; EchoInfo response = echoService.echo(request);服務端開發
1.開發服務實現類
public class EchoServiceImpl {@ProtobufPRCService(serviceName = "echoService", methodName = "echo") public EchoInfo doEcho(EchoInfo info) { EchoInfo ret = new EchoInfo(); ret.setMessage("hello:" + info.message); return ret; }
}</pre>
服務發布的RPC方法必須用@ProtobufPRCService注解進行標識
2.發布RPC服務
RpcServer rpcServer = new RpcServer();EchoServiceImpl echoServiceImpl = new EchoServiceImpl(); rpcServer.registerService(echoServiceImpl); rpcServer.start(1031);</pre> <p></p>
上面的代碼實現把 EchoServiceImpl 的RPC服務發布出去
性能測試
機器配置:
- Linux 64G內存 6核 12線程
- Intel(R) Xeon(R) CPU E5645 @ 2.40GHz
</ul>性能測試結果如下:平均QPS: 20000+
---------------------Performance Result------------------------- send byte size: 40;receive byte size: 46 | total count| time took(ms)| average(ms)| QPS| threads| | 100000| 7535| 0| 14285| 1| ---------------------Performance Result------------------------- ---------------------Performance Result------------------------- send byte size: 1135;receive byte size: 1135 | total count| time took(ms)| average(ms)| QPS| threads| | 100000| 10055| 0| 10000| 1| ---------------------Performance Result------------------------- ---------------------Performance Result------------------------- send byte size: 40;receive byte size: 46 | total count| time took(ms)| average(ms)| QPS| threads| | 100000| 5022| 0| 20000| 2| ---------------------Performance Result------------------------- ---------------------Performance Result------------------------- send byte size: 40;receive byte size: 46 | total count| time took(ms)| average(ms)| QPS| threads| | 100000| 4969| 0| 25000| 4| ---------------------Performance Result------------------------- ---------------------Performance Result------------------------- send byte size: 40;receive byte size: 46 | total count| time took(ms)| average(ms)| QPS| threads| | 100000| 4927| 0| 25000| 20| ---------------------Performance Result------------------------- ---------------------Performance Result------------------------- send byte size: 1135;receive byte size: 1135 | total count| time took(ms)| average(ms)| QPS| threads| | 100000| 5861| 0| 20000| 20| ---------------------Performance Result------------------------- ---------------------Performance Result------------------------- send byte size: 1135;receive byte size: 1135 | total count| time took(ms)| average(ms)| QPS| threads| | 100000| 5814| 0| 20000| 40|