首页 > 其他分享 >.Net Core(.Net6)创建grpc

.Net Core(.Net6)创建grpc

时间:2023-02-14 22:35:17浏览次数:63  
标签:Core grpc await public result var new Net Message

1.环境要求

.Net6,Visual Studio 2019 以上

官方文档: https://learn.microsoft.com/zh-cn/aspnet/core/tutorials/grpc/grpc-start
Net Framework 版本: https://www.cnblogs.com/dennisdong/p/17119944.html

2.搭建帮助类

2.1 新建类库

GrpcCommon

2.2 新建文件夹

文件夹:Certs,Helpers,Models

2.3 安装依赖

NuGet依赖包Microsoft.AspNetCore.Authentication.JwtBeare 6.0.12,Newtonsoft.Json 13.0.2

2.4 新建项目文件

Models下新建JwtToken.csUserDetails.cs

namespace GrpcCommon.Models
{
    public class JwtToken
    {
        public string? UserId { get; set; }
        public string? Exp { get; set; }
        public string? Iss { get; set; }
    }
}
namespace GrpcCommon.Models
{
    public class UserDetails
    {
        public string? UserName { get; set; }
        public int Age { get; set; }
        public IEnumerable<string>? Friends { get; set; }
    }
}

Helpers下新建JwtHelper.cs

using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;

namespace GrpcCommon.Helpers
{
    public class JwtHelper
    {
        /// <summary>
        /// 颁发JWT Token
        /// </summary>
        /// <param name="securityKey"></param>
        /// <param name="accountName"></param>
        /// <returns></returns>
        public static string GenerateJwt(string securityKey, string accountName)
        {
            var claims = new List<Claim>
            {
                new Claim("userid", accountName)
            };

            //秘钥 (SymmetricSecurityKey 对安全性的要求,密钥的长度太短会报出异常)
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwt = new JwtSecurityToken(
                issuer: "https://ifcloud.com/zerotrust",
                claims: claims,
                expires: DateTime.Now.AddMinutes(1),
                signingCredentials: credentials);
            var jwtHandler = new JwtSecurityTokenHandler();
            var encodedJwt = jwtHandler.WriteToken(jwt);
            return encodedJwt;
        }

        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="token"></param>
        /// <param name="securityKey"></param>
        /// <returns></returns>
        public static Tuple<bool, string> ValidateJwt(string token, string securityKey)
        {
            try
            {
                //对称秘钥
                SecurityKey key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(securityKey));
                //校验token
                var validateParameter = new TokenValidationParameters()
                {
                    ValidateAudience = false,
                    ValidIssuer = "https://ifcloud.com/zerotrust",
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = key,
                    ClockSkew = TimeSpan.Zero//校验过期时间必须加此属性
                };
                var jwtToken = new JwtSecurityTokenHandler().ValidateToken(token, validateParameter, out _);
                var claimDic = new Dictionary<string, string>();

                foreach (var claim in jwtToken.Claims)
                {
                    claimDic.TryAdd(claim.Type, claim.Value);
                }

                var payLoad = JsonConvert.SerializeObject(claimDic);

                return new Tuple<bool, string>(true, payLoad);
            }
            catch (SecurityTokenExpiredException expired)
            {
                //token过期
                return new Tuple<bool, string>(false, expired.Message);
            }
            catch (SecurityTokenNoExpirationException noExpiration)
            {
                //token未设置过期时间
                return new Tuple<bool, string>(false, noExpiration.Message);
            }
            catch (SecurityTokenException tokenEx)
            {
                //表示token错误
                return new Tuple<bool, string>(false, tokenEx.Message);
            }
            catch (Exception err)
            {
                // 解析出错
                Console.WriteLine(err.StackTrace);
                return new Tuple<bool, string>(false, err.Message);
            }
        }
    }
}

3.生成SSL证书(可跳过)

3.1 下载安装openssl

参考文章:https://www.cnblogs.com/dingshaohua/p/12271280.html

3.2 生成证书密钥

GrpcCommonCerts下右键打开命令窗口输入openssl

genrsa -out key.pem 2048

3.3 生成pem证书

req -new -x509 -key key.pem -out cert.pem -days 3650

3.4 pem证书转换成pfx证书

pkcs12 -export -out cert.pfx -inkey key.pem -in cert.pem

4.搭建grpc服务器

4.1 新建grpc服务

GrpcServer

4.2 新建文件夹

文件夹:Protos及其子文件夹Google

4.3 下载google protobuf文件

https://github.com/protocolbuffers/protobuf/releases/download/v21.12/protoc-21.12-win64.zip
其他版本参考:https://github.com/protocolbuffers/protobuf/releases
下载不了的文章末尾有源码地址

下载解压后将\include\google\protobuf中的所有文件放在Protos下的Google

4.4 新建proto文件

Protos下新建文件example.proto

syntax = "proto3";

package example;
import "Protos/Google/struct.proto";

option csharp_namespace = "GrpcExample";

service ExampleServer {
	// Unary
	rpc UnaryCall (ExampleRequest) returns (ExampleResponse);

	// Server streaming
	rpc StreamingFromServer (ExampleRequest) returns (stream ExampleResponse);

	// Client streaming
	rpc StreamingFromClient (stream ExampleRequest) returns (ExampleResponse);

	// Bi-directional streaming
	rpc StreamingBothWays (stream ExampleRequest) returns (stream ExampleResponse);
}

message ExampleRequest {
	string securityKey = 1;
	string userId = 2;
	google.protobuf.Struct userDetail = 3;
	string token = 4;
}

message ExampleResponse {
	int32 code = 1;
	bool result = 2;
	string message = 3;
}

4.5 编译生成Stub

GrpcServer项目右键编辑项目文件添加内容

<ItemGroup>
	<Protobuf Include="Protos\example.proto" GrpcServices="Server" />
</ItemGroup>

4.6 添加ssl证书(可跳过)

修改Program.cs

builder.WebHost
    .ConfigureKestrel(serviceOpt =>
    {
        var httpPort = builder.Configuration.GetValue<int>("port:http");
        var httpsPort = builder.Configuration.GetValue<int>("port:https");
        serviceOpt.Listen(IPAddress.Any, httpPort, opt => opt.UseConnectionLogging());
        serviceOpt.Listen(IPAddress.Any, httpsPort, listenOpt =>
        {
            var enableSsl = builder.Configuration.GetValue<bool>("enableSsl");
            if (enableSsl)
            {
                listenOpt.UseHttps("Certs\\cert.pfx", "1234.com");
            }
            else
            {
                listenOpt.UseHttps();
            }

            listenOpt.UseConnectionLogging();
        });
    });

修改appsettings.json,添加配置项

  "port": {
    "http": 5000,
    "https": 7000
  },
  "enableSsl": true

4.7 新建服务类

ExampleService

using Grpc.Core;
using GrpcCommon.Helpers;
using GrpcCommon.Models;
using GrpcExampleServer;
using Newtonsoft.Json;

namespace GrpcServer.Services
{
    public class ExampleService : ExampleServer.ExampleServerBase
    {
        private readonly ILogger<ExampleService> _logger;

        public ExampleService(ILogger<ExampleService> logger)
        {
            _logger = logger;
        }

