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

准备工作

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

 namespace netstd ThriftCore

 service Calculator{

   i32 Add(:i32 num1, :i32 num2)
string SayHello();
} service ThirdCalculator{ i32 Add(:i32 num1, :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=)]
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", );
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(,).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 ,可以随意更改定制,也希望大家能支持我的商业化行为,有钱赚才有动力去创造出优秀的产品框架。

如何使用thrift 服务引擎组件的更多相关文章

  1. 浅谈surging服务引擎中的rabbitmq组件和容器化部署

    1.前言 上个星期完成了surging 的0.9.0.1 更新工作,此版本通过nuget下载引擎组件,下载后,无需通过代码build集成,引擎会通过Sidecar模式自动扫描装配异构组件来构建服务引擎 ...

  2. servicemix-3.2.1 内置的服务引擎和绑定组件

    服务引擎: servicemix-bean servicemix-camel servicemix-cxf-se servicemix-drools servicemix-eip servicemix ...

  3. python thrift 服务端与客户端使用

    一.简介 thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, ...

  4. Slickflow.NET 开源工作流引擎基础介绍(二) -- 引擎组件和业务模块的交互

    集成流程引擎的必要性 业务过程的变化是在BPM系统中常见的现象,企业管理层需要不断优化组织架构,改造业务流程,不可避免地带来了业务流程的变化,企业信息系统就会随之面临重构的可能性.一种直接的方式是改造 ...

  5. Apache OFBiz 学习笔记 之 服务引擎 二

    加载服务定义文件   ofbiz-component.xml:所有的服务定义文件在每个组件的ofbi-component.xml文件中   加载服务定义 例:framework/common/ofbi ...

  6. Apache OFBiz 学习笔记 之 服务引擎 一

    概述     服务定义为一段独立的逻辑顺序,当多个服务组合一起时可完成不同类型的业务需求     服务有很多类型,WorkFlow.Rules.Java.SOAP.BeanShell等.java类型的 ...

  7. 如何从零开始实现一个soa远程调用服务基础组件

    说起soa远程调用基础组件,最著名的莫过于淘宝的dubbo了,目前很多的大型互联网公司都有一套自己的远程服务调用分布式框架,或者是使用开源的(例如dubbo),或者是自己基于某种协议(例如hessia ...

  8. surging 微服务引擎 1.0 正式发布

    surging 是一个分布式微服务引擎,提供高性能RPC远程服务调用,服务引擎支持http.TCP.WS.Mqtt协议,采用Zookeeper.Consul作为surging服务的注册中心,集成了哈希 ...

  9. Slickflow.NET 开源工作流引擎基础介绍(二) -- 引擎组件和业务系统的集成

    集成流程引擎的必要性 业务过程的变化是在BPM系统中常见的现象,企业管理层需要不断优化组织架构,改造业务流程,不可避免地带来了业务流程的变化,企业信息系统就会随之面临重构的可能性.一种直接的方式是改造 ...

随机推荐

  1. 9.2ArrayList 集合 案例,学生管理系统

    循环的使用 添加学生:while嵌套for,for设置变量,内嵌if更新变量.if语句判断变量值 修改学生:for循环内嵌if,获取循环中的某个值. package day9_ArrayList.AL ...

  2. String 对象-->substr() 方法

    1.定义和用法 substr() 方法可在字符串中抽取从 开始 下标开始的指定数目的字符. 语法: string.substr(start,length) 参数: start:提取开始下标 lengt ...

  3. sqlserver2008 服务器实例连接

    一.sqlserver 配置管理器里面,看sqlserver服务那个,如果只有一个mssqlserver,那就用local或者.来访问,如果不是,可能有命名实例.

  4. 全平台阅读器 StartReader

    前段时间在网上闲逛, 发现了一款全平台阅读器 StartReader, 用了一阵子感觉还不错,网址是: https://www.startreader.com/ 感觉这款阅读器是程序员的福音,it人员 ...

  5. 一道简单的SQL注入题

    这是我真正意义上来说做的第一道SQL题目,感觉从这个题目里还是能学到好多东西的,这里记录一下这个题目的writeup和在其中学到的东西 link:https://www.ichunqiu.com/ba ...

  6. kafka消息分区机制原理

    背景 kafka如何支撑海量消息的集中写入? 答案就是消息分区. 核心思想是:负载均衡,采用合适的分区策略把消息写到不同的broker上的分区中: 其它的产品中有类似的思想. 比如monogodb, ...

  7. [GO] linux 下安装GO

    yum install mercurial安装 mercurial包 安装git包 yum install git 安装gcc yum install gcc 然后就可以下载golang的压缩包了 对 ...

  8. [Yii2] 快速套模板,加载JS,CSS(HTML标签<base>)

    刚开始学,弄了好久,不知道怎么加载JS.CSS,以及怎么不加载YII2自带的模板!其实真的好简单! 补: 其实是我垃圾,YII2默认访问路径是WEB,所以把style文件放到web下就能加载! 首先把 ...

  9. Springboot:属性常量赋值以及yml配置文件语法(四)

    方式一: 注解赋值 构建javaBean:com\springboot\vo\Dog 1:@Component:注册bean到spring容器中 2:添加get set toString方法 3:使用 ...

  10. 【Django 2.2文档系列】Model 外键中的on_delete参数用法

    场景 我们用Django的Model时,有时候需要关联外键.关联外键时,参数:on_delete的几个配置选项到底是干嘛的呢,你知道吗? 参数介绍 models.CASCADE 级联删除.Django ...