aspnetcore微服务之间grpc通信,无proto文件

这篇具有很好参考价值的文章主要介绍了aspnetcore微服务之间grpc通信,无proto文件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

aspnetcore微服务之间通信grpc,一般服务对外接口用restful架构,HTTP请求,服务之间的通信grpc多走内网。

以前写过一篇grpc和web前端之间的通讯,代码如下:

exercisebook/grpc/grpc-web at main · liuzhixin405/exercisebook (github.com)

 

本次是微服务之间的通信使用了开源软件MagicOnion,该软件定义接口约束免去proto复杂配置,类似orleans或者webservice,服务调用都通过约定接口规范做传输调用,使用起来非常简单和简洁。

下面通过服务之间调用的示例代码做演示:

aspnetcore微服务之间grpc通信,无proto文件

Server里面包含简单jwt的token的生成,client和002需要调用登录,通过外部接口调用传入用户和密码,内部再调用jwt服务。

aspnetcore微服务之间grpc通信,无proto文件

aspnetcore微服务之间grpc通信,无proto文件

aspnetcore微服务之间grpc通信,无proto文件

 

服务之间调用如果不用proto的话,那么接口必须是公共部分,值得注意的是接口的参数和返回值必须 包含[MessagePackObject(true)]的特性,硬性条件。返回值必须被UnaryResult包裹,接口继承MagicOnion的IService,有兴趣深入的自己研究源码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MagicOnion;
using MessagePack;

namespace MicroService.Shared
{
    public interface IAccountService:IService<IAccountService>
    {
        UnaryResult<SignInResponse> SignInAsync(string signInId, string password);
        UnaryResult<CurrentUserResponse> GetCurrentUserNameAsync();
        UnaryResult<string> DangerousOperationAsync();
    }

    [MessagePackObject(true)]
    public class SignInResponse
    {
        public long UserId { get; set; }
        public string Name { get; set; }
        public string Token { get; set; }
        public DateTimeOffset Expiration { get; set; }
        public bool Success { get; set; }

        public static SignInResponse Failed { get; } = new SignInResponse() { Success = false };

        public SignInResponse() { }

        public SignInResponse(long userId, string name, string token, DateTimeOffset expiration)
        {
            Success = true;
            UserId = userId;
            Name = name;
            Token = token;
            Expiration = expiration;
        }
    }

    [MessagePackObject(true)]
    public class CurrentUserResponse
    {
        public static CurrentUserResponse Anonymous { get; } = new CurrentUserResponse() { IsAuthenticated = false, Name = "Anonymous" };

        public bool IsAuthenticated { get; set; }
        public string Name { get; set; }
        public long UserId { get; set; }
    }
}

aspnetcore微服务之间grpc通信,无proto文件

上面GrpcClientPool和IGrpcClientFactory是我封装的客户端请求的一个链接池,跟MagicOnion没有任何关系。客户端如果使用原生的Grpc.Net.Client库作为客户端请求完全可以,通过 MagicOnionClient.Create<IAccountService>(channel)把grpcchannel塞入拿到接口服务即可。

服务端代码如下:

using JwtAuthApp.Server.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.IdentityModel.Tokens;

namespace JwtAuthApp.Server
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.WebHost.ConfigureKestrel(options =>
            {
                options.ConfigureEndpointDefaults(endpointOptions =>
                {
                    endpointOptions.Protocols = HttpProtocols.Http2;
                });
            });
            builder.Services.AddGrpc();
            builder.Services.AddMagicOnion();

            builder.Services.AddSingleton<JwtTokenService>();
            builder.Services.Configure<JwtTokenServiceOptions>(builder.Configuration.GetSection("JwtAuthApp.Server:JwtTokenService"));
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String(builder.Configuration.GetSection("JwtAuthApp.Server:JwtTokenService:Secret").Value!)),
                        RequireExpirationTime = true,
                        RequireSignedTokens = true,
                        ClockSkew = TimeSpan.FromSeconds(10),

                        ValidateIssuer = false,
                        ValidateAudience = false,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,
                    };
