如何使用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 ,可以随意更改定制,也希望大家能支持我的商业化行为,有钱赚才有动力去创造出优秀的产品框架。

posted @ 2020-05-05 21:58  fanly11  阅读(1545)  评论(1编辑  收藏  举报