Skip to content

Commit

Permalink
Merge pull request #223 from nacos-group/dev
Browse files Browse the repository at this point in the history
prepare v1.3.4
  • Loading branch information
catcherwong authored Aug 10, 2022
2 parents cca6ba4 + 68df6f5 commit f12c66a
Show file tree
Hide file tree
Showing 32 changed files with 956 additions and 312 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_Stable_1x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ jobs:
run: java -version
- name: Setup Stable 1.x Nacos Server
run: |
wget https://github.com/alibaba/nacos/releases/download/1.4.3/nacos-server-1.4.3.tar.gz
tar -xvf nacos-server-1.4.3.tar.gz
wget https://github.com/alibaba/nacos/releases/download/1.4.4/nacos-server-1.4.4.tar.gz
tar -xvf nacos-server-1.4.4.tar.gz
cd nacos/bin
# sed -i 's/nacos.core.auth.enabled=false/nacos.core.auth.enabled=true/g' ../conf/application.properties
chmod 755 startup.sh
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build_Stable_2x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ jobs:
run: java -version
- name: Setup Stable 2.x Nacos Server
run: |
wget https://github.com/alibaba/nacos/releases/download/2.0.4/nacos-server-2.0.4.tar.gz
tar -xvf nacos-server-2.0.4.tar.gz
wget https://github.com/alibaba/nacos/releases/download/2.1.0/nacos-server-2.1.0.tar.gz
tar -xvf nacos-server-2.1.0.tar.gz
cd nacos/bin
# sed -i 's/nacos.core.auth.enabled=false/nacos.core.auth.enabled=true/g' ../conf/application.properties
chmod 755 startup.sh
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build_Unstable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
run: java -version
- name: Setup Unstable Nacos Server
run: |
wget https://github.com/alibaba/nacos/releases/download/2.1.0/nacos-server-2.1.0.tar.gz
tar -xvf nacos-server-2.1.0.tar.gz
wget https://github.com/alibaba/nacos/releases/download/2.1.1/nacos-server-2.1.1.tar.gz
tar -xvf nacos-server-2.1.1.tar.gz
cd nacos/bin
# sed -i 's/nacos.core.auth.enabled=false/nacos.core.auth.enabled=true/g' ../conf/application.properties
sed -i 's/Xms512m/Xms1024m/g' startup.sh
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ https://nacos-sdk-csharp.readthedocs.io/en/latest/
1. Configure in `Program.cs`

```cs
// after v1.3.3, we can use UseNacosConfig to simplify
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseNacosConfig(section: "NacosConfig", parser: null logAction: null)
// .UseNacosConfig(section: "NacosConfig", parser: Nacos.YamlParser.YamlConfigurationStringParser.Instance logAction: null)
// .UseNacosConfig(section: "NacosConfig", parser: Nacos.IniParser.IniConfigurationStringParser.Instance logAction: null)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

// before v1.3.3
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
Expand All @@ -55,7 +67,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
});
```

2. Modify `appsettings.json`
Expand Down
14 changes: 13 additions & 1 deletion README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ https://nacos-sdk-csharp.readthedocs.io/en/latest/
1.`Program.cs` 进行如下配置

```cs
// v1.3.3 版本之后, 可以用 UseNacosConfig 来简化
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseNacosConfig(section: "NacosConfig", parser: null logAction: null)
// .UseNacosConfig(section: "NacosConfig", parser: Nacos.YamlParser.YamlConfigurationStringParser.Instance logAction: null)
// .UseNacosConfig(section: "NacosConfig", parser: Nacos.IniParser.IniConfigurationStringParser.Instance logAction: null)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

// v1.3.3 版本之前
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
Expand All @@ -57,7 +69,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
});
```

2. 修改 `appsettings.json`
Expand Down
11 changes: 11 additions & 0 deletions docs/guide/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ SDK 在 v1.x 版本之后,就是默认用 grpc 的方式和 nacos server 对
针对 b 的情况,需要把 9848 暴露出来。

如果修改了默认端口或者是通过环境变量设置了偏移,自行调整对应端口,参考 https://nacos.io/zh-cn/docs/2.0.0-compatibility.html

