In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how C # uses Thrift as a RPC framework". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Thrift as a RPC framework in C#".
Perfect the development tools to install thrift in vs2019 through nuget
Right-click the management Nuget package in the project, and enter thrift in the open interface, as shown below:
We choose ApacheThrift to install. After installation, there is a reference to Thrift45.dll in our project, as shown below:
Download the thrift code generation tool
Thrift is a communication development RPC framework that supports heterogeneous languages and heterogeneous platforms. It describes the structures, classes and services you want to define through the intermediate language IDL, and generates the corresponding code in each language through the code generation tool.
Next, we go to the Thrift project on the Apache website to download the Thrift code generation tool, as shown below:
We put the downloaded exe file in the root directory of our project for later use, as shown below:
Now that we have prepared the development tools for thrift, let's write the thrift interface description file.
Thrift description file
The interface description file provided by thrift is a format file ending in thrift. Let's explain the syntax of this IDL.
It provides definitions of basic types, including bool, i16, i32, i64, double, string
Enum
Struct encapsulates some basic types, corresponding to the Class field in C#
There are also some definitions of collections, including that set corresponds to the HashSet in C #, which is a collection of unique elements (the Thrift framework defines the THashSet type corresponding to it); list corresponds to the List;map in C # corresponds to the Dictionary of C #
Service this is the focus of thrift, it is a collection of methods, thrift will generate client code and server code according to this definition
Exception corresponds to the Exception type in C #
For thrift's definition of types and sample code, please open this URL: IDL description
In the IDL description file, in addition to the definition of the type, Thrift also needs to remember the following knowledge points:
Namespace is defined in the header of the description file. Generate a multi-level directory with the current file as the root directory; add the namespace namespace to the generated code. The code is as follows:
Namespace csharp WpfApp1.ceshi
Include is defined in the header of the description file. Use this command to include other thrift description files, and the thrift file specified by include is also compiled when the generated code is compiled. The code is as follows:
Include "pe1.thrift"
For usage, the pe1.thrift file is defined as follows:
Struct pe1 {1: i32 id;}
In other files, introduce the above file, defined as follows:
Include "pe1.thrift" service GuidGenService {string getGuid (1:pe1.pe1 pe1);}
The typedef type definition command defines an alias for the types supported by thrift. The code is as follows:
Typedef i32 intstruct People {1: int ID;2: string Name;3: i16 Age;4: list onelist;5: set oneSet;6: map oneset;}
When the description file is defined, we generate the code file with the following cmd command:
Thrift-0.12.0.exe-r-gen csharp People.thrift
First, we need to jump the current path to the path where thrift-0.12.0.exe is located through cd
Example
I defined a description file as follows:
Namespace csharp WpfApp1.ceshiinclude "pe1.thrift" typedef i32 intstruct People {1: int ID;2: string Name;3: i16 Age;4: list onelist;5: set oneSet;6: map oneset;} service HelloService {void SetPeople (1: People people1);} service GuidGenService {string getGuid (1:pe1.pe1 pe1);}
After we execute the above command, the generated code structure is as follows:
When we open people.cs, we can see that struct people generates the following code:
Namespace WpfApp1.ceshi {# if! SILVERLIGHT [Serializable] # endif public partial class People: TBase {private int _ ID; private string _ Name; private short _ Age; private List _ onelist; private THashSet _ oneSet; private Dictionary _ oneset;.
You need to pay attention to the namespace of the people class here, which is consistent with what we talked about above.
The service GuidGenService we defined generates the following code:
This class is the focus of our use of the framework, it gives us a client-side and server-side code, from the figure we can see that the file generated under the two interfaces ISync and Iface, the former is synchronous method, the latter generated is an asynchronous call method, followed by a class for our client call, which inherits the iface interface, while iface inherits the ISync interface; next is a Processor, is the class used by the server.
Thank you for your reading, the above is the content of "how C # uses Thrift as a RPC framework". After the study of this article, I believe you have a deeper understanding of how C # uses Thrift as a RPC framework, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.