How to update HttpClient base address at runtime in ASP.net core

Viewed 729

I created several microservices using the ASP.net core API
One of these microservices returns the exact address of the other microservices
How to update the address of any of these microservices without restarting if the address is changed

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpClient("MainMicroservice", x =>
        {
            x.BaseAddress = new Uri("http://mainmicroservice.com");
        });
        services.AddHttpClient("Microservice1", x =>
        {
            x.BaseAddress = new Uri("http://microservice1.com");
        });
        services.AddHttpClient("Microservice2", x =>
        {
            x.BaseAddress = new Uri("http://microservice2.com");
        });
        services.AddHttpClient("Microservice3", x =>
        {
            x.BaseAddress = new Uri("http://microservice3.com");
        });
    }
}
public class Test
{
    private readonly IHttpClientFactory _client;

    public Test(IHttpClientFactory client)
    {
        _client = client;
    }

    public async Task<string> Test()
    {
        var repeat = false;

        do
        {
            try
            {
                return await _client
                    .CreateClient("Microservice1")
                    .GetStringAsync("Test")
                    .ConfigureAwait(false);
            }
            catch (HttpRequestException e) when (e.StatusCode == HttpStatusCode.NotFound)
            {
                var newAddress = await _client
                    .CreateClient("MainMicroservice")
                    .GetStringAsync("Microservice1")
                    .ConfigureAwait(false);

                //todo change address of microservice1

                repeat = true;
            }
        } while (repeat);
    }
}
1 Answers

If you are building a microservice-based solution sooner or later (rather sooner) you will encounter a situation when one service needs to talk to another. In order to do this caller must know the exact location of a target microservice in a network, they operate in.

You must somehow provide an IP address and port where the target microservice listens for requests. You can do it using configuration files or environment variables, but this approach has some drawbacks and limitations.

  1. First is that you have to maintain and properly deploy configuration files for all your environments: local development, test, pre-production, and production. Forgetting to update any of these configurations when adding a new service or moving an existing one to a different node will result in errors discovered at runtime.
  2. Second, the more important issue is that it works only in a static environment, meaning you cannot dynamically add/remove nodes, therefore you won’t be able to dynamically scale your system. The ability to scale and deploy given microservice autonomously is one of the key advantages of microservice-based architecture, and we do not want to lose this ability.

Therefore we need to introduce service discovery. Service discovery is a mechanism that allows services to find each other's network location. There are many possible implementations of this pattern.

We have two types of service discovery: client-side and server-side which you can find a good NuGet package to handle in ASP.NET projects.

Related