How to use the thrift service engine component

Keywords: calculator Windows Attribute less

This article describes how to use thrift to call RPC remotely if thrift components are integrated into the surging micro-service engine, then dotnetty or thrift can be selected as the service or surging can be invoked through thrift in other languages. Here is a brief description of how to use thrift

Dead work

First need to Official Web Download the Thrift compiler for Windows code generation tool, thrift-0.13.0.exe, and write a script file with the following code:

 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 }

Executing "thrift-0.13.0.exe--gen netstd tutorial.thrift" from the command line generates "gen-netstd\ThriftCoreCalculator.cs" and "gen-netstd\ThriftCore\ThirdCalculator.cs" files in the directory.This part is used as before, but the language part needs to specify netstd.When finished, add the gen-netstd directory to the project, install the ApacheThrift component package by nuget reference, and start writing a thrift-based microservice.

Create Business Interface

The first step is to write a business interface for the generated Calculator, ThirdCalculator, which inherits IAsync with the following interface code:

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     }

Create Business Domain Services

The service needs to inherit the IAsyncService, IThirdAsyncService business interface, and add the attribute BindProcessor to bind Processor with the following code:

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     }

Change Selection Rpc Component Configuration

If you have selected multiple components of the same type, you will need to install the following configuration code to configure surging.config, as follows:

Enable the ThriftModule component:

 

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     ]

 

Enable the DotNettyModule component:

 

"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 remote calls between services

The code is as follows:

var proxy = serviceProxyFactory.CreateProxy<IAsyncService>();
             var result = await proxy.SayHelloAsync();

 

How third-party clients invoke:

The code is as follows:

 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("Output results:{0},{1}", result, result1);
13             Console.ReadLine();
14         }
15     }

Result:

 

How to select dotnetty and thrift

Two RPC components, dotnetty and thrift, are implemented in the engine. How do you choose to use them?

First, by executing 10,000 calls, we compared the performance of the two components using and without Diagnostic dimensions. The following tests chose the messagepack serialization component

assembly Diagnostic not used Diagnostic used
Dotnetty About 1280 milliseconds Around 1680 milliseconds
Thrift 860 milliseconds or so About 1240 milliseconds

2. 40 MB less memory by using thrift.

3. Using thrift requires creating script files and generating thrift code from tools that dotnetty does not need.

In summary, thrift is used for performance and dotnetty for efficiency.

Ending Summary

Over the years, surging has developed into an excellent microservice engine for surgingCan develop well, but has launched commercial enterprise services, has reached enterprise support services with many enterprises, and considering the needs of later development, version 3.0+ and above will be changed to non-commercial agreement version, version 3.0 will be more powerful, can support multi-language mixed services, if you want to use surging 2.0 for free, you can change your customization at will, and I hope you can support my commercialization so that you can make money to create a good product framework.

Posted by DillyDong on Tue, 05 May 2020 20:02:48 -0700