1、前言

surging内部使用的是高性能RPC远程服务调用,如果用json.net序列化肯定性能上达不到最优,所以后面扩展了protobuf,messagepack序列化组件,以支持RPC二进制传输.

在这里需要感谢白纸无字Zonciu,新增了messagepack序列化,让surging 性能上跨了一大步。此篇文章我们来谈谈messagepack、protobuffer、json.net ,并且性能做下对比

开源地址:https://github.com/dotnetcore/surging

2、序列化组件

   2.1 surging 使用的是以下序列化组件:

json.net:surging 使用的是Newtonsoft.Json, 它是基于json格式的序列化和反序列化的组件.官方网站: http://json.codeplex.com/

protobuf:surging 使用的是protobuf-net, 它是基于二进制格式的序列化和反序列化的组件.官方网站: https://github.com/mgravell/protobuf-net

messagepack:surging 使用的是MessagePack-CSharp, 它是基于二进制格式的序列化和反序列化的组件.官方网站: https://github.com/neuecc/MessagePack-CSharp

      2.2 各个组件的优点

json.net 有以下优点:

侵入性:可以不添加attribute,就能进行序列化操作

灵活性:可以灵活性配置,比如允许被序列化的成员自定义名字,屏蔽的非序列化属性成员

可读性: 数据格式比较简单, 易于读写

依赖性:可以序列化成JObject,无需依赖对象进行序列化和泛型化。

  protobuf 有以下优点:

    性能高  序列化后体积相比Json和XML很小,适合RPC二进制传输
   跨语言:支持跨平台多语言
        兼容性:消息格式升级和兼容性还不错
        速度快 :序列化反序列化速度很快,快于Json的处理速速

messagepack有以下优点:

    性能高  序列化后体积相比Json和XML很小,适合RPC二进制传输
   跨语言:支持跨平台多语言
        兼容性:消息格式升级和兼容性还不错
        速度快 :序列化反序列化速度很快,快于Json的处理速度

针对于protobuf和messagepack都是基于二进制格式的序列化和反序列化,优点都一样,但是基于messagepack的MessagePack-CSharp组件侵入性更小,可以不需要加attribute,而且性能上更优.下一节来看看组件在surging 中的表现

3. 性能比较

服务端:

(注:如果不加UseProtoBufferCodec和UseMessagePackCodec就是json.net序列化)

var host = new ServiceHostBuilder()
.RegisterServices(option=> {
option.Initialize(); //初始化服务
option.RegisterServices();//依赖注入领域服务
option.RegisterRepositories();//依赖注入仓储
option.RegisterModules();//依赖注入第三方模块
option.RegisterServiceBus();//依赖注入ServiceBus
})
.RegisterServices(builder =>
{
builder.AddMicroService(option =>
{
option.AddServiceRuntime();//
// option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181")); //使用Zookeeper管理
option.UseConsulManager(new ConfigInfo("127.0.0.1:8500"));//使用Consul管理
option.UseDotNettyTransport();//使用Netty传输
option.UseRabbitMQTransport();//使用rabbitmq 传输
option.AddRabbitMQAdapt();//基于rabbitmq的消费的服务适配
// option.UseProtoBufferCodec();//基于protobuf序列化传输
option.UseMessagePackCodec();//基于MessagePack序列化传输
builder.Register(p => new CPlatformContainer(ServiceLocator.Current));//初始化注入容器
});
})
.SubscribeAt() //消息订阅
.UseServer("127.0.0.1", 98)
//.UseServer("127.0.0.1", 98,“true”) //自动生成Token
//.UseServer("127.0.0.1", 98,“123456789”) //固定密码Token
.UseStartup<Startup>()
.Build(); using (host.Run())
{
Console.WriteLine($"服务端启动成功,{DateTime.Now}。");
}

客户端:

   var host = new ServiceHostBuilder()
.RegisterServices(option =>
{
option.Initialize();
option.RegisterServices();
option.RegisterRepositories();
option.RegisterModules();
})
.RegisterServices(builder =>
{
builder.AddMicroService(option =>
{
option.AddClient();
option.AddClientIntercepted(typeof(CacheProviderInterceptor));
//option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181"));
option.UseConsulManager(new ConfigInfo("127.0.0.1:8500"));
option.UseDotNettyTransport();
option.UseRabbitMQTransport();
option.UseProtoBufferCodec();
//option.UseMessagePackCodec();
builder.Register(p => new CPlatformContainer(ServiceLocator.Current));
});
})
.UseClient()
.UseStartup<Startup>()
.Build(); using (host.Run())
{
Startup.Test(ServiceLocator.GetService<IServiceProxyFactory>());
Startup.TestRabbitMq();
}

测试  0  object(注:测试无参数)

  /// <summary>
