如何在领事馆自行注册服务 [英] How to self register a service with Consul
问题描述
我正在尝试 self 将我的ASP.NET Core应用程序注册到Consul注册表启动时注销,关机时注销.
I'm trying to self register my ASP.NET Core application to Consul registry on startup and deregister it on shutdown.
从此处,我可以收集到调用http api的信息[put /v1/agent/service/register
]可能是可行的方式(或者可能不是!).
From here I can gather that calling the http api [put /v1/agent/service/register
] might be the way to go (or maybe not!).
从我的应用程序中,我认为我将定位Startup
类,从添加我的.json
文件开始
From my app, I thought I'll target the Startup
class, starting with adding the my .json
file
public Startup(IHostingEnvironment env)
{
var builder = new Configuration().AddJsonFile("consulconfig.json");
Configuration = builder.Build();
}
但是现在,我陷入困境,因为ConfigureServices
方法告诉我那是我向容器添加服务的位置,而Configure
方法是我配置Http请求管道的位置.
But now, I'm stuck as ConfigureServices
method tells me thats where I add services to the container, and Configure
method is where I configure the Http request pipeline.
任何人都可以向我指出正确的方向,在线阅读,示例等.
Anybody to point me in the right directions, online readings, examples, etc.
推荐答案
首先,我建议使用 Consul.NET 与领事互动.使用它,服务注册可能类似于:
First of all I recommend to use Consul.NET to interact with Consul. Using it, a service registration may look like:
var registration = new AgentServiceRegistration
{
Name = "foo",
Port = 4242,
Address = "http://bar"
};
using (var client = new ConsulClient())
{
await client.Agent.ServiceRegister(registration);
}
现在,让我们在DI和松耦合的帮助下将此代码集成到ASP.NET Core启动过程中.将您的json文件读入ConsulOptions
实例(没有任何逻辑的DTO):
Now let's integrate this code into ASP.NET Core startup process with help of DI and loose coupling. Read your json file into ConsulOptions
instance (DTO without any logic):
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<ConsulOptions>(Configuration);
}
将所有与领事有关的逻辑封装在类ConsulService
中,并接受ConsulOptions
作为依赖项:
Encapsulate all Consul-related logic in class ConsulService
accepting ConsulOptions
as a dependency:
public class ConsulService : IDisposable
{
public ConsulService(IOptions<ConsulOptions> optAccessor) { }
public void Register()
{
//possible implementation of synchronous API
client.Agent.ServiceRegister(registration).GetAwaiter().GetResult();
}
}
将类本身添加到DI容器中:
Add the class itself to the DI container:
services.AddTransient<ConsulService>();
然后创建IApplicationBuilder
的扩展方法,并将其命名为:
Then create an extention method of IApplicationBuilder
and call it:
public void Configure(IApplicationBuilder app)
{
app.ConsulRegister();
}
在ConsulRegister
实现中,我们在应用程序启动/停止处添加了钩子:
In ConsulRegister
implementation we add our hooks on application start/stop:
public static class ApplicationBuilderExtensions
{
public static ConsulService Service { get; set; }
public static IApplicationBuilder ConsulRegister(this IApplicationBuilder app)
{
//design ConsulService class as long-lived or store ApplicationServices instead
Service = app.ApplicationServices.GetService<ConsulService>();
var life = app.ApplicationServices.GetService<IApplicationLifetime>();
life.ApplicationStarted.Register(OnStarted);
life.ApplicationStopping.Register(OnStopping);
return app;
}
private static void OnStarted()
{
Service.Register(); //finally, register the API in Consul
}
}
锁定缺失和静态字段是可以的,因为Startup
类在应用程序启动时仅执行一次.不要忘记在OnStopping
方法中注销API!
Locking absence and static fields are OK because the Startup
class is executed exactly once on application start. Don't forget to de-register the API in OnStopping
method!
这篇关于如何在领事馆自行注册服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!