        public override Task<ExampleResponse> UnaryCall(ExampleRequest request, ServerCallContext context)
        {
            Console.WriteLine(request.ToString());
            _logger.LogInformation(request.ToString());
            var tokenRes = JwtHelper.ValidateJwt(request.Token, request.SecurityKey);

            // 正常响应客户端一次
            ExampleResponse result;

            if (tokenRes.Item1)
            {
                var payLoad = JsonConvert.DeserializeObject<JwtToken>(tokenRes.Item2);
                if (payLoad == null)
                {
                    result = new ExampleResponse
                    {
                        Code = -1,
                        Result = false,
                        Message = "payLoad为空"
                    };
                }
                else
                {
                    if (!request.UserId.Equals(payLoad.UserId))
                    {
                        result = new ExampleResponse
                        {
                            Code = -1,
                            Result = false,
                            Message = "userid不匹配"
                        };
                    }
                    else
                    {
                        var userDetail = JsonConvert.DeserializeObject<UserDetails>(request.UserDetail.Fields.ToString());
                        result = new ExampleResponse
                        {
                            Code = 200,
                            Result = true,
                            Message = $"UnaryCall 单次响应: {request.UserId},{userDetail?.UserName}"
                        };
                    }
                }
            }
            else
            {
                // 正常响应客户端一次
                result = new ExampleResponse
                {
                    Code = -1,
                    Result = false,
                    Message = tokenRes.Item2
                };
            }
            return Task.FromResult(result);
        }

        public override async Task StreamingFromServer(ExampleRequest request, IServerStreamWriter<ExampleResponse> responseStream, ServerCallContext context)
        {
            // 无限响应客户端
            while (!context.CancellationToken.IsCancellationRequested)
            {
                await responseStream.WriteAsync(new ExampleResponse
                {
                    Code = 200,
                    Result = true,
                    Message = $"StreamingFromServer 无限响应: {Guid.NewGuid()}"
                });
                await Task.Delay(TimeSpan.FromSeconds(3), context.CancellationToken);
            }
        }

        public override async Task<ExampleResponse> StreamingFromClient(IAsyncStreamReader<ExampleRequest> requestStream, ServerCallContext context)
        {
            // 处理请求
            await foreach (var req in requestStream.ReadAllAsync())
            {
                Console.WriteLine(req.UserId);
            }

            // 响应客户端
            return new ExampleResponse
            {
                Code = 200,
                Result = true,
                Message = $"StreamingFromClient 单次响应: {Guid.NewGuid()}"
            };
        }

        public override async Task StreamingBothWays(IAsyncStreamReader<ExampleRequest> requestStream, IServerStreamWriter<ExampleResponse> responseStream, ServerCallContext context)
        {
            // 服务器响应客户端一次
            // 处理请求
            //await foreach (var req in requestStream.ReadAllAsync())
            //{
            //    Console.WriteLine(req.UserName);
            //}

            // 请求处理完成之后只响应一次
            //await responseStream.WriteAsync(new ExampleResponse
            //{
            //    Code = 200,
            //    Result = true,
            //    Message = $"StreamingBothWays 单次响应: {Guid.NewGuid()}"
            //});
            //await Task.Delay(TimeSpan.FromSeconds(3), context.CancellationToken);

            // 服务器响应客户端多次
            // 处理请求
            var readTask = Task.Run(async () =>
            {
                await foreach (var req in requestStream.ReadAllAsync())
                {
                    Console.WriteLine(req.UserId);
                }
            });

            // 请求未处理完之前一直响应
            while (!readTask.IsCompleted)
            {
                await responseStream.WriteAsync(new ExampleResponse
                {
                    Code = 200,
                    Result = true,
                    Message = $"StreamingBothWays 请求处理完之前的响应: {Guid.NewGuid()}"
                });
                await Task.Delay(TimeSpan.FromSeconds(3), context.CancellationToken);
            }

            // 也可以无限响应客户端
            //while (!context.CancellationToken.IsCancellationRequested)
            //{
            //    await responseStream.WriteAsync(new ExampleResponse
            //    {
            //        Code = 200,
            //        Result = true,
            //        Message = $"StreamingFromServer 无限响应: {Guid.NewGuid()}"
            //    });
            //    await Task.Delay(TimeSpan.FromSeconds(3), context.CancellationToken);
            //}
        }
    }
}

5.搭建grpc客户端

5.1 新建控制台程序

GrpcClient

5.2 拷贝文件夹

GrpcServer下的Protos拷贝一份到GrpcClient

5.3 安装依赖包

Google.Protobuf 3.21.12,Grpc.Net.Client 2.51.0,Grpc.Tools 2.51.0,Newtonsoft.Json 13.0.2