3. nacos-sdk-csharp 版本与 nacos server 版本关系
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

nacos server 目前主要有 1.x 版本和 2.x 版本

nacos-sdk-csharp 有 0.x unofficial 版本 和 1.x 版本

nacos-sdk-csharp 0.x unofficial 版本 只能应用于 nacos server 1.x 版本

nacos-sdk-csharp 1.x 版本 可以同时应用于 nacos server 1.x 版本 和 2.x 版本
65 changes: 65 additions & 0 deletions samples/App3/Controllers/OpenApiController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace App3.Controllers
{
using Microsoft.AspNetCore.Mvc;
using Nacos.V2.Utils;
using System.Threading.Tasks;

[ApiController]
[Route("o")]
public class OpenApiController : ControllerBase
{
private readonly Nacos.OpenApi.INacosOpenApi _api;

public OpenApiController(Nacos.OpenApi.INacosOpenApi api)
{
_api = api;
}

// GET o/n-g
[HttpGet("n-g")]
public async Task<string> NamespaceGetAll()
{
var list = await _api.GetNamespacesAsync().ConfigureAwait(false);

var res = list.ToJsonString();

return res ?? "GetAllInstances";
}

// GET o/n-g
[HttpGet("n-c")]
public async Task<string> NamespaceCreate(string i, string n)
{
var flag = await _api.CreateNamespaceAsync(i, n, "").ConfigureAwait(false);

return flag.ToString();
}

// GET o/n-u
[HttpGet("n-u")]
public async Task<string> NamespaceUpdate(string i, string n)
{
var flag = await _api.UpdateNamespaceAsync(i, n, "").ConfigureAwait(false);

return flag.ToString();
}

// GET o/n-u
[HttpGet("n-d")]
public async Task<string> NamespaceDelete(string i)
{
var flag = await _api.DeleteNamespaceAsync(i).ConfigureAwait(false);

return flag.ToString();
}

// GET o/metrics
[HttpGet("metrics")]
public async Task<string> GetMetrics()
{
var flag = await _api.GetMetricsAsync(false).ConfigureAwait(false);

return flag.ToJsonString();
}
}
}
8 changes: 8 additions & 0 deletions samples/App3/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nacos.V2.DependencyInjection;
using Nacos.OpenApi;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -27,6 +28,13 @@
x.NamingUseRpc = true;
});

builder.Services.AddNacosOpenApi(x =>
{
x.ServerAddresses = new System.Collections.Generic.List<string> { "http://localhost:8848/" };
x.EndPoint = "";
x.Namespace = "cs";
});

builder.Services.AddControllers();

var app = builder.Build();
Expand Down
26 changes: 23 additions & 3 deletions src/Nacos.AspNetCore/UriTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, in
address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
if (!string.IsNullOrWhiteSpace(address))
{
var url = ReplaceAddress(address, preferredNetworks);
var url = ReplaceAddress(address, preferredNetworks);

var uris = url.Split(splitChars).Select(x => new Uri(x));

foreach (var item in uris)
{
if (!IPAddress.TryParse(item.Host, out _))
{
throw new Nacos.V2.Exceptions.NacosException("Invalid ip address from ASPNETCORE_URLS");
}
}

return url.Split(splitChars).Select(x => new Uri(x));
return uris;
}

// 4. --urls
Expand All @@ -74,7 +84,17 @@ public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, in

var url = ReplaceAddress(address, preferredNetworks);

return url.Split(splitChars).Select(x => new Uri(x));
var uris = url.Split(splitChars).Select(x => new Uri(x));

foreach (var item in uris)
{
if (!IPAddress.TryParse(item.Host, out _))
{
throw new Nacos.V2.Exceptions.NacosException("Invalid ip address from --urls");
}
}

return uris;
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Nacos/OpenApi/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Nacos.OpenApi
{
internal class Constants
{
public static readonly string HttpClientName = "OpenApi";
}
}
140 changes: 140 additions & 0 deletions src/Nacos/OpenApi/DefaultNacosOpenApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
namespace Nacos.OpenApi
{
using Microsoft.Extensions.Options;
using Nacos.V2.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

public class DefaultNacosOpenApi : INacosOpenApi
{
private const string _namespacePath = "nacos/v1/console/namespaces";
private const string _metricsPath = "nacos/v1/ns/operator/metrics";

private readonly IHttpClientFactory _httpClientFactory;
private readonly Nacos.V2.NacosSdkOptions _options;

public DefaultNacosOpenApi(IHttpClientFactory httpClientFactory, IOptions<Nacos.V2.NacosSdkOptions> optionsAccs)
{
this._httpClientFactory = httpClientFactory;
this._options = optionsAccs.Value;
}

public async Task<bool> CreateNamespaceAsync(string customNamespaceId, string namespaceName, string namespaceDesc)
{
var client = _httpClientFactory.CreateClient(Constants.HttpClientName);

var content = new StringContent($"customNamespaceId={customNamespaceId}&namespaceName={namespaceName}&namespaceDesc={namespaceDesc}");
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
var req = new HttpRequestMessage(HttpMethod.Post, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_namespacePath}");
req.Content = content;

var resp = await client.SendAsync(req).ConfigureAwait(false);

if (resp.IsSuccessStatusCode)
{
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
bool.TryParse(res, out bool result);
return result;
}
else
{
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "CreateNamespaceAsync exception");
}
}

public async Task<bool> DeleteNamespaceAsync(string namespaceId)
{
var client = _httpClientFactory.CreateClient(Constants.HttpClientName);

var content = new StringContent($"namespaceId={namespaceId}");
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
var req = new HttpRequestMessage(HttpMethod.Delete, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_namespacePath}");
req.Content = content;

var resp = await client.SendAsync(req).ConfigureAwait(false);

if (resp.IsSuccessStatusCode)
{
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
bool.TryParse(res, out bool result);
return result;
}
else
{
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "DeleteNamespaceAsync exception");
}
}

public async Task<NacosMetrics> GetMetricsAsync(bool onlyStatus)
{
var client = _httpClientFactory.CreateClient(Constants.HttpClientName);

var req = new HttpRequestMessage(HttpMethod.Get, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_metricsPath}?onlyStatus={onlyStatus.ToString().ToLower()}");

var resp = await client.SendAsync(req).ConfigureAwait(false);

if (resp.IsSuccessStatusCode)
{
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
return res.ToObj<NacosMetrics>();
}
else
{
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "GetMetricsAsync exception");
}
}

public async Task<List<NacosNamespace>> GetNamespacesAsync()
{
var client = _httpClientFactory.CreateClient(Constants.HttpClientName);

var req = new HttpRequestMessage(HttpMethod.Get, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_namespacePath}");

var resp = await client.SendAsync(req).ConfigureAwait(false);

if (resp.IsSuccessStatusCode)
{
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
var jobj = Newtonsoft.Json.Linq.JObject.Parse(res);

if (jobj.TryGetValue("data", System.StringComparison.OrdinalIgnoreCase, out var val))
{
return val.ToString().ToObj<List<NacosNamespace>>();
}
else
{
return new List<NacosNamespace>();
}
}
else
{
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "GetNamespacesAsync exception");
}
}

public async Task<bool> UpdateNamespaceAsync(string namespaceId, string namespaceName, string namespaceDesc)
{
var client = _httpClientFactory.CreateClient(Constants.HttpClientName);

var content = new StringContent($"customNamespaceId={namespaceId}&namespaceName={namespaceName}&namespaceDesc={namespaceDesc}");
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
var req = new HttpRequestMessage(HttpMethod.Post, $"{_options.ServerAddresses.First().TrimEnd('/')}/{_namespacePath}");
req.Content = content;

var resp = await client.SendAsync(req).ConfigureAwait(false);

if (resp.IsSuccessStatusCode)
{
var res = await resp.Content.ReadAsStringAsync().ConfigureAwait(false);
bool.TryParse(res, out bool result);
return result;
}
else
{
throw new Nacos.V2.Exceptions.NacosException((int)resp.StatusCode, "UpdateNamespaceAsync exception");
}
}
}
}
Loading

0 comments on commit f12c66a

Please sign in to comment.