使用ASP.NET CORE注册consul服务注册发现中心失败
Use ASP.NET CORE to register consul service registration discovery center failed
我参考了一些文档,使用docker下载安装了consul,非常顺利,顺利打开了注册中心。但是我想结合asp.net核心使用,找到我当前的服务,但是失败了。我有点晕,请帮帮我!
这是我的一些配置
enter image description here
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHealthChecks("/Health", new HealthCheckOptions()
{
ResultStatusCodes =
{
[Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Healthy] = StatusCodes.Status200OK,
[Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Degraded] = StatusCodes.Status503ServiceUnavailable,
[Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable
}
});
我启动了项目,但仍然没有看到我的服务enter image description here
由于你的配置class信息不全,我这里做了一个简单的demo,你可以参考如下:
1.First 创建一个新的 RegisterConsul 扩展方法:
public static class AppBuilderExtensions
{
public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IHostApplicationLifetime lifetime, ServiceEntity serviceEntity)
{
var consulClient = new ConsulClient(x => x.Address = new Uri($"http://{serviceEntity.ConsulIP}:{serviceEntity.ConsulPort}"));//Consul address requesting registration
var httpCheck = new AgentServiceCheck()
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//How long does it take to register after the service starts
Interval = TimeSpan.FromSeconds(10),//Health check interval, or heartbeat interval
HTTP = $"http://{serviceEntity.IP}:{serviceEntity.Port}/WeatherForecast",//Health check address
Timeout = TimeSpan.FromSeconds(5)
};
// Register service with consul
var registration = new AgentServiceRegistration()
{
Checks = new[] { httpCheck },
ID = Guid.NewGuid().ToString(),
Name = serviceEntity.ServiceName,
Address = serviceEntity.IP,
Port = serviceEntity.Port,
Tags = new[] { $"urlprefix-/{serviceEntity.ServiceName}" }//Add a tag tag in the format of urlprefix-/servicename so that Fabio can recognize it
};
consulClient.Agent.ServiceRegister(registration).Wait();//Register when the service starts, the internal implementation is actually to register using the Consul API (initiated by HttpClient)
lifetime.ApplicationStopping.Register(() =>
{
consulClient.Agent.ServiceDeregister(registration.ID).Wait();//Unregister when the service stops
});
return app;
}
}
2.ServiceEntity 型号:
public string IP { get; set; }
public int Port { get; set; }
public string ServiceName { get; set; }
public string ConsulIP { get; set; }
public int ConsulPort { get; set; }
3.Add以下配置到配置文件中,这里添加到appSetting.json
"Consul": {
"IP": "localhost",
"Port": 44326,
"ServiceName": "hotel_api",
"ConsulIP": "Your consul address ",
"ConsulPort": Your consul port number
}
4.Then在Startup的Configure方法中添加class
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Microsoft.Extensions.Hosting.IHostApplicationLifetime lifetime)
{
ServiceEntity serviceEntity = new ServiceEntity
{
IP = "localhost",
Port = Convert.ToInt32(Configuration["Consul:Port"]),
ServiceName = Configuration["Consul:ServiceName"],
ConsulIP = Configuration["Consul:ConsulIP"],
ConsulPort = Convert.ToInt32(Configuration["Consul:ConsulPort"])
};
app.RegisterConsul(lifetime, serviceEntity);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
5.Result:
我参考了一些文档,使用docker下载安装了consul,非常顺利,顺利打开了注册中心。但是我想结合asp.net核心使用,找到我当前的服务,但是失败了。我有点晕,请帮帮我!
这是我的一些配置
enter image description here
public void ConfigureServices(IServiceCollection services)
{
services.AddHealthChecks();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHealthChecks("/Health", new HealthCheckOptions()
{
ResultStatusCodes =
{
[Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Healthy] = StatusCodes.Status200OK,
[Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Degraded] = StatusCodes.Status503ServiceUnavailable,
[Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable
}
});
我启动了项目,但仍然没有看到我的服务enter image description here
由于你的配置class信息不全,我这里做了一个简单的demo,你可以参考如下:
1.First 创建一个新的 RegisterConsul 扩展方法:
public static class AppBuilderExtensions
{
public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app, IHostApplicationLifetime lifetime, ServiceEntity serviceEntity)
{
var consulClient = new ConsulClient(x => x.Address = new Uri($"http://{serviceEntity.ConsulIP}:{serviceEntity.ConsulPort}"));//Consul address requesting registration
var httpCheck = new AgentServiceCheck()
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5),//How long does it take to register after the service starts
Interval = TimeSpan.FromSeconds(10),//Health check interval, or heartbeat interval
HTTP = $"http://{serviceEntity.IP}:{serviceEntity.Port}/WeatherForecast",//Health check address
Timeout = TimeSpan.FromSeconds(5)
};
// Register service with consul
var registration = new AgentServiceRegistration()
{
Checks = new[] { httpCheck },
ID = Guid.NewGuid().ToString(),
Name = serviceEntity.ServiceName,
Address = serviceEntity.IP,
Port = serviceEntity.Port,
Tags = new[] { $"urlprefix-/{serviceEntity.ServiceName}" }//Add a tag tag in the format of urlprefix-/servicename so that Fabio can recognize it
};
consulClient.Agent.ServiceRegister(registration).Wait();//Register when the service starts, the internal implementation is actually to register using the Consul API (initiated by HttpClient)
lifetime.ApplicationStopping.Register(() =>
{
consulClient.Agent.ServiceDeregister(registration.ID).Wait();//Unregister when the service stops
});
return app;
}
}
2.ServiceEntity 型号:
public string IP { get; set; }
public int Port { get; set; }
public string ServiceName { get; set; }
public string ConsulIP { get; set; }
public int ConsulPort { get; set; }
3.Add以下配置到配置文件中,这里添加到appSetting.json
"Consul": {
"IP": "localhost",
"Port": 44326,
"ServiceName": "hotel_api",
"ConsulIP": "Your consul address ",
"ConsulPort": Your consul port number
}
4.Then在Startup的Configure方法中添加class
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Microsoft.Extensions.Hosting.IHostApplicationLifetime lifetime)
{
ServiceEntity serviceEntity = new ServiceEntity
{
IP = "localhost",
Port = Convert.ToInt32(Configuration["Consul:Port"]),
ServiceName = Configuration["Consul:ServiceName"],
ConsulIP = Configuration["Consul:ConsulIP"],
ConsulPort = Convert.ToInt32(Configuration["Consul:ConsulPort"])
};
app.RegisterConsul(lifetime, serviceEntity);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
5.Result: