久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

基于.NET CORE微服務(wù)框架

 昵稱65578837 2019-07-30

1,、前言

surging內(nèi)部使用的是高性能RPC遠(yuǎn)程服務(wù)調(diào)用,,如果用json.net序列化肯定性能上達(dá)不到最優(yōu),,所以后面擴(kuò)展了protobuf,messagepack序列化組件,以支持RPC二進(jìn)制傳輸.

在這里需要感謝白紙無字Zonciu,,新增了messagepack序列化,,讓surging 性能上跨了一大步,。此篇文章我們來談?wù)刴essagepack、protobuffer,、json.net ,,并且性能做下對(duì)比

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

2、序列化組件

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

json.net:surging 使用的是Newtonsoft.Json,, 它是基于json格式的序列化和反序列化的組件.官方網(wǎng)站: http://json./

protobuf:surging 使用的是protobuf-net,, 它是基于二進(jìn)制格式的序列化和反序列化的組件.官方網(wǎng)站: https://github.com/mgravell/protobuf-net

messagepack:surging 使用的是MessagePack-CSharp, 它是基于二進(jìn)制格式的序列化和反序列化的組件.官方網(wǎng)站: https://github.com/neuecc/MessagePack-CSharp

      2.2 各個(gè)組件的優(yōu)點(diǎn)

json.net 有以下優(yōu)點(diǎn):

侵入性:可以不添加attribute,就能進(jìn)行序列化操作

靈活性:可以靈活性配置,,比如允許被序列化的成員自定義名字,,屏蔽的非序列化屬性成員

可讀性: 數(shù)據(jù)格式比較簡(jiǎn)單, 易于讀寫

依賴性:可以序列化成JObject,無需依賴對(duì)象進(jìn)行序列化和泛型化,。

  protobuf 有以下優(yōu)點(diǎn):

    性能高  序列化后體積相比Json和XML很小,,適合RPC二進(jìn)制傳輸
   跨語言:支持跨平臺(tái)多語言
        兼容性:消息格式升級(jí)和兼容性還不錯(cuò)
        速度快 :序列化反序列化速度很快,快于Json的處理速速

messagepack有以下優(yōu)點(diǎn):

    性能高  序列化后體積相比Json和XML很小,,適合RPC二進(jìn)制傳輸
   跨語言:支持跨平臺(tái)多語言
        兼容性:消息格式升級(jí)和兼容性還不錯(cuò)
        速度快 :序列化反序列化速度很快,,快于Json的處理速度

針對(duì)于protobuf和messagepack都是基于二進(jìn)制格式的序列化和反序列化,優(yōu)點(diǎn)都一樣,,但是基于messagepack的MessagePack-CSharp組件侵入性更小,,可以不需要加attribute,而且性能上更優(yōu).下一節(jié)來看看組件在surging 中的表現(xiàn)

3. 性能比較

服務(wù)端:

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

  1. var host = new ServiceHostBuilder()

  2. .RegisterServices(option=> {

  3. option.Initialize(); //初始化服務(wù)

  4. option.RegisterServices();//依賴注入領(lǐng)域服務(wù)

  5. option.RegisterRepositories();//依賴注入倉(cāng)儲(chǔ)

  6. option.RegisterModules();//依賴注入第三方模塊

  7. option.RegisterServiceBus();//依賴注入ServiceBus

  8. })

  9. .RegisterServices(builder =>

  10. {

  11. builder.AddMicroService(option =>

  12. {

  13. option.AddServiceRuntime();//

  14. // option.UseZooKeeperManager(new ConfigInfo('127.0.0.1:2181')); //使用Zookeeper管理

  15. option.UseConsulManager(new ConfigInfo('127.0.0.1:8500'));//使用Consul管理

  16. option.UseDotNettyTransport();//使用Netty傳輸

  17. option.UseRabbitMQTransport();//使用rabbitmq 傳輸

  18. option.AddRabbitMQAdapt();//基于rabbitmq的消費(fèi)的服務(wù)適配

  19. // option.UseProtoBufferCodec();//基于protobuf序列化傳輸

  20. option.UseMessagePackCodec();//基于MessagePack序列化傳輸

  21. builder.Register(p => new CPlatformContainer(ServiceLocator.Current));//初始化注入容器

  22. });

  23. })

  24. .SubscribeAt() //消息訂閱

  25. .UseServer('127.0.0.1', 98)

  26. //.UseServer('127.0.0.1', 98,“true”) //自動(dòng)生成Token

  27. //.UseServer('127.0.0.1', 98,,“123456789”) //固定密碼Token

  28. .UseStartup<Startup>()

  29. .Build();

  30. using (host.Run())

  31. {

  32. Console.WriteLine($'服務(wù)端啟動(dòng)成功,,{DateTime.Now}。');

  33. }

