ASP.NET Core使用Nacos SDK訪問阿里雲ACM
背景
前段時間,cranelee 在Github上給老黃提了個issues, 問到了如何用Nacos的SDK訪問阿里雲ACM。
https://github.com/catcherwong/nacos-sdk-csharp/issues/13
剛看到這個issues的時候,老黃也是覺得一臉懵逼,好像這兩者沒有什麼必然聯繫,打開ACM的文檔一看,就知道爲什麼了。
原來Java和Go的已經是可以用nacos的SDK來訪問的了。那就說明兩者是兼容的。
這段時間抽空看了一下,把這個功能基本實現了。
下面就簡單介紹一下。
簡單看看ACM
開通ACM之後,可以看到類似這樣的界面。其實和Nacos控制檯的配置部分差不遠。
要使用這個的話,需要幾個東西,一個是ACM上面的命名空間,一個是AccessKey ID,一個是AccessKey Secret。
其中的AK/SK可以在命名空間詳情裏面獲取。
然後就是添加配置了。
三張圖,看個大概就好了,下面來具體看看在.NET Core中怎麼使用。
如何使用
安裝最新預覽版的SDK
<ItemGroup> <PackageReference Include="nacos-sdk-csharp-unofficial.Extensions.Configuration" Version="0.2.7-alpha7" /> </ItemGroup>
注:目前還沒有發佈正式版,不過不影響正常使用了。
修改Program
public class Program { public static void Main(string[] args) { // 處理編碼問題 System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((context, builder) => { // 這兩行代碼就是關鍵 var c = builder.Build(); builder.AddNacosConfiguration(c.GetSection("NacosConfig")); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }
調整appsettings.json
{ "NacosConfig": { "Optional": false, "DataId": "msconfigapp", "Group": "", "Tenant": "<換成您的命名空間>", "ServerAddresses": [], "AccessKey": "<換成您的AK>", "SecretKey": "<換成您的SK>", "EndPoint": "acm.aliyun.com" } }
注:由於老黃開通個人開通的,沒有內網服務器,所以用的是公網的EndPoint,這個需要根據情況自行調整。
實體映射(非必須)
public class AppSettings { public string Str { get; set; } public int Num { get; set; } public List<int> Arr { get; set; } public SubObj SubObj { get; set; } } public class SubObj { public string a { get; set; } }
爲了方便和配置一一對應,可以建立實體,做一個映射。
加了這個的,需要在Startup上面配置一下。
public void ConfigureServices(IServiceCollection services) { // others ... services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); }
讀取配置
這裏用控制器做爲示例
[ApiController] [Route("api/[controller]")] public class ConfigController : ControllerBase { private readonly ILogger<ConfigController> _logger; private readonly IConfiguration _configuration; private readonly AppSettings _settings; private readonly AppSettings _sSettings; private readonly AppSettings _mSettings; public ConfigController( ILogger<ConfigController> logger, IConfiguration configuration, IOptions<AppSettings> options, IOptionsSnapshot<AppSettings> sOptions, IOptionsMonitor<AppSettings> _mOptions ) { _logger = logger; _configuration = configuration; _settings = options.Value; _sSettings = sOptions.Value; _mSettings = _mOptions.CurrentValue; } [HttpGet] public string Get() { string id = Guid.NewGuid().ToString("N"); _logger.LogInformation($"============== begin {id} ====================="); var conn = _configuration.GetConnectionString("Default"); _logger.LogInformation($"{id} conn = {conn}"); var version = _configuration["version"]; _logger.LogInformation($"{id} version = {version}"); var str1 = Newtonsoft.Json.JsonConvert.SerializeObject(_settings); _logger.LogInformation($"{id} IOptions = {str1}"); var str2 = Newtonsoft.Json.JsonConvert.SerializeObject(_sSettings); _logger.LogInformation($"{id} IOptionsSnapshot = {str2}"); var str3 = Newtonsoft.Json.JsonConvert.SerializeObject(_mSettings); _logger.LogInformation($"{id} IOptionsMonitor = {str3}"); _logger.LogInformation($"==============================================="); _logger.LogInformation($"==============================================="); _logger.LogInformation($"==============================================="); return "ok"; } }
附上一張操作動圖
在ACM上修改之後,程序是可以馬上讀取到的。
下面是本文的示例代碼。
https://github.com/catcherwong-archive/2020/tree/master/06/NacosACMDemo
小結
Nacos和ACM的操作基本都是一致的,比較不一樣的地方是,從直連Nacos變成要先去地址服務拿到Nacos的地址後再操作。