#if DEBUG
                    options.RequireHttpsMetadata = false;
#endif
                });
            builder.Services.AddAuthorization();

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            app.UseAuthentication();

            app.UseAuthorization();


            app.MapControllers();
            app.MapMagicOnionService();
            app.Run();
        }
    }
}
实际上跟组件有关的代码只有这么多了,剩下的就是jwt的。
 builder.WebHost.ConfigureKestrel(options =>
            {
                options.ConfigureEndpointDefaults(endpointOptions =>
                {
                    endpointOptions.Protocols = HttpProtocols.Http2;
                });
            });
            builder.Services.AddGrpc();
            builder.Services.AddMagicOnion();
            app.MapMagicOnionService();

当然作为服务的提供者实现IAccountService的接口是必须的。

using Grpc.Core;
using JwtAuthApp.Server.Authentication;
using System.Security.Claims;
using MagicOnion;
using MagicOnion.Server;
using MicroService.Shared;
using Microsoft.AspNetCore.Authorization;

namespace JwtAuthApp.Server.GrpcService
{
    [Authorize]
    public class AccountService : ServiceBase<IAccountService>, IAccountService
    {
        private static IDictionary<string, (string Password, long UserId, string DisplayName)> DummyUsers = new Dictionary<string, (string, long, string)>(StringComparer.OrdinalIgnoreCase)
        {
            {"signInId001", ("123456", 1001, "Jack")},
            {"signInId002", ("123456", 1002, "Rose")},
        };

        private readonly JwtTokenService _jwtTokenService;

        public AccountService(JwtTokenService jwtTokenService)
        {
            _jwtTokenService = jwtTokenService ?? throw new ArgumentNullException(nameof(jwtTokenService));
        }

        [AllowAnonymous]
        public async UnaryResult<SignInResponse> SignInAsync(string signInId, string password)
        {
            await Task.Delay(1); // some workloads...

            if (DummyUsers.TryGetValue(signInId, out var userInfo) && userInfo.Password == password)
            {
                var (token, expires) = _jwtTokenService.CreateToken(userInfo.UserId, userInfo.DisplayName);

                return new SignInResponse(
                    userInfo.UserId,
                    userInfo.DisplayName,
                    token,
                    expires
                );
            }

            return SignInResponse.Failed;
        }

        [AllowAnonymous]
        public async UnaryResult<CurrentUserResponse> GetCurrentUserNameAsync()
        {
            await Task.Delay(1); // some workloads...

            var userPrincipal = Context.CallContext.GetHttpContext().User;
            if (userPrincipal.Identity?.IsAuthenticated ?? false)
            {
                if (!int.TryParse(userPrincipal.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value, out var userId))
                {
                    return CurrentUserResponse.Anonymous;
                }

                var user = DummyUsers.SingleOrDefault(x => x.Value.UserId == userId).Value;
                return new CurrentUserResponse()
                {
                    IsAuthenticated = true,
                    UserId = user.UserId,
                    Name = user.DisplayName,
                };
            }

            return CurrentUserResponse.Anonymous;
        }

        [Authorize(Roles = "Administrators")]
        public async UnaryResult<string> DangerousOperationAsync()
        {
            await Task.Delay(1); // some workloads...

            return "rm -rf /";
        }
    }
}

当然jwt服务的代码也必不可少,还有密钥串json文件。