客戶端:

  1. var host = new ServiceHostBuilder()

  2. .RegisterServices(option =>

  3. {

  4. option.Initialize();

  5. option.RegisterServices();

  6. option.RegisterRepositories();

  7. option.RegisterModules();

  8. })

  9. .RegisterServices(builder =>

  10. {

  11. builder.AddMicroService(option =>

  12. {

  13. option.AddClient();

  14. option.AddClientIntercepted(typeof(CacheProviderInterceptor));

  15. //option.UseZooKeeperManager(new ConfigInfo('127.0.0.1:2181'));

  16. option.UseConsulManager(new ConfigInfo('127.0.0.1:8500'));

  17. option.UseDotNettyTransport();

  18. option.UseRabbitMQTransport();

  19. option.UseProtoBufferCodec();

  20. //option.UseMessagePackCodec();

  21. builder.Register(p => new CPlatformContainer(ServiceLocator.Current));

  22. });

  23. })

  24. .UseClient()

  25. .UseStartup<Startup>()

  26. .Build();

  27. using (host.Run())

  28. {

  29. Startup.Test(ServiceLocator.GetService<IServiceProxyFactory>());

  30. Startup.TestRabbitMq();

  31. }

測(cè)試  0  object(注:測(cè)試無參數(shù))

  1. /// <summary>

  2. /// 測(cè)試

  3. /// </summary>

  4. /// <param name='serviceProxyFactory'></param>

  5. public static void Test(IServiceProxyFactory serviceProxyFactory)

  6. {

  7. Task.Run(async () =>

  8. {

  9. var userProxy = serviceProxyFactory.CreateProxy<IUserService>('User');

  10. await userProxy.GetUserId('user');

  11. do

  12. {

  13. Console.WriteLine('正在循環(huán) 1w次調(diào)用 GetUser.....');

  14. //1w次調(diào)用

  15. var watch = Stopwatch.StartNew();

  16. for (var i = 0; i < 10000; i )

  17. {

  18. var a =userProxy.GetDictionary().Result;

  19. }

  20. watch.Stop();

  21. Console.WriteLine($'1w次調(diào)用結(jié)束,,執(zhí)行時(shí)間:{watch.ElapsedMilliseconds}ms');

  22. Console.ReadLine();

  23. } while (true);

  24. }).Wait();

  25. }

測(cè)試  1  object(注:測(cè)試參數(shù)傳對(duì)象)

  1. /// <summary>

  2. /// 測(cè)試

  3. /// </summary>

  4. /// <param name='serviceProxyFactory'></param>

  5. public static void Test(IServiceProxyFactory serviceProxyFactory)

  6. {

  7. Task.Run(async () =>

  8. {

  9. var userProxy = serviceProxyFactory.CreateProxy<IUserService>('User');

  10. await userProxy.GetUserId('user');

  11. do

  12. {

  13. Console.WriteLine('正在循環(huán) 1w次調(diào)用 GetUser.....');

  14. //1w次調(diào)用

  15. var watch = Stopwatch.StartNew();

  16. for (var i = 0; i < 10000; i )

  17. {

  18. var a =userProxy.GetUser(new UserModel { UserId = 1 }).Result;

  19. }

  20. watch.Stop();

  21. Console.WriteLine($'1w次調(diào)用結(jié)束,,執(zhí)行時(shí)間:{watch.ElapsedMilliseconds}ms');

  22. Console.ReadLine();

  23. } while (true);

  24. }).Wait();

  25. }

