如何使用thrift 服務引擎組件

在本文中將介紹如何通過thrift 組件集成到surging 微服務引擎中,然後可以選擇dotnetty 或thrift作爲服務遠程調用RPC,也可以通過其它語言的thrift 調用surging 服務,下面將簡單介紹如何使用thrift

準備工作

 首先需要到官網下載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     }

創建業務領域服務

服務需要繼承IAsyncService、IThirdAsyncService業務接口,並且添加特性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;"
      }
    ]

服務之間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 ,可以隨意更改定製,也希望大家能支持我的商業化行爲,有錢賺纔有動力去創造出優秀的產品框架。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章