In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the method of using Grpc in .net Core". The content of the explanation in 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 "the method of using Grpc in .net Core".
I. Overview of Grpc
GRPC is based on the idea of defining a service and specifying its methods, parameters, and return types that can be called remotely. By default, gRPC uses protocol buffers as the interface definition language to describe the service interface and payload message structure. Other alternatives can be used if necessary.
The defined services are divided into four types:
Single RPC, that is, the client sends a request to the server and gets a reply from the server, just like a normal function call. This is the most commonly used.
Server-side streaming RPC, that is, the client sends a request to the server to obtain a data stream to read a series of messages. The client reads from the returned data stream until there are no more messages.
Client streaming RPC, that is, the client writes and sends a series of messages to the server using a data stream provided. Once the client finishes writing the message, it waits for the server to read the message and return a reply.
Bi-directional streaming RPC, that is, both sides can send a series of messages through a read-write data stream respectively. The two data flow operations are independent of each other, so the client and the server can read and write in any order they want. for example, the server can wait for all client messages before writing the reply, or it can read a message and then write a message, or some other way of combining reading and writing. The order of messages in each data stream is maintained.
2. Use Grpc in Net Core
There are three steps to using Grpc: define the Grpc service, implement the Grpc service, and invoke the Grpc service.
2.1 define Grpc services
1. Select .net Core in VS to create a class library.
2. Introduce Google.Protobuf and Grpc.Core packages
3. Create a proto file and define a SsmServer service. Two methods are provided in the service, one is the simplest single RPC method, and the other is a bidirectional streaming RPC. The latter file generates a C# class file with the same name.
Syntax = "proto3"; / / Syntax specifies proto3
The namespace of the C # file is generated after package ShenDa.SSM.Grpc; / /
Import "Protos/Common.proto"
Import "Protos/Health.proto"
Import "Protos/User.proto"; / / specify the location to define the parameters used in the service
Service SsmService {
/ / single RPC for health check
Rpc Health (EmptyRequest) returns (HealthResponse) {}
/ / two-way flow
Rpc User_Add (stream UserAddRequest) returns (stream UserAddResponse) {}
}
Defined Health.proto file
Syntax = "proto3"
Package ShenDa.SSM.Grpc
Message HealthResponse {/ / return parameters
Bool Success=1; / / each field must specify a sequence number
String Message=2
}
The above services are defined. Now you need to generate this proto file into a C # file, either using commands or tools. Here I use tools to generate.
5. Reference Grpc.Tools, and then specify the proto file to be generated in the project file.
When you build the project, the corresponding C# file is generated in the obj folder. Other files generate the corresponding entity class, but the proto file of the defined service, which is special, generates a class file with the same name, which contains
An abstract class with the name service name + Base. It contains the virtual two methods that we define.
A partial class named Service name + Client, which inherits ClientBase
All of the above Grpc services have been defined. Because it is impossible for the client to use each by adding an application project, we also need to package the client-side Nuget package.
6. Generate the Nuget package and provide it to the client to use
Package through VS settings to generate Nuget package, and generate Grpc client Nuget package.
2.2 implement Grpc services
Create the project through VS's GRPC template, define the implementation class and inherit the abstract class generated above, and then override the method we defined.
Public partial class SsmServiceImpl: SsmService.SsmServiceBase
{
Public override async Task Health (EmptyRequest request, ServerCallContext context)
{
Var response = new HealthResponse ()
{
Message = string.Empty
Success = true
}
Return await Task.FromResult (response)
}
}
Configure the Grpc service
Public void ConfigureServices (IServiceCollection services)
{
Services.AddGrpc ()
}
Public void Configure (IApplicationBuilder app, IWebHostEnvironment env)
{
App.UseEndpoints (endpoints = >
{
Endpoints.MapGrpcService (); / / implementation of the injection service.
Endpoints.MapGet ("/", async context = >
{
Await context.Response.WriteAsync ("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
})
})
}
Screenshot of the project structure: Zhengzhou to see where good http://www.ytsgnk.com/ is in the Men's Hospital.
Zhengzhou to see where to http://www.zztjnk.com/ in the Men's Hospital
2.3 client calls Grpc
1. Add references to Google.Protobuf, Grpc.Core, Grpc.Net.Client and the Nuget package ShenDa.SSM.Grpc that you just generated
2. Call
Class Program
{
Static async Task Main (string [] args)
{
Var channel = GrpcChannel.ForAddress ("https://localhost:5001");
Var client = new SsmServiceClient (channel)
Await HealthCheck (client)
}
Public static async Task HealthCheck (SsmServiceClient client)
{
Var response = await client.HealthAsync (new EmptyRequest ())
System.Console.WriteLine (response.Success? "Health": "connection failed")
}
}
Thank you for reading, the above is the content of "the method of using Grpc in .net Core". After the study of this article, I believe you have a deeper understanding of the problem of using Grpc in .net Core, 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.