/// 测试
/// </summary>
/// <param name="serviceProxyFactory"></param>
public static void Test(IServiceProxyFactory serviceProxyFactory)
{
Task.Run(async () =>
{
var userProxy = serviceProxyFactory.CreateProxy<IUserService>("User");
await userProxy.GetUserId("user");
do
{
Console.WriteLine("正在循环 1w次调用 GetUser.....");
//1w次调用
var watch = Stopwatch.StartNew();
for (var i = 0; i < 10000; i++)
{
var a =userProxy.GetDictionary().Result;
}
watch.Stop();
Console.WriteLine($"1w次调用结束,执行时间:{watch.ElapsedMilliseconds}ms");
Console.ReadLine();
} while (true);
}).Wait();
}

测试  1  object(注:测试参数传对象)

    /// <summary>
/// 测试
/// </summary>
/// <param name="serviceProxyFactory"></param>
public static void Test(IServiceProxyFactory serviceProxyFactory)
{
Task.Run(async () =>
{ var userProxy = serviceProxyFactory.CreateProxy<IUserService>("User");
await userProxy.GetUserId("user");
do
{
Console.WriteLine("正在循环 1w次调用 GetUser.....");
//1w次调用
var watch = Stopwatch.StartNew();
for (var i = 0; i < 10000; i++)
{
var a =userProxy.GetUser(new UserModel { UserId = 1 }).Result;
}
watch.Stop();
Console.WriteLine($"1w次调用结束,执行时间:{watch.ElapsedMilliseconds}ms");
Console.ReadLine();
} while (true);
}).Wait();
}

测试  10  object(注:测试参数传List 集合对象)

   /// <summary>
/// 测试
/// </summary>
/// <param name="serviceProxyFactory"></param>
public static void Test(IServiceProxyFactory serviceProxyFactory)
{
Task.Run(async () =>
{
var userProxy = serviceProxyFactory.CreateProxy<IUserService>("User");
await userProxy.GetUserId("user");
var list = new List<UserModel>();
for(int i=0;i<10;i++)
{
list.Add(new UserModel { UserId = 1, Age = 18, Name = "fanly" });
}
do
{
Console.WriteLine("正在循环 1w次调用 GetUser.....");
//1w次调用
var watch = Stopwatch.StartNew();
for (var i = 0; i < 10000; i++)
{
var a =userProxy.Get(list).Result;
}
watch.Stop();
Console.WriteLine($"1w次调用结束,执行时间:{watch.ElapsedMilliseconds}ms");
Console.ReadLine();
} while (true);
}).Wait();
}

测试100 object(注:测试参数传List 集合对象)

    /// <summary>
/// 测试
/// </summary>
/// <param name="serviceProxyFactory"></param>
public static void Test(IServiceProxyFactory serviceProxyFactory)
{
Task.Run(async () =>
{
var userProxy = serviceProxyFactory.CreateProxy<IUserService>("User");
await userProxy.GetUserId("user");
var list = new List<UserModel>();
for(int i=;i<;i++)
{
list.Add(new UserModel { UserId = , Age = , Name = "fanly" });
}
do
{
Console.WriteLine("正在循环 1w次调用 GetUser.....");
//1w次调用
var watch = Stopwatch.StartNew();
for (var i = ; i < ; i++)
{
var a =userProxy.Get(list).Result;
}
watch.Stop();
Console.WriteLine($"1w次调用结束,执行时间:{watch.ElapsedMilliseconds}ms");
Console.ReadLine();
} while (true);
}).Wait();
}

通过以上测试代码,我们得到了如下的测试结果

通过上图,可以发现messagepack不管是小数据量还是大数据量都保持比较稳定的性能,而json.net 在100object平均已经达到了1.1ms,和messagepack、protobuffer比差太多,而 protobuffer在此次测试中表现的极其不稳定只有在1 object 和100 object 性能比较不错,但是与messagepack比还是相差比较大。所以我建议还是使用messagepack,性能上更优,侵入性也非常低

我们来看看性能最优的messagepack 详细测试数据

o object:

1 object:

10 object:

100 object

测试环境

CPU:Intel Core i7-4710MQ

内存:16G

硬盘:1T SSD+512G HDD

网络:局域网

6、总结

surging 已经完成JWT验证和AppSecret验证,下篇文章会详细介绍surging 身份认证,如感兴趣请多关注或者加入QQ群:615562965

