Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
catcherwong committed Feb 21, 2021
1 parent 7d6ae0d commit ca23dc6
Show file tree
Hide file tree
Showing 4 changed files with 564 additions and 49 deletions.
74 changes: 49 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

csharp(dotnet core) implementation of [nacos](https://nacos.io/) OpenAPI.

![Build](https://github.com/nacos-group/nacos-sdk-csharp/workflows/Build/badge.svg) ![Release](https://github.com/nacos-group/nacos-sdk-csharp/workflows/Release/badge.svg) ![](https://img.shields.io/nuget/v/nacos-sdk-csharp-unofficial.svg) ![](https://img.shields.io/nuget/vpre/nacos-sdk-csharp-unofficial.svg) ![](https://img.shields.io/nuget/dt/nacos-sdk-csharp-unofficial) ![](https://img.shields.io/github/license/nacos-group/nacos-sdk-csharp)
![Build](https://github.com/nacos-group/nacos-sdk-csharp/workflows/Build/badge.svg) ![Release](https://github.com/nacos-group/nacos-sdk-csharp/workflows/Release/badge.svg) ![](https://img.shields.io/nuget/v/nacos-sdk-csharp.svg) ![](https://img.shields.io/nuget/vpre/nacos-sdk-csharp.svg) ![](https://img.shields.io/nuget/dt/nacos-sdk-csharp) ![](https://img.shields.io/github/license/nacos-group/nacos-sdk-csharp)

![](./media/prj.png)

Expand All @@ -11,13 +11,15 @@ csharp(dotnet core) implementation of [nacos](https://nacos.io/) OpenAPI.
Choose a package that you need.

```bash
dotnet add package nacos-sdk-csharp-unofficial
dotnet add package nacos-sdk-csharp-unofficial.AspNetCore
dotnet add package nacos-sdk-csharp-unofficial.Extensions.Configuration
dotnet add package nacos-sdk-csharp-unofficial.YamlParser
dotnet add package nacos-sdk-csharp-unofficial.IniParser
dotnet add package nacos-sdk-csharp
dotnet add package nacos-sdk-csharp.AspNetCore
dotnet add package nacos-sdk-csharp.Extensions.Configuration
dotnet add package nacos-sdk-csharp.YamlParser
dotnet add package nacos-sdk-csharp.IniParser
```

> NOTE: The packages' name has remove the suffix `unofficial`.
## Features

- Basic OpenApi Usages
Expand All @@ -41,7 +43,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>

// read configuration from config files
// it will use default json parser to parse the configuration store in nacos server.
builder.AddNacosConfiguration(c.GetSection("NacosConfig"));
builder.AddNacosV2Configuration(c.GetSection("NacosConfig"));
// you also can specify ini or yaml parser as well.
// builder.AddNacosConfiguration(c.GetSection("NacosConfig"), Nacos.IniParser.IniConfigurationStringParser.Instance);
// builder.AddNacosConfiguration(c.GetSection("NacosConfig"), Nacos.YamlParser.YamlConfigurationStringParser.Instance);
Expand All @@ -57,16 +59,25 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
```JSON
{
"NacosConfig": {
"Optional": false,
"DataId": "msconfigapp",
"Group": "",
"Tenant": "f47e0ae1-982a-4a64-aea3-52506492a3d4",
"Listeners": [
{
"Optional": false,
"DataId": "common",
"Group": "DEFAULT_GROUP"
},
{
"Optional": false,
"DataId": "demo",
"Group": "DEFAULT_GROUP"
}
],
"Tenant": "csharp-demo",
"ServerAddresses": [ "http://localhost:8848/" ],
"UserName": "test2",
"Password": "123456",
"AccessKey": "",
"SecretKey": "",
"EndPoint": "acm.aliyun.com:8080"
"EndPoint": "acm.aliyun.com"
}
}
```
Expand Down Expand Up @@ -128,7 +139,7 @@ public class Startup
{
// ...
services.AddNacosAspNetCore(Configuration);
services.AddNacosAspNet(Configuration);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand All @@ -142,20 +153,29 @@ Modify `appsettings.json`

```JSON
"nacos": {
"EndPoint": "sub-domain.aliyun.com:8080",
"ServerAddresses": [ "http://localhost:8848" ],
"DefaultTimeOut": 15000,
"Namespace": "",
"Namespace": "cs",
"ListenInterval": 1000,
"ServiceName": "App1",
"ClusterName": "",
"GroupName": "",
"Weight": 100,
"GroupName": "DEFAULT_GROUP",
"ClusterName": "DEFAULT",
"Ip": "",
"PreferredNetworks": "", // select an IP that matches the prefix as the service registration IP
"UserName": "test2",
"Password": "123456",
"Port": 0,
"Weight": 100,
"RegisterEnabled": true,
"InstanceEnabled": true,
"Ephemeral": true,
"Secure": false,
"AccessKey": "",
"SecretKey": "",
"EndPoint": "sub-domain.aliyun.com:8080",
"UserName": "",
"Password": "",
"ConfigUseRpc": true,
"NamingUseRpc": true,
"NamingLoadCacheAtStart": "",
"LBStrategy": "WeightRandom", //WeightRandom WeightRoundRobin
"Metadata": {
"aa": "bb",
Expand All @@ -171,19 +191,23 @@ Modify `appsettings.json`
[ApiController]
public class ValuesController : ControllerBase
{
private readonly INacosServerManager _serverManager;
private readonly Nacos.V2.INacosNamingService _svc;

public ValuesController(INacosServerManager serverManager)
public ValuesController(Nacos.V2.INacosNamingService svc)
{
_serverManager = serverManager;
_svc = svc;
}

[HttpGet("test")]
public async Task<IActionResult> Test()
{
// need to know the service name.
// support WeightRandom and WeightRoundRobin.
var baseUrl = await _serverManager.GetServerAsync("App2");
var instance = await _svc.SelectOneHealthyInstance("App2", "DEFAULT_GROUP");
var host = $"{instance.Ip}:{instance.Port}";

var baseUrl = instance.Metadata.TryGetValue("secure", out _)
? $"https://{host}"
: $"http://{host}";

if(string.IsNullOrWhiteSpace(baseUrl))
{
Expand Down
73 changes: 49 additions & 24 deletions README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

基于C#(dotnet core)实现 [nacos](https://nacos.io/) OpenAPI 的官方版本

![Build](https://github.com/nacos-group/nacos-sdk-csharp/workflows/Build/badge.svg) ![Release](https://github.com/nacos-group/nacos-sdk-csharp/workflows/Release/badge.svg) ![](https://img.shields.io/nuget/v/nacos-sdk-csharp-unofficial.svg) ![](https://img.shields.io/nuget/vpre/nacos-sdk-csharp-unofficial.svg) ![](https://img.shields.io/nuget/dt/nacos-sdk-csharp-unofficial) ![](https://img.shields.io/github/license/nacos-group/nacos-sdk-csharp)
![Build](https://github.com/nacos-group/nacos-sdk-csharp/workflows/Build/badge.svg) ![Release](https://github.com/nacos-group/nacos-sdk-csharp/workflows/Release/badge.svg) ![](https://img.shields.io/nuget/v/nacos-sdk-csharp.svg) ![](https://img.shields.io/nuget/vpre/nacos-sdk-csharp.svg) ![](https://img.shields.io/nuget/dt/nacos-sdk-csharp) ![](https://img.shields.io/github/license/nacos-group/nacos-sdk-csharp)


![](./media/prj.png)

Expand All @@ -11,13 +12,15 @@
选择您需要的包。

```bash
dotnet add package nacos-sdk-csharp-unofficial
dotnet add package nacos-sdk-csharp-unofficial.AspNetCore
dotnet add package nacos-sdk-csharp-unofficial.Extensions.Configuration
dotnet add package nacos-sdk-csharp-unofficial.YamlParser
dotnet add package nacos-sdk-csharp-unofficial.IniParser
dotnet add package nacos-sdk-csharp
dotnet add package nacos-sdk-csharp.AspNetCore
dotnet add package nacos-sdk-csharp.Extensions.Configuration
dotnet add package nacos-sdk-csharp.YamlParser
dotnet add package nacos-sdk-csharp.IniParser
```

> 注: 包名里面的`unofficial`后缀已经被移除。
## 功能特性

- 基本的Open Api接口封装
Expand All @@ -41,7 +44,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>

// 从配置文件读取Nacos相关配置
// 默认会使用JSON解析器来解析存在Nacos Server的配置
builder.AddNacosConfiguration(c.GetSection("NacosConfig"));
builder.AddNacosV2Configuration(c.GetSection("NacosConfig"));
// 也可以按需使用ini或yaml的解析器
// builder.AddNacosConfiguration(c.GetSection("NacosConfig"), Nacos.IniParser.IniConfigurationStringParser.Instance);
// builder.AddNacosConfiguration(c.GetSection("NacosConfig"), Nacos.YamlParser.YamlConfigurationStringParser.Instance);
Expand All @@ -57,10 +60,19 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
```JSON
{
"NacosConfig": {
"Optional": false,
"DataId": "msconfigapp",
"Group": "",
"Tenant": "f47e0ae1-982a-4a64-aea3-52506492a3d4",
"Listeners": [
{
"Optional": false,
"DataId": "common",
"Group": "DEFAULT_GROUP"
},
{
"Optional": false,
"DataId": "demo",
"Group": "DEFAULT_GROUP"
}
],
"Tenant": "csharp-demo",
"ServerAddresses": [ "http://localhost:8848/" ],
"UserName": "test2",
"Password": "123456",
Expand Down Expand Up @@ -128,7 +140,7 @@ public class Startup
{
// ...
services.AddNacosAspNetCore(Configuration);
services.AddNacosAspNet(Configuration);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand All @@ -142,20 +154,29 @@ public class Startup

```JSON
"nacos": {
"EndPoint": "sub-domain.aliyun.com:8080",
"ServerAddresses": [ "http://localhost:8848" ],
"DefaultTimeOut": 15000,
"Namespace": "",
"Namespace": "cs",
"ListenInterval": 1000,
"ServiceName": "App1",
"ClusterName": "",
"GroupName": "",
"Weight": 100,
"GroupName": "DEFAULT_GROUP",
"ClusterName": "DEFAULT",
"Ip": "",
"PreferredNetworks": "", // select an IP that matches the prefix as the service registration IP
"UserName": "test2",
"Password": "123456",
"Port": 0,
"Weight": 100,
"RegisterEnabled": true,
"InstanceEnabled": true,
"Ephemeral": true,
"Secure": false,
"AccessKey": "",
"SecretKey": "",
"EndPoint": "sub-domain.aliyun.com:8080",
"UserName": "",
"Password": "",
"ConfigUseRpc": true,
"NamingUseRpc": true,
"NamingLoadCacheAtStart": "",
"LBStrategy": "WeightRandom", //WeightRandom WeightRoundRobin
"Metadata": {
"aa": "bb",
Expand All @@ -171,19 +192,23 @@ public class Startup
[ApiController]
public class ValuesController : ControllerBase
{
private readonly INacosServerManager _serverManager;
private readonly Nacos.V2.INacosNamingService _svc;

public ValuesController(INacosServerManager serverManager)
public ValuesController(Nacos.V2.INacosNamingService svc)
{
_serverManager = serverManager;
_svc = svc;
}

[HttpGet("test")]
public async Task<IActionResult> Test()
{
// 这里需要知道被调用方的服务名
// 支持加权随机和加权轮询
var baseUrl = await _serverManager.GetServerAsync("App2");
var instance = await _svc.SelectOneHealthyInstance("App2", "DEFAULT_GROUP")
var host = $"{instance.Ip}:{instance.Port}";

var baseUrl = instance.Metadata.TryGetValue("secure", out _)
? $"https://{host}"
: $"http://{host}";

if(string.IsNullOrWhiteSpace(baseUrl))
{
Expand Down
Loading

0 comments on commit ca23dc6

Please sign in to comment.