測(cè)試  10  object(注:測(cè)試參數(shù)傳List 集合對(duì)象)

  1. /// <summary>

  2. /// 測(cè)試

  3. /// </summary>

  4. /// <param name='serviceProxyFactory'></param>

  5. public static void Test(IServiceProxyFactory serviceProxyFactory)

  6. {

  7. Task.Run(async () =>

  8. {

  9. var userProxy = serviceProxyFactory.CreateProxy<IUserService>('User');

  10. await userProxy.GetUserId('user');

  11. var list = new List<UserModel>();

  12. for(int i=0;i<10;i )

  13. {

  14. list.Add(new UserModel { UserId = 1, Age = 18, Name = 'fanly' });

  15. }

  16. do

  17. {

  18. Console.WriteLine('正在循環(huán) 1w次調(diào)用 GetUser.....');

  19. //1w次調(diào)用

  20. var watch = Stopwatch.StartNew();

  21. for (var i = 0; i < 10000; i )

  22. {

  23. var a =userProxy.Get(list).Result;

  24. }

  25. watch.Stop();

  26. Console.WriteLine($'1w次調(diào)用結(jié)束,執(zhí)行時(shí)間:{watch.ElapsedMilliseconds}ms');

  27. Console.ReadLine();

  28. } while (true);

  29. }).Wait();

  30. }

測(cè)試100 object(注:測(cè)試參數(shù)傳List 集合對(duì)象)

  1.    /// <summary>

  2. /// 測(cè)試

  3. /// </summary>

  4. /// <param name='serviceProxyFactory'></param>

  5. public static void Test(IServiceProxyFactory serviceProxyFactory)

  6. {

  7. Task.Run(async () =>

  8. {

  9. var userProxy = serviceProxyFactory.CreateProxy<IUserService>('User');

  10. await userProxy.GetUserId('user');

  11. var list = new List<UserModel>();

  12. ;i<;i )

  13. {

  14. list.Add(, Age = , Name = 'fanly' });

  15. }

  16. do

  17. {

  18. Console.WriteLine('正在循環(huán) 1w次調(diào)用 GetUser.....');

  19. //1w次調(diào)用

  20. var watch = Stopwatch.StartNew();

  21. ; i < ; i )

  22. {

  23. var a =userProxy.Get(list).Result;

  24. }

  25. watch.Stop();

  26. Console.WriteLine($'1w次調(diào)用結(jié)束,,執(zhí)行時(shí)間:{watch.ElapsedMilliseconds}ms');

  27. Console.ReadLine();

  28. } while (true);

  29. }).Wait();

  30. }

通過以上測(cè)試代碼,,我們得到了如下的測(cè)試結(jié)果

通過上圖,可以發(fā)現(xiàn)messagepack不管是小數(shù)據(jù)量還是大數(shù)據(jù)量都保持比較穩(wěn)定的性能,,而json.net 在100object平均已經(jīng)達(dá)到了1.1ms,,和messagepack,、protobuffer比差太多,,而 protobuffer在此次測(cè)試中表現(xiàn)的極其不穩(wěn)定只有在1 object 和100 object 性能比較不錯(cuò),但是與messagepack比還是相差比較大,。所以我建議還是使用messagepack,,性能上更優(yōu),侵入性也非常低

我們來看看性能最優(yōu)的messagepack 詳細(xì)測(cè)試數(shù)據(jù)

o object:

1 object:

10 object:

100 object

測(cè)試環(huán)境

CPU:Intel Core i7-4710MQ

內(nèi)存:16G

硬盤:1T SSD 512G HDD

網(wǎng)絡(luò):局域網(wǎng)

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多