微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

写在前面

在日常开发中,我这边比较熟悉的配置中心有,携程Apollo,阿里Nacos(配置中心,服务治理一体)

之前文章:

Asp.Net Core与携程阿波罗(Apollo)的第一次亲密接触

总体来说,Apollo和Nacos社区都比较活跃,可能是后发优势或者我们技术选型的原因,我们生产选用的是Nacos,我个人也是优先推荐Nacos(熟悉点,集成方便)。不管怎么说两者都是比较不错的配置中心;

今天我带来了另一个配置中心:微软Azure App Configuration,希望给大家带来一点技术选型上的帮助;

本文先讲讲微软Azure配置中心 App Configuration 在Asp.Net Core中的集成;

本文假设你有一点点微软云Azure的了解,能创建和使用基本的服务,能大概知道什么是云平台;

没有Azure的话找个双币信用卡去开一个免费的(免费12个月);

先在Azure云管理界面创建一个AppConfiguration

[Azure 管理后台]搜索找到App Configuration(我这里是世纪互联的)

https://portal.azure.cn/#home)

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

点这个create

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

添加资源分组服务名称

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

  1. 资源分组就是当前创建这个服务的所在的分组(没有点击Create new)
  2. 服务名称就是当前创建服务名称

创建后稍等一会就可以用了;

新增一些测试配置

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

OK,目前服务创建成功,且写入了一些测试配置,后面看看怎么在Asp.net Core里面用了;

在Asp.Net Core中集成

基本使用

1、先随便创建一个WebApi服务(我这里用net6)

勾选启用swagger

2、安装nuget

install-package Microsoft.Azure.AppConfiguration.AspNetCore 

3、appsetting.json加入连接字符串

  "ConnectionStrings": {     "AppConfig": "<your app connection string >"   }, 

4、修改Program.cs

var connectionString = builder.Configuration.GetConnectionString("AppConfig");  builder.Host.ConfigureAppConfiguration((hostingContext, config) => {     ////简单使用只配置connection string     config.AddAzureAppConfiguration(connectionString); }); 

5、使用

创建AzureConfigController

AzureConfigController 
 [Route("api/azure/config/[action]")]     public class AzureConfigController : PassportApiController     {         private readonly ILogger _logger;         private readonly IConfiguration _configuration;         private readonly Settings _settings;          public AzureConfigController(IConfiguration configuration, IAzureStorageApi azureStorageApi, IOptionsSnapshot<Settings> settings)         {             _configuration = configuration;             _settings = settings.Value;         }          /// <summary>         /// 读取配置string         /// </summary>         /// <param name="key">The key.</param>         /// <returns></returns>         [HttpGet]         public IActionResult Get(string key)         {             var result = _configuration[key];              return Success("get config success", result);         }          /// <summary>         /// 读取配置对象         /// </summary>         /// <param name="key">The key.</param>         /// <returns></returns>         [HttpGet]         public async Task<IActionResult> GetObject(string key)         {             var result = _configuration.GetSection(key).Get<AzureStorageConfig>();              return Success("get config success", result);         }     } 

读取string配置

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

读取配置的整个对象

对象key这样配置:

AzureStorageConfig1:ConnectionString

AzureStorageConfig1:ContainerName

...

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

ok,就这么简单~

根据环境变量读取配置

我们一般Asp.Net Core Web应用程序是用环境变量去加载不同配置的,一般我们的配置这样:

appsettings.json appsettings.<HostEnvironment>.json 

同一个key的配置在不同Development和Production环境是可以不一样的。

在Azure App Configuration ,引入一个Label的概念来实现;

修改集成代码:

builder.Host.ConfigureAppConfiguration((hostingContext, config) => {     ////简单使用只配置connection string     //config.AddAzureAppConfiguration(connectionString);      //配置不同功能     config.AddAzureAppConfiguration(options =>     {         //启用Label(多环境)支持         options.Connect(connectionString)             .Select(KeyFilter.Any, LabelFilter.Null)//配置过滤器,读取空Lable的配置             .Select(KeyFilter.Any, hostingContext.HostingEnvironment.EnvironmentName); //配置过滤器,只读取某个环境的配置     }); }); 

管理后台给一个Key设置环境变量:

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

这样TestKey1只有在对应环境变量才有值,TestKey2在所有环境变量都有值(空Label);

测试

这里简单测下Development环境的

微软Azure配置中心 App Configuration (一):轻松集成到Asp.Net Core

总结

目前只是一个非常简单的集成,可以看到集成是非常简单的。后面我们再讲下怎么主动更新配置,怎样启用功能开关等其他高级特性;

另外,我们这里测试都是手填配置到Azure管理后台,其实它也是支持配置的导入导出的,无需担心;

源码

https://github.com/gebiWangshushu/Hei.Azure.Test

[参考]

https://docs.microsoft.com/en-us/azure/azure-app-configuration/overview

发表评论

评论已关闭。

相关文章