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