基于.NET CORE微服务框架 -谈谈surging 的messagepack、protobuffer、json.net 序列化的更多相关文章

  1. 基于.NET CORE微服务框架 -谈谈surging API网关

    1.前言 对于最近surging更新的API 网关大家也有所关注,也收到了不少反馈提出是否能介绍下Api网关,那么我们将在此篇文章中剥析下surging的Api 网关 开源地址:https://git ...

  2. 基于.NET CORE微服务框架 -谈谈surging的服务容错降级

    一.前言 对于不久开源的surging受到不少.net同学的青睐,也受到.net core学习小组的关注,邀请加入.NET China Foundation以方便国内.net core开源项目的推广, ...

  3. 基于.NET CORE微服务框架 -谈谈Cache中间件和缓存降级

    1.前言 surging受到不少.net同学的青睐,也提了不少问题,提的最多的是什么时候集成API 网关,在这里回答大家最近已经开始着手研发,应该在1,2个月内会有个初版API网关,其它像Token身 ...

  4. 基于.NET CORE微服务框架 -surging的介绍和简单示例 (开源)

    一.前言 至今为止编程开发已经11个年头,从 VB6.0,ASP时代到ASP.NET再到MVC, 从中见证了.NET技术发展,从无畏无知的懵懂少年,到现在的中年大叔,从中的酸甜苦辣也只有本人自知.随着 ...

  5. 基于.NET CORE微服务框架 -浅析如何使用surging

    1.前言 surging受到大家这么强烈的关注,我感到非常意外,比如有同僚在公司的分享会上分享surging, 还有在博客拿其它的RPC框架,微服务做对比等等,这些举动都让我感觉压力很大,毕竟作为个人 ...

  6. 基于.NET CORE微服务框架 -surging 基于messagepack、protobuffer、json.net 性能对比

    1.前言 surging内部使用的是高性能RPC远程服务调用,如果用json.net序列化肯定性能上达不到最优,所以后面扩展了protobuf,messagepack序列化组件,以支持RPC二进制传输 ...

  7. 基于.NET CORE微服务框架 -Api网关服务管理

    1.前言 经过10多天的努力,surging 网关已经有了大致的雏形,后面还会持续更新完善,请大家持续关注研发的动态 最近也更新了surging新的版本 更新内容: 1. 扩展Zookeeper封装2 ...

  8. 基于thrift的微服务框架

    前一阵开源过一个基于spring-boot的rest微服务框架,今天再来一篇基于thrift的微服务加框,thrift是啥就不多了,大家自行百度或参考我之前介绍thrift的文章, thrift不仅支 ...

  9. 基于Spring-Cloud的微服务框架设计

    基于Spring-Cloud的微服务框架设计 先进行大的整体的框架整理,然后在针对每一项进行具体的详细介绍

随机推荐

  1. SDCC2013大会笔记整理

    2013-8-30 大会首日 百度移动云三大框架:Clouda.SiteApp.Appbuilder MBaaS解决高性能Server很难构建的问题. 百度开放云的区域运营服务于创业者 ------- ...

  2. 体育Bank2016会议笔记

    补注:会议全称应该是体育Bank2016体育投融资总裁年会 新华社体育部徐仁基 演讲主题:帮郭川找到大海-->帮民众找到自己真正的体育爱好 激发和培养体育市场是重中之重 将体育培养成生活习惯.生 ...

  3. ExtJs radiogroup form.loadRecord方法无法赋值正确解决办法

    一.radiogroup的name和radio的name一致,inputValue为整形 { xtype: 'radiogroup', fieldLabel: '是否有效', name: 'statu ...

  4. 如何利用Python网络爬虫抓取微信朋友圈的动态(上)

    今天小编给大家分享一下如何利用Python网络爬虫抓取微信朋友圈的动态信息,实际上如果单独的去爬取朋友圈的话,难度会非常大,因为微信没有提供向网易云音乐这样的API接口,所以很容易找不到门.不过不要慌 ...

  5. SpringCloud实战-Ribbon客户端负载均衡

    前面我们已经完成了注册中心和服务提供者两个基础组件.接着介绍使用Spring Cloud Ribbon在客户端负载均衡的调用服务. ribbon 是一个客户端负载均衡器,可以简单的理解成类似于 ngi ...

  6. Java容器:Set

    Set和数学中的集合十分类似,在Java中,Set是一种绝不会包含两个相等元素的存储结构.在阅读此文前请阅读Java容器:Map. Set方法 增添方法: boolean add(E e); bool ...

  7. lambda隐藏函数的嵌套

    # 隐藏函数嵌套 f = (lambda a,b :a if a>b else b)(1000, 2000008) print((lambda a,g:a if a > g else g) ...

  8. String 和 new String

    String s1=”welcome”; String s2= “welcome:; String s3 =new String(”welcome”);  //新建对象   指向新的内存   比较内容 ...

  9. PAT1079 :Total Sales of Supply Chain

    1079. Total Sales of Supply Chain (25) 时间限制 250 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  10. PAT1121:Damn Single

    1121. Damn Single (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue "Dam ...