Painless microservice patterns for .NET

For all their benefits, microservice architectures expose many distributed computing problems that traditional monoliths avoided. Steeltoe brings the power of resilient, scalable microservices to .NET developers, without all the headache.

01

Service Discovery

How do you make the interactions between your microservices reliable and failure tolerant? For starters, you need a service registry—basically a phone book for your microservices—so service consumers know exactly where to find healthy service instances. Steeltoe includes a .NET client for Netflix Eureka so your microservices can register themselves and discover other registered services.


  public void ConfigureServices(IServiceCollection services) 
  { 
    ...
    services.AddDiscoveryClient(Configuration);
    ...
  }  
  public class FortuneService : IFortuneService
  {
      DiscoveryHttpClientHandler _handler;

      private const string RANDOM_FORTUNE_URL = "http://fortuneService/api/fortunes/random";

      public FortuneService(IDiscoveryClient client)
      {
          _handler = new DiscoveryHttpClientHandler(client);
      }

      public async Task RandomFortuneAsync()
      {
          var client = GetClient();
          return await client.GetStringAsync(RANDOM_FORTUNE_URL);
      }

      private HttpClient GetClient()
      {
          var client = new HttpClient(_handler, false);
          return client;
      }
  }

02

Config Server

“Strict separation of config from code” has become a cloud mandate, but that begs the question, where do you put it? And once you’ve externalized your config from your app, how do you track who changed what, when? Steeltoe leverages Spring Cloud Config Server so you can store your app’s config in a centralized, version-controlled git repo and then load it at runtime.

 var builder = new ConfigurationBuilder()
                  .SetBasePath(env.ContentRootPath)
                  .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                  .AddEnvironmentVariables()
                  .AddConfigServer(env);
              Configuration = builder.Build();

03

Cloud Connectors

Steeltoe automatically wires up common backing services like Redis, RabbitMQ, MySQL and Postgres because no microservice is an island. And because it was built by Pivotal, Steeltoe integrates elegantly with Cloud Foundry.

 
 public void ConfigureServices(IServiceCollection services) 
 { 
   ...
   services.AddMySqlConnection(Configuration);
   ...
 }
 ...
 public IActionResult MySqlData(
        [FromServices] MySqlConnection dbConnection)
    {
        dbConnection.Open();

        MySqlCommand cmd = new MySqlCommand("SELECT * FROM TestData;", dbConnection);
        MySqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            ViewData["Key" + rdr[0]] = rdr[1];
        }

        rdr.Close();
        dbConnection.Close();

        return View();
    }
}
                

What They're Saying