using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace JwtAuthApp.Server.Authentication
{
    public class JwtTokenService
    {
        private readonly SymmetricSecurityKey _securityKey;

        public JwtTokenService(IOptions<JwtTokenServiceOptions> jwtTokenServiceOptions)
        {
            _securityKey = new SymmetricSecurityKey(Convert.FromBase64String(jwtTokenServiceOptions.Value.Secret));
        }

        public (string Token, DateTime Expires) CreateToken(long userId, string displayName)
        {
            var jwtTokenHandler = new JwtSecurityTokenHandler();
            var expires = DateTime.UtcNow.AddSeconds(10);
            var token = jwtTokenHandler.CreateEncodedJwt(new SecurityTokenDescriptor()
            {
                SigningCredentials = new SigningCredentials(_securityKey, SecurityAlgorithms.HmacSha256),
                Subject = new ClaimsIdentity(new[]
                {
                    new Claim(ClaimTypes.Name, displayName),
                    new Claim(ClaimTypes.NameIdentifier, userId.ToString()),
                }),
                Expires = expires,
            });

            return (token, expires);
        }
    }

    public class JwtTokenServiceOptions
    {
        public string Secret { get; set; }
    }
}
{
    "JwtAuthApp.Server": {
        "JwtTokenService": {
            /* 64 bytes (512 bits) secret key */
            "Secret": "/Z8OkdguxFFbaxOIG1q+V9HeujzMKg1n9gcAYB+x4QvhF87XcD8sQA4VsdwqKVuCmVrXWxReh/6dmVXrjQoo9Q=="
        }
    },
    "Logging": {
        "LogLevel": {
            "Default": "Trace",
            "System": "Information",
            "Microsoft": "Information"
        }
    }
}

上面的代码完全可以运行一个jwt服务了。

下面就是客户端代码,因为两个客户端是一样的只是做测试,所以列出一个就够了。

using Login.Client.GrpcClient;
using MicroService.Shared.GrpcPool;
using MicroService.Shared;

namespace Login.Client
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            builder.Services.AddSwaggerGen();
            builder.Services.AddTransient<IGrpcClientFactory<IAccountService>, LoginClientFactory>();
            builder.Services.AddTransient(sp => new GrpcClientPool<IAccountService>(sp.GetService<IGrpcClientFactory<IAccountService>>(), builder.Configuration, builder.Configuration["Grpc:Service:JwtAuthApp.ServiceAddress"]));

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            app.UseAuthorization();


            app.MapControllers();

            app.Run();
        }
    }
}

客户端Program.cs只是注入了连接池,没有其他任何多余代码,配置文件当然必不可少。

  builder.Services.AddTransient<IGrpcClientFactory<IAccountService>, LoginClientFactory>();
  builder.Services.AddTransient(sp => new GrpcClientPool<IAccountService>(sp.GetService<IGrpcClientFactory<IAccountService>>(), builder.Configuration, builder.Configuration["Grpc:Service:JwtAuthApp.ServiceAddress"]));
{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "AllowedHosts": "*",
    "Grpc": {
        "Service": {
            "JwtAuthApp.ServiceAddress": "https://localhost:7021"
        }, 
        "maxConnections": 10,
        "handoverTimeout":10  // seconds
    }
}

登录的对外接口如下:

using System.ComponentModel.DataAnnotations;
using System.Threading.Channels;
using Grpc.Net.Client;
using Login.Client.GrpcClient;
using MagicOnion.Client;
using MicroService.Shared;
using MicroService.Shared.GrpcPool;
using Microsoft.AspNetCore.Mvc;

namespace Login.Client.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class LoginController : ControllerBase
    {


        private readonly ILogger<LoginController> _logger;
        private IConfiguration _configuration;
        private readonly IGrpcClientFactory<IAccountService> _grpcClientFactory;
        private readonly GrpcClientPool<IAccountService> _grpcClientPool;
        public LoginController(ILogger<LoginController> logger, IConfiguration configuration, IGrpcClientFactory<IAccountService> grpcClientFactory, GrpcClientPool<IAccountService> grpcClientPool)
        {

            _configuration = configuration;
            _logger = logger;
            _grpcClientFactory = grpcClientFactory;
            _grpcClientPool = grpcClientPool;
        }

        [HttpGet(Name = "Login")]
        public async Task<ActionResult<Tuple<bool,string?>>> Login([Required]string signInId, [Required]string pwd)
        {
            SignInResponse authResult;
            /*using (var channel = GrpcChannel.ForAddress(_configuration["JwtAuthApp.ServiceAddress"])) 
            {
                //var accountClient = MagicOnionClient.Create<IAccountService>(channel);

                 
            }*/

            var client = _grpcClientPool.GetClient();
            try
            {
                // 使用client进行gRPC调用
                authResult = await client.SignInAsync(signInId, pwd);
            }
            finally
            {
                _grpcClientPool.ReleaseClient(client);
            }
            return (authResult!=null && authResult.Success)?  Tuple.Create(true,authResult.Token): Tuple.Create(false,string.Empty);
        }
    }
}

