Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe

234 vues

Publié le

How do you avoid a messy jumble of configuration management? First, you can rule out holding settings internally to the app. Hard coding values in a compiled artifact limits almost everything about an application’s ability to scale. Attaching a .config file to each instance of the application is also not reasonable. (Yes, your externalized config values from the artifact.) But you’re no better off with managing and updating things.

This class will visit the Microsoft.Extensions.Configuration package and the many options it offers .NET Core developers. We’ll cover everything from using the default providers (and what’s going on under the covers) to custom providers implemented in Steeltoe. There are many stops in between where developers can achieve the best mix of business requirements and technical needs.

Attend this class to learn the following:

● How to use external configurations with Spring Config using Steeltoe
● Best practices for externalizing configuration
● How to get the most from Spring Config without adding complexity

David Dieruf, Principal Product Marketing Manager, Pivotal

Publié dans : Technologie
  • DOWNLOAD FULL BOOKS, INTO AVAILABLE FORMAT ......................................................................................................................... ......................................................................................................................... ,DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ,DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ,DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ,DOWNLOAD FULL. PDF EBOOK here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ,DOWNLOAD FULL. EPUB Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ,DOWNLOAD FULL. doc Ebook here { https://tinyurl.com/yyxo9sk7 } ......................................................................................................................... ......................................................................................................................... ......................................................................................................................... .............. Browse by Genre Available eBooks ......................................................................................................................... Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, Cookbooks, Crime, Ebooks, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult,
       Répondre 
    Voulez-vous vraiment ?  Oui  Non
    Votre message apparaîtra ici
  • Soyez le premier à aimer ceci

Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe

  1. 1. .NET Core: Configuration Fundamentals David Dieruf - Pivotal July, 2019
  2. 2. Let's Talk About... Cloud-native environment and goals Why we are here Designing config and environment variables Different patterns About .NET Core configuration Building config providers in WebHost and its hierarchy of values The options pattern Extending basic config with objects External configuration servers How they work and why you choose them Putting the pieces together Using Steeltoe and Spring Config to reach cloud-native goals
  3. 3. Environment: Local Network Services: DB, Cache, SMB Environment: Stage Environment: Prod Team Foundation Server (CI/CD): Git, Build, Release My-App Network Services: DB, Cache, SMB Network Services: DB, Cache, SMB My-App My-App - One build - Test once (for all environments) - Parity between environments - Automation My-App My-App public void ConfigureServices(IServiceCollection services) { String connString = Environment.GetEnvironmentVariable("conn_string"); services.AddDbContext<MyContext>(options => options.UseSqlServer(connString)); }
  4. 4. Environment variables in the cloud How is the value set? Do you have to manually update the value per instance, or script the action? How easy is promotion? Do you have to recompile to move between environments? Does your build pipeline manipulate settings? Do you have a history of changes? Do you know when a value was set and by who? Atomic value in the cloud Per app instance App can not run without a value Agnostic to its platform or IaaS (portable)
  5. 5. Appsettings.json ● [Good] External to the compiled application ● [Good] No recompilation needed ● [Challenge] Have to change at every instance Internal Class Command Line ● [Good] Secure information in compiled bits ● [Challenge] Hard coded values in app ● [Challenge] Can not change without recompiling ● [Good] External to the compiled application ● [Challenge] Have to change at every instance ● [Challenge] Lost values because no source check-in (Manual) Environment Variables Popular Configuration Design ● [Good] External to the compiled application ● [Good] Application compiled once and promoted through environment ● [Challenge] Have to change at every host class MyClass { string connString = "xxxx" } { "ConnectionStrings": { "a-database": "xxxxx" } } C: dotnet run my-app ` --conn "xxxx" ` --something "yyyyy" string connString = Environment.GetEnvironmentVari able("my-conn")
  6. 6. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration .NET Core Configuration
  7. 7. Using Default WebHost Loading configuration into your app public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); - use Kestrel as the web server and configure it using the application's configuration providers - set the ContentRootPath to the result of GetCurrentDirectory() - load IConfiguration from 'appsettings.json' and 'appsettings.[EnvironmentName].json' - load IConfiguration from User Secrets when EnvironmentName is 'Development' using the entry assembly - load IConfiguration from environment variables - load IConfiguration from supplied command line args - configure the ILoggerFactory to log to the console and debug output - enable IIS integration program.cs WebHost.CreateDefaultBuilder(args)
  8. 8. Build your own WebHost Loading configuration into your app public static IWebHost BuildWebHost(string[] args) { var builder = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); config.AddEnvironmentVariables(); config.AddCommandLine(args); }) .UseIISIntegration() .UseStartup<Startup>(); return builder.Build(); } program.cs - Options manipulate how configuration values are overwritten - Options to [not] include other configuration providers, like custom external config server provider var builder = new WebHostBuilder()
  9. 9. Config value hierarchy Overwrite values based on environment public static IWebHost BuildWebHost(string[] args) { var builder = new WebHostBuilder() ... .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json"); config.AddEnvironmentVariables(); config.AddCommandLine(args); }) ... 1. Load values from appsettings.json 2. (if provided) Load values from environment specific appsettings.{env}.json 3. (if present) Load values from environment variables 4. (if present) Load values provided when app was started config.AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json"); config.AddEnvironmentVariables(); config.AddCommandLine(args);
  10. 10. { "my_val": "something" } Example Value Overwriting 1. config.AddJsonFile("appsettings.json") { "my_val": "another" } 2. .AddJsonFile("appsettings.staging.json") PS c: $env:my_val = "yet another" 3. config.AddEnvironmentVariables() PS c: dotnet run my-app --my_val "again another" 4. config.AddCommandLine(args) my_val == “again another”
  11. 11. Configuration Providers Supported providers for configuration of ASP.NET core app Provider Provides configuration from… Azure Key Vault Configuration Provider (Security topics) Azure Key Vault Azure App Configuration Provider (Azure documentation) Azure App Configuration Command-line Configuration Provider Command-line parameters Custom configuration provider Custom source Environment Variables Configuration Provider Environment variables File Configuration Provider Files (INI, JSON, XML) Key-per-file Configuration Provider Directory files Memory Configuration Provider In-memory collections User secrets (Secret Manager) (Security topics) File in the user profile directory
  12. 12. Consuming Values Using Microsoft.Extensions.Configuration public class ValuesController : Controller{ private readonly IConfiguration _config; public ValuesController(IConfiguration config){ _config = config; } [HttpGet] public ActionResult<string> Get(){ string val = _config.GetValue<string>("my_val"); return val; } } - No consideration for handler type(s) - Simple key:value store - Can be complex type ValuesController.cs
  13. 13. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options Options Pattern
  14. 14. Options Pattern Using Microsoft.Extensions.Configuration.Options { "section0": { "key0": "value", "key1": "value" }, "section1": { "key0": "value", "key1": "value" } } public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.Configure<SectionZeroOptions>(Configuration.GetSection("section0")); ... } public class SectionZeroOptions { public string key0 {get;set;} public string key1 { get; set; } } public class MyService : IMyService { private IOptions<SectionZeroOptions> _SectionZeroOptions; public MyService(IOptions<SectionZeroOptions> sectionZeroOptions) { _SectionZeroOptions = sectionZeroOptions; } public string DoSomething(){ string key0 = _SectionZeroOptions.Value.key0; string key1 = _SectionZeroOptions.Value.key1; return key0; } } 1. appsettings.json values 2. Matching class to json 3. Enable the Options configuration 4. Consume values { "section0": { "key0": "value", "key1": "value" }, "section1": { "key0": "value", "key1": "value" } } public class SectionZeroOptions { public string key0 {get;set;} public string key1 { get; set; } } public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.Configure<SectionZeroOptions>(Configuration.GetSection("section0")); ... } public class MyService : IMyService { private IOptions<SectionZeroOptions> _SectionZeroOptions; public MyService(IOptions<SectionZeroOptions> sectionZeroOptions) { _SectionZeroOptions = sectionZeroOptions; } public string DoSomething(){ string key0 = _SectionZeroOptions.Value.key0; string key1 = _SectionZeroOptions.Value.key1; return key0; } }
  15. 15. Business Case using options pattern You have an api that is responsible for approving loans. There are business rules within the app that need to be applied, to decide approval. One of those rules compares the loan amount being asked for, to a max loan amount the business is willing to approve. The max amount changes throughout the year based on other events. Place the max approval amount (integer) in an external config and give yourself the flexibility to change values at any moment, with no downtime.
  16. 16. External Configuration Server Custom configuration provider
  17. 17. About Config Server At boot time… ➔ Platform puts values in environment variables ➔ Client looks for server ➔ Client gets config by NAME ➔ Config organised by PROFILE At runtime... ➔ Config can be refreshed live ➔ Call /actuator/refresh ➔ Values get reloaded ➔ No need for restart!
  18. 18. Environment: Stage Environment: ProdEnvironment: Local Network Services: DB, Cache, SMB Team Foundation Server (CI/CD): Git, Build, Release Network Services: DB, Cache, SMB Network Services: DB, Cache, SMB Config Server: development.yml, staging.yml, production.yml string connString = Environment.GetEnvironmentVariable("conn_string"); string configUri = Environment.GetEnvironmentVariable("config_uri");
  19. 19. Simplicity vs Flexibility External configuration can be challenging in itself It offers so much design options that things can get out of hand quickly. Use the domain around you to decide how far to go. Config First Approach Don’t hold anything in your appsettings.json, only a pointer to the config server. Everything is filled from there.
  20. 20. Steeltoe and Spring Config Putting the pieces together
  21. 21. 2. Set the application Name (in appsettings.json)... Setup Configuration Client: .NET / Steeltoe / Spring Config 4. Include the custom config provider... 1: Add the dependencies... (Steeltoe for Pivotal Spring Cloud Config Client) 3. Add your external configuration (in Git)... "spring": { "application": { "name": "loan-application-service" }, "cloud": { "config": { "uri": "http://10.0.7.254:8888", "validate_certificates": false, "fail_fast": true ... <PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServerCore" ... /> public void ConfigureServices(IServiceCollection services){ services.AddOptions(); services.Configure<Services.SomeValuesOptions> (Configuration.GetSection("SomeValues")); services.AddMvc(); ... } WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) =>{ ILoggerFactory fac = GetLoggerFactory(hostingContext.HostingEnvironment); var env = hostingContext.HostingEnvironment; config.AddConfigServer(env, fac); }) ... public class SomeValuesOptions { public string scheme { get; set; } public string address { get; set; } } program.cs startup.cs config options pattern Logging: IncludeScopes: false Debug: LogLevel: Default: Information SomeValues: scheme: https address: www.pivotal.io 5. Retrieve values and map to class... <PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServerCore" ... /> "spring": { "application": { "name": "loan-application-service" }, "cloud": { "config": { "uri": "http://127.0.0.1:8888", "validate_certificates": false, "fail_fast": true ... Logging: IncludeScopes: false Debug: LogLevel: Default: Information SomeValues: scheme: https address: www.pivotal.io WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) =>{ ILoggerFactory fac = GetLoggerFactory(hostingContext.HostingEnvironment); var env = hostingContext.HostingEnvironment; config.AddConfigServer(env, fac); }) ... public void ConfigureServices(IServiceCollection services){ services.AddOptions(); services.Configure<Services.SomeValuesOptions> (Configuration.GetSection("SomeValues")); services.AddMvc(); ... } public class SomeValuesOptions { public string scheme { get; set; } public string address { get; set; } } "name": "loan-application-service" config.AddConfigServer(env, fac); "uri": "http://127.0.0.1:8888"
  22. 22. © Copyright 2019 Pivotal Software, Inc. All rights Reserved. Learn More. Become a cloud-native .NET rock star https://springoneplatform.io/2019/training Pivotal Platform Acceleration Lab for Developers (.NET) Experience best practices building cloud-native applications in a hands-on setting with Pivotal educators and consultants. The program is focused on enabling customers and partners through “doing it” rather than “talking about it”. https://pivotal.io/platform-acceleration-lab
  23. 23. © Copyright 2019 Pivotal Software, Inc. All rights Reserved. Questions? Thank you
  24. 24. Refreshing Values - Optionally provide an environment specific json config Startup.cs public static IWebHost BuildWebHost(string[] args) { var builder = new WebHostBuilder() ... .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json", reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddEnvironmentVariables(); config.AddCommandLine(args); }) ...
  25. 25. Optional providers - Optionally provide an environment specific json config Startup.cs public static IWebHost BuildWebHost(string[] args) { var builder = new WebHostBuilder() ... .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); config.AddEnvironmentVariables(); config.AddCommandLine(args); }) ...

×