GRPC C # Learning

Source: Internet
Author: User

The previous days Grpc released version 1.0, representing the GRPC has formally entered the stable phase.

Today we are going to learn Grpc C #. And now support. NET Core for perfect cross-platform.

Traditional. NET can make cross-platform calls through mono.

Github:https://github.com/grpc/grpc

GRPC Brief Introduction:

GRPC is a high-performance, general-purpose, open-source RPC framework developed by Google primarily for mobile applications and based on the HTTP/2 protocol standards, developed based on the PROTOBUF (Protocol buffers) serialization protocol, and supports many development languages. GRPC provides an easy way to precisely define services and automatically generate a highly reliable client function library for iOS, Android, and background support services. Clients take advantage of advanced streaming and linking capabilities to help conserve bandwidth, reduce the number of TCP links, conserve CPU usage, and battery life.

GRPC supports multiple languages and is able to automatically generate client and server function libraries based on language. Currently, version C Grpc, Java version Grpc-java and go version grpc-go are available on GitHub, and other language versions are actively under development, with GRPC support for C, C + +, node. js, Python, Ruby, Objective-c, PHP, and C # and other languages, Grpc-java has supported Android development.

GRPC has been applied to Google's cloud services and externally available APIs, and its main scenarios are as follows:

Low latency, highly scalable, distributed systems
Mobile application clients that communicate with cloud servers
Design language independent, efficient, accurate new protocol
Layered design for easy expansion of all facets, such as authentication, load balancing, logging, monitoring, etc.

Reference Documentation:

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

This article focuses on how traditional. NET Applications support GRPC.

Here's the official start.

New Project

First, we create a Grpcdemo class library.

Then add two console applications Grpcserver grpcclient.

The final project structure is as follows:

Defining services

After we've built the project, we'll define the service.

Here we add a helloworld.proto content to the Grpcdemo project as follows:

The following main definition of a GRPC service there is a SayHello RPC method

" Proto3 " ;p ackage grpcdemo;service grpc {  rpc SayHello (hellorequest) returns (helloreply) {}}message Hellorequest {   string1;} Message helloreply {  string1;}
Generating code using Grpc.tools

After defining the service, we can generate the code.

First you need to add a reference:

Add Grpc and google.protobuf to each project

Install-package GRPC
Install-package Google.protobuf

Then add tools to the Grpcdemo project Grpc.tools

NuGet command line:

Install-package Grpc.tools

Then execute the following command at the command line, noting that the directory that executes the command is the upper-level 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 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 refer to Grpcdemo respectively.

Create Server and Client

Let's write the server and the client.

The first is the service side:

Program.cs

    classGRPCImpl:gRPC.gRPCBase {//implementing the SayHello method         Public OverrideTaskSayHello (hellorequest request, Servercallcontext context) {returnTask.fromresult (Newhelloreply {Message ="Hello"+request.        Name}); }    }    classProgram {Const intPort =9007;  Public Static voidMain (string[] args) {Server server=NewServer {Services= {Grpc.bindservice (NewGrpcimpl ())}, Ports= {NewServerPort ("localhost", Port, Servercredentials.insecure)}            }; Server.            Start (); Console.WriteLine ("GRPC Server listening on port"+Port); Console.WriteLine ("any key exit ...");            Console.readkey (); Server. Shutdownasync ().        Wait (); }    }

The service side needs to implement the SayHello method.

Then the client Program.cs

    classProgram {Static voidMain (string[] args) {Channel Channel=NewChannel ("127.0.0.1:9007", channelcredentials.insecure); varClient =Newgrpc.grpcclient (channel); varreply= client. SayHello (Newhellorequest {Name ="Linezero" }); Console.WriteLine ("from"+reply.            Message); Channel. Shutdownasync ().            Wait (); Console.WriteLine ("any key exit ...");        Console.readkey (); }    }

Then we will generate both client and server.

Proceed to the corresponding directory, start Grpcserver First, and then execute grpcclient.

The communication was successful and the GRPC was realized.

This article source Github:https://github.com/linezero/blog/tree/master/grpcdemo

If you think this article is helpful to you, please click " recommend ", thank you.

GRPC C # Learning

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.