客户端就剩下一个返回服务的接口工厂了

using Grpc.Net.Client;
using MagicOnion.Client;
using MicroService.Shared;
using MicroService.Shared.GrpcPool;

namespace Login.Client.GrpcClient
{
    public class LoginClientFactory : IGrpcClientFactory<IAccountService>
    {
        public IAccountService Create(GrpcChannel channel)
        {
            return MagicOnionClient.Create<IAccountService>(channel);
        }
    }
}

最后就是连接池的实现:

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Net.Client;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace MicroService.Shared.GrpcPool
{
    public class GrpcClientPool<TClient>
    {
        private readonly static ConcurrentBag<TClient> _clientPool = new ConcurrentBag<TClient>();
       
        private readonly IGrpcClientFactory<TClient> _clientFactory;
      
        private readonly int _maxConnections;
        private readonly TimeSpan _handoverTimeout;
        private readonly string _address;
        private readonly DateTime _now;
        public GrpcClientPool(IGrpcClientFactory<TClient> clientFactory,
            IConfiguration configuration,string address)
        {
            _now =  DateTime.Now;
            _clientFactory = clientFactory;
            _maxConnections = int.Parse(configuration["Grpc:maxConnections"]?? throw new ArgumentNullException("grpc maxconnections is null"));
            _handoverTimeout = TimeSpan.FromSeconds(double.Parse(configuration["Grpc:maxConnections"]??throw new ArgumentNullException("grpc timeout is null")));
            _address = address;
        }

        public TClient GetClient()
        {
            if (_clientPool.TryTake(out var client))
            {
                return client;
            }

            if (_clientPool.Count < _maxConnections)
            {
                var channel = GrpcChannel.ForAddress(_address);
                client = _clientFactory.Create(channel);
                _clientPool.Add(client);
                return client;
            }

            if (!_clientPool.TryTake(out client) && DateTime.Now.Subtract(_now) > _handoverTimeout)
            {
                throw new TimeoutException("Failed to acquire a connection from the pool within the specified timeout.");
            }
            return client;
        }

        public void ReleaseClient(TClient client)
        {
            if (client == null)
            {
                return;
            }
            _clientPool.Add(client);
        }
    }
}

上面已经演示过了接口调用的接口,这里不再展示,代码示例如下:

liuzhixin405/efcore-template (github.com)

 

不想做池化客户端注入的代码全部不需要了,只需要下面代码就可以了,代码会更少更精简。

 SignInResponse authResult;
            using (var channel = GrpcChannel.ForAddress(_configuration["JwtAuthApp.ServiceAddress"])) 
            {
                var accountClient = MagicOnionClient.Create<IAccountService>(channel);
                 authResult = await accountClient.SignInAsync(user, pwd);
            }

 文章来源地址https://www.toymoban.com/news/detail-711433.html

