In the previous post Eureka Server (using Steeltoe) revisited I went through the process of running a Eureka instance and creating a default template based ASP.NET Web API which registers itself with the Eureka server as the application weatherapi.
Let’s now create a basic ASP.NET MVC project to interact with the Eureka server, get an instance of the API service and use it.
Note: This post is almost 100% based on the Channel 9 video Service Discovery with Steeltoe, so credit should go to Tim Hess for the sample code.
- Create an ASP.NET Core application and then select Web Application (Model View Controller)
- Add the following NuGet packages, Steeltoe.Discovery.ClientCore, Steeltoe.Discovery.Eureka and System.Net.Http.Json
- Update the appsettings.json with the following
"eureka": { "client": { "serviceUrl": "http://locahost:8761/eureka/", "shouldFetchRegistry": "true", "shouldRegisterWithEureka": false, "validateCertificates": false } }
Notice we set shouldFetchRegistry to true as we want to get the latest registry information, and we set shouldRegisterWidthEureka to false as, in this case, we don’t want to register this client. Ofcourse change this is your client also exposes services.
- Within Startup.cs, ConfigureServices add the following
services.AddDiscoveryClient(Configuration); services.AddHttpClient("weather", client => client.BaseAddress = new Uri("http://weatherapi/")) .AddServiceDiscovery();
The interesting thing here is that we associate the name “weather” with an address which looks like a URL but really is the name of the service within Eureka that we want to access. Then, by using the AddServiceDiscovery this will be converted to an instance URL representing the instance of the service associated with the app name.
Note: we can also use load balancing strategies, such as round robin or random.
- Finally within the Startup.cs, method Configure, add the following
app.UseDiscoveryClient();
-
We’re going to simply copy the WeatherForecast class from the service and add to the client, here it is
public class WeatherForecast { public DateTime Date { get; set; } public int TemperatureC { get; set; } public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); public string Summary { get; set; } }
- Within HomeController.cs we need to add a variable of type IHttpClientFactory which will be injected into the constructor and then uses to call defined HTTP client, this will then used service discovery to return the URL of an instance (as already discussed) and then we’re use that to call the weather API to get a list of values. Here’s the changes required to HomeController.cs
private readonly IHttpClientFactory _httpClientFactory; public HomeController(IHttpClientFactory httpClientFactory, ILogger<HomeController> logger) { _httpClientFactory = httpClientFactory; _logger = logger; } public async Task<IActionResult> Index() { var client = _httpClientFactory.CreateClient("weather"); var weather = await client.GetFromJsonAsync<IList<WeatherForecast>>("weatherforecast"); return View(weather); }
- Finally, let’s change the Index.cshtml to display the weather forecast data returned in the Index method. Firstly we declare the @model and then simply create a table to output the items from that model in columns and rows, so here’s the change to be added to the top of the file
@model IList<EurekaWebClient.Controllers.WeatherForecast>
and within the main div, just add the following
<table class="table"> <tr><th>Day of Week</th><th>Summary</th><th>Temp</th></tr> @foreach (var weather in Model) { <tr><td>@weather.Date.DayOfWeek</td><td>@weather.Summary</td><td>@weather.TemperatureF</td></tr> } </table>
That should be it. Ensure Eureka is running, your service is up and you should now see the weather service data in a nice little table.