c#---Socean.Rpc之EasyProxy

目录

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

2.Socean.RPC框架实测

3.Socean.Rpc之EasyProxy

简介

这几天给Socean.RPC加上了动态代理,简称EasyProxy,特点是性能高、稳定性好、使用简便

 

使用入门:

服务端 :

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.启动服务

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

 

客户端:

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.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



你可能感兴趣的:(c#---Socean.Rpc之EasyProxy)