Beispiel #1
0
        public static void RegisterWithConsul(this IHostApplicationLifetime lifetime,
                                              IConsulConfiguration consulConfiguration, ILoggerFactory loggerFactory)
        {
            if (!consulConfiguration.Enabled)
            {
                return;
            }

            var consulClient = new ConsulClient(config => { config.Address = consulConfiguration.ConsulAddress; });

            var logger = loggerFactory.CreateLogger <IHostApplicationLifetime>();

            var address = consulConfiguration.ServiceUri;

            var registration = new AgentServiceRegistration
            {
                ID      = $"{consulConfiguration.ServiceId}-{address.Port}",
                Name    = consulConfiguration.ServiceName,
                Address = $"{address.Scheme}://{address.Host}",
                Port    = address.Port,
                Tags    = consulConfiguration.Tags
            };

            logger.LogInformation("Registering with Consul");
            consulClient.Agent.ServiceDeregister(registration.ID).Wait();
            consulClient.Agent.ServiceRegister(registration).Wait();

            lifetime.ApplicationStopping.Register(() =>
            {
                logger.LogInformation("Unregistering from Consul");
                consulClient.Agent.ServiceDeregister(registration.ID).Wait();
            });
        }
Beispiel #2
0
 public ConsulExtension(
     IHostApplicationLifetime applicationLifetime,
     IConsulConfiguration consulConfiguration,
     ILoggerFactory loggerFactory)
 {
     _consulConfiguration = consulConfiguration;
     _loggerFactory       = loggerFactory;
     _applicationLifetime = applicationLifetime;
 }