Using Service Discovery with Eureka Server
This tutorial takes you through setting up two .NET Core applications using services discovery. The first will register it's endpoints for discovery, and the second will discover the first's services.
Note
For more detailed examples, please refer to the FortuneTeller (Discovery) solution in the Steeltoe Samples Repository.
First, start a Eureka Server using the Steeltoe dockerfile, start a local instance of Eureka.
docker run --publish 8761:8761 steeltoeoss/eureka-server
Next, create a .NET Core WebAPI that registers itself as a service.
Create a new ASP.NET Core WebAPI app with the Steeltoe Initializr
Name the project "EurekaRegisterExample"
Add the "Eureka Discovery Client" dependency
Click Generate to download a zip containing the new project
Extract the zipped project and open in your IDE of choice
Set the Eureka instance address in appsettings.json
{ "spring": { "application": { "name": "EurekaRegisterExample" } }, "eureka": { "client": { "serviceUrl": "http://localhost:8761/eureka/", "shouldFetchRegistry": "false", "shouldRegisterWithEureka": true, "validateCertificates": false }, "instance": { "port": "8080", "ipAddress": "localhost", "preferIpAddress": true } } }
Tip
Looking for additional params to use when connecting? Have a look at the docs.
Change "applicationUrl" to "http://localhost:8080", in launchSettings.json
"EurekaRegisterExample": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "http://localhost:8080", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }
Run the application and confirm it has registered with Eureka
dotnet run <PATH_TO>\EurekaRegisterExample.csproj
Navigate to the endpoint (you may need to change the port number) http://localhost:8080/api/WeatherForecast
- Navigate to the Eureka dashboard at http://localhost:8761/ to see the service listed.
- Leave the application running while you continue to the next steps, you'll be connecting to it.
Now, create another .NET Core WebAPI that will discover the registered service.
Create a new ASP.NET Core WebAPI app with the Steeltoe Initializr
Name the project "EurekaDiscoverExample"
Add the "Eureka Discovery Client" dependency
Click Generate to download a zip containing the new project
Extract the zipped project and open in your IDE of choice
Set the Eureka instance address in appsettings.json
{ "spring": { "application": { "name": "EurekaDiscoverExample" } }, "eureka": { "client": { "serviceUrl": "http://localhost:8761/eureka/", "shouldFetchRegistry": "true", "shouldRegisterWithEureka": false, "validateCertificates": false }, "instance": {} } }
Change "applicationUrl" to "http://localhost:8081" and "launchBrowser" to false, in launchSettings.json
"EurekaDiscoverExample": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "http://localhost:8081", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }
Change the WeatherForecast controller to make a request to the discovery service and return the result in contollers\WeatherForecastController.cs
using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Mvc; using Steeltoe.Common.Discovery; using System.Net.Http; [Route("api/[controller]")] [ApiController] public class WeatherForecastController : ControllerBase { private readonly ILogger _logger; DiscoveryHttpClientHandler _handler; public WeatherForecastController(ILogger<WeatherForecastController> logger, IDiscoveryClient client) { _logger = logger; _handler = new DiscoveryHttpClientHandler(client); } // GET api/values [HttpGet] public async Task<string> Get() { var client = new HttpClient(_handler, false); return await client.GetStringAsync("http://EurekaRegisterExample/api/WeatherForecast"); } }
Note
Notice the use of
EurekaRegisterExample
as the URI. Because Discovery has been enabled, the negotiation with the discovery Server happens automatically.
Run the app to see discovery in action
dotnet run <PATH_TO>\EurekaDiscoverExample.csproj
Navigate to the endpoint (you may need to change the port number) http://localhost:8081/api/WeatherForecast
Once the discovery app loads in the browser you will see these values that were retrieved from the registered app. "[ { "date": "2023-04-07T15:49:53.1328335+05:00", "temperatureC": 46, "temperatureF": 114, "summary": "Hot" }, { "date": "2023-04-08T15:49:53.1332687+05:00", "temperatureC": 11, "temperatureF": 51, "summary": "Hot" }, { "date": "2023-04-09T15:49:53.1332704+05:00", "temperatureC": 49, "temperatureF": 120, "summary": "Hot" }, { "date": "2023-04-10T15:49:53.1332705+05:00", "temperatureC": 41, "temperatureF": 105, "summary": "Balmy" }, { "date": "2023-04-11T15:49:53.1332706+05:00", "temperatureC": -4, "temperatureF": 25, "summary": "Freezing" } ]"