您的位置:首页 > 其它

如何使用thrift 服务引擎组件

2020-05-05 21:58 232 查看

在本文中将介绍如何通过thrift 组件集成到surging 微服务引擎中,然后可以选择dotnetty 或thrift作为服务远程调用RPC,也可以通过其它语言的thrift 调用surging 服务,下面将简单介绍如何使用thrift

准备工作

 首先需要到官网下载Thrift compiler for Windows代码生成工具,thrift-0.13.0.exe,然后编写脚本文件,代码如下:

namespace netstd ThriftCore

service Calculator{

i32 Add(1:i32 num1, 2:i32 num2)
string SayHello();
}

service ThirdCalculator{

i32 Add(1:i32 num1, 2:i32 num2)
string SayHello();
}

在命令行中执行“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:
[ServiceBundle("api/{Service}/{Method}")]
public interface IAsyncService: ThriftCore.Calculator.IAsync,  IServiceKey
{
[Command(ExecutionTimeoutInMilliseconds=10000)]
Task<int> @AddAsync(int num1, int num2, CancellationToken cancellationToken = default(CancellationToken));

Task<string> SayHelloAsync(CancellationToken cancellationToken = default(CancellationToken));
}

IThirdAsyncService:

[ServiceBundle("api/{Service}/{Method}")]
public interface IThirdAsyncService : ThriftCore.ThirdCalculator.IAsync, IServiceKey
{
Task<int> @AddAsync(int num1, int num2, CancellationToken cancellationToken = default(CancellationToken));

Task<string> SayHelloAsync(CancellationToken cancellationToken = default(CancellationToken));
}

创建业务领域服务

服务需要继承IAsyncService、IThirdAsyncService业务接口,并且添加特性BindProcessor绑定Processor,代码如下:

AsyncService:

[BindProcessor(typeof(AsyncProcessor))]
public class AsyncService : ProxyServiceBase, IAsyncService
{
public Task<int> AddAsync(int num1, int num2, CancellationToken cancellationToken = default)
{
return Task.FromResult(num1 + num2);
}

public Task<string> SayHelloAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult("hello world");
}
}

ThirdAsyncService:

[BindProcessor(typeof(AsyncProcessor))]
public class ThirdAsyncService : ProxyServiceBase, IThirdAsyncService
{
public Task<int> AddAsync(int num1, int num2, CancellationToken cancellationToken = default)
{
return Task.FromResult(num1 + num2);
}

public Task<string> SayHelloAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult("hello world,third");
}
}

更改选择Rpc组件配置

如果选择了多种同类型的组件,就需要安装以下配置代码配置surging.config,  配置如下:

启用ThriftModule 组件:

 

"Packages": [
{
"TypeName": "EnginePartModule",
"Using": "${UseEngineParts}|ServiceProxyModule;ThriftModule;SerilogModule;NLogModule;MessagePackModule;ConsulModule;WSProtocolModule;MqttProtocolModule;EventBusRabbitMQModule;CachingModule;KestrelHttpModule;DnsProtocolModule;SwaggerModule;ApiGeteWayModule;SkywalkingModule;KestrelNLogModule;KestrelNLogModule;ServiceHostModule;GrpcModule;ApolloModule;"
}
]

 

启用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();

 

第三方客户端如何调用:

代码如下:

class Program
{
static void Main(string[] args)
{
var transport = new TSocketTransport("127.0.0.1", 981);
var tran = new TFramedTransport(transport);
var protocol = new TBinaryProtocol(tran);
var mp = new TMultiplexedProtocol(protocol, "AsyncService");
var client = new Client(mp);
var result=  client.AddAsync(1,2).Result;
var result1 = client.SayHelloAsync().Result;
Console.WriteLine("输出结果:{0},{1}", result, result1);
Console.ReadLine();
}
}

结果:

 

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: