MyGit

catcherwong/nacos-sdk-csharp

Fork: 27 Star: 166 (更新于 2024-11-01 19:51:58)

license: MIT

Language: C# .

🌹 nacos csharp sdk

最后发布版本: v0.2.7 ( 2020-07-08 23:05:08)

官方网址 GitHub网址

nacos-sdk-csharp             中文

Unofficial csharp(dotnet core) implementation of nacos OpenAPI.

This project was moved to https://github.com/nacos-group/nacos-sdk-csharp

CI Build Status

Platform Build Server Master Status
Github Action Linux/Windows nacos-sdk-csharp CI

Installation

dotnet add package nacos-sdk-csharp-unofficial

Features

  • Basic OpenApi Usages
  • Integrate ASP.NET Core Configuration System
  • Service Registration and Discovery With ASP.NET Core
  • Integrate With Aliyun ACM
  • ...

Basic Usage

Simple Configuration Usage

  1. Configure in Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((context, builder) =>
        {
            var c = builder.Build();

            // read configuration from config files
            builder.AddNacosConfiguration(c.GetSection("NacosConfig"));
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
  1. Modify appsettings.json
{
  "NacosConfig": {
    "Optional": false,
    "DataId": "msconfigapp",
    "Group": "",
    "Tenant": "f47e0ae1-982a-4a64-aea3-52506492a3d4",
    "ServerAddresses": [ "http://localhost:8848/" ],
    "UserName": "test2",
    "Password": "123456",
    "AccessKey": "",
    "SecretKey": "",
    "EndPoint": "acm.aliyun.com"
  }
}
  1. Use via .NET Core's Way
[ApiController]
[Route("api/[controller]")]
public class ConfigController : ControllerBase
{
    private readonly IConfiguration _configuration;
    private readonly AppSettings _settings;
    private readonly AppSettings _sSettings;
    private readonly AppSettings _mSettings;
    
    public ConfigController(
        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()
    {
        // ....
       
        return "ok";
    }

}

Service Registration and Discovery

  1. Service Registration

Configure in Program.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // ...

        services.AddNacosAspNetCore(Configuration);
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // ...
    }
}

Modify appsettings.json

"nacos": {
    "ServerAddresses": [ "http://localhost:8848" ],
    "DefaultTimeOut": 15000,
    "Namespace": "",
    "ListenInterval": 1000,
    "ServiceName": "App1"
  }
  1. Service Discovery
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private readonly INacosServerManager _serverManager;

    public ValuesController(INacosServerManager serverManager)
    {
        _serverManager = serverManager;
    }

    [HttpGet("test")]
    public async Task<IActionResult> Test()
    {        
        // need to know the service name.
        // at this time only support random way.
        var baseUrl = await _serverManager.GetServerAsync("App2");
                    
        if(string.IsNullOrWhiteSpace(baseUrl))
        {
            return "empty";
        }

        var url = $"{baseUrl}/api/values";

        using (HttpClient client = new HttpClient())
        {
            var result = await client.GetAsync(url);
            return await result.Content.ReadAsStringAsync();
        }
    }
}

最近版本更新:(数据更新于 2024-09-23 07:14:14)

2020-07-08 23:05:08 v0.2.7

主题(topics):

csharp, nacos, netcore, sdk

catcherwong/nacos-sdk-csharp同语言 C#最近更新仓库

2024-11-19 11:38:46 jellyfin/jellyfin

2024-11-15 10:33:01 DigitalRuby/IPBan

2024-11-13 04:41:56 dotnet/runtime

2024-11-07 00:43:03 huiyadanli/RevokeMsgPatcher

2024-11-06 13:04:33 Pik-4/HsMod

2024-11-05 02:46:31 microsoft/PowerToys