Using Service Discovery with Hashicorp Consul server

This tutorital 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.
First, start a Hashicorp Consul server. Depending on your hosting platform this is done in several ways.
Next, create a .NET Core WebAPI that registers itself as a service.
  1. Create a new ASP.NET Core WebAPI app with the Steeltoe Initializr
    • SteeltoeVersion: 2.3 for the latest stable
    • Project Metadata:
      Name: Consul_Register_Example
      Target Framework: netcoreapp3.0 is the latest stable
    • Dependencies: Discovery
    • Click Generate Project to download a zip containing the new project
  2. Extract the zipped project and open in your IDE of choice (we use Visual Studio)
  3. Set the instance address in appsettings.json
  4. tip
    Looking for additional params to use when connecting? Have a look at the docs
  5. Validate the port number the app will be served on, in Properties\launchSettings.json
  6. Run the application and confirm it has registered with Eureka
Then, create another .NET Core WebAPI that will discover the registered service.
  1. Create a new ASP.NET Core WebAPI app with the Steeltoe Initializr
    • SteeltoeVersion: 2.3 for the latest stable
    • Project Metadata:
      Name: Consul_Discover_Example
      Target Framework: netcoreapp3.0 is the latest stable
    • Dependencies: Discovery
    • Click Generate Project to download a zip containing the new project
  2. Update the discovery values to not register in appsettings.json
  3. Validate the port number the app will be served on, in Properties\launchSettings.json
  4. Chnage the values controller to make a request to the discovery service and return the result in contollers\ValuesController.cs
    using System.Threading.Tasks;
    using Microsoft.Extensions.Logging;
    using Microsoft.AspNetCore.Mvc;
    using Steeltoe.Common.Discovery;
    using System.Net.Http;
    
    namespace Eureka_Discover_Example.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class ValuesController : ControllerBase
        {
            private readonly ILogger _logger;
            DiscoveryHttpClientHandler _handler;
            public ValuesController(ILogger<ValuesController> 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://Consul_Register_Example/api/values");
            }
        }
    }
    note
    Notice the use of Consul_Register_Example as the URI. Because Discovery has been enabled, the negotiation with the discovery Server happens automatically.
  5. Run the app to see discovery in action
  6. Once the discovery app loads in the browser you will see the 2 values that were retrieved from the registered app.
    ["value1","value2"]