在本文中將介紹如何經過thrift 組件集成到surging 微服務引擎中,而後能夠選擇dotnetty 或thrift做爲服務遠程調用RPC,也能夠經過其它語言的thrift 調用surging 服務,下面將簡單介紹如何使用thriftapache
準備工做api
首先須要到官網下載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 }
在命令行中執行「thrift-0.13.0.exe --gen netstd tutorial.thrift」,會在目錄下生成「gen-netstd\ThriftCore\Calculator.cs」,「gen-netstd\ThriftCore\ThirdCalculator.cs」兩個文件。這部分使用與之前一致,只是語言部分須要指定netstd。完成後,將gen-netstd目錄加入到項目中,而且經過nuget引用安裝ApacheThrift 組件包,而後開始編寫基於thrift 的微服務。微服務
建立業務接口工具
首先要針對於生成的Calculator,ThirdCalculator編寫業務接口,業務接口須要繼承IAsync,接口代碼以下:性能
IAsyncService:
1 [ServiceBundle("api/{Service}/{Method}")] 2 public interface IAsyncService: ThriftCore.Calculator.IAsync, IServiceKey 3 { 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, IServiceKey 3 { 4 Task<int> @AddAsync(int num1, int num2, CancellationToken cancellationToken = default(CancellationToken)); 5 6 Task<string> SayHelloAsync(CancellationToken cancellationToken = default(CancellationToken)); 7 }
建立業務領域服務spa
服務須要繼承IAsyncService、IThirdAsyncService業務接口,而且添加特性BindProcessor綁定Processor,代碼以下:命令行
AsyncService:3d
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;" } ]
服務之間RPC遠程調用
代碼以下:
var proxy = serviceProxyFactory.CreateProxy<IAsyncService>(); var result = await proxy.SayHelloAsync();
第三方客戶端如何調用:
代碼以下:
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("輸出結果:{0},{1}", result, result1); 13 Console.ReadLine(); 14 } 15 }
結果:
如何選擇dotnetty 和 thrift
引擎中實現了dotnetty 和 thrift 兩個RPC組件,須要如何選擇使用呢?
第一,經過執行10000次調用,咱們使用和未使用Diagnostic兩個維度來對比兩個組件的性能,如下測試選擇的是messagepack 序列化組件
組件 | 未使用Diagnostic | 已使用Diagnostic |
Dotnetty | 1280毫秒左右 | 1680毫秒左右 |
Thrift | 860毫秒左右 | 1240毫秒左右 |
2.經過使用thrift 內存少了40mb。
3.使用thrift 須要建立腳本文件,而且經過工具生成thrift代碼,而dotnetty不須要。
經過以上幾點對比,總結下,若是追求性能就用thrift,若是選擇高效,不繁瑣就用dotnetty.
結尾總結
經過幾年的發展,surging 已經發展成優秀的微服務引擎,爲了surging 能良好的發展,而推出了商業化企業服務,已經和多家企業達成了企業支持服務,而且考慮到後期發展須要,3.0+以上版本更改爲非商用協議版本,3.0版本將會更增強大,能夠支持多語言混合服務,若是你們想免費可使用surging 2.0 ,能夠隨意更改定製,也但願你們能支持個人商業化行爲,有錢賺纔有動力去創造出優秀的產品框架。