5.4 编译生成Stub

GrpcServer项目右键编辑项目文件添加内容,注意这里是Client

<ItemGroup>
	<Protobuf Include="Protos\example.proto" GrpcServices="Client" />
</ItemGroup>

5.5 新建测试类

ExampleTest.cs

using System.Security.Cryptography.X509Certificates;
using Grpc.Net.Client;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using GrpcCommon.Helpers;
using GrpcExample;

namespace GrpcClient.Test
{
    internal class ExampleTest
    {
        public static void Run()
        {
            // 常规请求响应
            UnaryCall();

            // 服务器流响应
            StreamingFromServer();

            // 客户端流响应
            StreamingFromClient();

            // 双向流响应
            StreamingBothWays();
        }

        /// <summary>
        /// 创建客户端链接
        /// </summary>
        /// <param name="enableSsl"></param>
        /// <returns></returns>
        private static ExampleServer.ExampleServerClient CreateClient(bool enableSsl = true)
        {
            GrpcChannel channel;
            if (enableSsl)
            {
                const string serverUrl = "https://localhost:7000";
                Console.WriteLine($"尝试链接服务器,{serverUrl}");

                var handler = new HttpClientHandler();
                // 添加证书
                handler.ClientCertificates.Add(new X509Certificate2("Certs\\cert.pfx", "1234.com"));

                // 忽略证书
                handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
                channel = GrpcChannel.ForAddress(serverUrl, new GrpcChannelOptions
                {
                    HttpClient = new HttpClient(handler)
                });
            }
            else
            {
                const string serverUrl = "http://localhost:5000";
                Console.WriteLine($"尝试链接服务器,{serverUrl}");
                channel = GrpcChannel.ForAddress(serverUrl);
            }

            Console.WriteLine("服务器链接成功");
            return new ExampleServer.ExampleServerClient(channel);
        }

        private static async void UnaryCall()
        {
            var client = CreateClient();
            const string securityKey = "Dennis!@#$%^123456.com";
            var userId = Guid.NewGuid().ToString();
            var token = JwtHelper.GenerateJwt(securityKey, userId);
            var result = await client.UnaryCallAsync(new ExampleRequest
            {
                SecurityKey = securityKey,
                UserId = "Dennis",
                UserDetail = new Struct
                {
                    Fields =
                    {
                        ["userName"] = Value.ForString("Dennis"),
                        ["age"] = Value.ForString("18"),
                        ["friends"] = Value.ForList(new Value
                        {
                            ListValue = new ListValue
                            {
                                Values =
                                {
                                    new List<Value>
                                    {
                                        Value.ForString("Roger"),
                                        Value.ForString("YueBe")
                                    }
                                }
                            }
                        })
                    }
                },
                Token = token
            });
            Console.WriteLine($"Code={result.Code},Result={result.Result},Message={result.Message}");
        }

        private static async void StreamingFromServer()
        {
            var client = CreateClient();
            var result = client.StreamingFromServer(new ExampleRequest
            {
                UserId = "Dennis"
            });

            await foreach (var resp in result.ResponseStream.ReadAllAsync())
            {
                Console.WriteLine($"Code={resp.Code},Result={resp.Result},Message={resp.Message}");
            }
        }

