Jprotobuf-rpc-socket

jopen 10年前發布 | 27K 次閱讀 網絡工具包 jprotobuf

Jprotobuf-rpc-socket

Protobuf RPC是一種基于TCP協議的二進制RPC通信協議。它以Protobuf作為基本的數據交換格式,并基于Protobuf內置的RPC Service形式,規定了通信雙方之間的數據交換協議,以實現完整的RPC調用。

關聯項目:https://github.com/jhunters/jprotobuf

協議規范

https://github.com/Baidu-ecom/Jprotobuf-rpc-socket/wiki/RPC%E9%80%9A%E8%AE%AF%E5%8D%8F%E8%AE%AE%E8%A7%84%E8%8C%83

使用示例

環境要求: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|

    項目主頁:http://www.baiduhome.net/lib/view/home/1415928795117

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!