Exemplo n.º 1
0
        //the method check the service discovery parameter register ipaddress to generate service agent
        //those service agents will deregister when the app stop
        public static IApplicationBuilder UseConsulRegisterService(this IApplicationBuilder app, IConfiguration configuration)
        {
            ConsulServiceDiscoveryOption serviceDiscoveryOption = new ConsulServiceDiscoveryOption();

            configuration.GetSection("ServiceDiscovery").Bind(serviceDiscoveryOption);
            app.UseConsulRegisterService(serviceDiscoveryOption);
            return(app);
        }
Exemplo n.º 2
0
        //the method check the service discovery parameter register ipaddress to generate service agent
        //those service agents will deregister when the app stop
        public static IApplicationBuilder UseConsulRegisterService(this IApplicationBuilder app, ConsulServiceDiscoveryOption serviceDiscoveryOption)
        {
            var applicationLifetime = app.ApplicationServices.GetRequiredService <IApplicationLifetime>() ??
                                      throw new ArgumentException("Missing Dependency", nameof(IApplicationLifetime));

            if (serviceDiscoveryOption.Consul == null)
            {
                throw new ArgumentException("Missing Dependency", nameof(serviceDiscoveryOption.Consul));
            }

            if (string.IsNullOrEmpty(serviceDiscoveryOption.ServiceName))
            {
                throw new ArgumentException("service name must be configure", nameof(serviceDiscoveryOption.ServiceName));
            }

            IEnumerable <Uri> addresses = null;

            if (serviceDiscoveryOption.Endpoints != null && serviceDiscoveryOption.Endpoints.Length > 0)
            {
                addresses = serviceDiscoveryOption.Endpoints.Select(p => new Uri(p));
            }
            else
            {
                var features = app.Properties["server.Features"] as FeatureCollection;
                addresses = features.Get <IServerAddressesFeature>().Addresses.Select(p => new Uri(p)).ToArray();
            }

            var serviceChecks = new List <AgentServiceCheck>();

            foreach (var address in addresses)
            {
                var serviceID = GetServiceId(serviceDiscoveryOption.ServiceName, address);

                Uri healthCheck = null;
                if (!string.IsNullOrEmpty(serviceDiscoveryOption.HealthCheckTemplate))
                {
                    healthCheck = new Uri(address, serviceDiscoveryOption.HealthCheckTemplate);
                }
                var registryInformation = app.AddTenant(serviceDiscoveryOption.ServiceName, serviceDiscoveryOption.Version, address, healthCheckUri: healthCheck, tags: new[] { $"urlprefix-/{serviceDiscoveryOption.ServiceName}" });

                applicationLifetime.ApplicationStopping.Register(() =>
                {
                    app.RemoveTenant(registryInformation.Id);
                });
            }
            return(app);
        }
        public static IApplicationBuilder UseConsulRegisterService(this IApplicationBuilder app, ConsulServiceDiscoveryOption serviceDiscoveryOption)
        {
            var applicationLifetime = app.ApplicationServices.GetRequiredService <IApplicationLifetime>() ??
                                      throw new ArgumentException("Missing Dependency", nameof(IApplicationLifetime));

            if (serviceDiscoveryOption.Consul == null)
            {
                throw new ArgumentException("Missing Dependency", nameof(serviceDiscoveryOption.Consul));
            }

            if (string.IsNullOrEmpty(serviceDiscoveryOption.ServiceName))
            {
                throw new ArgumentException("service name must be configure", nameof(serviceDiscoveryOption.ServiceName));
            }

            Uri address = null;

            if (!string.IsNullOrWhiteSpace(serviceDiscoveryOption.Endpoint))
            {
                address = new Uri(serviceDiscoveryOption.Endpoint);
            }
            else
            {
                var features = app.Properties["server.Features"] as FeatureCollection;
                address = features.Get <IServerAddressesFeature>()?.Addresses?.Select(p => new Uri(p))?.FirstOrDefault();
            }

            if (address != null)
            {
                Uri healthCheck = null;
                if (!string.IsNullOrEmpty(serviceDiscoveryOption.HealthCheckTemplate))
                {
                    healthCheck = new Uri(serviceDiscoveryOption.HealthCheckTemplate);
                }

                var registryInformation = app.AddTenant(serviceDiscoveryOption.ServiceName,
                                                        serviceDiscoveryOption.Version,
                                                        address,
                                                        healthCheckUri: healthCheck,
                                                        tags: new[] { $"urlprefix-/{serviceDiscoveryOption.ServiceName}" });

                applicationLifetime.ApplicationStopping.Register(() =>
                {
                    app.RemoveTenant(registryInformation.Id);
                });
            }
            return(app);
        }