Location>code7788 >text

Service discovery is performed and invoked using

Popularity:206 ℃/2024-09-09 01:06:46

synopsis

Service Discovery is a key feature in modern microservice architectures. It allows microservices to dynamically find each other without relying on hard-coded addresses. It used to be that if you searched for .NET Service Discovery, odds are you'd come up with a bunch of articles on Eureka, Consul, and so on. Now Microsoft brings us an official package: . This package, from the Aspire project, provides an easy way to implement service discovery in .

Installing the Nuget Package

First, you need to install the Service Discovery package provided by Microsoft. Use the following command to add the package to your project:

dotnet add package 

This step ensures that your project has the required dependencies to use Service Discovery.

Configuration and registration services

Next, you need to configure and register Service Discovery in your project. open the maybe file and add the following code:

();

(static http =>
{
    ();
});

This code registers Service Discovery with the Dependency Injection container and configures the default HTTP client to use Service Discovery.

Configuring Service Endpoints

In order for Service Discovery to know how to find other services, it needs to be configured in a configuration file (such as the) in which the service endpoint is defined. For example:

{
  "Services": {
    "weatherReport": {
      "http": [
        "localhost:5089",
        "127.0.0.1:5089"
      ],
      "https": []
    }
  }
}

In this configuration, we have defined a configuration namedweatherReport Service Discovery will use this information to find and access the service.

HTTP calls using the service name

Once the configuration is complete, you can pass theService Nameto make HTTP calls. The following code shows how to use theIHttpClientFactory Make a service call:

("/report", async (IHttpClientFactory factory) =>
{
    const string serviceName = "weatherReport";
    var client = ();
    var response = await ($"http://{serviceName}/weatherforecast");
    var content = await ();

    return content;
});

This code creates an HTTP client that passes the service nameweatherReport Initiates the request and returns the response content.

Start the service and try to make the call:

By looking at the logs you can see thathttp://weatherreport/weatherforecast convertiblehttp://127.0.0.1:5089 maybehttp://localhost:5089 http calls.

load balancing

If the service is configured with multiple endpoints. We often need to configure the load-balancing policy when making service calls as follows.

<CatalogServiceClient>(
    static client =>  = new("http://weatherReport"));
  .AddServiceDiscovery();
  • : Always call the first

  • :: Polling calls

  • : Random call

  • : The explanation is too long read the original English。Power-of-two-choices, which attempts to pick the least heavily loaded endpoint based on the Power of Two Choices algorithm for distributed load balancing, degrading to randomly selecting an endpoint when either of the provided endpoints do not have the IEndpointLoadFeature

summarize

Service Discovery is an important component for realizing microservice architecture. NET, through simple configuration and use, you can use service names instead of hardcode IPs and ports, which can greatly simplify the invocation between services. It also allows you to configure different invocation policies for load balancing.

Follow my public number to play with technology together