Table of Contents
Note

This guide applies to Steeltoe v3. This component has been removed from v4.

Implementing Circuit Breakers

This tutorial takes you through setting up a .NET Core application that implements a circuit breaker pattern.

Note

For more detailed examples, please refer to the FortuneTeller (Circuit Breaker) project in the Steeltoe Samples Repository.

Start a instance of the Hystrix dashboard

(Depending on your hosting platform this is done in several ways.)

  1. There are a few images available on Docker Hub that provide basic Hystrix Dashboard functionality. The following image is provided by the Steeltoe team for testing and development:
docker run --rm -ti -p 7979:7979 --name steeltoe-hystrix steeltoeoss/hystrix-dashboard

Once this image is up and running, you should be able to browse to your local dashboard and provide the address of the Hystrix stream(s) you wish to monitor.

NOTE: This image may be running on a separate network than your application. Remember to provide a stream address that is accessible from within the Docker network as the application will be running on your host. This may require using the external IP address of your workstation or the name of the machine instead of 127.0.0.1 or localhost.

Alternatively, to run a Hystrix Dashboard with Java on your local workstation

  1. Install Java 8 JDK.
  2. Install Maven 3.x.
  3. Clone the Spring Cloud Samples Hystrix dashboard: cd https://github.com/spring-cloud-samples/hystrix-dashboard
  4. Change to the hystrix dashboard directory: cd hystix-dashboard
  5. Start the server mvn spring-boot:run
  6. Open a browser window and connect to the dashboard: http://localhost:7979/hystrix

Create a .NET Core WebAPI that implements circuit breaker pattern

  1. Create a new ASP.NET Core WebAPI app with the Steeltoe Initializr

  2. Name the project "CircuitBreakerExample"

  3. Add the "Netflix Hystrix Circuit Breaker" dependency

  4. Click Generate Project to download a zip containing the new project

  5. Extract the zipped project and open in your IDE of choice

  6. Add the following to appsettings.json

    {
      "Spring": {
        "Application": {
          "Name": "mycircuitbreaker"
        }
      },
      "Hystrix": {
        "command": {
          "MyCircuitBreaker": {
            "threadPoolKeyOverride": "MyCircuitBreakerTPool"
          }
        }
      }
    }
    
  7. Open up the .csproj file that was generated for you and replace the package reference:

    <PackageReference Include="Steeltoe.CircuitBreaker.Hystrix.MetricsStreamCore" Version="$(SteeltoeVersion)" />
    

    with

    <PackageReference Include="Steeltoe.CircuitBreaker.Hystrix.MetricsEventsCore" Version="$(SteeltoeVersion)" />
    
  8. Replace default "GET" controller method in WeatherForcastController.cs (Controllers folder) with the below:

    [HttpGet]
    public async Task<ActionResult<string>> GetAsync()
    {
        HelloHystrixCommand cb = new HelloHystrixCommand("ThisIsMyBreaker");
        cb.IsFallbackUserDefined = true;
        return await cb.ExecuteAsync();
    }
    

Run the application

dotnet run <PATH_TO>\CircuitBreakerExample.csproj

Navigate to the endpoint http://localhost:5000/WeatherForecast

  1. Navigate to the dashboard at http://localhost:7979/hystrix and enter the application stream url in the stream url text box (ex. http://localhost:5000/hystrix/hystrix.stream) Circuit Breaker Landing

NOTE: The stream url http://localhost:5000/hystrix/hystrix.stream will only work if the Hystrix dashboard is running on your local host. You will have to use a different URL, one that is accessible from Docker if you are running the dashboard using Docker.

  1. Refresh the application in your browser a few times and go back to the dashboard to see it logging live activity. Circuit Breaker Dashboard