c#---Socean.Rpc之EasyProxy

目錄html

1.高性能RPC框架:Socean.RPCgit

2.Socean.RPC框架實測github

 

簡介併發

這幾天給Socean.RPC加上了動態代理,簡稱EasyProxy,特色是性能高、穩定性好、使用簡便框架

 

使用入門:ide

服務端 :函數

1.定義序列化器和消息處理器性能

public class RpcSerializer : Socean.Rpc.DynamicProxy.IRpcSerializer
{
    public object Deserialize(string content, Type type)
    {
        return Newtonsoft.Json.JsonConvert.DeserializeObject(content, type);
    }

    public string Serialize(object obj)
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
    }
}   

public class CustomMessageProcessor : Socean.Rpc.DynamicProxy.EasyProxyMessageProcessor
{
    public override void Init()
    {
        RegisterServices(Assembly.GetExecutingAssembly(), new RpcSerializer());
    }
}

 

2.定義服務測試

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

    public double Price { get; set; }
}

[RpcService]
public class BookService
{
    public bool RegisterForSale(Book book)
    {
        Console.WriteLine("RegisterForSale,bookName:{0},bookPrice:{1}", book.Name, book.Price);
        return true;
    }

    public void AddStock(string bookName, int count)
    {
        Console.WriteLine("AddStock,bookName:{0},count:{1}", bookName, count);
    }
}

 

3.啓動服務spa

var server = new RpcServer();
server.Bind(IPAddress.Parse("127.0.0.1"), 11111);
server.Start<CustomMessageProcessor>();

 

客戶端:

1.定義序列化器

public class RpcSerializer : Socean.Rpc.DynamicProxy.IRpcSerializer
{
    public object Deserialize(string content, Type type)
    {
        return Newtonsoft.Json.JsonConvert.DeserializeObject(content, type);
    }

    public string Serialize(object obj)
    {
        return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
    }
}

 

2.定義服務接口

[RpcProxy(ServiceName = "BookService")]
public interface IBookService
{
    bool RegisterForSale(Book book);

    void AddStock(string bookName, int count);
}

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

    public double Price { get; set; }
}

 

3.生成代理服務

var bookServiceProxy = EasyProxyGenerator<IBookService>.Create(IPAddress.Parse("127.0.0.1"), 11111, new RpcSerializer();

 

4.執行函數

 bookServiceProxy.RegisterForSale(new Book { Name = "相對論", Price = 108.88 });
 bookServiceProxy.AddStock("相對論", 1000);

 其餘

    簡單測試了一下IBookService.AddStock方法,在個人破舊筆記本上測試,併發量大約5w/秒(壓測時請註釋掉AddStock內部的Console.WriteLine函數,由於此函數併發不高,會影響測試結果)

項目地址

項目地址:https://github.com/ch00486259/Socean.Rpc
相關文章
相關標籤/搜索