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的地址后再操作。

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

網頁設計一頭霧水該從何著手呢? 台北網頁設計公司幫您輕鬆架站!

網頁設計公司推薦不同的風格,搶佔消費者視覺第一線

※Google地圖已可更新顯示潭子電動車充電站設置地點!!

※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

※別再煩惱如何寫文案,掌握八大原則!

網頁設計最專業,超強功能平台可客製化

※回頭車貨運收費標準