GRPC C # learning,

Source: Internet
Author: User

GRPC C # learning,

GRPC version 1.0 was released the other day, representing that gRPC has entered a stable stage.

Today we will learn gRPC C #. It also supports. NET Core to achieve perfect cross-platform.

Traditional. NET can be called across platforms through Mono.

GitHub: https://github.com/grpc/grpc

GRPC introduction:

GRPC is a high-performance and common open-source RPC framework. It is designed by Google for mobile application development based on HTTP/2 Protocol standards and developed based on ProtoBuf (Protocol Buffers) serialization Protocol, it also supports many development languages. GRPC provides a simple method to precisely define services and automatically generate highly reliable client function libraries for iOS, Android, and background support services. The client makes full use of the advanced stream and link functions to help reduce bandwidth, TCP connections, CPU usage, and battery life.

GRPC supports multiple languages and automatically generates client and server functional libraries based on the language. Currently, grpc C, grpc Java, and grpc Go are available on GitHub, grpc supports C, C ++, and Node. grpc-java supports Android development in js, Python, Ruby, Objective-C, PHP, and C # languages.

GRPC has been applied to Google's cloud services and external APIs. Its main application scenarios are as follows:

Low latency, high scalability, and distributed system
Mobile Application client that communicates with ECs
New Protocol with independent, efficient, and accurate design languages
Hierarchical Design that facilitates various aspects of scaling, such as authentication, Server Load balancer, logging, and monitoring

Reference:

Http://www.infoq.com/cn/news/2015/03/grpc-google-http2-protobuf

This article mainly explains how traditional. NET Applications Support gRPC.

The following is the official start.

Create a project

First, create a gRPCDemo class library.

Then add two console applications, gRPCServer gRPCClient.

The final project structure is as follows:

Define Service

After the project is created, we define the service.

Here we add a helloworld. proto content in the gRPCDemo project as follows:

The following mainly defines a gRPC service with a SayHello rpc method.

syntax = "proto3";package gRPCDemo;service gRPC {  rpc SayHello (HelloRequest) returns (HelloReply) {}}message HelloRequest {  string name = 1;}message HelloReply {  string message = 1;}
Use Grpc. Tools to generate code

After the service is defined, we can generate code.

First, add reference:

Add Grpc and Google. Protobuf to each project

Install-Package Grpc
Install-Package Google.Protobuf

Add the Grpc. Tools tool in the gRPCDemo project.

NuGet command line:

Install-Package Grpc.Tools

Then run the following command on the command line. Note that the directory for executing the command is the upper directory of packages.

 

packages\Grpc.Tools.1.0.0\tools\windows_x86\protoc.exe -IgRPCDemo --csharp_out gRPCDemo  gRPCDemo\helloworld.proto --grpc_out gRPCDemo --plugin=protoc-gen-grpc=packages\Grpc.Tools.1.0.0\tools\windows_x86\grpc_csharp_plugin.exe

After the execution, there will be more Helloworld. cs and HelloworldGrpc. cs classes in the gRPCDemo Directory, which can be included in the gRPCDemo project.

Then gRPCServer gRPCClient references gRPCDemo.

 

Create a server and a client

Next we will write the server and client

 

The first is the server:

Program. cs

Class gRPCImpl: gRPC. gRPCBase {// implement the SayHello method public override Task <HelloReply> SayHello (HelloRequest request, ServerCallContext context) {return Task. fromResult (new HelloReply {Message = "Hello" + request. name}) ;}} class Program {const int Port = 9007; public static void Main (string [] args) {Server server = new Server {Services = {gRPC. bindService (new gRPCImpl ()}, Ports = {new ServerPort ("localhost", Port, ServerCredentials. insecure) }}; server. start (); Console. writeLine ("gRPC server listening on port" + Port); Console. writeLine ("any key to exit... "); Console. readKey (); server. shutdownAsync (). wait ();}}

The server must implement the SayHello method.

Then the client Program. cs

Class Program {static void Main (string [] args) {Channel channel = new Channel ("127.0.0.1: 9007", ChannelCredentials. insecure); var client = new gRPC. gRPCClient (channel); var reply = client. sayHello (new HelloRequest {Name = "LineZero"}); Console. writeLine ("from" + reply. message); channel. shutdownAsync (). wait (); Console. writeLine ("any key to exit... "); Console. readKey ();}}

Then we generate both the Client and Server.

Run the command in the corresponding directory. First, start gRPCServer and then execute gRPCClient.

GRPC is implemented after the communication is successful.

 

The source code GitHub: https://github.com/linezero/Blog/tree/master/gRPCDemo

 

If you think this article is helpful to you, click"Recommendation", Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.