private static ConsulClient CreateConsulClient(ServiceConfigModel serviceConfig)
 {
     return(new ConsulClient(config =>
     {
         config.Address = serviceConfig.ServiceDiscoveryAddress;
     }));
 }
        public static void RegisterConsulServices(this IServiceCollection services, ServiceConfigModel serviceConfigModel)
        {
            if (serviceConfigModel == null)
            {
                throw new ArgumentNullException(nameof(serviceConfigModel));
            }

            var consulClient = CreateConsulClient(serviceConfigModel);

            services.AddSingleton(serviceConfigModel);
            services.AddSingleton <IHostedService, ServiceDiscoveryHostedService>();
            services.AddSingleton <IConsulClient, ConsulClient>(p => consulClient);
        }
        public static ServiceConfigModel GetServiceConfig(this IConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var serviceConfig = new ServiceConfigModel
            {
                ServiceDiscoveryAddress = configuration.GetValue <Uri>("ServiceConfig:serviceDiscoveryAddress"),
                ServiceAddress          = configuration.GetValue <Uri>("ServiceConfig:serviceAddress"),
                ServiceName             = configuration.GetValue <string>("ServiceConfig:serviceName"),
                ServiceId = configuration.GetValue <string>("ServiceConfig:serviceId")
            };

            return(serviceConfig);
        }
Ejemplo n.º 4
0
 public ConsulHelper(ConsulConfigModel consulConfig, ServiceConfigModel serviceConfig)
 {
     _consulConfig  = consulConfig;
     _serviceConfig = serviceConfig;
 }
Ejemplo n.º 5
0
 public ServiceDiscoveryHostedService(IConsulClient client, ServiceConfigModel config)
 {
     _client = client;
     _config = config;
 }