        private static async void StreamingFromClient()
        {
            var client = CreateClient();
            var result = client.StreamingFromClient();

            // 发送请求
            for (var i = 0; i < 5; i++)
            {
                await result.RequestStream.WriteAsync(new ExampleRequest
                {
                    UserId = $"StreamingFromClient 第{i}次请求"
                });
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

            // 等待请求发送完毕
            await result.RequestStream.CompleteAsync();

            var resp = result.ResponseAsync.Result;
            Console.WriteLine($"Code={resp.Code},Result={resp.Result},Message={resp.Message}");
        }

        private static async void StreamingBothWays()
        {
            var client = CreateClient();
            var result = client.StreamingBothWays();

            // 发送请求
            for (var i = 0; i < 5; i++)
            {
                await result.RequestStream.WriteAsync(new ExampleRequest
                {
                    UserId = $"StreamingBothWays 第{i}次请求"
                });
                await Task.Delay(TimeSpan.FromSeconds(1));
            }

            // 处理响应
            var respTask = Task.Run(async () =>
            {
                await foreach (var resp in result.ResponseStream.ReadAllAsync())
                {
                    Console.WriteLine($"Code={resp.Code},Result={resp.Result},Message={resp.Message}");
                }
            });

            // 等待请求发送完毕
            await result.RequestStream.CompleteAsync();

            // 等待响应处理
            await respTask;
        }
    }
}

5.6 修改程序入口

Program.cs

using GrpcClient.Test;
using Microsoft.Extensions.Hosting;

// Example测试
ExampleTest.Run();

Console.WriteLine("==================");
Console.WriteLine("按Ctrl+C停止程序");
Console.WriteLine("==================");

// 监听Ctrl+C
await new HostBuilder().RunConsoleAsync();

6.运行项目

6.1 拷贝证书

把整个Certs文件夹分别拷贝到GrpcServerGrpcClient下的\bin\Debug\Certs

6.2 启动程序

先运行GrpcServer在运行GrpcClient即可

6.3 调试

右键解决方案-->属性-->启动项目-->选择多个启动项目-->F5调试即可

7.源码地址

https://gitee.com/dennisdong/net-grpc

标签:Core,grpc,await,public,result,var,new,Net,Message
From: https://www.cnblogs.com/dennisdong/p/17120990.html

相关文章

  • .Net Framework创建grpc
    1.环境要求.NetFramework4.8.NetCore版本:https://www.cnblogs.com/dennisdong/p/17120990.html2.Stub和Proto2.1新建类库GrpcCommon2.2新建文件夹和配置文件......
  • AspNetCore基础课程
    AspNetCore基础课程我打算录制一个基础课程从原理、思维、实现等角度详细讲解。IOC、Options、Configuration、Logging、AOP、管道中间件、路由终结点,mvc的原理。掌握这些......
  • .NET6 API 部署标准流程
    一、安装dotnet环境#第一步:将Microsoft包签名密钥添加到受信任密钥列表,并添加Microsoft包存储库sudorpm-Uvhhttps://packages.microsoft.com/config/centos/7/packa......
  • Kubernetes控制平面组件:调度器和控制器
    一、调度器1、调度器简述kube-scheduler负责分配调度Pod到集群内的节点上,它监听kube-apiserver,查询还未分配Node的Pod,然后根据调度策略为这些Pod分......
  • Social Network 题解
    题意:题目翻译是有问题的,题目的真正意思其实是\(∀i∈[1,d]\),求在满足\([1,i]\)的规定的前提下恰好连\(i\)条边的无向图中度数最大联通块的大小减\(1\)。思路考虑......
  • win10安装sql server2008遇到 无法安装.net 3.5 错误
        如图所示,多次安装无效。用镜像安装Dism/online/enable-feature/featurename:NetFX3/All/Source:H:\sources\sxs/LimitAccess也无效,出现错误为错误:0......
  • .net core 使用 IOption 依赖注入获取配置值
    1appsettins配置{"Logging":{"LogLevel":{"Default":"Information","Microsoft.AspNetCore":"Warning"}},"AllowedHosts":"*",......
  • 【论文阅读】- 我对“AlexNet”的理解
    ......
  • Kubernetes:基于命令行终端UI的管理工具 K9s
    写在前面K9s是一个基于终端UI的K8S管理工具博文内容为k9s在windows、Linux以及docker安装Demo简单的热键使用。理解不足小伙伴帮忙指正我所渴求的,無非是......
  • .NET为什么推荐RabbitMQ消息队列作为首选开发工具
    支持.NetCore(2.0及以上)/.NetFramework(4.5及以上),可以部署在Docker,Windows,Linux,Mac。RabbitMQ作为一款主流的消息队列工具早已广受欢迎。相比于其它的MQ工具,Ra......