C × EasyProxy of socean.rpc

Catalog

1.High performance RPC framework: Socean.RPC

2.Actual measurement of Socean.RPC frame

 

brief introduction

EasyProxy is a dynamic proxy implementation of Socean.RPC, which is characterized by high performance, good stability and easy to use

 

Getting started:

Server:

1. Define serializer and message handler

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

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

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

 

Client:

1. Define serializer

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. Define service interface

[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. Generate agent service

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

 

4. Execute function

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

Other

The IBookService.AddStock method was tested briefly. It was tested on my shabby notebook, with a concurrency of about 5w / S (please note the Console.WriteLine function inside AddStock during the pressure test, because the concurrency of this function is not high, which will affect the test results)

Project address

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



Keywords: C# JSON github

Added by joebudden on Tue, 11 Feb 2020 19:23:18 +0200