在本文中將介紹如何通過thrift 組件集成到surging 微服務(wù)引擎中,然后可以選擇dotnetty 或thrift作為服務(wù)遠(yuǎn)程調(diào)用RPC,也可以通過其它語言的thrift 調(diào)用surging 服務(wù),下面將簡單介紹如何使用thrift
準(zhǔn)備工作
首先需要到官網(wǎng)下載Thrift compiler for Windows代碼生成工具,thrift-0.13.0.exe,然后編寫腳本文件,代碼如下:
1 namespace netstd ThriftCore 2 3 service Calculator{ 4 5 i32 Add(1:i32 num1, 2:i32 num2) 6 string SayHello(); 7 } 8 9 10 service ThirdCalculator{11 12 i32 Add(1:i32 num1, 2:i32 num2)13 string SayHello();14 }
在命令行中執(zhí)行“thrift-0.13.0.exe --gen netstd tutorial.thrift”,會在目錄下生成“gen-netstd\ThriftCore\Calculator.cs”,“gen-netstd\ThriftCore\ThirdCalculator.cs”兩個文件。這部分使用與以前一致,只是語言部分需要指定netstd。完成后,將gen-netstd目錄加入到項(xiàng)目中,并且通過nuget引用安裝ApacheThrift 組件包,然后開始編寫基于thrift 的微服務(wù)。
創(chuàng)建業(yè)務(wù)接口
首先要針對于生成的Calculator,ThirdCalculator編寫業(yè)務(wù)接口,業(yè)務(wù)接口需要繼承IAsync,接口代碼如下:
IAsyncService:
1 [ServiceBundle("api/{Service}/{Method}")]2 public interface IAsyncService: ThriftCore.Calculator.IAsync, IServiceKey3 {4 [Command(ExecutionTimeoutInMilliseconds=10000)]5 Task<int> @AddAsync(int num1, int num2, CancellationToken cancellationToken = default(CancellationToken));6 7 Task<string> SayHelloAsync(CancellationToken cancellationToken = default(CancellationToken));8 }
IThirdAsyncService:
1 [ServiceBundle("api/{Service}/{Method}")]2 public interface IThirdAsyncService : ThriftCore.ThirdCalculator.IAsync, IServiceKey3 {4 Task<int> @AddAsync(int num1, int num2, CancellationToken cancellationToken = default(CancellationToken));5 6 Task<string> SayHelloAsync(CancellationToken cancellationToken = default(CancellationToken));7 }
創(chuàng)建業(yè)務(wù)領(lǐng)域服務(wù)
服務(wù)需要繼承IAsyncService、IThirdAsyncService業(yè)務(wù)接口,并且添加特性BindProcessor綁定Processor,代碼如下:
AsyncService:
1 [BindProcessor(typeof(AsyncProcessor))] 2 public class AsyncService : ProxyServiceBase, IAsyncService 3 { 4 public Task<int> AddAsync(int num1, int num2, CancellationToken cancellationToken = default) 5 { 6 return Task.FromResult(num1 + num2); 7 } 8 9 public Task<string> SayHelloAsync(CancellationToken cancellationToken = default)10 {11 return Task.FromResult("hello world");12 }13 }
ThirdAsyncService:
1 [BindProcessor(typeof(AsyncProcessor))] 2 public class ThirdAsyncService : ProxyServiceBase, IThirdAsyncService 3 { 4 public Task<int> AddAsync(int num1, int num2, CancellationToken cancellationToken = default) 5 { 6 return Task.FromResult(num1 + num2); 7 } 8 9 public Task<string> SayHelloAsync(CancellationToken cancellationToken = default)10 {11 return Task.FromResult("hello world,third");12 }13 }
更改選擇Rpc組件配置
如果選擇了多種同類型的組件,就需要安裝以下配置代碼配置surging.config, 配置如下:
啟用ThriftModule 組件:
1 "Packages": [2 {3 "TypeName": "EnginePartModule",4 "Using": "${UseEngineParts}|ServiceProxyModule;ThriftModule;SerilogModule;NLogModule;MessagePackModule;ConsulModule;WSProtocolModule;MqttProtocolModule;EventBusRabbitMQModule;CachingModule;KestrelHttpModule;DnsProtocolModule;SwaggerModule;ApiGeteWayModule;SkywalkingModule;KestrelNLogModule;KestrelNLogModule;ServiceHostModule;GrpcModule;ApolloModule;"5 }6 ]
啟用DotNettyModule 組件:
"Packages": [ { "TypeName": "EnginePartModule", "Using": "${UseEngineParts}|ServiceProxyModule;DotNettyModule;SerilogModule;NLogModule;MessagePackModule;ConsulModule;WSProtocolModule;MqttProtocolModule;EventBusRabbitMQModule;CachingModule;KestrelHttpModule;DnsProtocolModule;SwaggerModule;ApiGeteWayModule;SkywalkingModule;KestrelNLogModule;KestrelNLogModule;ServiceHostModule;GrpcModule;ApolloModule;" } ]
服務(wù)之間RPC遠(yuǎn)程調(diào)用
代碼如下:
var proxy = serviceProxyFactory.CreateProxy<IAsyncService>(); var result = await proxy.SayHelloAsync();
第三方客戶端如何調(diào)用:
代碼如下:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 var transport = new TSocketTransport("127.0.0.1", 981); 6 var tran = new TFramedTransport(transport); 7 var protocol = new TBinaryProtocol(tran); 8 var mp = new TMultiplexedProtocol(protocol, "AsyncService"); 9 var client = new Client(mp);10 var result= client.AddAsync(1,2).Result;11 var result1 = client.SayHelloAsync().Result;12 Console.WriteLine("輸出結(jié)果:{0},{1}", result, result1);13 Console.ReadLine();14 }15 }
結(jié)果:
如何選擇dotnetty 和 thrift
引擎中實(shí)現(xiàn)了dotnetty 和 thrift 兩個RPC組件,需要如何選擇使用呢?
第一,通過執(zhí)行10000次調(diào)用,我們使用和未使用Diagnostic兩個維度來對比兩個組件的性能,以下測試選擇的是messagepack 序列化組件
組件 | 未使用Diagnostic | 已使用Diagnostic |
Dotnetty | 1280毫秒左右 | 1680毫秒左右 |
Thrift | 860毫秒左右 | 1240毫秒左右 |
2.通過使用thrift 內(nèi)存少了40mb。
3.使用thrift 需要創(chuàng)建腳本文件,并且通過工具生成thrift代碼,而dotnetty不需要。
通過以上幾點(diǎn)對比,總結(jié)下,如果追求性能就用thrift,如果選擇高效,不繁瑣就用dotnetty.
結(jié)尾總結(jié)
通過幾年的發(fā)展,surging 已經(jīng)發(fā)展成優(yōu)秀的微服務(wù)引擎,為了surging 能良好的發(fā)展,而推出了商業(yè)化企業(yè)服務(wù),已經(jīng)和多家企業(yè)達(dá)成了企業(yè)支持服務(wù),并且考慮到后期發(fā)展需要,3.0+以上版本更改成非商用協(xié)議版本,3.0版本將會更加強(qiáng)大,可以支持多語言混合服務(wù),如果大家想免費(fèi)可以使用surging 2.0 ,可以隨意更改定制,也希望大家能支持我的商業(yè)化行為,有錢賺才有動力去創(chuàng)造出優(yōu)秀的產(chǎn)品框架。