日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

netcore中使用grpc

發(fā)布時間:2025/7/14 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 netcore中使用grpc 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

簡介

? ?grpc是由google公司開發(fā)的一個高性能、開源和通用的RPC框架,采用HTTP/2通信。

? 1.gRPC的傳輸使用http/2支持雙向流。

? 2.支持多語言,例如java、go、php、net、node等多種語言.

? 3.gRPC支持多平臺

? 4.性能好,效率高

??

1.HTTP/2

?HTTP/2 提供了連接多路復用、雙向流、服務器推送、請求優(yōu)先級、首部壓縮等機制。可以節(jié)省帶寬、降低TCP鏈接次數(shù)、節(jié)省CPU,幫助移動設備延長電池壽命等。gRPC 的協(xié)議設計上使用了HTTP2 現(xiàn)有的語義,請求和響應的數(shù)據(jù)使用HTTP Body 發(fā)送,其他的控制信息則用Header 表示。

?

使用

? 以下演示基于netcore2.2進行創(chuàng)建運行

?

? 運行前準備

? 因為netcore不會生成依賴包,所以需要先新建一個net framework的類庫程序把需要用到的文件下載來

? 1.新建net framework程序并且安裝grpc.tools?

? ??

2.安裝成功之后項目文件夾里會生成一個packages文件,在這里找到我們所需要的工具(x86還64請根據(jù)自己電腦自行選擇)

?

?

?1.環(huán)境準備

  • ? GrpcClient? ?命令行程序
  • ? GrpcLibray? 類庫程序
  • ? GrpcServer 命令行程序

?

?

2.安裝項目依賴

? ? GrpcClient和GrpcLibray和GrpcServer 都需要安裝通用的插件

install-package Grpc install-package Goole.Protobuf

?3.在GrpcLibray程序當中把前面我們用netframework添加的exe文件添加到該項目當中

4.添加命令行用來生成供其他程序調用的代碼(hello.cmd)

protoc -I . --csharp_out ./server --grpc_out ./server --plugin=protoc-gen-grpc=grpc_csharp_plugin.exe hello.proto

5.編寫接口服務(hello.proto)

syntax = "proto3"; package GrpcLibrary; service HelloService{ rpc GetSum(GetMsgNumRequest) returns (GetMsgSumReply){};rpc test(GetLotID)returns(GetLotAll){}; } message GetMsgNumRequest { int32 Num1 = 1; int32 Num2 = 2;} message GetMsgSumReply { int32 Sum = 1;}message GetLotID{int32 Num1=1;string s=2;}message GetLotAll{ enum Lists{mes=0;wip=1;eap=2;dcs=3; } Lists t=1;repeated UserList user=2; }message UserList{string userName=1;string pwd=2; }

6.在GrpcServer當中引用GrpcLibrary,并且重寫定義的方法

using Grpc.Core; using GrpcLibrary; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks;namespace GrpcServer {class Program{public class GrpcImpl : HelloService.HelloServiceBase{public override async Task<GetMsgSumReply> GetSum(GetMsgNumRequest request, ServerCallContext context){var result = new GetMsgSumReply();result.Sum = request.Num1 + request.Num2;return result;}public override async Task<GetLotAll> test(GetLotID request, ServerCallContext context){var result = new GetLotAll();string s = "這里一共有:";if (request.Num1 == 1){s += "1個字符";}s += request.S;UserList userList = new UserList();userList.UserName = "張三";result.User.Add(userList);List<UserList> userLists = new List<UserList>();result.User.AddRange(userLists);result.T = GetLotAll.Types.Lists.Wip;return result;}}private static Server _server;static void Main(string[] args){_server = new Server {Services = {HelloService.BindService(new GrpcImpl()) },Ports = { new ServerPort("localhost",8088,ServerCredentials.Insecure)}};_server.Start();Console.WriteLine("grpc ServerListening On Port 8088");Console.WriteLine("任意鍵退出...");Console.ReadKey();_server?.ShutdownAsync().Wait();}} }

7.grpcClient客戶端也需要引用GrpcLibrary

using Grpc.Core; using GrpcLibrary; using System; using System.Collections.Generic; using System.Text;namespace GrpcClient {public static class HelloClient{private static Channel _channel;private static HelloService.HelloServiceClient _client;static HelloClient(){_channel = new Channel("localhost:8088",ChannelCredentials.Insecure);_client = new HelloService.HelloServiceClient(_channel);}public static GetMsgSumReply getSum(int num1, int num2){return _client.GetSum(new GetMsgNumRequest { Num1=num1,Num2=num2});}public static GetLotAll GetLotAll(int num1, string s){return _client.test(new GetLotID { Num1 = num1, S = s });}} }



?8.客戶端調用

using GrpcLibrary; using System;namespace GrpcClient {class Program{static void Main(string[] args){GetMsgSumReply helloMsg = HelloClient.getSum(11,2) ;GetLotAll getLotAll = HelloClient.GetLotAll(132, "我是老哈哈哈哈");Console.WriteLine("grpc Client Call GetSum():" + helloMsg.Sum);string user = string.Empty;foreach (var item in getLotAll.User){user += item;}Console.WriteLine("grpc Client Call GetLotAll():" + user);Console.WriteLine("任意鍵退出...");Console.ReadKey();}} }

?

?

?

運行如下圖所示,先運行服務器端,在運行客戶端

?源碼地址:https://github.com/zhengyazhao/grpc.git

轉載于:https://www.cnblogs.com/zhengyazhao/p/11120645.html

總結

以上是生活随笔為你收集整理的netcore中使用grpc的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。