In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you the meaning of calling methods REST and RPC in .NET Core micro-services. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1. REST or RPC? 1.1REST & RPC
Interface calls between microservices usually consist of two parts, serialization and communication protocols. Common serialization protocols include json, xml, hession, protobuf, thrift, text, bytes, etc. Http, soap, websockect,RPC are usually implemented based on TCP, and common frameworks such as dubbo,netty, mina and thrift are popular in communication.
REST: strictly speaking, the interface is very standard, and the operation object is the resource. The four operations on the resource (post, get, put, delete), and the parameters are all placed on URL, but not strictly speaking, Http+json, Http+xml, common http api can be called Rest interface.
RPC: what we often call a remote procedure call is to call a remote method as if it were a local method. Communication protocols are mostly in binary mode.
1.2 HTTP vs high performance binary protocol
HTTP is relatively more standardized, more standard, more general-purpose, no matter which language supports the HTTP protocol. If you are open to API, such as an open platform, there are a variety of external programming languages, you can not refuse to support each language, accordingly, if you use HTTP, there is no doubt that before you achieve SDK, all languages are supported, so now open source middleware, basically the first few protocols supported include RESTful.
The performance of the RPC protocol is much higher, such as Protobuf, Thrift, Kyro, etc. (if serialization is taken into account), the throughput is about twice that of http. The response time is also better. Do not underestimate this performance loss, it is recognized that micro-services do better, for example, netflix, Ali, have been reported to merge services to improve performance. If it is a delivery project, performance is more important, because what you sell to customers often depends on a weak advantage in performance.
Therefore, the best practice is generally external REST, internal RPC, but the pursuit of extreme performance will cost a lot of extra cost, so generally internal is also REST, but for individual interfaces with high performance requirements, RPC is used.
II. Case structure
Here, suppose there are two services, a ClinetService and a PaymentService, in which PaymentService has two parts, one is the WebApi part based on REST style, it is mainly responsible for some query services that do not require performance, and the other is TCP-based RPC Server, which is mainly responsible for some services with high performance requirements, such as money-related interfaces such as payment and expenditure. Suppose that when consuming ClientService, User needs to call PaymentService to obtain Payment History according to the customer account (go REST) and conduct transaction operations (go RPC).
3. REST calls 3.1.A useful REST Client: WebApiClient
Anyone who has used Java Feign Client knows that a good declarative REST client can save us a lot of effort. Under. Net, the big nine in the garden wrote a REST Client:WebApiClient similar to Feign Client. WebApiClient is an open source httpClient client library on github, internally based on HttpClient development, is a framework that only needs to define C # interface (interface) and related features, and can call http-api asynchronously, supporting .net framework4.5+, netcoreapp2.0 and netstandard2.0. Its GitHub address is: https://github.com/dotnetcore/WebApiClient
How to install it?
NuGet > Install-Package WebApiClient-JIT
3.2Using examples: follow API Gateway
Step1. Define HTTP Interfac
It should be noted here that since we are going to use the API gateway, the HttpHost address defined here is a fake, which will be overwritten when the specific call is made later, but one must be written here, otherwise it cannot be used.
Step2. Can be called asynchronously in Controller
Here the implementation of PaymentService is very simple, which returns a collection of String:
The final call result is as follows:
3.3 use examples: directly access specific services
In the case of a large number of services and multiple instances deployed for a single service, we can transfer through the API gateway, but when we do not need to transfer through the API gateway in some scenarios, such as high performance requirements and low load, a single instance is sufficient, we can directly connect with the service to be communicated, thus eliminating the need to make a detour from the API gateway.
Step1. Change the HTTP interface:
By the same token, the HttpHost here needs to be overwritten later because we have configured it in the configuration file.
Step2. Change the calling code:
The final call result is as follows:
4. Brief introduction of RPC call 4.1 Thrift
Thrift is a software framework for the development of extensible and cross-language services. It combines a powerful software stack and code generation engine to build seamless and efficient services that integrate seamlessly between Cellular languages, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml.
Of course, there is also a choice of gRPC, but from the online performance tests, Thrift performance should be more than 2 times better than gRPC, but the documentation of gRPC is much more friendly than Thrift.
4.2 use of Thrift
(1) download Thrift (select Windows version here)
After the download is complete, unzip it. Here I rename it to thrift.exe (without the version number). It will be more convenient to type it on the command line later.
(2) write a PaymentService.thrift, which is an IDL intermediate language
(3) generate C # code according to thrift syntax rules
Cmd > thrift.exe-gen csharp PaymentService.thrift
(4) create a Contracts class library project and put the generated C # code into it
4.3Add RPC Server
(1) add a console project as our Payment Service RPC Server and reference the Contracts class library project
(2) introduce thrift-netcore package:
NuGet > Install-Package apache-thrift-netcore
(3) add a new PaymentService implementation class
The log is output here for testing purposes only.
(4) write the main program to start RPC Server.
(5) if there are multiple service implementations, you can also start them as follows:
4.4 call RPC
The apache-thrift-netcore package is also introduced in ClientService, and then modified at the place of call as follows:
The final test results are as follows:
What do you mean by REST and RPC in the .NET Core microservice? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.