.NET微服务从0到1:服务注册与发现(Consul)

栏目: IT技术 · 发布时间: 4年前

内容简介:以下为单机环境构建脚本,用于本机测试,生产环境中应当进行集群搭建执行以下命令运行consul

Consul搭建

基于 Docker 搭建Consul

以下为单机环境构建脚本,用于本机测试,生产环境中应当进行集群搭建

version: '3'
services:
  consul:
    image: consul:1.7.1
    container_name: consul
    volumes:
      - /c/docker/consul/data:/consul/data
      - /c/docker/consul:/consul/config
    ports:
      - 8300:8300
      - 8301:8301
      - 8301:8301/udp
      - 8302:8302
      - 8302:8302/udp
      - 8400:8400
      - 8500:8500
      - 53:53/udp
    command: agent -server -bind=0.0.0.0 -client=0.0.0.0 -node=consul_Server1 -bootstrap-expect=1 -ui

成功搭建后,访问8500端口,你可以看到如下界面

.NET微服务从0到1:服务注册与发现(Consul)

基于Windows搭建Consul

点击 下载Consul

执行以下命令运行consul

consul agent -dev

ServiceA集成Consul做服务注册

配置前一篇文章中的ServiceA

Install-Package Consul -Version 0.7.2.6
  • Consul配置模型
public class ServiceDisvoveryOptions
{
    public string ServiceName { get; set; }

    public ConsulOptions Consul { get; set; }
}

public class ConsulOptions
{
    public string HttpEndpoint { get; set; }

    public DnsEndpoint DnsEndpoint { get; set; }
}

public class DnsEndpoint
{
    public string Address { get; set; }

    public int Port { get; set; }

    public IPEndPoint ToIPEndPoint()
    {
        return new IPEndPoint(IPAddress.Parse(Address), Port);
    }
}
  • 添加appsetting.json
"ServiceDiscovery": {
    "ServiceName": "ServiceA",
    "Consul": {
      "HttpEndpoint": "http://127.0.0.1:8500",
      "DnsEndpoint": {
        "Address": "127.0.0.1",
        "Port": 8600
      }
    }
  }
  • Consul服务注册实现
private void ConfigureConsul(IApplicationBuilder app, IOptions<ServiceDisvoveryOptions> serviceOptions, IConsulClient consul, IHostApplicationLifetime lifetime)
{
    var features = app.Properties["server.Features"] as FeatureCollection;
    var addresses = features.Get<IServerAddressesFeature>()
        .Addresses
        .Select(p => new Uri(p));

    foreach (var address in addresses)
    {
        var serviceId = $"{serviceOptions.Value.ServiceName}_{address.Host}:{address.Port}";

        var httpCheck = new AgentServiceCheck()
        {
            DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1),
            Interval = TimeSpan.FromSeconds(30),
            HTTP = new Uri(address, "health").OriginalString
        };

        var registration = new AgentServiceRegistration()
        {
            Checks = new[] { httpCheck },
            Address = address.Host,
            ID = serviceId,
            Name = serviceOptions.Value.ServiceName,
            Port = address.Port
        };

        consul.Agent.ServiceRegister(registration).GetAwaiter().GetResult();

        lifetime.ApplicationStopping.Register(() =>
        {
            consul.Agent.ServiceDeregister(serviceId).GetAwaiter().GetResult();
        });
    }
}
  • 配置服务到DI容器
public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions();
    services.Configure<ServiceDisvoveryOptions>(Configuration.GetSection("ServiceDiscovery"));

    services.AddSingleton<IConsulClient>(p => new ConsulClient(cfg =>
    {
        var serviceConfiguration = p.GetRequiredService<IOptions<ServiceDisvoveryOptions>>().Value;

        if (!string.IsNullOrEmpty(serviceConfiguration.Consul.HttpEndpoint))
        {
            // if not configured, the client will use the default value "127.0.0.1:8500"
            cfg.Address = new Uri(serviceConfiguration.Consul.HttpEndpoint);
        }
    }));

    services.AddHealthChecks();
    services.AddControllers();
}
  • 将Consul相关配置添加到管道
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<ServiceDisvoveryOptions> serviceDisvoveryOptions, IConsulClient consul, IHostApplicationLifetime lifetime)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

    app.UseAuthorization();

    ConfigureConsul(app, serviceDisvoveryOptions, consul, lifetime);

    app.UseHealthChecks("/health");

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}
  • 运行ServiceA

    .NET微服务从0到1:服务注册与发现(Consul)

    可以看到健康检查已通过,并且服务ServiceA已成功注册到Consul

Ocelot集成Consul做服务发现

安装nuget包依赖

Install-Package Ocelot.Provider.Consul -Version 14.0.11

在上一篇文章的基础上,我们先修改 ocelot.json
删除了ServiceA路由中的DownstreamHostAndPorts写死的地址和端口 ,改为使用 Consul 服务发现

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/service-a/{url}",
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "SampleKey",
        "AllowedScopes": [ "gateway_api" ]
      },
      "ServiceName": "ServiceA",
      "LoadBalancerOptions": {
        "Type": "LeastConnection"
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost",
    "ServiceDiscoveryProvider": {
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    }
  }
}

向容器中添加Consul服务

public void ConfigureServices(IServiceCollection services)
{
    services.AddOcelot()
        .AddConsul();
}

启动各个服务,查看结果

.NET微服务从0到1:服务注册与发现(Consul)

可以看到已成功执行

更多参考


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

视觉链

视觉链

吴佳敏 / 机械工业出版社 / 59.00

这是一部能帮助视觉设计师开悟的著作,由携程网UED视觉高级经理撰写,是她9年互联网视觉设计经验的总结和奉献。 全书从设计师的专业能力、设计方向、设计技巧、设计理念、设计规范5个维度展开,其中前4项可以构成一个完整的视觉设计工作链,在这个链条上每一环都是后面一环的支撑,缺一不可。但是在这个链条之上必须配以设计规范,才能让这个链条更加稳固。因此本章主要分为5章: 第1章:首先介绍了互联网产......一起来看看 《视觉链》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具