C# -- 高性能RPC框架:Socean.RPC

簡介git

Socean.RPC是一個.Net下的高性能RPC框架,框架以高性能、高穩定性爲目標,底層基於socket,無第三方庫引用,代碼簡潔,總代碼量大約在2000行,框架性能較高,在普通PC上測試,長鏈接可達14w每秒的處理量,支持10000+長鏈接消息處理(客戶端每100毫秒發1個短消息),內存和cpu佔有率偏低github

 

開發背景框架

好用的輪子太少,只能本身弄個了,但願能促進.Net社區的發展異步

 

框架特色socket

高性能、超穩定、支持異步、資源佔用很小、代碼簡介、清晰易懂async

 

使用介紹性能

server sample :測試

1.定義實體spa

public class Book
{
  public string Name { get; set; }
}

 定義MessageProcessorcode

public class DefaultMessageProcessor : IMessageProcessor
{
public void Init()
{
}   
public async Task<ResponseBase> Process(Socean.Rpc.Core.Message.FrameData frameData)   {  

         var title = Encoding.UTF8.GetString(frameData.TitleBytes);

    if (title == "/books/namechange")
    {
      var content = Encoding.UTF8.GetString(frameData.ContentBytes);

      //here we use newtonsoft.Json serializer 
      //you need add refer "newtonsoft.Json.dll"
      var book = JsonConvert.DeserializeObject<Book>(content);
      book.Name = "new name";

      var responseContent = JsonConvert.SerializeObject(book);
      return new BytesResponse(Encoding.UTF8.GetBytes(responseContent));
    }

    if (title == "test empty return")
    {
      return new EmptyResponse();
    }

    return new ErrorResponse((byte)ResponseCode.SERVICE_NOT_FOUND);
  }
}

 

2.啓動服務

var server = new RpcServer();
server.Bind(IPAddress.Any, 11111);
server.Start<DefaultMessageProcessor>(); 

 

client sample:

1.定義實體

public class Book
{
  public string Name { get; set; }
}

2.執行調用

public Book ChangeBookName(Book book)
{
  using (var rpcClient =  new FastRpcClient(IPAddress.Parse("127.0.0.1"), 11111))
  {
    var requestContent = JsonConvert.SerializeObject(book);
    var response = rpcClient.Query(Encoding.UTF8.GetBytes("/books/namechange"), Encoding.UTF8.GetBytes(requestContent));
    var content = Encoding.UTF8.GetString(response.ContentBytes);
    return JsonConvert.DeserializeObject<Book>(content);   }
}

其餘

NetworkSettings類可修改鏈接超時時間等參數

 

項目地址

https://github.com/ch00486259/Socean.Rpc

相關文章
相關標籤/搜索