public AdvertApiClient(IConfiguration configuration, HttpClient client, IMapper mapper) { this._client = client; this._mapper = mapper; var discoveryClient = new AmazonServiceDiscoveryClient(); var discoveryTask = discoveryClient.DiscoverInstancesAsync(new DiscoverInstancesRequest { ServiceName = "advertapi", NamespaceName = "WebAdvertisement" }); discoveryTask.Wait(); // This is just an example and it shouldnot be run from Constructor. best to have its own class /* * Service Discovery does not provide LB, so one thing you can do is to randomize * this list, you can try to linq expiration to randomize and randomly pick up * any instance just to scatter the load across all the instances */ var instances = discoveryTask.Result.Instances; // how to get Ip var ipv4 = instances[0].Attributes["AWS_INSTANCE_IPV4"]; var port = instances[0].Attributes["AWS_INSTANCE_PORT"]; // then use ipv4 and port to create base url _baseAddress = configuration.GetSection("AdvertApi").GetValue <string>("BaseUrl"); _client.BaseAddress = new Uri(_baseAddress); }
public BaseAmazonDiscoveryHttpClient(string serviceNamespace, string serviceName, AmazonConfiguration amazonConfiguration, HttpClient client) : base(client) { var discoverClient = new AmazonServiceDiscoveryClient(amazonConfiguration.Credentials, amazonConfiguration.RegionEndpoint); var discoverInstanceRequest = new DiscoverInstancesRequest { ServiceName = serviceName, NamespaceName = serviceNamespace, HealthStatus = HealthStatus.HEALTHY.Value }; var discoveryTask = discoverClient.DiscoverInstancesAsync(discoverInstanceRequest); discoveryTask.Wait(); var instances = discoveryTask.Result.Instances; var instance = instances.FirstOrDefault(); var ipv4 = instance.Attributes["AWS_INSTANCE_IPV4"]; var port = instance.Attributes["AWS_INSTANCE_PORT"]; _baseUrl = $"http://{ipv4}:{port}"; }
private void DiscoverServiceUrl() { if (environment.IsDevelopment()) { SetDefaultUrl(); } else { var serviceDiscoveryClient = new AmazonServiceDiscoveryClient(Amazon.RegionEndpoint.EUWest2); var task = serviceDiscoveryClient.DiscoverInstancesAsync(new DiscoverInstancesRequest { ServiceName = "advertapi", NamespaceName = "WebAdvertisement", }); task.Wait(); var instances = task.Result.Instances; if (instances.Any()) { var ipv4 = instances[0].Attributes["AWS_INSTANCE_IPV4"]; var port = instances[0].Attributes["AWS_INSTANCE_PORT"]; baseAddress = $"http://{ipv4}:{port}"; } else { SetDefaultUrl(); } } }
public AdvertApiClient(IConfiguration configuration, HttpClient client, IMapper mapper) { _client = client; _mapper = mapper; var discoveryClient = new AmazonServiceDiscoveryClient(); var discoveryTask = discoveryClient.DiscoverInstancesAsync(new DiscoverInstancesRequest() { ServiceName = "AdvertAPI", NamespaceName = "WebAdvertisement", }); discoveryTask.GetAwaiter(); var instances = discoveryTask.Result.Instances; var ipv4 = instances[0].Attributes["AWS_INSTANCE_IPV4"]; var port = instances[0].Attributes["AWS_INSTANCE_PORT"]; _baseAddress = configuration.GetSection("AdvertApi").GetValue <string>("BaseUrl"); }