在本文中将介绍如何经过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 ,能够随意更改定制,也但愿你们能支持个人商业化行为,有钱赚才有动力去创造出优秀的产品框架。