php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 578|回复: 0

.NET Core(.NET6)中gRPC注册到Consul 目录

[复制链接]

2871

主题

2881

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
7285
贡献
0
注册时间
2021-4-14
最后登录
2024-9-20
在线时间
716 小时
QQ
发表于 2022-4-18 10:08:02 | 显示全部楼层 |阅读模式
一、简介
上一篇文章介绍了.NET Core 中使用gRPC,在微服务中,我们通常要把服务做成服务注册,服务发现的方式,那么这里来说一下gRPC是如何注册到Consul中的。

Consul的安装这里就不介绍了,在之前的篇文章中已经写过:Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服务注册,服务发现

这里Consul已经安装好。
image.png
二、gRPC注册到Consul
1.扩展gRPC注册到Consul封装类
这里沿用上一篇的gRPC的代码,如果服务带api和gRPC的话用http的方式或gRPC的方式注册到可以,http的方式上面文章中的Consul注册和发现中已经有,这里介绍单gRPC的服务的注册。

先在appsettings.json中加入Consul信息代码
[mw_shl_code=applescript,true]{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Kestrel": {
    "EndpointDefaults": {
      "Protocols": "Http2"
    }
  },
  "Consul": {
    "consulAddress": "http://127.0.0.1:8500",
    "serviceName": "api_gRPC",
    "currentIp": "127.0.0.1",
    "currentPort": "5246"
  }
}[/mw_shl_code]
然后新建ConsulRegister.cs封装注册到Consul的类
[mw_shl_code=applescript,true]/// <summary>
    /// Consul注册
    /// </summary>
    public static class ConsulRegister
    {
        //服务注册
        public static IApplicationBuilder UseConsul(this IApplicationBuilder app, IConfiguration configuration)
        {
            // 获取主机生命周期管理接口
            var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();

            ConsulClient client = new ConsulClient(c =>
            {
                c.Address = new Uri(configuration["Consul:consulAddress"]);
                c.Datacenter = "dc1";
            });
            string ip = configuration["ip"]; //优先接收变量的值
            string port = configuration["port"]; //优先接收变量的值
            string currentIp = configuration["Consul:currentIP"];
            string currentPort = configuration["Consul:currentPort"];

            ip = string.IsNullOrEmpty(ip) ? currentIp : ip; //当前程序的IP
            port = string.IsNullOrEmpty(port) ? currentPort : port; //当前程序的端口
            string serviceId = $"service:{ip}:{port}";//服务ID,一个服务是唯一的
            //服务注册
            client.Agent.ServiceRegister(new AgentServiceRegistration()
            {
                ID = serviceId, //唯一的
                Name = configuration["Consul:serviceName"], //组名称-Group
                Address = ip, //ip地址
                Port = int.Parse(port), //端口
                Tags = new string[] { "api站点" },
                Check = new AgentServiceCheck()
                {
                    Interval = TimeSpan.FromSeconds(10),//多久检查一次心跳
                    GRPC = $"{ip}:{port}", //gRPC注册特有
                    GRPCUseTLS=false,//支持http
                    Timeout = TimeSpan.FromSeconds(5),//超时时间
                    DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5) //服务停止多久后注销服务
                }

            }).Wait();
            //应用程序终止时,注销服务
            lifetime.ApplicationStopping.Register(() =>
            {
                client.Agent.ServiceDeregister(serviceId).Wait();
            });
            return app;
        }
    }[/mw_shl_code]
Program.cs增加使用这个扩展类
[mw_shl_code=applescript,true]using GrpcDemo.Service.Services;
using GrpcDemo.Service.Utils;

var builder = WebApplication.CreateBuilder(args);

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682

// Add services to the container.
builder.Services.AddGrpc();

var app = builder.Build();
IConfiguration _configuration = builder.Configuration;

// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>();
app.MapGrpcService<OrderService>();
app.MapGrpcService<HealthCheckService>();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");

app.UseConsul(_configuration);
app.Run();[/mw_shl_code]
2.创建健康检查gRPC服务
1.新建健康检查proto文件HealthCheck.proto
[mw_shl_code=applescript,true]syntax = "proto3";

package grpc.health.v1;

message HealthCheckRequest {
    string service = 1;
}

message HealthCheckResponse {
    enum ServingStatus {
        UNKNOWN = 0;
        SERVING = 1;
        NOT_SERVING = 2;
    }
    ServingStatus status = 1;
}

service Health {
    rpc Check(HealthCheckRequest) returns (HealthCheckResponse);

    rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);
}[/mw_shl_code]
2.新建健康检查服务实现上面proto协议HealthCheckService.cs
[mw_shl_code=applescript,true]public class HealthCheckService : Health.HealthBase
    {
        public override Task<HealthCheckResponse> Check(HealthCheckRequest request, ServerCallContext context)
        {
            Console.WriteLine($"This is {nameof(HealthCheckService)} Check ");
            //TODO:检查逻辑
            return Task.FromResult(new HealthCheckResponse() { Status = HealthCheckResponse.Types.ServingStatus.Serving });
        }

        public override async Task Watch(HealthCheckRequest request, IServerStreamWriter<HealthCheckResponse> responseStream, ServerCallContext context)
        {
            //TODO:检查逻辑
            await responseStream.WriteAsync(new HealthCheckResponse()
            { Status = HealthCheckResponse.Types.ServingStatus.Serving });
        }
    }[/mw_shl_code]
3.在Program.cs中把服务注册到gRPC管道
[mw_shl_code=applescript,true]using GrpcDemo.Service.Services;
using GrpcDemo.Service.Utils;

var builder = WebApplication.CreateBuilder(args);

// Additional configuration is required to successfully run gRPC on macOS.
// For instructions on how to configure Kestrel and gRPC clients on macOS, visit https://go.microsoft.com/fwlink/?linkid=2099682

// Add services to the container.
builder.Services.AddGrpc();

//配置获取


var app = builder.Build();
IConfiguration _configuration = builder.Configuration;

// Configure the HTTP request pipeline.
app.MapGrpcService<GreeterService>();
app.MapGrpcService<OrderService>();
app.MapGrpcService<HealthCheckService>();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");

app.UseConsul(_configuration);
app.Run();[/mw_shl_code]
到这里服务注册就完成了,服务发现和上面简介的链接文章中一模一样,启动项目查看效果。
image.png
image.png

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )

GMT+8, 2024-9-20 08:11 , Processed in 0.188192 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表