到了这里,关于aspnetcore微服务之间grpc通信,无proto文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • NestJS使用gRPC实现微服务通信

    代码仓库地址:https://github.com/zeng-jc/rpc-grpc-practice 1.1 基本概念 gRPC 基于 Protocol Buffers(protobuf)作为接口定义语言(IDL),意味着你可以使用 protobuf 来定义你的服务接口,gRPC生成的代码可以用于多种语言(C++, Java, Python, Go, C#, Ruby, Node.js),所以使用gRPC就能实现跨语言之间进

    2024年02月04日
    浏览(31)
  • SpringBoot + Kotlin 中使用 GRPC 进行服务通信

    示例项目见:kotlin-grpc 一、导入依赖: 二、设置Proto 将 proto 文件放在 src/mian/proto 目录下 执行 ./gradlew clean build build成功则会在 build/generated/source/proto/main 下生成对应的 grpc 、 grpckt 、 java 文件 在程序中可以直接导包引入 三、Server端 写一个 service 在 main 入口引入启动 四、Cl

    2024年02月16日
    浏览(30)
  • .NetCore gRpc 客户端与服务端的单工通信Demo

    方式一 使用vs 2022(也可以是其他版本)创建一个grpc的服务,如下这样 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uipEG9Xu-1687172462785)(C:UsersAdministratorAppDataRoamingTyporatypora-user-imagesimage-20230619183828284.png)] 简单方便,创建项目后的目录结构如下图

    2024年02月09日
    浏览(42)
  • 微服务之间的通信方式

    目录 微服务之间的通信方式 根据场景选择对应的通信方式 异步通信区别与选择 .Net示例 java示例 微服务之间的通信方式常见的有以下几种: 同步通信:微服务之间通过请求-响应的方式进行通信,例如RESTful API和RPC。通信过程中,请求方需要等待响应方的返回结果,因此可靠

    2024年02月05日
    浏览(22)
  • chrome扩展在popup、background、content之间通信解决传输文件问题

            示例扩展API版本MV2。         以弹出窗口(popup)和背景页面(background page)为例。         在浏览器中,弹出窗口(popup)和背景页面(background page)之间可以通过消息通道进行通信。但是,由于 安全限制 ,弹出窗口 不能直接访问背景页面的文件系统或

    2024年02月14日
    浏览(29)
  • python模块websockets,浏览器与服务器之间的双向通信

    一、简介 WebSocket是一种在Web浏览器和服务器之间进行实时双向通信的协议。它通过建立一条持久的连接,允许服务器主动向客户端推送数据,实现实时性和双向通信的能力。 与传统的HTTP请求-响应模式不同,WebSocket提供了一个长时间运行的连接,可以在客户端和服务器之间进

    2024年02月21日
    浏览(34)
  • 客户端请求+返回 服务端之间的请求和返回 实现rpc通信

    背景:         1.无论什么类型的游戏,我们都会有rpc通信的需求。         2.由于客户端直连的是游戏服,如果工会,匹配之类的服务是单独的服务的话,必然要进行游戏服到业务服之间的转发,我们是否需要再转发时单独定义Req和Res就是我们考虑到的需求。         3.在

    2024年01月25日
    浏览(29)
  • 请求的服务“Microsoft.AspNetCore.Mvc.ViewFeatures”。尚未注册ITempDataDictionaryFactory。(学习笔记)

    修改 Startup.cs 中的 ConfigureServices方法 修改 Startup.cs 中的 ConfigureServices方法 https://www.cnblogs.com/ansheng/p/14239237.html

    2024年02月12日
    浏览(20)
  • webots学习笔记——URDF和PROTO文件生成及导入综述参考

        在学习webots过程中,对于URDF文件生成、PROTO文件的生成,学习了CSDN上的多篇博客,终于实现了自己的文件导入及初步仿真动作的实现。现做一简单汇总,并简要介绍,希望能帮助到有需要的人。       一点体会,有时候很难照着一篇博客从头到尾跟下来就能复现,涉及

    2024年04月22日
    浏览(31)
  • Windows之间使用FTP服务传输文件

    服务器Win10,客户端Win11,两个电脑在同一局域网,我是使用一根网线连接 没有新建用户 没有考虑权限和安全等各种问题,只为了两个电脑传输文件快 在入站规则右击,新建规则 打开文件资源管理器,地址栏输入ftp://ip,输入服务器的用户和密码。 如果出现错误“打开ftp服

    2024年02月16日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包