跳至主要內容

1.2 服务注册

linguicheng...小于 1 分钟

什么是服务注册?

服务注册是指服务启动后将该服务的IP、端口等信息注册到Consul

创建一个Web Api项目

安装依赖包

dotnet add package Wing.Consul

Program代码

using Wing;

var builder = WebApplication.CreateBuilder(args);

builder.Host.AddWing(builder => builder.AddConsul());

// Add services to the container.

builder.Services.AddControllers();

builder.Services.AddWing();

var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

添加配置

{
  // 是否启用配置中心,默认启用
  "ConfigCenterEnabled": false,
  "Consul": {
    "Url": "http://localhost:8500",
    "Service": {
      //Http  Grpc
      "Option": "Http",
      "HealthCheck": {
        "Url": "http://localhost:1210/health",
        //单位:秒
        "Timeout": 10,
        //单位:秒
        "Interval": 10
      },
      "Name": "Wing.Demo_1.2.1",
      "Host": "localhost",
      "Port": 1210,
      "Tag": "",
      "LoadBalancer": {
        //RoundRobin  WeightRoundRobin LeastConnection
        "Option": "WeightRoundRobin",
        //权重
        "Weight": 60
      },
      "Scheme": "http",
      "Developer": "linguicheng"
    },
    //定时同步数据时间间隔,单位:秒 小于等于0表示立即响应
    "Interval": 10,
    //数据中心
    "DataCenter": "dc1",
    //等待时间,单位:分钟
    "WaitTime": 3
  }
}

查看运行效果

  • 程序运行后,打开consul UI管理界面,可以看到注册服务Wing.Demo_1.2,如下图:
上次编辑于:
贡献者: linguicheng