简介
Socean.RPC是一个.Net下的高性可以RPC框架,框架以高性可以、高稳定性为目标,底层基于socket,代码简洁,总代码量大约在2000行,无第三方库引用,框架性可以较高,在双核i5笔记本上可达10w+每秒的处理量,支持10000长连接消息发送(每秒发10个短消息),均稳定运行,内存和cpu占有率偏低
开发背景
好用的轮子太少,只可以自己弄个了,希望可以促进.Net社区的发展
使用介绍
server sample :
1.定义实体
public class Book { public string Name { get; set; } }
定义MessageProcessor
public class DefaultMessageProcessor : IMessageProcessor { public ResponseBase Process(string title, byte[] contentBytes) { if (title == "book/name/change") { var content = Encoding.UTF8.GetString(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 == "empty") { return new EmptyResponse(); } return new ErrorResponse(ResponseErrorCode.SERVICE_NOT_FOUND); } }
2.启动服务
var server = new KeepAliveRpcServer(); server.Bind(IPAddress.Any, 11111); server.AutoReconnect = true; server.MessageProcessor = new DefaultMessageProcessor(); server.Start();
client sample:
1.定义实体
public class Book { public string Name { get; set; } }
2.Run调用
public Book ChangeBookName(Book book) { using (var rpcClient = ShortConnectionRpcClientFactory.Create(IPAddress.Parse("127.0.0.1"), 11111)) { var requestContent = JsonConvert.SerializeObject(book); var response = rpcClient.Query("book/name/change", Encoding.UTF8.GetBytes(requestContent)); var content = Encoding.UTF8.GetString(response.ContentBytes); return JsonConvert.DeserializeObject<Book>(content); } }
其他
NetworkSettings类可修改连接超时时间等参数
若果要进行性可以测试(load test),最好是在客户端把NetworkSettings.ClientDetectReceiveInterval设置成1,并提升线程优先级至ThreadPriority.Highest
项目地址
https://github.com/ch00486259/Socean.Rpc
最高并发处理量应该在30w以上,谁要是有40核服务器,可以帮我测试下