├── media └── prj.png ├── src ├── Nacos │ ├── Naming │ │ ├── Result │ │ │ ├── TcpHealthParams.cs │ │ │ ├── HttpHealthParams.cs │ │ │ ├── MySqlHealthParams.cs │ │ │ ├── HealthChecker.cs │ │ │ ├── BaseHealthParams.cs │ │ │ ├── ListClusterServersResult.cs │ │ │ ├── ListServicesResult.cs │ │ │ ├── Host.cs │ │ │ ├── GetCurrentClusterLeaderResult.cs │ │ │ ├── Selector.cs │ │ │ ├── BeatInfo.cs │ │ │ ├── ClusterServer.cs │ │ │ ├── GetMetricsResult.cs │ │ │ ├── ListInstancesResult.cs │ │ │ ├── Cluster.cs │ │ │ ├── GetServiceResult.cs │ │ │ ├── ClusterLeader.cs │ │ │ ├── GetInstanceResult.cs │ │ │ └── GetSwitchesResult.cs │ │ └── Requests │ │ │ ├── ListClusterServersRequest.cs │ │ │ ├── ModifySwitchesRequest.cs │ │ │ ├── GetServiceRequest.cs │ │ │ ├── RemoveServiceRequest.cs │ │ │ ├── ListServicesRequest.cs │ │ │ ├── ListInstancesRequest.cs │ │ │ ├── SendHeartbeatRequest.cs │ │ │ ├── ModifyInstanceHealthStatusRequest.cs │ │ │ ├── ModifyServiceRequest.cs │ │ │ ├── CreateServiceRequest.cs │ │ │ ├── RemoveInstanceRequest.cs │ │ │ ├── GetInstanceRequest.cs │ │ │ ├── ModifyInstanceRequest.cs │ │ │ └── RegisterInstanceRequest.cs │ ├── Utilities │ │ ├── CharacterUtil.cs │ │ ├── JsonUtil.cs │ │ ├── HashUtil.cs │ │ ├── HttpClientFactoryUtil.cs │ │ └── ParamUtil.cs │ ├── Config │ │ ├── Core │ │ │ └── Listener.cs │ │ ├── Failover │ │ │ ├── ILocalConfigInfoProcessor.cs │ │ │ ├── MemoryLocalConfigInfoProcessor.cs │ │ │ └── FileLocalConfigInfoProcessor.cs │ │ ├── NacosConfigClient.cs │ │ ├── Requests │ │ │ ├── RemoveConfigRequest.cs │ │ │ ├── RemoveListenerRequest.cs │ │ │ ├── GetConfigRequest.cs │ │ │ ├── ListenerConfigRequest.cs │ │ │ ├── PublishConfigRequest.cs │ │ │ └── AddListenerRequest.cs │ │ └── Http │ │ │ ├── HttpAgent.cs │ │ │ ├── IHttpAgent.cs │ │ │ ├── HttpAgentCommon.cs │ │ │ └── ServerHttpAgent.cs │ ├── Common │ │ ├── BaseRequest.cs │ │ ├── RequestPathValue.cs │ │ ├── NacosOptions.cs │ │ └── ConstValue.cs │ ├── Exceptions │ │ └── NacosException.cs │ ├── Nacos.csproj │ ├── INacosConfigClient.cs │ ├── Security │ │ └── SecurityProxy.cs │ ├── INacosNamingClient.cs │ └── DependencyInjection │ │ └── ServiceCollectionExtensions.cs ├── Nacos.AspNetCore │ ├── LoadBalance │ │ ├── LBStrategyName.cs │ │ ├── ILBStrategy.cs │ │ ├── WeightRoundRobinLBStrategy.cs │ │ └── WeightRandomLBStrategy.cs │ ├── NacosServer.cs │ ├── INacosServerManager.cs │ ├── Nacos.AspNetCore.csproj │ ├── NacosAspNetCoreOptions.cs │ ├── NacosServerManager.cs │ ├── ServiceCollectionExtensions.cs │ └── StatusReportBgTask.cs └── Nacos.Microsoft.Extensions.Configuration │ ├── INacosConfigurationParser.cs │ ├── Impl │ ├── NacosMsConfigClient.cs │ └── MsConfigServerHttpAgent.cs │ ├── Nacos.Microsoft.Extensions.Configuration.csproj │ ├── NacosConfigurationExtensions.cs │ ├── NacosConfigurationSource.cs │ ├── NacosConfigurationProvider.cs │ └── JsonConfigurationParser.cs ├── samples ├── MsConfigApp │ ├── SubObj.cs │ ├── appsettings.json │ ├── MsConfigApp.csproj │ ├── AppSettings.cs │ ├── Startup.cs │ ├── Controllers │ │ └── ConfigController.cs │ └── Program.cs ├── App3 │ ├── appsettings.Development.json │ ├── App3.csproj │ ├── appsettings.json │ ├── Program.cs │ ├── Startup.cs │ └── Controllers │ │ └── ValuesController.cs ├── App1 │ ├── appsettings.Development.json │ ├── App1.csproj │ ├── appsettings.json │ ├── Program.cs │ ├── Startup.cs │ └── Controllers │ │ └── ValuesController.cs └── App2 │ ├── appsettings.Development.json │ ├── App2.csproj │ ├── appsettings.json │ ├── Program.cs │ ├── Startup.cs │ └── Controllers │ └── ValuesController.cs ├── tests ├── Nacos.Microsoft.Extensions.Configuration.Tests │ ├── JsonConfigurationParserTest.cs │ └── Nacos.Microsoft.Extensions.Configuration.Tests.csproj └── Nacos.Tests │ ├── MetricsTest.cs │ ├── ClusterTest.cs │ ├── SwitchesTest.cs │ ├── Nacos.Tests.csproj │ ├── TestBase.cs │ ├── ServiceTest.cs │ ├── ConfigTest.cs │ └── InstanceTest.cs ├── .github └── workflows │ ├── build.yml │ └── release.yml ├── LICENSE ├── README.zh-cn.md ├── README.md ├── nacos-sdk-csharp.sln └── .gitignore /media/prj.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/catcherwong/nacos-sdk-csharp/HEAD/media/prj.png -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/TcpHealthParams.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class TcpHealthParams : BaseHealthParams { } 4 | } 5 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/HttpHealthParams.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class HttpHealthParams : BaseHealthParams { } 4 | } 5 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/MySqlHealthParams.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class MySqlHealthParams : BaseHealthParams { } 4 | } 5 | -------------------------------------------------------------------------------- /samples/MsConfigApp/SubObj.cs: -------------------------------------------------------------------------------- 1 | namespace MsConfigApp 2 | { 3 | public class SubObj 4 | { 5 | public string a { get; set; } 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/HealthChecker.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class HealthChecker 4 | { 5 | public string Type { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /samples/App3/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/LoadBalance/LBStrategyName.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | public enum LBStrategyName 4 | { 5 | WeightRoundRobin, 6 | WeightRandom, 7 | Ext1 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/Nacos.Microsoft.Extensions.Configuration.Tests/JsonConfigurationParserTest.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/catcherwong/nacos-sdk-csharp/HEAD/tests/Nacos.Microsoft.Extensions.Configuration.Tests/JsonConfigurationParserTest.cs -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/NacosServer.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | public class NacosServer 4 | { 5 | public string Url { get; set; } 6 | 7 | public double Weight { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /samples/App1/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /samples/App2/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/INacosServerManager.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | using System.Threading.Tasks; 4 | 5 | public interface INacosServerManager 6 | { 7 | Task GetServerAsync(string serviceName); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/BaseHealthParams.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class BaseHealthParams 4 | { 5 | public int Max { get; set; } 6 | public int Min { get; set; } 7 | public double Factor { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/ListClusterServersResult.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class ListClusterServersResult 6 | { 7 | public List Servers { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/ListServicesResult.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class ListServicesResult 6 | { 7 | public int Count { get; set; } 8 | public List Doms { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/Nacos/Utilities/CharacterUtil.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Utilities 2 | { 3 | using System; 4 | 5 | public class CharacterUtil 6 | { 7 | public static string OneEncode = Char.ConvertFromUtf32(1); 8 | 9 | public static string TwoEncode = Char.ConvertFromUtf32(2); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/Nacos.Microsoft.Extensions.Configuration/INacosConfigurationParser.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Microsoft.Extensions.Configuration 2 | { 3 | using System.Collections.Generic; 4 | 5 | public interface INacosConfigurationParser 6 | { 7 | IDictionary Parse(string input); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /samples/App1/App1.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /samples/App2/App2.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /samples/App3/App3.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /samples/App1/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Warning" 5 | } 6 | }, 7 | "AllowedHosts": "*", 8 | "nacos": { 9 | "ServerAddresses": [ "http://localhost:8848" ], 10 | "DefaultTimeOut": 15000, 11 | "Namespace": "", 12 | "ListenInterval": 1000, 13 | "ServiceName": "App1" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /samples/App2/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Warning" 5 | } 6 | }, 7 | "AllowedHosts": "*", 8 | "nacos": { 9 | "ServerAddresses": [ "http://localhost:8848" ], 10 | "DefaultTimeOut": 15000, 11 | "Namespace": "", 12 | "ListenInterval": 1000, 13 | "ServiceName": "App2" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Nacos/Config/Core/Listener.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Threading; 4 | 5 | public class Listener 6 | { 7 | public Listener(string name, Timer timer) 8 | { 9 | this.Name = name; 10 | this.Timer = timer; 11 | } 12 | 13 | public string Name { get; private set; } 14 | public Timer Timer { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/Nacos.Tests/MetricsTest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Tests 2 | { 3 | using System.Threading.Tasks; 4 | using Xunit; 5 | 6 | public class MetricsTest : TestBase 7 | { 8 | [Fact] 9 | public async Task GetMetrics_Should_Succeed() 10 | { 11 | var res = await _namingClient.GetMetricsAsync(); 12 | Assert.NotNull(res); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/Host.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class Host 4 | { 5 | public bool Valid { get; set; } 6 | public bool Marked { get; set; } 7 | public string InstanceId { get; set; } 8 | public int Port { get; set; } 9 | public string Ip { get; set; } 10 | public double Weight { get; set; } 11 | public object Metadata { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/GetCurrentClusterLeaderResult.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class GetCurrentClusterLeaderResult 4 | { 5 | public int HeartbeatDueMs { get; set; } 6 | public string Ip { get; set; } 7 | public int LeaderDueMs { get; set; } 8 | public string State { get; set; } 9 | public int Term { get; set; } 10 | public string VoteFor { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /samples/App3/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | }, 9 | "AllowedHosts": "*", 10 | "nacos": { 11 | "ServerAddresses": [ "http://localhost:8848" ], 12 | "DefaultTimeOut": 15000, 13 | "Namespace": "", 14 | "ListenInterval": 1000, 15 | "ServiceName": "App3" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/Selector.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class Selector 4 | { 5 | /// 6 | /// The types of selector accepted by Nacos 7 | /// 8 | /// 1. unknown not match any type 9 | /// 2. none not filter out any entity 10 | /// 3. label select by label 11 | /// 12 | /// 13 | public string Type { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /samples/MsConfigApp/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "NacosConfig": { 3 | "Optional": false, 4 | "DataId": "msconfigapp", 5 | "Group": "", 6 | "Tenant": "f47e0ae1-982a-4a64-aea3-52506492a3d4", 7 | //"Tenant": "", 8 | "ServerAddresses": [ "http://localhost:8848/" ], 9 | "UserName": "test2", 10 | "Password": "123456", 11 | //"ServerAddresses": [], 12 | "AccessKey": "", 13 | "SecretKey": "", 14 | "EndPoint": "acm.aliyun.com" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Nacos/Config/Failover/ILocalConfigInfoProcessor.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Threading.Tasks; 4 | 5 | public interface ILocalConfigInfoProcessor 6 | { 7 | Task GetFailoverAsync(string dataId, string group, string tenant); 8 | 9 | Task GetSnapshotAync(string dataId, string group, string tenant); 10 | 11 | Task SaveSnapshotAsync(string dataId, string group, string tenant, string config); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/BeatInfo.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class BeatInfo 6 | { 7 | public int port { get; set; } 8 | public string ip { get; set; } 9 | public double weight { get; set; } 10 | public string serviceName { get; set; } 11 | public string cluster { get; set; } 12 | public Dictionary metadata { get; set; } = new Dictionary(); 13 | public bool scheduled { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/ClusterServer.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class ClusterServer 4 | { 5 | public string Ip { get; set; } 6 | public int ServePort { get; set; } 7 | public string Site { get; set; } 8 | public double Weight { get; set; } 9 | public double AdWeight { get; set; } 10 | public bool Alive { get; set; } 11 | public int LastRefTime { get; set; } 12 | public string LastRefTimeStr { get; set; } 13 | public string Key { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/LoadBalance/ILBStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | using System.Collections.Generic; 4 | 5 | public interface ILBStrategy 6 | { 7 | /// 8 | /// Strategy Name 9 | /// 10 | LBStrategyName Name { get; } 11 | 12 | /// 13 | /// Get instance 14 | /// 15 | /// server list 16 | /// The instance 17 | string GetInstance(List list); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /samples/App1/Program.cs: -------------------------------------------------------------------------------- 1 | namespace App1 2 | { 3 | using Microsoft.AspNetCore; 4 | using Microsoft.AspNetCore.Hosting; 5 | 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | CreateWebHostBuilder(args).Build().Run(); 11 | } 12 | 13 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseUrls("http://127.0.0.1:9876") 16 | .UseStartup(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /samples/App2/Program.cs: -------------------------------------------------------------------------------- 1 | namespace App2 2 | { 3 | using Microsoft.AspNetCore; 4 | using Microsoft.AspNetCore.Hosting; 5 | 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | CreateWebHostBuilder(args).Build().Run(); 11 | } 12 | 13 | public static IWebHostBuilder CreateWebHostBuilder(string[] args) => 14 | WebHost.CreateDefaultBuilder(args) 15 | .UseUrls("http://127.0.0.1:9877") 16 | .UseStartup(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Nacos/Common/BaseRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public abstract class BaseRequest 6 | { 7 | /// 8 | /// Checks whether request is valid 9 | /// 10 | /// 11 | public abstract void CheckParam(); 12 | 13 | /// 14 | /// Convert request to params of API 15 | /// 16 | /// 17 | public abstract Dictionary ToDict(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/GetMetricsResult.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class GetMetricsResult 4 | { 5 | public int ServiceCount { get; set; } 6 | 7 | public double Load { get; set; } 8 | 9 | public double Mem { get; set; } 10 | 11 | public int ResponsibleServiceCount { get; set; } 12 | 13 | public int InstanceCount { get; set; } 14 | 15 | public double Cpu { get; set; } 16 | 17 | public string Status { get; set; } 18 | 19 | public int ResponsibleInstanceCount { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/ListInstancesResult.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class ListInstancesResult 6 | { 7 | public string Dom { get; set; } 8 | public int CacheMillis { get; set; } 9 | public string UseSpecifiedURL { get; set; } 10 | public List Hosts { get; set; } 11 | public string Checksum { get; set; } 12 | public long LastRefTime { get; set; } 13 | public string Env { get; set; } 14 | public string Clusters { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /samples/MsConfigApp/MsConfigApp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Nacos/Exceptions/NacosException.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Exceptions 2 | { 3 | using System; 4 | 5 | public class NacosException : Exception 6 | { 7 | public NacosException(string message) : base(message) 8 | { 9 | this.ErrorMsg = message; 10 | } 11 | 12 | public NacosException(int code, string message) : base(message) 13 | { 14 | this.ErrorCode = code; 15 | this.ErrorMsg = message; 16 | } 17 | 18 | public int ErrorCode { get; set; } 19 | 20 | public string ErrorMsg { get; set; } 21 | } 22 | } -------------------------------------------------------------------------------- /samples/App3/Program.cs: -------------------------------------------------------------------------------- 1 | namespace App3 2 | { 3 | using Microsoft.AspNetCore.Hosting; 4 | using Microsoft.Extensions.Hosting; 5 | 6 | public class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | CreateHostBuilder(args).Build().Run(); 11 | } 12 | 13 | public static IHostBuilder CreateHostBuilder(string[] args) => 14 | Host.CreateDefaultBuilder(args) 15 | .ConfigureWebHostDefaults(webBuilder => 16 | { 17 | webBuilder.UseStartup(); 18 | }); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/Cluster.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class Cluster 6 | { 7 | /// 8 | /// Health check config of this cluster 9 | /// 10 | public HealthChecker HealthChecker { get; set; } 11 | 12 | /// 13 | /// 14 | /// 15 | public Dictionary Metadata { get; set; } 16 | 17 | /// 18 | /// Name of cluster 19 | /// 20 | public string Name { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/GetServiceResult.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class GetServiceResult 6 | { 7 | public Dictionary Metadata { get; set; } 8 | public string GroupName { get; set; } 9 | public string NamespaceId { get; set; } 10 | public string Name { get; set; } 11 | public Selector Selector { get; set; } 12 | 13 | /// 14 | /// protect threshold 15 | /// 保护阈值 16 | /// 17 | public double ProtectThreshold { get; set; } 18 | public List Clusters { get; set; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /samples/MsConfigApp/AppSettings.cs: -------------------------------------------------------------------------------- 1 | namespace MsConfigApp 2 | { 3 | using System.Collections.Generic; 4 | 5 | /* 6 | { 7 | "ConnectionStrings": { 8 | "Default": "Server=127.0.0.1;Port=3306;Database=demo;User Id=root;Password=123456;" 9 | }, 10 | "version": "测试version", 11 | "AppSettings": { 12 | "Str": "val", 13 | "num": 1, 14 | "arr": [1, 2, 3], 15 | "subobj": { 16 | "a": "b" 17 | } 18 | } 19 | } 20 | */ 21 | public class AppSettings 22 | { 23 | public string Str { get; set; } 24 | 25 | public int Num { get; set; } 26 | 27 | public List Arr { get; set; } 28 | 29 | public SubObj SubObj { get; set; } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/ClusterLeader.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public class ClusterLeader 4 | { 5 | public int HeartbeatDueMs { get; set; } 6 | 7 | public string Ip { get; set; } 8 | 9 | public int LeaderDueMs { get; set; } 10 | 11 | /// 12 | /// 1. LEADER Leader of the cluster, only one leader stands in a cluster 13 | /// 2. FOLLOWER Follower of the cluster, report to and copy from leader 14 | /// 3. CANDIDATE Candidate leader to be elected 15 | /// 16 | public string State { get; set; } 17 | 18 | public int Term { get; set; } 19 | 20 | public string VoteFor { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | windows: 7 | name: build on windows 8 | runs-on: windows-latest 9 | steps: 10 | - uses: actions/checkout@v1 11 | - name: Setup .NET Core 12 | uses: actions/setup-dotnet@v1 13 | with: 14 | dotnet-version: 3.1.200 15 | - name: Build with dotnet 16 | run: dotnet build --configuration Release 17 | 18 | linux: 19 | name: build on linux 20 | runs-on: ubuntu-latest 21 | 22 | steps: 23 | - uses: actions/checkout@v1 24 | - name: Setup .NET Core 25 | uses: actions/setup-dotnet@v1 26 | with: 27 | dotnet-version: 3.1.200 28 | - name: Build with dotnet 29 | run: dotnet build --configuration Release 30 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/ListClusterServersRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class ListClusterServersRequest : BaseRequest 6 | { 7 | /// 8 | /// if return healthy servers only 9 | /// 10 | public bool? Healthy { get; set; } 11 | 12 | public override void CheckParam() 13 | { 14 | //return true; 15 | } 16 | 17 | public override Dictionary ToDict() 18 | { 19 | var dict = new Dictionary(); 20 | 21 | if (Healthy.HasValue) 22 | dict.Add("healthy", Healthy.Value.ToString()); 23 | 24 | return dict; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Nacos.Tests/ClusterTest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Tests 2 | { 3 | using System.Threading.Tasks; 4 | using Xunit; 5 | 6 | public class ClusterTest : TestBase 7 | { 8 | [Fact] 9 | public async Task ListClusterServers_Should_Succeed() 10 | { 11 | var request = new ListClusterServersRequest 12 | { 13 | 14 | }; 15 | 16 | var res = await _namingClient.ListClusterServersAsync(request); 17 | Assert.NotNull(res); 18 | } 19 | 20 | [Fact] 21 | public async Task GetCurrentClusterLeader_Should_Succeed() 22 | { 23 | var res = await _namingClient.GetCurrentClusterLeaderAsync(); 24 | Assert.NotNull(res); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/Nacos.Tests/SwitchesTest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Tests 2 | { 3 | using System.Threading.Tasks; 4 | using Xunit; 5 | 6 | public class SwitchesTest : TestBase 7 | { 8 | [Fact] 9 | public async Task GetSwitches_Should_Succeed() 10 | { 11 | var res = await _namingClient.GetSwitchesAsync(); 12 | Assert.NotNull(res); 13 | } 14 | 15 | [Fact] 16 | public async Task ModifySwitches_Should_Succeed() 17 | { 18 | var request = new ModifySwitchesRequest 19 | { 20 | Debug = true, 21 | Entry = "test", 22 | Value = "test" 23 | }; 24 | 25 | var res = await _namingClient.ModifySwitchesAsync(request); 26 | Assert.True(res); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Nacos/Utilities/JsonUtil.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Utilities 2 | { 3 | using Newtonsoft.Json; 4 | using Newtonsoft.Json.Linq; 5 | 6 | public static class JsonUtil 7 | { 8 | public static string ToJsonString(this object obj) 9 | { 10 | return JsonConvert.SerializeObject(obj); 11 | } 12 | 13 | public static T ToObj(this string json) 14 | { 15 | if (string.IsNullOrWhiteSpace(json)) return default(T); 16 | 17 | return JsonConvert.DeserializeObject(json); 18 | } 19 | 20 | public static string GetPropValue(this string json, string prop) 21 | { 22 | if (string.IsNullOrWhiteSpace(json)) return string.Empty; 23 | 24 | var jObj = JObject.Parse(json); 25 | 26 | return jObj.Value(prop); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/Nacos.Tests/Nacos.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/Nacos/Common/RequestPathValue.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public static class RequestPathValue 4 | { 5 | public const string CONFIGS = "/nacos/v1/cs/configs"; 6 | public const string CONFIGS_LISTENER = "/nacos/v1/cs/configs/listener"; 7 | 8 | public const string INSTANCE = "/nacos/v1/ns/instance"; 9 | public const string INSTANCE_LIST = "/nacos/v1/ns/instance/list"; 10 | public const string INSTANCE_BEAT = "/nacos/v1/ns/instance/beat"; 11 | public const string INSTANCE_HEALTH = "/nacos/v1/ns/health/instance"; 12 | 13 | public const string SERVICE = "/nacos/v1/ns/service"; 14 | public const string SERVICE_LIST = "/nacos/v1/ns/service/list"; 15 | 16 | public const string SWITCHES = "/nacos/v1/ns/operator/switches"; 17 | 18 | public const string SERVERS = "/nacos/v1/ns/operator/servers"; 19 | 20 | public const string LEADER = "/nacos/v1/ns/raft/leader"; 21 | 22 | public const string METRICS = "/nacos/v1/ns/operator/metrics"; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/LoadBalance/WeightRoundRobinLBStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | using System.Collections.Generic; 4 | using System.Threading; 5 | 6 | public class WeightRoundRobinLBStrategy : ILBStrategy 7 | { 8 | public LBStrategyName Name => LBStrategyName.WeightRoundRobin; 9 | 10 | private int _count; 11 | 12 | public string GetInstance(List list) 13 | { 14 | var listStr = new List(); 15 | 16 | foreach (var item in list) 17 | { 18 | for (int i = 0; i < (int)item.Weight; i++) 19 | { 20 | listStr.Add(item.Url); 21 | } 22 | } 23 | 24 | var len = list.Count; 25 | 26 | var instance = list[_count % len]; 27 | 28 | Interlocked.Increment(ref _count); 29 | 30 | // Interlocked.Exchange(ref _count, Interlocked.Increment(ref _count) % len); 31 | 32 | return instance.Url; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Catcher Wong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/Nacos.Microsoft.Extensions.Configuration.Tests/Nacos.Microsoft.Extensions.Configuration.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | 16 | 17 | all 18 | runtime; build; native; contentfiles; analyzers; buildtransitive 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/Nacos/Config/NacosConfigClient.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Microsoft.Extensions.Logging; 4 | using Microsoft.Extensions.Options; 5 | using Nacos.Config.Http; 6 | using System.Collections.Generic; 7 | 8 | public class NacosConfigClient : AbstNacosConfigClient 9 | { 10 | private readonly IHttpAgent _httpAgent; 11 | private readonly ILocalConfigInfoProcessor _processor; 12 | 13 | public NacosConfigClient( 14 | ILoggerFactory loggerFactory 15 | , IOptionsMonitor optionAccs 16 | , IHttpAgent httpAgent 17 | , ILocalConfigInfoProcessor processor) 18 | { 19 | _logger = loggerFactory.CreateLogger(); 20 | _options = optionAccs.CurrentValue; 21 | _httpAgent = httpAgent; 22 | _processor = processor; 23 | 24 | listeners = new List(); 25 | } 26 | 27 | public override IHttpAgent GetAgent() 28 | { 29 | return _httpAgent; 30 | } 31 | 32 | public override ILocalConfigInfoProcessor GetProcessor() 33 | { 34 | return _processor; 35 | } 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/Nacos.Microsoft.Extensions.Configuration/Impl/NacosMsConfigClient.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Microsoft.Extensions.Configuration 2 | { 3 | using global::Microsoft.Extensions.Logging; 4 | using Nacos; 5 | using Nacos.Config.Http; 6 | using System.Collections.Generic; 7 | 8 | public class NacosMsConfigClient : AbstNacosConfigClient 9 | { 10 | private readonly Nacos.Config.Http.IHttpAgent _httpAgent; 11 | private readonly ILocalConfigInfoProcessor _processor; 12 | 13 | public NacosMsConfigClient( 14 | ILoggerFactory loggerFactory 15 | , NacosOptions options) 16 | { 17 | _logger = loggerFactory.CreateLogger(); 18 | _options = options; 19 | _processor = new MemoryLocalConfigInfoProcessor(); 20 | 21 | _httpAgent = new MsConfigServerHttpAgent(_options); 22 | 23 | listeners = new List(); 24 | } 25 | 26 | public override IHttpAgent GetAgent() 27 | { 28 | return _httpAgent; 29 | } 30 | 31 | public override ILocalConfigInfoProcessor GetProcessor() 32 | { 33 | return _processor; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /samples/App1/Startup.cs: -------------------------------------------------------------------------------- 1 | namespace App1 2 | { 3 | using Microsoft.AspNetCore.Builder; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Hosting; 8 | 9 | public class Startup 10 | { 11 | public Startup(IConfiguration configuration) 12 | { 13 | Configuration = configuration; 14 | } 15 | 16 | public IConfiguration Configuration { get; } 17 | 18 | 19 | public void ConfigureServices(IServiceCollection services) 20 | { 21 | services.AddNacosAspNetCore(Configuration); 22 | services.AddControllers(); 23 | } 24 | 25 | 26 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 27 | { 28 | if (env.IsDevelopment()) 29 | { 30 | app.UseDeveloperExceptionPage(); 31 | } 32 | 33 | app.UseRouting(); 34 | 35 | app.UseEndpoints(endpoints => 36 | { 37 | endpoints.MapControllers(); 38 | }); 39 | 40 | //app.UseNacosAspNetCore(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /samples/App2/Startup.cs: -------------------------------------------------------------------------------- 1 | namespace App2 2 | { 3 | using Microsoft.AspNetCore.Builder; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Hosting; 8 | 9 | public class Startup 10 | { 11 | public Startup(IConfiguration configuration) 12 | { 13 | Configuration = configuration; 14 | } 15 | 16 | public IConfiguration Configuration { get; } 17 | 18 | 19 | public void ConfigureServices(IServiceCollection services) 20 | { 21 | services.AddNacosAspNetCore(Configuration); 22 | services.AddControllers(); 23 | } 24 | 25 | 26 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 27 | { 28 | if (env.IsDevelopment()) 29 | { 30 | app.UseDeveloperExceptionPage(); 31 | } 32 | 33 | app.UseRouting(); 34 | 35 | app.UseEndpoints(endpoints => 36 | { 37 | endpoints.MapControllers(); 38 | }); 39 | 40 | //app.UseNacosAspNetCore(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /samples/App3/Startup.cs: -------------------------------------------------------------------------------- 1 | namespace App3 2 | { 3 | using Microsoft.AspNetCore.Builder; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Hosting; 8 | 9 | public class Startup 10 | { 11 | public Startup(IConfiguration configuration) 12 | { 13 | Configuration = configuration; 14 | } 15 | 16 | public IConfiguration Configuration { get; } 17 | 18 | 19 | public void ConfigureServices(IServiceCollection services) 20 | { 21 | services.AddNacosAspNetCore(Configuration); 22 | services.AddControllers(); 23 | } 24 | 25 | 26 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 27 | { 28 | if (env.IsDevelopment()) 29 | { 30 | app.UseDeveloperExceptionPage(); 31 | } 32 | 33 | app.UseRouting(); 34 | 35 | app.UseEndpoints(endpoints => 36 | { 37 | endpoints.MapControllers(); 38 | }); 39 | 40 | //app.UseNacosAspNetCore(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Nacos/Config/Requests/RemoveConfigRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class RemoveConfigRequest : BaseRequest 7 | { 8 | /// 9 | /// The tenant, corresponding to the namespace field of Nacos 10 | /// 11 | public string Tenant { get; set; } 12 | 13 | /// 14 | /// Configuration ID 15 | /// 16 | public string DataId { get; set; } 17 | 18 | /// 19 | /// Configuration group 20 | /// 21 | public string Group { get; set; } 22 | 23 | public override void CheckParam() 24 | { 25 | ParamUtil.CheckKeyParam(DataId, Group); 26 | } 27 | 28 | public override Dictionary ToDict() 29 | { 30 | var dict = new Dictionary 31 | { 32 | { "dataId", DataId }, 33 | { "group", Group }, 34 | }; 35 | 36 | if (!string.IsNullOrWhiteSpace(Tenant)) 37 | dict.Add("tenant", Tenant); 38 | 39 | return dict; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Nacos/Config/Requests/RemoveListenerRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using Nacos.Utilities; 6 | 7 | public class RemoveListenerRequest : BaseRequest 8 | { 9 | /// 10 | /// Tenant information. It corresponds to the Namespace field in Nacos. 11 | /// 12 | public string Tenant { get; set; } 13 | 14 | /// 15 | /// Configuration ID 16 | /// 17 | public string DataId { get; set; } 18 | 19 | /// 20 | /// Configuration group 21 | /// 22 | public string Group { get; set; } 23 | 24 | /// 25 | /// Callbacks when configuration was changed 26 | /// 27 | /// The callbacks. 28 | public List Callbacks { get; set; } = new List(); 29 | 30 | public override void CheckParam() 31 | { 32 | ParamUtil.CheckTDG(Tenant, DataId, Group); 33 | } 34 | 35 | public override Dictionary ToDict() 36 | { 37 | return new Dictionary(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /samples/MsConfigApp/Startup.cs: -------------------------------------------------------------------------------- 1 | namespace MsConfigApp 2 | { 3 | using Microsoft.AspNetCore.Builder; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Hosting; 8 | 9 | public class Startup 10 | { 11 | public Startup(IConfiguration configuration) 12 | { 13 | Configuration = configuration; 14 | } 15 | 16 | public IConfiguration Configuration { get; } 17 | 18 | public void ConfigureServices(IServiceCollection services) 19 | { 20 | services.Configure(Configuration.GetSection("AppSettings")); 21 | services.AddControllers(); 22 | } 23 | 24 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 25 | { 26 | if (env.IsDevelopment()) 27 | { 28 | app.UseDeveloperExceptionPage(); 29 | } 30 | 31 | app.UseRouting(); 32 | 33 | app.UseAuthorization(); 34 | 35 | app.UseEndpoints(endpoints => 36 | { 37 | endpoints.MapControllers(); 38 | }); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/ModifySwitchesRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | public class ModifySwitchesRequest : BaseRequest 7 | { 8 | /// 9 | /// switch name 10 | /// 11 | public string Entry { get; set; } 12 | 13 | /// 14 | /// switch value 15 | /// 16 | public string Value { get; set; } 17 | 18 | /// 19 | /// if affect the local server, true means yes, false means no, default true 20 | /// 21 | public bool? Debug { get; set; } 22 | 23 | public override void CheckParam() 24 | { 25 | //return !string.IsNullOrWhiteSpace(Entry) && !string.IsNullOrWhiteSpace(Value); 26 | } 27 | 28 | public override Dictionary ToDict() 29 | { 30 | var dict = new Dictionary 31 | { 32 | { "entry", Entry }, 33 | { "value", Value }, 34 | }; 35 | 36 | if (Debug.HasValue) 37 | dict.Add("debug", Debug.Value.ToString()); 38 | 39 | return dict; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/GetServiceRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class GetServiceRequest : BaseRequest 7 | { 8 | /// 9 | /// service name 10 | /// 11 | public string ServiceName { get; set; } 12 | 13 | /// 14 | /// namespace id 15 | /// 16 | public string NamespaceId { get; set; } 17 | 18 | /// 19 | /// group name 20 | /// 21 | public string GroupName { get; set; } 22 | 23 | public override void CheckParam() 24 | { 25 | ParamUtil.CheckServiceName(ServiceName); 26 | } 27 | 28 | public override Dictionary ToDict() 29 | { 30 | var dict = new Dictionary 31 | { 32 | { "serviceName", ServiceName }, 33 | }; 34 | 35 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 36 | dict.Add("namespaceId", NamespaceId); 37 | 38 | if (!string.IsNullOrWhiteSpace(GroupName)) 39 | dict.Add("groupName", GroupName); 40 | 41 | return dict; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/RemoveServiceRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class RemoveServiceRequest : BaseRequest 7 | { 8 | /// 9 | /// service name 10 | /// 11 | public string ServiceName { get; set; } 12 | 13 | /// 14 | /// namespace id 15 | /// 16 | public string NamespaceId { get; set; } 17 | 18 | /// 19 | /// group name 20 | /// 21 | public string GroupName { get; set; } 22 | 23 | public override void CheckParam() 24 | { 25 | ParamUtil.CheckServiceName(ServiceName); 26 | } 27 | 28 | public override Dictionary ToDict() 29 | { 30 | var dict = new Dictionary 31 | { 32 | { "serviceName", ServiceName }, 33 | }; 34 | 35 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 36 | dict.Add("namespaceId", NamespaceId); 37 | 38 | if (!string.IsNullOrWhiteSpace(GroupName)) 39 | dict.Add("groupName", GroupName); 40 | 41 | return dict; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/LoadBalance/WeightRandomLBStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | public class WeightRandomLBStrategy : ILBStrategy 8 | { 9 | public LBStrategyName Name => LBStrategyName.WeightRandom; 10 | 11 | public string GetInstance(List list) 12 | { 13 | var dict = BuildScore(list); 14 | 15 | var instance = string.Empty; 16 | 17 | var rd = new Random().NextDouble(); 18 | 19 | foreach (var item in dict) 20 | { 21 | if (item.Value >= rd) 22 | { 23 | instance = item.Key; 24 | break; 25 | } 26 | } 27 | 28 | return instance; 29 | } 30 | 31 | private Dictionary BuildScore(List list) 32 | { 33 | var dict = new Dictionary(); 34 | var total = list.Sum(x => x.Weight); 35 | var cur = 0d; 36 | 37 | foreach (var item in list) 38 | { 39 | cur += item.Weight; 40 | dict.Add(item.Url, cur / total); 41 | } 42 | 43 | return dict; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Nacos/Config/Requests/GetConfigRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using Nacos.Utilities; 6 | 7 | public class GetConfigRequest : BaseRequest 8 | { 9 | /// 10 | /// Tenant information. It corresponds to the Namespace field in Nacos. 11 | /// 12 | public string Tenant { get; set; } 13 | 14 | /// 15 | /// Configuration ID 16 | /// 17 | public string DataId { get; set; } 18 | 19 | /// 20 | /// Configuration group 21 | /// 22 | public string Group { get; set; } 23 | 24 | public override void CheckParam() 25 | { 26 | ParamUtil.CheckKeyParam(DataId, Group); 27 | } 28 | 29 | public override Dictionary ToDict() 30 | { 31 | var dict = new Dictionary 32 | { 33 | { "dataId", DataId }, 34 | { "group", Group } 35 | }; 36 | 37 | if (!string.IsNullOrWhiteSpace(Tenant)) 38 | { 39 | dict.Add("tenant", Tenant); 40 | } 41 | 42 | return dict; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/GetInstanceResult.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class GetInstanceResult 6 | { 7 | /// 8 | /// user extended attributes 9 | /// 10 | public Dictionary Metadata { get; set; } 11 | 12 | /// 13 | /// unique id of this instance 14 | /// 15 | public string InstanceId { get; set; } 16 | 17 | /// 18 | /// instance port 19 | /// 20 | public int Port { get; set; } 21 | 22 | /// 23 | /// 24 | /// 25 | public string Service { get; set; } 26 | 27 | /// 28 | /// instance health status 29 | /// 30 | public bool Healthy { get; set; } 31 | 32 | /// 33 | /// instance ip 34 | /// 35 | public string Ip { get; set; } 36 | 37 | /// 38 | /// cluster information of instance 39 | /// 40 | public string ClusterName { get; set; } 41 | 42 | /// 43 | /// instance weight 44 | /// 45 | public double Weight { get; set; } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /tests/Nacos.Tests/TestBase.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Tests 2 | { 3 | using Microsoft.Extensions.DependencyInjection; 4 | using Nacos; 5 | using System; 6 | 7 | public class TestBase 8 | { 9 | protected INacosNamingClient _namingClient; 10 | protected INacosConfigClient _configClient; 11 | 12 | public TestBase() 13 | { 14 | IServiceCollection services = new ServiceCollection(); 15 | services.AddNacos(configure => 16 | { 17 | configure.DefaultTimeOut = 8000; 18 | configure.ServerAddresses = new System.Collections.Generic.List { "http://localhost:8848", }; 19 | configure.AccessKey = ""; 20 | configure.SecretKey = ""; 21 | configure.Namespace = "f47e0ae1-982a-4a64-aea3-52506492a3d4"; 22 | //configure.Namespace = ""; 23 | configure.UserName = "aa"; 24 | //configure.UserName = "test"; 25 | configure.Password = "123456"; 26 | configure.EndPoint = "acm.aliyun.com"; 27 | }); 28 | 29 | IServiceProvider serviceProvider = services.BuildServiceProvider(); 30 | _namingClient = serviceProvider.GetService(); 31 | _configClient = serviceProvider.GetService(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Nacos/Nacos.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | netstandard2.0 6 | nacos-sdk-csharp-unofficial 7 | $(NugetVersion) 8 | Catcher Wong 9 | Catcher Wong 10 | nacos csharp sdk (unofficial) 11 | nacos,csharp,sdk 12 | https://github.com/catcherwong/nacos-sdk-csharp 13 | https://github.com/catcherwong/nacos-sdk-csharp 14 | https://github.com/catcherwong/nacos-sdk-csharp 15 | 16 | 1. Introduce Proxy. 17 | 18 | 19 | 20 | 21 | true 22 | $(NoWarn);1591 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/ListServicesRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | public class ListServicesRequest : BaseRequest 7 | { 8 | /// 9 | /// current page number 10 | /// 11 | public int PageNo { get; set; } 12 | 13 | /// 14 | /// page size 15 | /// 16 | public int PageSize { get; set; } 17 | 18 | /// 19 | /// namespace id 20 | /// 21 | public string NamespaceId { get; set; } 22 | 23 | /// 24 | /// group name 25 | /// 26 | public string GroupName { get; set; } 27 | 28 | public override void CheckParam() 29 | { 30 | //return PageNo > 0 && PageSize > 0; 31 | } 32 | 33 | public override Dictionary ToDict() 34 | { 35 | var dict = new Dictionary 36 | { 37 | { "pageNo", PageNo.ToString() }, 38 | { "pageSize", PageSize.ToString() }, 39 | }; 40 | 41 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 42 | dict.Add("namespaceId", NamespaceId); 43 | 44 | if (!string.IsNullOrWhiteSpace(GroupName)) 45 | dict.Add("groupName", GroupName); 46 | 47 | return dict; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /samples/App2/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | namespace App2.Controllers 2 | { 3 | using System.Collections.Generic; 4 | using System.Net.Http; 5 | using Microsoft.AspNetCore.Mvc; 6 | using Nacos.AspNetCore; 7 | 8 | [Route("api/[controller]")] 9 | [ApiController] 10 | public class ValuesController : ControllerBase 11 | { 12 | private readonly INacosServerManager _serverManager; 13 | 14 | public ValuesController(INacosServerManager serverManager) 15 | { 16 | _serverManager = serverManager; 17 | } 18 | 19 | // GET api/values 20 | [HttpGet] 21 | public ActionResult> Get() 22 | { 23 | return new string[] { "value1", "value2", "App2" }; 24 | } 25 | 26 | // GET api/values/test 27 | [HttpGet("test")] 28 | public ActionResult Test() 29 | { 30 | var baseUrl = _serverManager.GetServerAsync("App1").GetAwaiter().GetResult(); ; 31 | 32 | if (string.IsNullOrWhiteSpace(baseUrl)) 33 | { 34 | return "empty"; 35 | } 36 | 37 | var url = $"{baseUrl}/api/values"; 38 | 39 | using (HttpClient client = new HttpClient()) 40 | { 41 | var result = client.GetAsync(url).GetAwaiter().GetResult(); 42 | return result.Content.ReadAsStringAsync().GetAwaiter().GetResult(); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /samples/App3/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | namespace App3.Controllers 2 | { 3 | using Microsoft.AspNetCore.Mvc; 4 | using Nacos.AspNetCore; 5 | using System.Collections.Generic; 6 | using System.Net.Http; 7 | 8 | [ApiController] 9 | [Route("[controller]")] 10 | public class ValuesController : ControllerBase 11 | { 12 | private readonly INacosServerManager _serverManager; 13 | 14 | public ValuesController(INacosServerManager serverManager) 15 | { 16 | _serverManager = serverManager; 17 | } 18 | 19 | // GET api/values 20 | [HttpGet] 21 | public ActionResult> Get() 22 | { 23 | return new string[] { "value1", "value2", "App3" }; 24 | } 25 | 26 | // GET api/values/test 27 | [HttpGet("test")] 28 | public ActionResult Test() 29 | { 30 | var baseUrl = _serverManager.GetServerAsync("App1").GetAwaiter().GetResult(); ; 31 | 32 | if (string.IsNullOrWhiteSpace(baseUrl)) 33 | { 34 | return "empty"; 35 | } 36 | 37 | var url = $"{baseUrl}/api/values"; 38 | 39 | using (HttpClient client = new HttpClient()) 40 | { 41 | var result = client.GetAsync(url).GetAwaiter().GetResult(); 42 | return result.Content.ReadAsStringAsync().GetAwaiter().GetResult(); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | build_artifact: 10 | name: Build and upload artifact 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Setup .NET Core 16 | uses: actions/setup-dotnet@v1 17 | with: 18 | dotnet-version: 3.1.200 19 | - name: Build with dotnet 20 | run: dotnet build --configuration Release 21 | - name: Pack with dotnet 22 | run: dotnet pack /home/runner/work/nacos-sdk-csharp/nacos-sdk-csharp/nacos-sdk-csharp.sln -o /home/runner/work/nugetpkgs -c Release --no-build 23 | - name: Upload artifact 24 | uses: actions/upload-artifact@v1 25 | with: 26 | name: nugetpkgs 27 | path: /home/runner/work/nugetpkgs 28 | 29 | release_nuget: 30 | name: Release to Nuget 31 | needs: build_artifact 32 | runs-on: ubuntu-latest 33 | 34 | steps: 35 | - name: Download build artifacts 36 | uses: actions/download-artifact@v1 37 | with: 38 | name: nugetpkgs 39 | - name: list nugetpkgs 40 | run: ls nugetpkgs 41 | - name: test dotnet cmd 42 | run: dotnet --version 43 | - name: Release 44 | run: | 45 | for file in nugetpkgs/*.nupkg 46 | do 47 | dotnet nuget push $file -k ${{ secrets.NUGET_API_KEY }} --skip-duplicate -s https://www.nuget.org/api/v2/package 48 | done -------------------------------------------------------------------------------- /samples/App1/Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | namespace App1.Controllers 2 | { 3 | using System.Collections.Generic; 4 | using System.Net.Http; 5 | using Microsoft.AspNetCore.Mvc; 6 | using Nacos.AspNetCore; 7 | 8 | [Route("api/[controller]")] 9 | [ApiController] 10 | public class ValuesController : ControllerBase 11 | { 12 | private readonly INacosServerManager _serverManager; 13 | 14 | public ValuesController(INacosServerManager serverManager) 15 | { 16 | _serverManager = serverManager; 17 | } 18 | 19 | // GET api/values 20 | [HttpGet] 21 | public ActionResult> Get() 22 | { 23 | return new string[] { "value1", "value2", "App1" }; 24 | } 25 | 26 | // GET api/values/test 27 | [HttpGet("test")] 28 | public ActionResult Test() 29 | { 30 | var baseUrl = _serverManager.GetServerAsync("App2").GetAwaiter().GetResult(); 31 | 32 | if(string.IsNullOrWhiteSpace(baseUrl)) 33 | { 34 | return "empty"; 35 | } 36 | 37 | var url = $"{baseUrl}/api/values"; 38 | 39 | using (HttpClient client = new HttpClient()) 40 | { 41 | var result = client.GetAsync(url).GetAwaiter().GetResult(); 42 | return result.Content.ReadAsStringAsync().GetAwaiter().GetResult(); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Nacos/Utilities/HashUtil.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Utilities 2 | { 3 | using System; 4 | using System.Security.Cryptography; 5 | using System.Text; 6 | 7 | public static class HashUtil 8 | { 9 | public static string GetMd5(string value) 10 | { 11 | var result = string.Empty; 12 | 13 | if (string.IsNullOrEmpty(value)) 14 | { 15 | return result; 16 | } 17 | 18 | using (var md5 = MD5.Create()) 19 | { 20 | byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(value)); 21 | var sBuilder = new StringBuilder(); 22 | foreach (byte t in data) 23 | { 24 | sBuilder.Append(t.ToString("x2")); 25 | } 26 | result = sBuilder.ToString(); 27 | } 28 | 29 | return result; 30 | } 31 | 32 | public static string GetHMACSHA1(string value, string key) 33 | { 34 | byte[] secrectKey = Encoding.UTF8.GetBytes(key); 35 | using (HMACSHA1 hmac = new HMACSHA1(secrectKey)) 36 | { 37 | hmac.Initialize(); 38 | 39 | byte[] bytes_hmac_in = Encoding.UTF8.GetBytes(value); 40 | byte[] bytes_hamc_out = hmac.ComputeHash(bytes_hmac_in); 41 | 42 | string str_hamc_out = Convert.ToBase64String(bytes_hamc_out); 43 | 44 | return str_hamc_out; 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Nacos/Config/Http/HttpAgent.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Config.Http 2 | { 3 | using System.Collections.Generic; 4 | using System.Net.Http; 5 | using System.Threading.Tasks; 6 | 7 | public abstract class HttpAgent : IHttpAgent 8 | { 9 | public async Task DeleteAsync(string path, Dictionary headers, Dictionary paramValues, int timeout = 8000) 10 | => await ReqApiAsync(HttpMethod.Delete, path, headers, paramValues, timeout); 11 | 12 | public async Task GetAsync(string path, Dictionary headers, Dictionary paramValues, int timeout = 8000) 13 | => await ReqApiAsync(HttpMethod.Get, path, headers, paramValues, timeout); 14 | 15 | public async Task PostAsync(string path, Dictionary headers, Dictionary paramValues, int timeout = 8000) 16 | => await ReqApiAsync(HttpMethod.Post, path, headers, paramValues, timeout); 17 | 18 | public string GetName() => AbstGetName(); 19 | public string GetNamespace() => AbstGetNamespace(); 20 | public string GetTenant() => AbstGetTenant(); 21 | 22 | public abstract Task ReqApiAsync(HttpMethod httpMethod, string path, Dictionary headers, Dictionary paramValues, int timeout); 23 | public abstract string AbstGetName(); 24 | public abstract string AbstGetNamespace(); 25 | public abstract string AbstGetTenant(); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Nacos/Common/NacosOptions.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class NacosOptions 6 | { 7 | /// 8 | /// nacos server addresses. 9 | /// 10 | /// 11 | /// http://10.1.12.123:8848,https://10.1.12.124:8848 12 | /// 13 | public List ServerAddresses { get; set; } 14 | 15 | /// 16 | /// EndPoint 17 | /// 18 | public string EndPoint { get; set; } 19 | 20 | public string ContextPath { get; set; } = "nacos"; 21 | 22 | public string ClusterName { get; set; } = "serverlist"; 23 | 24 | /// 25 | /// default timeout, unit is Milliseconds. 26 | /// 27 | public int DefaultTimeOut { get; set; } = 15000; 28 | 29 | /// 30 | /// default namespace 31 | /// 32 | public string Namespace { get; set; } = ""; 33 | 34 | /// 35 | /// accessKey 36 | /// 37 | public string AccessKey { get; set; } 38 | 39 | /// 40 | /// secretKey 41 | /// 42 | public string SecretKey { get; set; } 43 | 44 | public string UserName { get; set; } 45 | 46 | public string Password { get; set; } 47 | 48 | /// 49 | /// listen interval, unit is millisecond. 50 | /// 51 | public int ListenInterval { get; set; } = 1000; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/Nacos.AspNetCore.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | netcoreapp3.1 6 | nacos-sdk-csharp-unofficial.AspNetCore 7 | $(NugetVersion) 8 | Catcher Wong 9 | Catcher Wong 10 | nacos csharp sdk (unofficial) 11 | nacos,csharp,sdk 12 | https://github.com/catcherwong/nacos-sdk-csharp 13 | https://github.com/catcherwong/nacos-sdk-csharp 14 | https://github.com/catcherwong/nacos-sdk-csharp 15 | 16 | 1. IHostedService. 17 | 2. LoadBalance Strategy. 18 | 19 | 20 | 21 | 22 | true 23 | $(NoWarn);1591 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/Nacos.Microsoft.Extensions.Configuration/Nacos.Microsoft.Extensions.Configuration.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | netstandard2.0 6 | nacos-sdk-csharp-unofficial.Extensions.Configuration 7 | $(NugetVersion) 8 | Catcher Wong 9 | Catcher Wong 10 | nacos csharp sdk (unofficial) 11 | nacos,csharp,sdk,msconfig 12 | https://github.com/catcherwong/nacos-sdk-csharp 13 | https://github.com/catcherwong/nacos-sdk-csharp 14 | https://github.com/catcherwong/nacos-sdk-csharp 15 | 16 | 1. Support more feature configuration. 17 | 18 | 19 | 20 | 21 | true 22 | $(NoWarn);1591 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/Nacos/INacosConfigClient.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Threading.Tasks; 4 | 5 | public interface INacosConfigClient 6 | { 7 | /// 8 | /// Gets configurations in Nacos 9 | /// 10 | /// request 11 | /// 12 | Task GetConfigAsync(GetConfigRequest request); 13 | 14 | /// 15 | /// Publishes configurations in Nacos 16 | /// 17 | /// request 18 | /// 19 | Task PublishConfigAsync(PublishConfigRequest request); 20 | 21 | /// 22 | /// Deletes configurations in Nacos 23 | /// 24 | /// request 25 | /// 26 | Task RemoveConfigAsync(RemoveConfigRequest request); 27 | 28 | /// 29 | /// Listen configuration. 30 | /// 31 | /// request. 32 | /// 33 | Task AddListenerAsync(AddListenerRequest request); 34 | 35 | /// 36 | /// Delete Listening 37 | /// 38 | /// request. 39 | /// 40 | Task RemoveListenerAsync(RemoveListenerRequest request); 41 | 42 | //Task getConfigAndSignListener(); 43 | 44 | /// 45 | /// Get server status 46 | /// 47 | /// 48 | Task GetServerStatus(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/ListInstancesRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class ListInstancesRequest : BaseRequest 7 | { 8 | /// 9 | /// Service name 10 | /// 11 | public string ServiceName { get; set; } 12 | 13 | /// 14 | /// ID of namespace 15 | /// 16 | public string NamespaceId { get; set; } 17 | 18 | /// 19 | /// Cluster name, splited by comma 20 | /// 21 | public string Clusters { get; set; } 22 | 23 | /// 24 | /// group name 25 | /// 26 | public string GroupName { get; set; } 27 | 28 | /// 29 | /// Return healthy instance or not 30 | /// 31 | public bool? HealthyOnly { get; set; } 32 | 33 | public override void CheckParam() 34 | { 35 | ParamUtil.CheckServiceName(ServiceName); 36 | } 37 | 38 | public override Dictionary ToDict() 39 | { 40 | var dict = new Dictionary 41 | { 42 | { "serviceName", ServiceName }, 43 | }; 44 | 45 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 46 | dict.Add("namespaceId", NamespaceId); 47 | 48 | if (!string.IsNullOrWhiteSpace(Clusters)) 49 | dict.Add("clusters", Clusters); 50 | 51 | if (!string.IsNullOrWhiteSpace(GroupName)) 52 | dict.Add("groupName", GroupName); 53 | 54 | if (HealthyOnly.HasValue) 55 | dict.Add("healthyOnly", HealthyOnly.Value.ToString()); 56 | 57 | return dict; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Nacos/Config/Failover/MemoryLocalConfigInfoProcessor.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Concurrent; 4 | using System.Threading.Tasks; 5 | 6 | public class MemoryLocalConfigInfoProcessor : ILocalConfigInfoProcessor 7 | { 8 | private readonly ConcurrentDictionary _cache; 9 | 10 | public MemoryLocalConfigInfoProcessor() 11 | { 12 | _cache = new ConcurrentDictionary(); 13 | } 14 | 15 | private string GetCacheKey(string dataId, string group, string tenant) 16 | { 17 | return $"{tenant}-{group}-{dataId}"; 18 | } 19 | 20 | public async Task GetFailoverAsync(string dataId, string group, string tenant) 21 | { 22 | var cacheKey = GetCacheKey(dataId, group, tenant); 23 | 24 | _cache.TryGetValue(cacheKey, out string config); 25 | 26 | return await Task.FromResult(config); 27 | } 28 | 29 | public async Task GetSnapshotAync(string dataId, string group, string tenant) 30 | { 31 | var cacheKey = GetCacheKey(dataId, group, tenant); 32 | 33 | _cache.TryGetValue(cacheKey, out string config); 34 | 35 | return await Task.FromResult(config); 36 | } 37 | 38 | public async Task SaveSnapshotAsync(string dataId, string group, string tenant, string config) 39 | { 40 | var cacheKey = GetCacheKey(dataId, group, tenant); 41 | 42 | if (string.IsNullOrEmpty(config)) 43 | { 44 | _cache.TryRemove(cacheKey,out _); 45 | } 46 | else 47 | { 48 | _cache.AddOrUpdate(cacheKey, config, (k, v) => config); 49 | } 50 | 51 | await Task.Yield(); 52 | } 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/SendHeartbeatRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class SendHeartbeatRequest : BaseRequest 7 | { 8 | /// 9 | /// Service Name 10 | /// 11 | public string ServiceName { get; set; } 12 | 13 | /// 14 | /// beat content 15 | /// 16 | public string Beat => BeatInfo.ToJsonString(); 17 | 18 | /// 19 | /// beat info 20 | /// 21 | public BeatInfo BeatInfo { get; set; } 22 | 23 | /// 24 | /// group name 25 | /// 26 | public string GroupName { get; set; } 27 | 28 | /// 29 | /// if instance is ephemeral 30 | /// 31 | public bool? Ephemeral { get; set; } 32 | 33 | /// 34 | /// namespace id 35 | /// 36 | public string NameSpaceId { get; set; } 37 | 38 | public override void CheckParam() 39 | { 40 | //return BeatInfo != null && !string.IsNullOrWhiteSpace(ServiceName); 41 | } 42 | 43 | public override Dictionary ToDict() 44 | { 45 | var dict = new Dictionary 46 | { 47 | { "serviceName", ServiceName }, 48 | { "beat", Beat }, 49 | }; 50 | 51 | if (!string.IsNullOrWhiteSpace(NameSpaceId)) 52 | dict.Add("namespaceId", NameSpaceId); 53 | 54 | if (Ephemeral.HasValue) 55 | dict.Add("ephemeral", Ephemeral.Value.ToString()); 56 | 57 | 58 | if (!string.IsNullOrWhiteSpace(GroupName)) 59 | dict.Add("groupName", GroupName); 60 | 61 | return dict; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Nacos/Config/Requests/ListenerConfigRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class ListenerConfigRequest : BaseRequest 7 | { 8 | /// 9 | /// A packet field indicating the configuration ID. 10 | /// 11 | public string DataId { get; set; } 12 | 13 | /// 14 | /// A packet field indicating the configuration group. 15 | /// 16 | public string Group { get; set; } 17 | 18 | /// 19 | /// A packet field indicating the MD5 value of the configuration. 20 | /// 21 | public string ContentMD5 => HashUtil.GetMd5(Content); 22 | 23 | /// 24 | /// A packet field indicating tenant information. It corresponds to the Namespace field in Nacos. 25 | /// 26 | public string Tenant { get; set; } 27 | 28 | /// 29 | /// The configuration value 30 | /// 31 | public string Content { get; set; } 32 | 33 | /// 34 | /// A request to listen for data packets 35 | /// 36 | public string ListeningConfigs => string.IsNullOrWhiteSpace(Tenant) 37 | ? $"{DataId}{CharacterUtil.TwoEncode}{Group}{CharacterUtil.TwoEncode}{ContentMD5}{CharacterUtil.OneEncode}" 38 | : $"{DataId}{CharacterUtil.TwoEncode}{Group}{CharacterUtil.TwoEncode}{ContentMD5}{CharacterUtil.TwoEncode}{Tenant}{CharacterUtil.OneEncode}"; 39 | 40 | 41 | public override void CheckParam() 42 | { 43 | //return !string.IsNullOrWhiteSpace(DataId); 44 | } 45 | 46 | public override Dictionary ToDict() 47 | { 48 | return new Dictionary() 49 | { 50 | { "Listening-Configs", ListeningConfigs } 51 | }; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Result/GetSwitchesResult.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | public class GetSwitchesResult 7 | { 8 | public string Name { get; set; } 9 | public string Masters { get; set; } 10 | public object AdWeightMap { get; set; } 11 | public int DefaultPushCacheMillis { get; set; } 12 | public int ClientBeatInterval { get; set; } 13 | public int DefaultCacheMillis { get; set; } 14 | public double DistroThreshold { get; set; } 15 | public bool HealthCheckEnabled { get; set; } 16 | public bool DistroEnabled { get; set; } 17 | 18 | public bool EnableStandalone { get; set; } 19 | 20 | public bool PushEnabled { get; set; } 21 | 22 | public int CheckTimes { get; set; } 23 | 24 | public HttpHealthParams HttpHealthParams { get; set; } 25 | 26 | public TcpHealthParams TcpHealthParams { get; set; } 27 | 28 | public MySqlHealthParams MysqlHealthParams { get; set; } 29 | 30 | public List IncrementalList { get; set; } 31 | 32 | public int ServerStatusSynchronizationPeriodMillis { get; set; } 33 | public int ServiceStatusSynchronizationPeriodMillis { get; set; } 34 | public bool DisableAddIP { get; set; } 35 | public bool SendBeatOnly { get; set; } 36 | public Dictionary LimitedUrlMap { get; set; } 37 | public int DistroServerExpiredMillis { get; set; } 38 | public string PushGoVersion { get; set; } 39 | public string PushJavaVersion { get; set; } 40 | public string PushPythonVersion { get; set; } 41 | public string PushCVersion { get; set; } 42 | public bool EnableAuthentication { get; set; } 43 | public string OverriddenServerStatus { get; set; } 44 | public bool DefaultInstanceEphemeral { get; set; } 45 | public List HealthCheckWhiteList { get; set; } 46 | public string Checksum { get; set; } 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Nacos/Config/Http/IHttpAgent.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Net.Http; 3 | using System.Text; 4 | using System.Threading.Tasks; 5 | 6 | namespace Nacos.Config.Http 7 | { 8 | public interface IHttpAgent 9 | { 10 | /// 11 | /// Get 12 | /// 13 | /// path 14 | /// headers 15 | /// paramValues 16 | /// timeout 17 | Task GetAsync(string path, Dictionary headers, Dictionary paramValues, int timeout = 8000); 18 | 19 | /// 20 | /// Post 21 | /// 22 | /// path 23 | /// headers 24 | /// paramValues 25 | /// timeout 26 | Task PostAsync(string path, Dictionary headers, Dictionary paramValues, int timeout = 8000); 27 | 28 | /// 29 | /// Delete 30 | /// 31 | /// path 32 | /// headers 33 | /// paramValues 34 | /// timeout 35 | Task DeleteAsync(string path, Dictionary headers, Dictionary paramValues, int timeout = 8000); 36 | 37 | /// 38 | /// get name 39 | /// 40 | string GetName(); 41 | 42 | /// 43 | /// get namespace 44 | /// 45 | /// 46 | string GetNamespace(); 47 | 48 | /// 49 | /// get tenant 50 | /// 51 | /// 52 | string GetTenant(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/NacosAspNetCoreOptions.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | using System.Collections.Generic; 4 | 5 | public class NacosAspNetCoreOptions 6 | { 7 | /// 8 | /// nacos server addresses. 9 | /// 10 | /// 11 | /// http://10.1.12.123:8848,https://10.1.12.124:8848 12 | /// 13 | public List ServerAddresses { get; set; } 14 | 15 | /// 16 | /// default timeout, unit is Milliseconds. 17 | /// 18 | public int DefaultTimeOut { get; set; } = 15000; 19 | 20 | /// 21 | /// default namespace 22 | /// 23 | public string Namespace { get; set; } = ""; 24 | 25 | /// 26 | /// the name of the service. 27 | /// 28 | public string ServiceName { get; set; } 29 | 30 | /// 31 | /// the name of the cluster. 32 | /// 33 | /// The name of the cluster. 34 | public string ClusterName { get; set; } 35 | 36 | /// 37 | /// the name of the group. 38 | /// 39 | public string GroupName { get; set; } 40 | 41 | /// 42 | /// the weight of this instance. 43 | /// 44 | public double Weight { get; set; } = 100; 45 | 46 | /// 47 | /// the ip of this instance 48 | /// 49 | public string Ip { get; set; } 50 | 51 | /// 52 | /// the port of this instance 53 | /// 54 | public int Port { get; set; } 55 | 56 | /// 57 | /// the metadata of this instance 58 | /// 59 | public Dictionary Metadata { get; set; } 60 | 61 | /// 62 | /// Load Balance Strategy 63 | /// 64 | public string LBStrategy { get; set; } = LBStrategyName.WeightRandom.ToString(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Nacos.Microsoft.Extensions.Configuration/NacosConfigurationExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace Microsoft.Extensions.Configuration 2 | { 3 | using Nacos.Microsoft.Extensions.Configuration; 4 | using System; 5 | 6 | public static class NacosConfigurationExtensions 7 | { 8 | /// 9 | /// Add Nacos Configuration 10 | /// 11 | /// IConfigurationBuilder 12 | /// setup NacosConfigurationSource 13 | /// 14 | public static IConfigurationBuilder AddNacosConfiguration( 15 | this IConfigurationBuilder builder, Action action) 16 | { 17 | if (builder == null) 18 | { 19 | throw new ArgumentNullException(nameof(builder)); 20 | } 21 | 22 | if (action == null) 23 | { 24 | throw new ArgumentNullException(nameof(action)); 25 | } 26 | 27 | var source = new NacosConfigurationSource(); 28 | 29 | action(source); 30 | 31 | return builder.Add(source); 32 | } 33 | 34 | /// 35 | /// Add Nacos Configuration 36 | /// 37 | /// IConfigurationBuilder 38 | /// Configuration binding nacos configuration source 39 | /// 40 | public static IConfigurationBuilder AddNacosConfiguration( 41 | this IConfigurationBuilder builder, IConfiguration configuration) 42 | { 43 | if (builder == null) 44 | { 45 | throw new ArgumentNullException(nameof(builder)); 46 | } 47 | 48 | if (configuration == null) 49 | { 50 | throw new ArgumentNullException(nameof(configuration)); 51 | } 52 | 53 | var source = new NacosConfigurationSource(); 54 | 55 | configuration.Bind(source); 56 | 57 | return builder.Add(source); 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/ModifyInstanceHealthStatusRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class ModifyInstanceHealthStatusRequest : BaseRequest 7 | { 8 | /// 9 | /// ip of instance 10 | /// 11 | public string Ip { get; set; } 12 | 13 | /// 14 | /// port of instance 15 | /// 16 | public int Port { get; set; } 17 | 18 | /// 19 | /// service name 20 | /// 21 | public string ServiceName { get; set; } 22 | 23 | /// 24 | /// namespace id 25 | /// 26 | public string NamespaceId { get; set; } 27 | 28 | /// 29 | /// if healthy 30 | /// 31 | public bool Healthy { get; set; } 32 | 33 | /// 34 | /// cluster name 35 | /// 36 | public string ClusterName { get; set; } 37 | 38 | /// 39 | /// group name 40 | /// 41 | public string GroupName { get; set; } 42 | 43 | public override void CheckParam() 44 | { 45 | ParamUtil.CheckInstanceInfo(Ip, Port, ServiceName); 46 | } 47 | 48 | public override Dictionary ToDict() 49 | { 50 | var dict = new Dictionary 51 | { 52 | { "serviceName", ServiceName }, 53 | { "ip", Ip }, 54 | { "port", Port.ToString() }, 55 | { "healthy", Healthy.ToString() }, 56 | }; 57 | 58 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 59 | dict.Add("namespaceId", NamespaceId); 60 | 61 | if (!string.IsNullOrWhiteSpace(GroupName)) 62 | dict.Add("groupName", GroupName); 63 | 64 | if (!string.IsNullOrWhiteSpace(ClusterName)) 65 | dict.Add("clusterName", ClusterName); 66 | 67 | return dict; 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/ModifyServiceRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class ModifyServiceRequest : BaseRequest 7 | { 8 | /// 9 | /// service name 10 | /// 11 | public string ServiceName { get; set; } 12 | 13 | /// 14 | /// namespace id 15 | /// 16 | public string NamespaceId { get; set; } 17 | 18 | /// 19 | /// Protect threshold, set value from 0 to 1, default 0 20 | /// 21 | public float? ProtectThreshold { get; set; } 22 | 23 | /// 24 | /// visit strategy, a JSON string 25 | /// 26 | public string Selector { get; set; } 27 | 28 | /// 29 | /// metadata of service 30 | /// 31 | public string Metadata { get; set; } 32 | 33 | /// 34 | /// group name 35 | /// 36 | public string GroupName { get; set; } 37 | 38 | public override void CheckParam() 39 | { 40 | ParamUtil.CheckServiceName(ServiceName); 41 | } 42 | 43 | public override Dictionary ToDict() 44 | { 45 | var dict = new Dictionary 46 | { 47 | { "serviceName", ServiceName }, 48 | }; 49 | 50 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 51 | dict.Add("namespaceId", NamespaceId); 52 | 53 | if (!string.IsNullOrWhiteSpace(GroupName)) 54 | dict.Add("groupName", GroupName); 55 | 56 | if (!string.IsNullOrWhiteSpace(Metadata)) 57 | dict.Add("metadata", Metadata); 58 | 59 | if (!string.IsNullOrWhiteSpace(Selector)) 60 | dict.Add("selector", Selector); 61 | 62 | if (ProtectThreshold.HasValue) 63 | dict.Add("protectThreshold", ProtectThreshold.Value.ToString()); 64 | 65 | return dict; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/Nacos.Tests/ServiceTest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Tests 2 | { 3 | using System.Threading.Tasks; 4 | using Xunit; 5 | 6 | public class ServiceTest : TestBase 7 | { 8 | [Fact] 9 | public async Task CreateService_Should_Succeed() 10 | { 11 | var request = new CreateServiceRequest 12 | { 13 | ServiceName = "testservice", 14 | ProtectThreshold = 0.1f 15 | }; 16 | 17 | var res = await _namingClient.CreateServiceAsync(request); 18 | Assert.True(res); 19 | } 20 | 21 | [Fact] 22 | public async Task RemoveService_Should_Succeed() 23 | { 24 | var request = new RemoveServiceRequest 25 | { 26 | ServiceName = "testservice" 27 | }; 28 | 29 | var res = await _namingClient.RemoveServiceAsync(request); 30 | Assert.True(res); 31 | } 32 | 33 | [Fact] 34 | public async Task ModifyService_Should_Succeed() 35 | { 36 | var request = new ModifyServiceRequest 37 | { 38 | ServiceName = "testservice", 39 | ProtectThreshold = 0.5f, 40 | }; 41 | 42 | var res = await _namingClient.ModifyServiceAsync(request); 43 | Assert.True(res); 44 | } 45 | 46 | [Fact] 47 | public async Task GetService_Should_Succeed() 48 | { 49 | var request = new GetServiceRequest 50 | { 51 | ServiceName = "testservice", 52 | }; 53 | 54 | var res = await _namingClient.GetServiceAsync(request); 55 | Assert.NotNull(res); 56 | } 57 | 58 | [Fact] 59 | public async Task ListServices_Should_Succeed() 60 | { 61 | var request = new ListServicesRequest 62 | { 63 | PageNo = 1, 64 | PageSize = 2, 65 | }; 66 | 67 | var res = await _namingClient.ListServicesAsync(request); 68 | Assert.NotNull(res); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/CreateServiceRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | public class CreateServiceRequest : BaseRequest 8 | { 9 | /// 10 | /// service name 11 | /// 12 | public string ServiceName { get; set; } 13 | 14 | /// 15 | /// namespace id 16 | /// 17 | public string NamespaceId { get; set; } 18 | 19 | /// 20 | /// Protect threshold, set value from 0 to 1, default 0 21 | /// 22 | public float? ProtectThreshold { get; set; } 23 | 24 | /// 25 | /// visit strategy, a JSON string 26 | /// 27 | public string Selector { get; set; } 28 | 29 | /// 30 | /// metadata of service 31 | /// 32 | public string Metadata { get; set; } 33 | 34 | /// 35 | /// group name 36 | /// 37 | public string GroupName { get; set; } 38 | 39 | public override void CheckParam() 40 | { 41 | ParamUtil.CheckServiceName(ServiceName); 42 | } 43 | 44 | public override Dictionary ToDict() 45 | { 46 | var dict = new Dictionary 47 | { 48 | { "serviceName", ServiceName }, 49 | }; 50 | 51 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 52 | dict.Add("namespaceId", NamespaceId); 53 | 54 | if (!string.IsNullOrWhiteSpace(Metadata)) 55 | dict.Add("metadata", Metadata); 56 | 57 | if (!string.IsNullOrWhiteSpace(GroupName)) 58 | dict.Add("groupName", GroupName); 59 | 60 | if (!string.IsNullOrWhiteSpace(Selector)) 61 | dict.Add("selector", Selector); 62 | 63 | if (ProtectThreshold.HasValue) 64 | dict.Add("protectThreshold", ProtectThreshold.ToString()); 65 | 66 | return dict; 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/RemoveInstanceRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class RemoveInstanceRequest : BaseRequest 7 | { 8 | /// 9 | /// IP of instance 10 | /// 11 | public string Ip { get; set; } 12 | 13 | /// 14 | /// Port of instance 15 | /// 16 | public int Port { get; set; } 17 | 18 | /// 19 | /// Service name 20 | /// 21 | public string ServiceName { get; set; } 22 | 23 | /// 24 | /// ID of namespace 25 | /// 26 | public string NamespaceId { get; set; } 27 | 28 | /// 29 | /// Cluster name 30 | /// 31 | public string ClusterName { get; set; } 32 | 33 | /// 34 | /// group name 35 | /// 36 | public string GroupName { get; set; } 37 | 38 | /// 39 | /// if instance is ephemeral 40 | /// 41 | public bool? Ephemeral { get; set; } 42 | 43 | public override void CheckParam() 44 | { 45 | ParamUtil.CheckInstanceInfo(Ip, Port, ServiceName); 46 | } 47 | 48 | public override Dictionary ToDict() 49 | { 50 | var dict = new Dictionary 51 | { 52 | { "serviceName", ServiceName }, 53 | { "ip", Ip }, 54 | { "port", Port.ToString() }, 55 | }; 56 | 57 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 58 | dict.Add("namespaceId", NamespaceId); 59 | 60 | if (!string.IsNullOrWhiteSpace(GroupName)) 61 | dict.Add("groupName", GroupName); 62 | 63 | if (!string.IsNullOrWhiteSpace(ClusterName)) 64 | dict.Add("clusterName", ClusterName); 65 | 66 | if (Ephemeral.HasValue) 67 | dict.Add("ephemeral", Ephemeral.Value.ToString()); 68 | 69 | return dict; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Nacos/Config/Requests/PublishConfigRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using Nacos.Utilities; 6 | 7 | public class PublishConfigRequest : BaseRequest 8 | { 9 | /// 10 | /// The tenant, corresponding to the namespace field of Nacos 11 | /// 12 | public string Tenant { get; set; } 13 | 14 | /// 15 | /// Configuration ID 16 | /// 17 | public string DataId { get; set; } 18 | 19 | /// 20 | /// Configuration group 21 | /// 22 | public string Group { get; set; } 23 | 24 | /// 25 | /// Configuration content 26 | /// 27 | public string Content { get; set; } 28 | 29 | /// 30 | /// Configuration type, options value text, json, xml, yaml, html, properties 31 | /// 32 | public string Type { get; set; } 33 | 34 | /// 35 | /// Configuration application 36 | /// 37 | public string AppName { get; set; } 38 | 39 | /// 40 | /// Configuration tags 41 | /// 42 | public string Tag { get; set; } 43 | 44 | public override void CheckParam() 45 | { 46 | ParamUtil.CheckParam(DataId, Group, Content); 47 | } 48 | 49 | public override Dictionary ToDict() 50 | { 51 | var dict = new Dictionary 52 | { 53 | { "dataId", DataId }, 54 | { "group", Group }, 55 | { "content", Content } 56 | }; 57 | 58 | if (!string.IsNullOrWhiteSpace(Tenant)) 59 | dict.Add("tenant", Tenant); 60 | 61 | if (!string.IsNullOrWhiteSpace(Type)) 62 | dict.Add("type", Type); 63 | 64 | if (!string.IsNullOrWhiteSpace(AppName)) 65 | dict.Add("appName", AppName); 66 | 67 | if (!string.IsNullOrWhiteSpace(Tag)) 68 | dict.Add("tag", Tag); 69 | 70 | return dict; 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Nacos/Config/Requests/AddListenerRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using Nacos.Utilities; 6 | 7 | public class AddListenerRequest : BaseRequest 8 | { 9 | /// 10 | /// Tenant information. It corresponds to the Namespace field in Nacos. 11 | /// 12 | public string Tenant { get; set; } 13 | 14 | /// 15 | /// Configuration ID 16 | /// 17 | public string DataId { get; set; } 18 | 19 | /// 20 | /// Configuration group 21 | /// 22 | public string Group { get; set; } 23 | 24 | /// 25 | /// Callbacks when configuration was changed 26 | /// 27 | public List> Callbacks { get; set; } = new List>(); 28 | 29 | /// 30 | /// Configuraton value. 31 | /// 32 | /// The content. 33 | public string Content { get; set; } 34 | 35 | /// 36 | /// A packet field indicating the MD5 value of the configuration. 37 | /// 38 | public string ContentMD5 => HashUtil.GetMd5(Content); 39 | 40 | /// 41 | /// A request to listen for data packets 42 | /// 43 | public string ListeningConfigs => string.IsNullOrWhiteSpace(Tenant) 44 | ? $"{DataId}{CharacterUtil.TwoEncode}{Group}{CharacterUtil.TwoEncode}{ContentMD5}{CharacterUtil.OneEncode}" 45 | : $"{DataId}{CharacterUtil.TwoEncode}{Group}{CharacterUtil.TwoEncode}{ContentMD5}{CharacterUtil.TwoEncode}{Tenant}{CharacterUtil.OneEncode}"; 46 | 47 | 48 | public override void CheckParam() 49 | { 50 | ParamUtil.CheckTDG(Tenant, DataId, Group); 51 | } 52 | 53 | public override Dictionary ToDict() 54 | { 55 | var dict = new Dictionary 56 | { 57 | { "Listening-Configs", ListeningConfigs }, 58 | { "dataId", DataId }, 59 | { "group", Group }, 60 | }; 61 | 62 | if (!string.IsNullOrWhiteSpace(Tenant)) 63 | dict.Add("tenant", Tenant); 64 | 65 | return dict; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/GetInstanceRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | public class GetInstanceRequest : BaseRequest 8 | { 9 | /// 10 | /// IP of instance 11 | /// 12 | public string Ip { get; set; } 13 | 14 | /// 15 | /// Port of instance 16 | /// 17 | public int Port { get; set; } 18 | 19 | /// 20 | /// Service name 21 | /// 22 | public string ServiceName { get; set; } 23 | 24 | /// 25 | /// ID of namespace 26 | /// 27 | public string NamespaceId { get; set; } 28 | 29 | /// 30 | /// Return healthy instance or not 31 | /// 32 | public bool? HealthyOnly { get; set; } 33 | 34 | /// 35 | /// Cluster name 36 | /// 37 | public string Cluster { get; set; } 38 | 39 | /// 40 | /// group name 41 | /// 42 | public string GroupName { get; set; } 43 | 44 | /// 45 | /// if instance is ephemeral 46 | /// 47 | public bool? Ephemeral { get; set; } 48 | 49 | public override void CheckParam() 50 | { 51 | ParamUtil.CheckInstanceInfo(Ip, Port, ServiceName); 52 | } 53 | 54 | public override Dictionary ToDict() 55 | { 56 | var dict = new Dictionary 57 | { 58 | { "serviceName", ServiceName }, 59 | { "ip", Ip }, 60 | { "port", Port.ToString() }, 61 | }; 62 | 63 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 64 | dict.Add("namespaceId", NamespaceId); 65 | 66 | if (!string.IsNullOrWhiteSpace(Cluster)) 67 | dict.Add("cluster", Cluster); 68 | 69 | if (!string.IsNullOrWhiteSpace(GroupName)) 70 | dict.Add("groupName", GroupName); 71 | 72 | if (HealthyOnly.HasValue) 73 | dict.Add("healthyOnly", HealthyOnly.Value.ToString()); 74 | 75 | if (Ephemeral.HasValue) 76 | dict.Add("ephemeral", Ephemeral.Value.ToString()); 77 | 78 | return dict; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/NacosServerManager.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | using EasyCaching.Core; 4 | using Microsoft.Extensions.Options; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | public class NacosServerManager : INacosServerManager 11 | { 12 | private readonly INacosNamingClient _client; 13 | private readonly IEasyCachingProvider _provider; 14 | private readonly ILBStrategy _strategy; 15 | 16 | public NacosServerManager( 17 | INacosNamingClient client, 18 | IEasyCachingProviderFactory factory, 19 | IEnumerable strategies, 20 | IOptions optionsAccs) 21 | { 22 | _client = client; 23 | _provider = factory.GetCachingProvider("nacos.aspnetcore"); 24 | _strategy = strategies.FirstOrDefault(x => x.Name.ToString().Equals(optionsAccs.Value.LBStrategy, StringComparison.OrdinalIgnoreCase)) 25 | ?? new WeightRandomLBStrategy(); 26 | } 27 | 28 | public async Task GetServerAsync(string serviceName) 29 | { 30 | var cached = await _provider.GetAsync(serviceName, async () => 31 | { 32 | var serviceInstances = await _client.ListInstancesAsync(new ListInstancesRequest 33 | { 34 | ServiceName = serviceName, 35 | HealthyOnly = true, 36 | }); 37 | 38 | var baseUrl = string.Empty; 39 | 40 | if (serviceInstances != null && serviceInstances.Hosts != null && serviceInstances.Hosts.Any()) 41 | { 42 | var list = serviceInstances.Hosts.Select(x => new NacosServer 43 | { 44 | // it seems that nacos don't return the scheme 45 | // so here use http only. 46 | Url = $"http://{x.Ip}:{x.Port}" 47 | }).ToList(); 48 | 49 | return list; 50 | } 51 | 52 | return null; 53 | }, TimeSpan.FromSeconds(10)); 54 | 55 | if (cached.HasValue) 56 | { 57 | var list = cached.Value; 58 | var instance = _strategy.GetInstance(list); 59 | return instance; 60 | } 61 | else 62 | { 63 | return null; 64 | } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /samples/MsConfigApp/Controllers/ConfigController.cs: -------------------------------------------------------------------------------- 1 | namespace MsConfigApp.Controllers 2 | { 3 | using System; 4 | using Microsoft.AspNetCore.Mvc; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.Logging; 7 | using Microsoft.Extensions.Options; 8 | 9 | [ApiController] 10 | [Route("api/[controller]")] 11 | public class ConfigController : ControllerBase 12 | { 13 | private readonly ILogger _logger; 14 | private readonly IConfiguration _configuration; 15 | private readonly AppSettings _settings; 16 | private readonly AppSettings _sSettings; 17 | private readonly AppSettings _mSettings; 18 | 19 | public ConfigController( 20 | ILogger logger, 21 | IConfiguration configuration, 22 | IOptions options, 23 | IOptionsSnapshot sOptions, 24 | IOptionsMonitor _mOptions 25 | ) 26 | { 27 | _logger = logger; 28 | _configuration = configuration; 29 | _settings = options.Value; 30 | _sSettings = sOptions.Value; 31 | _mSettings = _mOptions.CurrentValue; 32 | } 33 | 34 | [HttpGet] 35 | public string Get() 36 | { 37 | string id = Guid.NewGuid().ToString("N"); 38 | 39 | _logger.LogInformation($"============== begin {id} ====================="); 40 | 41 | var conn = _configuration.GetConnectionString("Default"); 42 | _logger.LogInformation($"{id} conn = {conn}"); 43 | 44 | var version = _configuration["version"]; 45 | _logger.LogInformation($"{id} version = {version}"); 46 | 47 | var str1 = Newtonsoft.Json.JsonConvert.SerializeObject(_settings); 48 | _logger.LogInformation($"{id} IOptions = {str1}"); 49 | 50 | var str2 = Newtonsoft.Json.JsonConvert.SerializeObject(_sSettings); 51 | _logger.LogInformation($"{id} IOptionsSnapshot = {str2}"); 52 | 53 | var str3 = Newtonsoft.Json.JsonConvert.SerializeObject(_mSettings); 54 | _logger.LogInformation($"{id} IOptionsMonitor = {str3}"); 55 | 56 | _logger.LogInformation($"==============================================="); 57 | _logger.LogInformation($"==============================================="); 58 | _logger.LogInformation($"==============================================="); 59 | 60 | return "ok"; 61 | } 62 | 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/Nacos.Microsoft.Extensions.Configuration/NacosConfigurationSource.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Microsoft.Extensions.Configuration 2 | { 3 | using global::Microsoft.Extensions.Configuration; 4 | using System.Collections.Generic; 5 | 6 | public class NacosConfigurationSource : IConfigurationSource 7 | { 8 | /// 9 | /// Nacos Server Addresses 10 | /// 11 | public List ServerAddresses { get; set; } 12 | 13 | /// 14 | /// Determines if the Nacos Server is optional 15 | /// 16 | public bool Optional { get; set; } 17 | 18 | /// 19 | /// Configuration ID 20 | /// 21 | public string DataId { get; set; } 22 | 23 | /// 24 | /// Configuration group 25 | /// 26 | public string Group { get; set; } 27 | 28 | /// 29 | /// Tenant information. It corresponds to the Namespace field in Nacos. 30 | /// 31 | public string Tenant { get; set; } 32 | 33 | /// 34 | /// EndPoint 35 | /// 36 | public string EndPoint { get; set; } 37 | 38 | public string ContextPath { get; set; } = "nacos"; 39 | 40 | public string ClusterName { get; set; } = "serverlist"; 41 | 42 | /// 43 | /// default timeout, unit is Milliseconds. 44 | /// 45 | public int DefaultTimeOut { get; set; } = 15000; 46 | 47 | /// 48 | /// accessKey 49 | /// 50 | public string AccessKey { get; set; } 51 | 52 | /// 53 | /// secretKey 54 | /// 55 | public string SecretKey { get; set; } 56 | 57 | /// 58 | /// username 59 | /// 60 | public string UserName { get; set; } 61 | 62 | /// 63 | /// password 64 | /// 65 | public string Password { get; set; } 66 | 67 | /// 68 | /// The configuration parser, default is json 69 | /// 70 | public INacosConfigurationParser NacosConfigurationParser { get; set; } 71 | 72 | /// 73 | /// Build the provider 74 | /// 75 | /// 76 | /// 77 | public IConfigurationProvider Build(IConfigurationBuilder builder) 78 | { 79 | return new NacosConfigurationProvider(this); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /tests/Nacos.Tests/ConfigTest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Tests 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Threading.Tasks; 6 | using Xunit; 7 | 8 | public class ConfigTest : TestBase 9 | { 10 | [Fact] 11 | public async Task GetConfig_Should_Succeed() 12 | { 13 | var request = new GetConfigRequest 14 | { 15 | DataId = "dataId", 16 | Group = "DEFAULT_GROUP", 17 | //Tenant = "tenant" 18 | }; 19 | 20 | var res = await _configClient.GetConfigAsync(request); 21 | Assert.NotNull(res); 22 | Assert.Equal("test", res); 23 | } 24 | 25 | [Fact] 26 | public async Task PublishConfig_Should_Succeed() 27 | { 28 | var request = new PublishConfigRequest 29 | { 30 | DataId = "dataId", 31 | Group = "DEFAULT_GROUP", 32 | //Tenant = "tenant", 33 | Content = "test", 34 | Type = "text", 35 | AppName = "appdemo" 36 | }; 37 | 38 | var res = await _configClient.PublishConfigAsync(request); 39 | Assert.True(res); 40 | } 41 | 42 | [Fact] 43 | public async Task RemoveConfig_Should_Succeed() 44 | { 45 | var request = new RemoveConfigRequest 46 | { 47 | DataId = "dataId", 48 | Group = "DEFAULT_GROUP", 49 | //Tenant = "tenant" 50 | }; 51 | 52 | var res = await _configClient.RemoveConfigAsync(request); 53 | Assert.True(res); 54 | } 55 | 56 | [Fact] 57 | public async Task ListenerConfig_Should_Succeed() 58 | { 59 | var request = new AddListenerRequest 60 | { 61 | DataId = "dataId", 62 | //Group = "DEFAULT_GROUP", 63 | //Tenant = "tenant", 64 | Callbacks = new List> 65 | { 66 | x =>{ Console.WriteLine(x); }, 67 | } 68 | }; 69 | 70 | await _configClient.AddListenerAsync(request); 71 | 72 | Assert.True(true); 73 | 74 | await Task.Delay(1000); 75 | 76 | var rRequest = new RemoveListenerRequest 77 | { 78 | DataId = "dataId", 79 | }; 80 | 81 | await _configClient.RemoveListenerAsync(rRequest); 82 | 83 | await Task.Delay(50000); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Nacos/Config/Http/HttpAgentCommon.cs: -------------------------------------------------------------------------------- 1 | using Nacos.Utilities; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Net.Http; 5 | 6 | namespace Nacos.Config.Http 7 | { 8 | public static class HttpAgentCommon 9 | { 10 | public static void BuildHeader(HttpRequestMessage requestMessage, Dictionary headers) 11 | { 12 | requestMessage.Headers.Clear(); 13 | 14 | if (headers != null) 15 | { 16 | foreach (var item in headers) 17 | { 18 | requestMessage.Headers.Add(item.Key, item.Value); 19 | } 20 | } 21 | 22 | requestMessage.Headers.TryAddWithoutValidation("Client-Version", ConstValue.ClientVersion); 23 | requestMessage.Headers.TryAddWithoutValidation("User-Agent", ConstValue.ClientVersion); 24 | requestMessage.Headers.TryAddWithoutValidation("RequestId", Guid.NewGuid().ToString()); 25 | requestMessage.Headers.TryAddWithoutValidation("Request-Module", ConstValue.RequestModule); 26 | } 27 | 28 | public static void BuildSpasHeaders(HttpRequestMessage requestMessage, Dictionary paramValues, string accessKey = "", string secretKey = "") 29 | { 30 | if (!string.IsNullOrWhiteSpace(accessKey) && !string.IsNullOrWhiteSpace(secretKey)) 31 | { 32 | requestMessage.Headers.TryAddWithoutValidation("Spas-AccessKey", accessKey); 33 | BuildSignHeaders(requestMessage, paramValues, secretKey); 34 | } 35 | } 36 | 37 | public static void BuildSignHeaders(HttpRequestMessage requestMessage, Dictionary paramValues, string secretKey) 38 | { 39 | var timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString(); 40 | requestMessage.Headers.TryAddWithoutValidation("timeStamp", timeStamp); 41 | 42 | var resource = paramValues.ContainsKey("tenant") && paramValues["tenant"].Length > 0 43 | ? string.Concat(paramValues["tenant"], "+", paramValues["group"]) 44 | : paramValues["group"]; 45 | 46 | var signature = string.IsNullOrWhiteSpace(resource) 47 | ? HashUtil.GetHMACSHA1(timeStamp, secretKey) 48 | : HashUtil.GetHMACSHA1($"{resource}+{timeStamp}", secretKey); 49 | 50 | requestMessage.Headers.TryAddWithoutValidation("Spas-Signature", signature); 51 | } 52 | 53 | public static string BuildQueryString(Dictionary paramValues) 54 | { 55 | var query = new System.Text.StringBuilder(1024); 56 | 57 | foreach (var item in paramValues) 58 | { 59 | query.Append($"{item.Key}={item.Value}&"); 60 | } 61 | 62 | return query.ToString().TrimEnd('&'); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Nacos/Common/ConstValue.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | public static class ConstValue 4 | { 5 | /// 6 | /// default httpclient name 7 | /// 8 | public const string ClientName = "NacosClient"; 9 | 10 | /// 11 | /// nacos csharp client version 12 | /// 13 | public const string ClientVersion = "Nacos-CSharp-Client-v0.2.7"; 14 | 15 | /// 16 | /// nacos request module 17 | /// 18 | public const string RequestModule = "Naming"; 19 | 20 | /// 21 | /// default group 22 | /// 23 | public const string DefaultGroup = "DEFAULT_GROUP"; 24 | 25 | /// 26 | /// default long pulling timeout 27 | /// 28 | public const int LongPullingTimeout = 30; 29 | 30 | /// 31 | /// invalid param 32 | /// 33 | public const int CLIENT_INVALID_PARAM = -400; 34 | 35 | /// 36 | /// over client threshold 37 | /// 38 | public const int CLIENT_OVER_THRESHOLD = -503; 39 | 40 | /// 41 | /// invalid param 42 | /// 43 | public const int INVALID_PARAM = 400; 44 | 45 | /// 46 | /// no right 47 | /// 48 | public const int NO_RIGHT = 403; 49 | 50 | /// 51 | /// not found 52 | /// 53 | public const int NOT_FOUND = 404; 54 | 55 | /// 56 | /// conflict 57 | /// 58 | public const int CONFLICT = 409; 59 | 60 | /// 61 | /// conflict 62 | /// 63 | public const int SERVER_ERROR = 500; 64 | 65 | /// 66 | /// bad gateway 67 | /// 68 | public const int BAD_GATEWAY = 502; 69 | 70 | /// 71 | /// over threshold 72 | /// 73 | public const int OVER_THRESHOLD = 503; 74 | 75 | public static string ENV_LIST_KEY = "envList"; 76 | 77 | public static string ALL_IPS = "000--00-ALL_IPS--00--000"; 78 | 79 | public static string FAILOVER_SWITCH = "00-00---000-VIPSRV_FAILOVER_SWITCH-000---00-00"; 80 | 81 | public static string DEFAULT_NAMESPACE_ID = "public"; 82 | 83 | public static int REQUEST_DOMAIN_RETRY_COUNT = 3; 84 | 85 | public static string SERVER_ADDR_IP_SPLITER = ":"; 86 | 87 | public static string HTTP = "http://"; 88 | 89 | public static string HTTPS = "https://"; 90 | 91 | public static string HTTP_PREFIX = "http"; 92 | 93 | public static string ACCESS_TOKEN = "accessToken"; 94 | 95 | public static string TOKEN_TTL = "tokenTtl"; 96 | 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/ModifyInstanceRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class ModifyInstanceRequest : BaseRequest 7 | { 8 | /// 9 | /// IP of instance 10 | /// 11 | public string Ip { get; set; } 12 | 13 | /// 14 | /// Port of instance 15 | /// 16 | public int Port { get; set; } 17 | 18 | /// 19 | /// Service name 20 | /// 21 | public string ServiceName { get; set; } 22 | 23 | /// 24 | /// ID of namespace 25 | /// 26 | public string NamespaceId { get; set; } 27 | 28 | /// 29 | /// Weight 30 | /// 31 | public double? Weight { get; set; } 32 | 33 | /// 34 | /// Extended information, a JSON string 35 | /// 36 | public string Metadata { get; set; } 37 | 38 | /// 39 | /// Cluster name 40 | /// 41 | public string ClusterName { get; set; } 42 | 43 | /// 44 | /// group name 45 | /// 46 | public string GroupName { get; set; } 47 | 48 | /// 49 | /// if instance is ephemeral 50 | /// 51 | public bool? Ephemeral { get; set; } 52 | 53 | /// 54 | /// If enabled 55 | /// 56 | /// The enabled. 57 | public bool? Enabled { get; set; } 58 | 59 | public override void CheckParam() 60 | { 61 | ParamUtil.CheckInstanceInfo(Ip, Port, ServiceName); 62 | } 63 | 64 | public override Dictionary ToDict() 65 | { 66 | var dict = new Dictionary 67 | { 68 | { "serviceName", ServiceName }, 69 | { "ip", Ip }, 70 | { "port", Port.ToString() }, 71 | }; 72 | 73 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 74 | dict.Add("namespaceId", NamespaceId); 75 | 76 | if (!string.IsNullOrWhiteSpace(GroupName)) 77 | dict.Add("groupName", GroupName); 78 | 79 | if (!string.IsNullOrWhiteSpace(ClusterName)) 80 | dict.Add("clusterName", ClusterName); 81 | 82 | if (!string.IsNullOrWhiteSpace(Metadata)) 83 | dict.Add("metadata", Metadata); 84 | 85 | if (Ephemeral.HasValue) 86 | dict.Add("ephemeral", Ephemeral.ToString()); 87 | 88 | if (Enabled.HasValue) 89 | dict.Add("enabled", Enabled.Value.ToString()); 90 | 91 | if (Weight.HasValue) 92 | dict.Add("weight", Weight.Value.ToString()); 93 | 94 | return dict; 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/Nacos/Naming/Requests/RegisterInstanceRequest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using Nacos.Utilities; 4 | using System.Collections.Generic; 5 | 6 | public class RegisterInstanceRequest : BaseRequest 7 | { 8 | /// 9 | /// IP of instance 10 | /// 11 | public string Ip { get; set; } 12 | 13 | /// 14 | /// Port of instance 15 | /// 16 | public int Port { get; set; } 17 | 18 | /// 19 | /// service name 20 | /// 21 | public string ServiceName { get; set; } 22 | 23 | /// 24 | /// ID of namespace 25 | /// 26 | public string NamespaceId { get; set; } 27 | 28 | /// 29 | /// Weight 30 | /// 31 | public double? Weight { get; set; } 32 | 33 | /// 34 | /// enabled or not 35 | /// 36 | public bool? Enable { get; set; } 37 | 38 | /// 39 | /// healthy or not 40 | /// 41 | public bool? Healthy { get; set; } 42 | 43 | /// 44 | /// extended information 45 | /// 46 | public string Metadata { get; set; } 47 | 48 | /// 49 | /// cluster name 50 | /// 51 | public string ClusterName { get; set; } 52 | 53 | /// 54 | /// group name 55 | /// 56 | public string GroupName { get; set; } 57 | 58 | /// 59 | /// if instance is ephemeral 60 | /// 61 | public bool? Ephemeral { get; set; } 62 | 63 | public override void CheckParam() 64 | { 65 | ParamUtil.CheckInstanceInfo(Ip, Port, ServiceName); 66 | } 67 | 68 | public override Dictionary ToDict() 69 | { 70 | var dict = new Dictionary 71 | { 72 | { "serviceName", ServiceName }, 73 | { "ip", Ip }, 74 | { "port", Port.ToString() }, 75 | }; 76 | 77 | if (!string.IsNullOrWhiteSpace(NamespaceId)) 78 | dict.Add("namespaceId", NamespaceId); 79 | 80 | if (!string.IsNullOrWhiteSpace(GroupName)) 81 | dict.Add("groupName", GroupName); 82 | 83 | if (!string.IsNullOrWhiteSpace(Metadata)) 84 | dict.Add("metadata", Metadata); 85 | 86 | if (Weight.HasValue) 87 | dict.Add("weight", Weight.Value.ToString()); 88 | 89 | if (!string.IsNullOrWhiteSpace(ClusterName)) 90 | dict.Add("clusterName", ClusterName); 91 | 92 | if (Ephemeral.HasValue) 93 | dict.Add("ephemeral", Ephemeral.Value.ToString()); 94 | 95 | if (Healthy.HasValue) 96 | dict.Add("healthy", Healthy.Value.ToString()); 97 | 98 | return dict; 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/ServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace Microsoft.AspNetCore.Builder 2 | { 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Extensions; 6 | using Nacos; 7 | using Nacos.AspNetCore; 8 | using System; 9 | 10 | public static class ServiceCollectionExtensions 11 | { 12 | /// 13 | /// Add Nacos AspNetCore. 14 | /// 15 | /// services. 16 | /// configuration 17 | /// 18 | public static IServiceCollection AddNacosAspNetCore( 19 | this IServiceCollection services, 20 | IConfiguration configuration) 21 | { 22 | services.Configure(configuration.GetSection("nacos")); 23 | 24 | //services.AddNacos(configuration); 25 | services.AddNacosNaming(configuration); 26 | 27 | services.AddEasyCaching(options => 28 | { 29 | options.UseInMemory("nacos.aspnetcore"); 30 | }); 31 | 32 | services.AddSingleton(); 33 | 34 | // load balance strategies 35 | services.AddSingleton(); 36 | services.AddSingleton(); 37 | 38 | // IHostedService, report instance status 39 | services.AddHostedService(); 40 | 41 | return services; 42 | } 43 | 44 | /// 45 | /// Add Nacos AspNetCore. 46 | /// 47 | /// services. 48 | /// nacosAspNetCoreOptions. 49 | /// nacosOptions 50 | /// 51 | public static IServiceCollection AddNacosAspNetCore( 52 | this IServiceCollection services, 53 | Action nacosAspNetCoreOptions, 54 | Action nacosOptions 55 | ) 56 | { 57 | services.Configure(nacosAspNetCoreOptions); 58 | services.AddNacosNaming(nacosOptions); 59 | 60 | services.AddEasyCaching(options => 61 | { 62 | options.UseInMemory("nacos.aspnetcore"); 63 | }); 64 | 65 | services.AddSingleton(); 66 | 67 | // load balance strategies 68 | services.AddSingleton(); 69 | services.AddSingleton(); 70 | 71 | services.AddSingleton(); 72 | services.TryAddSingleton(); 73 | services.AddSingleton(); 74 | 75 | // IHostedService, report instance status 76 | services.AddHostedService(); 77 | 78 | return services; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /samples/MsConfigApp/Program.cs: -------------------------------------------------------------------------------- 1 | namespace MsConfigApp 2 | { 3 | using System.Collections.Generic; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.Hosting; 7 | using Serilog; 8 | using Serilog.Events; 9 | 10 | public class Program 11 | { 12 | public static void Main(string[] args) 13 | { 14 | var outputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}"; 15 | 16 | Log.Logger = new LoggerConfiguration() 17 | .Enrich.FromLogContext() 18 | .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) 19 | .MinimumLevel.Override("System", LogEventLevel.Warning) 20 | .MinimumLevel.Debug() 21 | .WriteTo.Console( 22 | outputTemplate: outputTemplate) 23 | /*.WriteTo.File( 24 | path: "logs/ApiTpl.log", 25 | outputTemplate: outputTemplate, 26 | rollingInterval: RollingInterval.Day, 27 | retainedFileCountLimit: 5, 28 | encoding: System.Text.Encoding.UTF8)*/ 29 | .CreateLogger(); 30 | 31 | System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); 32 | 33 | try 34 | { 35 | Log.ForContext().Information("Application starting..."); 36 | CreateHostBuilder(args).Build().Run(); 37 | } 38 | catch (System.Exception ex) 39 | { 40 | Log.ForContext().Fatal(ex, "Application start-up failed!!"); 41 | } 42 | finally 43 | { 44 | Log.CloseAndFlush(); 45 | } 46 | } 47 | 48 | public static IHostBuilder CreateHostBuilder(string[] args) => 49 | Host.CreateDefaultBuilder(args) 50 | .ConfigureAppConfiguration((context, builder) => 51 | { 52 | var c = builder.Build(); 53 | 54 | var dataId = c.GetValue("NacosConfig:DataId"); 55 | var group = c.GetValue("NacosConfig:Group"); 56 | var tenant = c.GetValue("NacosConfig:Tenant"); 57 | var optional = c.GetValue("NacosConfig:Optional"); 58 | var serverAddresses = c.GetSection("NacosConfig:ServerAddresses").Get>(); 59 | 60 | // read configuration from config files 61 | builder.AddNacosConfiguration(c.GetSection("NacosConfig")); 62 | 63 | //// hard code here 64 | //builder.AddNacosConfiguration(x => 65 | //{ 66 | // x.DataId = dataId; 67 | // x.Group = group; 68 | // x.Tenant = tenant; 69 | // x.Optional = optional; 70 | // x.ServerAddresses = serverAddresses; 71 | //}); 72 | }) 73 | .ConfigureWebHostDefaults(webBuilder => 74 | { 75 | webBuilder.UseStartup(); 76 | }) 77 | .UseSerilog(); 78 | } 79 | } -------------------------------------------------------------------------------- /src/Nacos.Microsoft.Extensions.Configuration/NacosConfigurationProvider.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Microsoft.Extensions.Configuration 2 | { 3 | using global::Microsoft.Extensions.Configuration; 4 | using global::Microsoft.Extensions.Logging.Abstractions; 5 | using System; 6 | 7 | internal class NacosConfigurationProvider : ConfigurationProvider 8 | { 9 | private readonly NacosConfigurationSource _configurationSource; 10 | 11 | private readonly INacosConfigurationParser _parser; 12 | 13 | private readonly INacosConfigClient _client; 14 | 15 | public NacosConfigurationProvider(NacosConfigurationSource configurationSource) 16 | { 17 | _configurationSource = configurationSource; 18 | 19 | _parser = configurationSource.NacosConfigurationParser ?? JsonConfigurationStringParser.Instance; 20 | 21 | _client = new NacosMsConfigClient(NullLoggerFactory.Instance, new NacosOptions 22 | { 23 | ServerAddresses = configurationSource.ServerAddresses, 24 | Namespace = configurationSource.Tenant, 25 | AccessKey = configurationSource.AccessKey, 26 | ClusterName = configurationSource.ClusterName, 27 | ContextPath = configurationSource.ContextPath, 28 | EndPoint = configurationSource.EndPoint, 29 | DefaultTimeOut = configurationSource.DefaultTimeOut, 30 | SecretKey = configurationSource.SecretKey, 31 | Password = configurationSource.Password, 32 | UserName = configurationSource.UserName, 33 | ListenInterval = 5000 34 | }) ; 35 | 36 | _client.AddListenerAsync(new AddListenerRequest 37 | { 38 | DataId = _configurationSource.DataId, 39 | Group = _configurationSource.Group, 40 | Tenant = _configurationSource.Tenant, 41 | Callbacks = new System.Collections.Generic.List> 42 | { 43 | x => CallBackReload(x) 44 | } 45 | }); 46 | } 47 | 48 | private void CallBackReload(string val) 49 | { 50 | try 51 | { 52 | var data = _parser.Parse(val); 53 | 54 | Data = data; 55 | 56 | OnReload(); 57 | } 58 | catch 59 | { 60 | if (!_configurationSource.Optional) 61 | { 62 | throw; 63 | } 64 | } 65 | } 66 | 67 | public override void Load() 68 | { 69 | try 70 | { 71 | var config = _client.GetConfigAsync(new GetConfigRequest 72 | { 73 | DataId = _configurationSource.DataId, 74 | Group = _configurationSource.Group, 75 | Tenant = _configurationSource.Tenant 76 | }).ConfigureAwait(false).GetAwaiter().GetResult(); 77 | 78 | var data = _parser.Parse(config); 79 | 80 | Data = data; 81 | } 82 | catch 83 | { 84 | if (!_configurationSource.Optional) 85 | { 86 | throw; 87 | } 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/Nacos/Utilities/HttpClientFactoryUtil.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Utilities 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Net.Http; 6 | using System.Threading.Tasks; 7 | 8 | public static class HttpClientFactoryUtil 9 | { 10 | public static async Task DoRequestAsync(this IHttpClientFactory factory, HttpMethod method, string url, string param = "", int timeOut = 8, Dictionary headers = null, string secretKey = "") 11 | { 12 | var client = factory.CreateClient(ConstValue.ClientName); 13 | client.Timeout = TimeSpan.FromSeconds(timeOut); 14 | 15 | var requestUrl = string.IsNullOrWhiteSpace(param) ? url : $"{url}?{param}"; 16 | 17 | var requestMessage = new HttpRequestMessage(method, requestUrl); 18 | 19 | BuildHeader(requestMessage, headers); 20 | 21 | if(!string.IsNullOrWhiteSpace(secretKey)) BuildSignHeader(requestMessage, param, secretKey); 22 | 23 | var responseMessage = await client.SendAsync(requestMessage); 24 | return responseMessage; 25 | } 26 | 27 | public static void BuildHeader(HttpRequestMessage requestMessage, Dictionary headers) 28 | { 29 | requestMessage.Headers.Clear(); 30 | 31 | if (headers != null) 32 | { 33 | foreach (var item in headers) 34 | { 35 | requestMessage.Headers.Add(item.Key, item.Value); 36 | } 37 | } 38 | 39 | requestMessage.Headers.TryAddWithoutValidation("Client-Version", ConstValue.ClientVersion); 40 | requestMessage.Headers.TryAddWithoutValidation("User-Agent", ConstValue.ClientVersion); 41 | requestMessage.Headers.TryAddWithoutValidation("RequestId", Guid.NewGuid().ToString()); 42 | requestMessage.Headers.TryAddWithoutValidation("Request-Module", ConstValue.RequestModule); 43 | requestMessage.Headers.TryAddWithoutValidation("exConfigInfo", "true"); 44 | } 45 | 46 | public static void BuildSignHeader(HttpRequestMessage requestMessage, string param, string secretKey) 47 | { 48 | if (string.IsNullOrWhiteSpace(param)) return; 49 | 50 | var timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString(); 51 | requestMessage.Headers.TryAddWithoutValidation("timeStamp", timeStamp); 52 | 53 | var dict = GetDictFromParam(param); 54 | 55 | var resource = dict.ContainsKey("tenant") && dict["tenant"].Length > 0 56 | ? string.Concat(dict["tenant"], "+", dict["group"]) 57 | : dict["group"]; 58 | 59 | var signature = string.IsNullOrWhiteSpace(resource) 60 | ? HashUtil.GetHMACSHA1(timeStamp, secretKey) 61 | : HashUtil.GetHMACSHA1($"{resource}+{timeStamp}", secretKey); 62 | 63 | requestMessage.Headers.TryAddWithoutValidation("Spas-Signature", signature); 64 | } 65 | 66 | private static Dictionary GetDictFromParam(string param) 67 | { 68 | var dict = new Dictionary(); 69 | var arr = param.Split('&'); 70 | foreach (var item in arr) 71 | { 72 | var kv = item.Split('='); 73 | dict.Add(kv[0], kv[1]); 74 | } 75 | return dict; 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tests/Nacos.Tests/InstanceTest.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Tests 2 | { 3 | using System.Threading.Tasks; 4 | using Xunit; 5 | 6 | public class InstanceTest : TestBase 7 | { 8 | [Fact] 9 | public async Task RegisterInstance_Should_Succeed() 10 | { 11 | var request = new RegisterInstanceRequest 12 | { 13 | ServiceName = "testservice", 14 | Ip = "192.168.0.74", 15 | Port = 9999 16 | }; 17 | 18 | var res = await _namingClient.RegisterInstanceAsync(request); 19 | Assert.True(res); 20 | } 21 | 22 | [Fact] 23 | public async Task RemoveInstance_Should_Succeed() 24 | { 25 | var request = new RemoveInstanceRequest 26 | { 27 | ServiceName = "testservice", 28 | Ip = "192.168.0.74", 29 | Port = 9999 30 | }; 31 | 32 | var res = await _namingClient.RemoveInstanceAsync(request); 33 | Assert.True(res); 34 | } 35 | 36 | [Fact] 37 | public async Task ModifyInstance_Should_Succeed() 38 | { 39 | var request = new ModifyInstanceRequest 40 | { 41 | ServiceName = "testservice", 42 | Ip = "192.168.0.74", 43 | Port = 5000 44 | }; 45 | 46 | var res = await _namingClient.ModifyInstanceAsync(request); 47 | Assert.True(res); 48 | } 49 | 50 | [Fact] 51 | public async Task ListInstances_Should_Succeed() 52 | { 53 | var request = new ListInstancesRequest 54 | { 55 | ServiceName = "testservice", 56 | }; 57 | 58 | var res = await _namingClient.ListInstancesAsync(request); 59 | Assert.NotNull(res); 60 | } 61 | 62 | [Fact] 63 | public async Task GetInstance_Should_Succeed() 64 | { 65 | var request = new GetInstanceRequest 66 | { 67 | ServiceName = "testservice", 68 | Ip = "192.168.0.74", 69 | Port = 9999, 70 | }; 71 | 72 | var res = await _namingClient.GetInstanceAsync(request); 73 | Assert.NotNull(res); 74 | } 75 | 76 | [Fact] 77 | public async Task SendHeartbeat_Should_Succeed() 78 | { 79 | var request = new SendHeartbeatRequest 80 | { 81 | ServiceName = "testservice", 82 | BeatInfo = new BeatInfo 83 | { 84 | serviceName = "testservice", 85 | ip = "192.168.0.74", 86 | port = 9999, 87 | } 88 | }; 89 | 90 | var res = await _namingClient.SendHeartbeatAsync(request); 91 | Assert.True(res); 92 | } 93 | 94 | [Fact] 95 | public async Task ModifyInstanceHealthStatus_Should_Succeed() 96 | { 97 | var request = new ModifyInstanceHealthStatusRequest 98 | { 99 | Ip = "192.168.0.74", 100 | Port = 9999, 101 | ServiceName = "testservice", 102 | Healthy = false, 103 | }; 104 | 105 | var res = await _namingClient.ModifyInstanceHealthStatusAsync(request); 106 | // 集群配置了健康检查时,该接口会返回错误 107 | Assert.False(res); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/Nacos/Utilities/ParamUtil.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Utilities 2 | { 3 | using Nacos.Exceptions; 4 | 5 | public static class ParamUtil 6 | { 7 | private static char[] validChars = new char[] { '_', '-', '.', ':' }; 8 | 9 | public static bool IsValid(string param) 10 | { 11 | if (string.IsNullOrWhiteSpace(param)) 12 | { 13 | return false; 14 | } 15 | 16 | for (int i = 0; i < param.Length; i++) 17 | { 18 | char ch = param[i]; 19 | if (char.IsLetterOrDigit(ch)) 20 | { 21 | continue; 22 | } 23 | else if (IsValidChar(ch)) 24 | { 25 | continue; 26 | } 27 | else 28 | { 29 | return false; 30 | } 31 | } 32 | return true; 33 | } 34 | 35 | private static bool IsValidChar(char ch) 36 | { 37 | foreach (var c in validChars) 38 | { 39 | if (c == ch) return true; 40 | } 41 | 42 | return false; 43 | } 44 | 45 | public static void CheckKeyParam(string dataId, string group) 46 | { 47 | if (string.IsNullOrWhiteSpace(dataId) || !IsValid(dataId)) 48 | { 49 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "dataId invalid"); 50 | } 51 | 52 | if (string.IsNullOrWhiteSpace(group) || !IsValid(group)) 53 | { 54 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "group invalid"); 55 | } 56 | } 57 | 58 | public static void CheckTDG(string tenant, string dataId, string group) 59 | { 60 | if (string.IsNullOrWhiteSpace(dataId) || !IsValid(dataId)) 61 | { 62 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "dataId invalid"); 63 | } 64 | 65 | if (string.IsNullOrWhiteSpace(group) || !IsValid(group)) 66 | { 67 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "group invalid"); 68 | } 69 | } 70 | 71 | public static void CheckTenant(string tenant) 72 | { 73 | if (string.IsNullOrWhiteSpace(tenant) || !IsValid(tenant)) 74 | { 75 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "tenant invalid"); 76 | } 77 | } 78 | 79 | public static void CheckParam(string dataId, string group, string content) 80 | { 81 | CheckKeyParam(dataId, group); 82 | if (string.IsNullOrWhiteSpace(content)) 83 | { 84 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "content invalid"); 85 | } 86 | } 87 | 88 | public static void CheckIpAndPort(string ip, int port) 89 | { 90 | if (string.IsNullOrWhiteSpace(ip)) 91 | { 92 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "ip invalid"); 93 | } 94 | 95 | if (port <= 0 || port > 65535) 96 | { 97 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "port invalid"); 98 | } 99 | } 100 | 101 | public static void CheckServiceName(string serviceName) 102 | { 103 | if (string.IsNullOrWhiteSpace(serviceName)) 104 | { 105 | throw new NacosException(ConstValue.CLIENT_INVALID_PARAM, "serviceName invalid"); 106 | } 107 | } 108 | 109 | public static void CheckInstanceInfo(string ip, int port, string serviceName) 110 | { 111 | CheckIpAndPort(ip, port); 112 | 113 | CheckServiceName(serviceName); 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/Nacos/Config/Failover/FileLocalConfigInfoProcessor.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.IO; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | public class FileLocalConfigInfoProcessor : ILocalConfigInfoProcessor 8 | { 9 | private readonly string FAILOVER_BASE = Path.Combine(Directory.GetCurrentDirectory(), "nacos-data", "data"); 10 | private readonly string SNAPSHOT_BASE = Path.Combine(Directory.GetCurrentDirectory(), "nacos-data", "snapshot"); 11 | 12 | public async Task GetFailoverAsync(string dataId, string group, string tenant) 13 | { 14 | string failoverFile; 15 | if (!string.IsNullOrEmpty(tenant)) 16 | { 17 | failoverFile = Path.Combine(SNAPSHOT_BASE, "config-data-tenant", tenant, group); 18 | } 19 | else 20 | { 21 | failoverFile = Path.Combine(SNAPSHOT_BASE, "config-data", group); 22 | } 23 | var file = new FileInfo(failoverFile + dataId); 24 | 25 | if (!file.Exists) 26 | { 27 | return null; 28 | } 29 | 30 | using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 31 | { 32 | byte[] readByte = new byte[fs.Length]; 33 | await fs.ReadAsync(readByte, 0, readByte.Length); 34 | string readStr = Encoding.UTF8.GetString(readByte); 35 | fs.Close(); 36 | return readStr; 37 | } 38 | } 39 | 40 | public async Task GetSnapshotAync(string dataId, string group, string tenant) 41 | { 42 | FileInfo file = GetSnapshotFile(dataId, group, tenant); 43 | 44 | if (!file.Exists) 45 | { 46 | return null; 47 | } 48 | 49 | using (FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) 50 | { 51 | byte[] readByte = new byte[fs.Length]; 52 | await fs.ReadAsync(readByte, 0, readByte.Length); 53 | string readStr = Encoding.UTF8.GetString(readByte); 54 | fs.Close(); 55 | return readStr; 56 | } 57 | } 58 | 59 | private FileInfo GetSnapshotFile(string dataId, string group, string tenant) 60 | { 61 | string snapshotFile; 62 | if (!string.IsNullOrEmpty(tenant)) 63 | { 64 | snapshotFile = Path.Combine(SNAPSHOT_BASE, "snapshot-tenant", tenant, group); 65 | } 66 | else 67 | { 68 | snapshotFile = Path.Combine(SNAPSHOT_BASE, "snapshot", group); 69 | } 70 | var file = new FileInfo(snapshotFile + dataId); 71 | return file; 72 | } 73 | 74 | public async Task SaveSnapshotAsync(string dataId, string group, string tenant, string config) 75 | { 76 | FileInfo snapshotFile = GetSnapshotFile(dataId, group, tenant); 77 | if (string.IsNullOrEmpty(config)) 78 | { 79 | if (snapshotFile.Exists) 80 | { 81 | snapshotFile.Delete(); 82 | } 83 | } 84 | else 85 | { 86 | if (snapshotFile.Directory != null && !snapshotFile.Directory.Exists) 87 | { 88 | snapshotFile.Directory.Create(); 89 | } 90 | 91 | using (FileStream fs = new FileStream(snapshotFile.FullName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) 92 | { 93 | byte[] bytes = Encoding.UTF8.GetBytes(config); 94 | await fs.WriteAsync(bytes, 0, bytes.Length); 95 | fs.Close(); 96 | } 97 | } 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/Nacos.Microsoft.Extensions.Configuration/JsonConfigurationParser.cs: -------------------------------------------------------------------------------- 1 | [assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Nacos.Microsoft.Extensions.Configuration.Tests")] 2 | 3 | namespace Nacos.Microsoft.Extensions.Configuration 4 | { 5 | using global::Microsoft.Extensions.Configuration; 6 | using Newtonsoft.Json; 7 | using Newtonsoft.Json.Linq; 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Globalization; 11 | using System.IO; 12 | using System.Linq; 13 | 14 | internal class JsonConfigurationStringParser : INacosConfigurationParser 15 | { 16 | private JsonConfigurationStringParser() { } 17 | 18 | internal static JsonConfigurationStringParser Instance = new JsonConfigurationStringParser(); 19 | 20 | private readonly IDictionary _data = new SortedDictionary(StringComparer.OrdinalIgnoreCase); 21 | private readonly Stack _context = new Stack(); 22 | private string _currentPath; 23 | 24 | private JsonTextReader _reader; 25 | 26 | public IDictionary Parse(string input) 27 | => new JsonConfigurationStringParser().ParseString(input); 28 | 29 | private IDictionary ParseString(string input) 30 | { 31 | _data.Clear(); 32 | _reader = new JsonTextReader(new StringReader(input)) 33 | { 34 | DateParseHandling = DateParseHandling.None 35 | }; 36 | 37 | var jsonConfig = JObject.Load(_reader); 38 | 39 | VisitJObject(jsonConfig); 40 | 41 | return _data; 42 | } 43 | 44 | private void VisitJObject(JObject jObject) 45 | { 46 | foreach (var property in jObject.Properties()) 47 | { 48 | EnterContext(property.Name); 49 | VisitProperty(property); 50 | ExitContext(); 51 | } 52 | } 53 | 54 | private void VisitProperty(JProperty property) 55 | { 56 | VisitToken(property.Value); 57 | } 58 | 59 | private void VisitToken(JToken token) 60 | { 61 | switch (token.Type) 62 | { 63 | case JTokenType.Object: 64 | VisitJObject(token.Value()); 65 | break; 66 | 67 | case JTokenType.Array: 68 | VisitArray(token.Value()); 69 | break; 70 | 71 | case JTokenType.Integer: 72 | case JTokenType.Float: 73 | case JTokenType.String: 74 | case JTokenType.Boolean: 75 | case JTokenType.Bytes: 76 | case JTokenType.Raw: 77 | case JTokenType.Null: 78 | VisitPrimitive(token.Value()); 79 | break; 80 | 81 | default: 82 | throw new FormatException( 83 | $"Unsupported JSON token '{ _reader.TokenType}' was found. Path '{_reader.Path}', line { _reader.LineNumber} position {_reader.LinePosition}."); 84 | } 85 | } 86 | 87 | private void VisitArray(JArray array) 88 | { 89 | for (int index = 0; index < array.Count; index++) 90 | { 91 | EnterContext(index.ToString()); 92 | VisitToken(array[index]); 93 | ExitContext(); 94 | } 95 | } 96 | 97 | private void VisitPrimitive(JValue data) 98 | { 99 | var key = _currentPath; 100 | 101 | if (_data.ContainsKey(key)) 102 | { 103 | throw new FormatException($"A duplicate key '{key}' was found."); 104 | } 105 | _data[key] = data.ToString(CultureInfo.InvariantCulture); 106 | } 107 | 108 | private void EnterContext(string context) 109 | { 110 | _context.Push(context); 111 | _currentPath = ConfigurationPath.Combine(_context.Reverse()); 112 | } 113 | 114 | private void ExitContext() 115 | { 116 | _context.Pop(); 117 | _currentPath = ConfigurationPath.Combine(_context.Reverse()); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /README.zh-cn.md: -------------------------------------------------------------------------------- 1 | # nacos-sdk-csharp             [English](./README.md) 2 | 3 | 基于C#(dotnet core)实现 [nacos](https://nacos.io/) OpenAPI 的非官方版本 4 | 5 | ![](https://img.shields.io/nuget/v/nacos-sdk-csharp-unofficial.svg) 6 | 7 | ![](./media/prj.png) 8 | 9 | ## CI构建状态 10 | 11 | | Platform | Build Server | Master Status | 12 | |--------- |------------- |---------| 13 | | Github Action | Linux/OSX |![nacos-sdk-csharp CI](https://github.com/catcherwong/nacos-sdk-csharp/workflows/nacos-sdk-csharp%20CI/badge.svg) 14 | | 15 | 16 | ## 安装Nuget包 17 | 18 | ```bash 19 | dotnet add package nacos-sdk-csharp-unofficial 20 | ``` 21 | 22 | ## 功能特性 23 | 24 | - 基本的Open Api接口封装 25 | - 集成ASP.NET Core的配置系统 26 | - 简易ASP.NET Core的服务注册和发现 27 | - 和阿里云应用配置管理(Application Configuration Management,简称 ACM)集成使用 28 | - ... 29 | 30 | ## 简易用法 31 | 32 | ### 配置 33 | 34 | 1. 在 `Program.cs` 进行如下配置 35 | 36 | ```cs 37 | public static IHostBuilder CreateHostBuilder(string[] args) => 38 | Host.CreateDefaultBuilder(args) 39 | .ConfigureAppConfiguration((context, builder) => 40 | { 41 | var c = builder.Build(); 42 | 43 | // read configuration from config files 44 | builder.AddNacosConfiguration(c.GetSection("NacosConfig")); 45 | }) 46 | .ConfigureWebHostDefaults(webBuilder => 47 | { 48 | webBuilder.UseStartup(); 49 | }) 50 | ``` 51 | 52 | 2. 修改 `appsettings.json` 53 | 54 | ```JSON 55 | { 56 | "NacosConfig": { 57 | "Optional": false, 58 | "DataId": "msconfigapp", 59 | "Group": "", 60 | "Tenant": "f47e0ae1-982a-4a64-aea3-52506492a3d4", 61 | "ServerAddresses": [ "http://localhost:8848/" ], 62 | "UserName": "test2", 63 | "Password": "123456", 64 | "AccessKey": "", 65 | "SecretKey": "", 66 | "EndPoint": "acm.aliyun.com" 67 | } 68 | } 69 | ``` 70 | 71 | 3. 用原生的.NET Core方式来读取Nacos配置 72 | 73 | ```cs 74 | [ApiController] 75 | [Route("api/[controller]")] 76 | public class ConfigController : ControllerBase 77 | { 78 | private readonly IConfiguration _configuration; 79 | private readonly AppSettings _settings; 80 | private readonly AppSettings _sSettings; 81 | private readonly AppSettings _mSettings; 82 | 83 | public ConfigController( 84 | IConfiguration configuration, 85 | IOptions options, 86 | IOptionsSnapshot sOptions, 87 | IOptionsMonitor _mOptions 88 | ) 89 | { 90 | _logger = logger; 91 | _configuration = configuration; 92 | _settings = options.Value; 93 | _sSettings = sOptions.Value; 94 | _mSettings = _mOptions.CurrentValue; 95 | } 96 | 97 | [HttpGet] 98 | public string Get() 99 | { 100 | // .... 101 | 102 | return "ok"; 103 | } 104 | 105 | } 106 | ``` 107 | 108 | ### 服务注册和发现 109 | 110 | 1. 服务注册 111 | 112 | 在 `Program.cs` 中配置 113 | 114 | ```cs 115 | public class Startup 116 | { 117 | public Startup(IConfiguration configuration) 118 | { 119 | Configuration = configuration; 120 | } 121 | 122 | public IConfiguration Configuration { get; } 123 | 124 | public void ConfigureServices(IServiceCollection services) 125 | { 126 | // ... 127 | 128 | services.AddNacosAspNetCore(Configuration); 129 | } 130 | 131 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 132 | { 133 | // ... 134 | } 135 | } 136 | ``` 137 | 138 | 修改 `appsettings.json` 139 | 140 | ```JSON 141 | "nacos": { 142 | "ServerAddresses": [ "http://localhost:8848" ], 143 | "DefaultTimeOut": 15000, 144 | "Namespace": "", 145 | "ListenInterval": 1000, 146 | "ServiceName": "App1" 147 | } 148 | ``` 149 | 150 | 2. 服务发现 151 | 152 | ```cs 153 | [Route("api/[controller]")] 154 | [ApiController] 155 | public class ValuesController : ControllerBase 156 | { 157 | private readonly INacosServerManager _serverManager; 158 | 159 | public ValuesController(INacosServerManager serverManager) 160 | { 161 | _serverManager = serverManager; 162 | } 163 | 164 | [HttpGet("test")] 165 | public async Task Test() 166 | { 167 | // 这里需要知道被调用方的服务名 168 | // 目前获取服务地址是随机的方式,后续会加入更多负载算法. 169 | var baseUrl = await _serverManager.GetServerAsync("App2"); 170 | 171 | if(string.IsNullOrWhiteSpace(baseUrl)) 172 | { 173 | return "empty"; 174 | } 175 | 176 | var url = $"{baseUrl}/api/values"; 177 | 178 | using (HttpClient client = new HttpClient()) 179 | { 180 | var result = await client.GetAsync(url); 181 | return await result.Content.ReadAsStringAsync(); 182 | } 183 | } 184 | } 185 | ``` 186 | -------------------------------------------------------------------------------- /src/Nacos/Security/SecurityProxy.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Security 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Net.Http; 6 | using System.Threading.Tasks; 7 | 8 | public class SecurityProxy 9 | { 10 | private static string LOGIN_URL = "/v1/auth/users/login"; 11 | 12 | private string contextPath; 13 | 14 | /// 15 | /// User's name 16 | /// 17 | private string _username; 18 | 19 | /// 20 | /// User's password 21 | /// 22 | private string _password; 23 | 24 | /// 25 | /// A token to take with when sending request to Nacos server 26 | /// 27 | private string _accessToken; 28 | 29 | /// 30 | /// TTL of token in seconds 31 | /// 32 | private long _tokenTtl; 33 | 34 | /// 35 | /// Last timestamp refresh security info from server 36 | /// 37 | private long _lastRefreshTime; 38 | 39 | /// 40 | /// time window to refresh security info in seconds 41 | /// 42 | private long _tokenRefreshWindow; 43 | 44 | public SecurityProxy(NacosOptions options) 45 | { 46 | _username = options.UserName ?? ""; 47 | _password = options.Password ?? ""; 48 | contextPath = options.ContextPath; 49 | contextPath = contextPath.StartsWith("/") ? contextPath : "/" + contextPath; 50 | } 51 | 52 | 53 | public async Task LoginAsync(List servers) 54 | { 55 | try 56 | { 57 | if ((DateTimeOffset.Now.ToUnixTimeSeconds() - _lastRefreshTime) < _tokenTtl - _tokenRefreshWindow) 58 | { 59 | return true; 60 | } 61 | 62 | foreach (var server in servers) 63 | { 64 | var flag = await LoginAsync(server); 65 | if (flag) 66 | { 67 | _lastRefreshTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); 68 | return true; 69 | } 70 | } 71 | } 72 | catch 73 | { 74 | } 75 | 76 | return false; 77 | } 78 | 79 | public string GetAccessToken() 80 | { 81 | return _accessToken; 82 | } 83 | 84 | private async Task LoginAsync(string server) 85 | { 86 | if (!string.IsNullOrWhiteSpace(_username)) 87 | { 88 | var dict = new Dictionary 89 | { 90 | { "username", _username }, 91 | { "password", _password } 92 | }; 93 | 94 | var url = $"http://{server}{contextPath}{LOGIN_URL}"; 95 | if (server.Contains(ConstValue.HTTP_PREFIX)) 96 | { 97 | url = $"{server}{contextPath}{LOGIN_URL}"; 98 | } 99 | 100 | try 101 | { 102 | using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) 103 | { 104 | client.Timeout = TimeSpan.FromMilliseconds(5000); 105 | 106 | var req = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, url) 107 | { 108 | Content = new FormUrlEncodedContent(dict) 109 | }; 110 | 111 | var resp = await client.SendAsync(req); 112 | 113 | if (!resp.IsSuccessStatusCode) 114 | { 115 | return false; 116 | } 117 | 118 | var content = await resp.Content.ReadAsStringAsync(); 119 | 120 | var obj = Newtonsoft.Json.Linq.JObject.Parse(content); 121 | 122 | if (obj.ContainsKey(ConstValue.ACCESS_TOKEN)) 123 | { 124 | _accessToken = obj.Value(ConstValue.ACCESS_TOKEN); 125 | _tokenTtl = obj.Value(ConstValue.TOKEN_TTL); 126 | _tokenRefreshWindow = _tokenTtl / 10; 127 | } 128 | } 129 | } 130 | catch 131 | { 132 | return false; 133 | } 134 | } 135 | 136 | return true; 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/Nacos/INacosNamingClient.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos 2 | { 3 | using System.Threading.Tasks; 4 | 5 | public interface INacosNamingClient 6 | { 7 | #region Instance 8 | /// 9 | /// Register an instance to service 10 | /// 11 | /// request 12 | /// 13 | Task RegisterInstanceAsync(RegisterInstanceRequest request); 14 | 15 | /// 16 | /// Delete instance from service 17 | /// 18 | /// request 19 | /// 20 | Task RemoveInstanceAsync(RemoveInstanceRequest request); 21 | 22 | /// 23 | /// Modify an instance of service 24 | /// 25 | /// request 26 | /// 27 | Task ModifyInstanceAsync(ModifyInstanceRequest request); 28 | 29 | /// 30 | /// Query instance list of service 31 | /// 32 | /// request 33 | /// 34 | Task ListInstancesAsync(ListInstancesRequest request); 35 | 36 | /// 37 | /// Query instance details of service 38 | /// 39 | /// request 40 | /// 41 | Task GetInstanceAsync(GetInstanceRequest request); 42 | 43 | /// 44 | /// Send instance beat 45 | /// 46 | /// request 47 | /// 48 | Task SendHeartbeatAsync(SendHeartbeatRequest request); 49 | 50 | /// 51 | /// Update instance health status, only works when the cluster health checker is set to NONE 52 | /// 53 | /// request 54 | /// 55 | Task ModifyInstanceHealthStatusAsync(ModifyInstanceHealthStatusRequest request); 56 | #endregion 57 | 58 | #region Service 59 | /// 60 | /// Create service 61 | /// 62 | /// request 63 | /// 64 | Task CreateServiceAsync(CreateServiceRequest request); 65 | 66 | /// 67 | /// Delete a service, only permitted when instance count is 0 68 | /// 69 | /// request 70 | /// 71 | Task RemoveServiceAsync(RemoveServiceRequest request); 72 | 73 | /// 74 | /// Update a service 75 | /// 76 | /// request 77 | /// 78 | Task ModifyServiceAsync(ModifyServiceRequest request); 79 | 80 | /// 81 | /// Query a service 82 | /// 83 | /// request 84 | /// 85 | Task GetServiceAsync(GetServiceRequest request); 86 | 87 | /// 88 | /// Query service list 89 | /// 90 | /// request 91 | /// 92 | Task ListServicesAsync(ListServicesRequest request); 93 | #endregion 94 | 95 | #region Switches 96 | /// 97 | /// Query system switches 98 | /// 99 | /// 100 | Task GetSwitchesAsync(); 101 | 102 | /// 103 | /// Update system switch 104 | /// 105 | /// request 106 | /// 107 | Task ModifySwitchesAsync(ModifySwitchesRequest request); 108 | #endregion 109 | 110 | #region Cluster 111 | /// 112 | /// Query server list 113 | /// 114 | /// request 115 | /// 116 | Task ListClusterServersAsync(ListClusterServersRequest request); 117 | 118 | /// 119 | /// Query the leader of current cluster 120 | /// 121 | /// 122 | Task GetCurrentClusterLeaderAsync(); 123 | #endregion 124 | 125 | #region Metrics 126 | /// 127 | /// Query system metrics 128 | /// 129 | /// 130 | Task GetMetricsAsync(); 131 | #endregion 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nacos-sdk-csharp             [中文](./README.zh-cn.md) 2 | 3 | Unofficial csharp(dotnet core) implementation of [nacos](https://nacos.io/) OpenAPI. 4 | 5 | # This project was moved to https://github.com/nacos-group/nacos-sdk-csharp 6 | 7 | ![](https://img.shields.io/nuget/v/nacos-sdk-csharp-unofficial.svg) 8 | 9 | ![](./media/prj.png) 10 | 11 | ## CI Build Status 12 | 13 | | Platform | Build Server | Master Status | 14 | |--------- |------------- |---------| 15 | | Github Action | Linux/Windows |![nacos-sdk-csharp CI](https://github.com/catcherwong/nacos-sdk-csharp/workflows/nacos-sdk-csharp%20CI/badge.svg) 16 | 17 | ## Installation 18 | 19 | ```bash 20 | dotnet add package nacos-sdk-csharp-unofficial 21 | ``` 22 | 23 | ## Features 24 | 25 | - Basic OpenApi Usages 26 | - Integrate ASP.NET Core Configuration System 27 | - Service Registration and Discovery With ASP.NET Core 28 | - Integrate With Aliyun ACM 29 | - ... 30 | 31 | ## Basic Usage 32 | 33 | ### Simple Configuration Usage 34 | 35 | 1. Configure in `Program.cs` 36 | 37 | ```cs 38 | public static IHostBuilder CreateHostBuilder(string[] args) => 39 | Host.CreateDefaultBuilder(args) 40 | .ConfigureAppConfiguration((context, builder) => 41 | { 42 | var c = builder.Build(); 43 | 44 | // read configuration from config files 45 | builder.AddNacosConfiguration(c.GetSection("NacosConfig")); 46 | }) 47 | .ConfigureWebHostDefaults(webBuilder => 48 | { 49 | webBuilder.UseStartup(); 50 | }) 51 | ``` 52 | 53 | 2. Modify `appsettings.json` 54 | 55 | ```JSON 56 | { 57 | "NacosConfig": { 58 | "Optional": false, 59 | "DataId": "msconfigapp", 60 | "Group": "", 61 | "Tenant": "f47e0ae1-982a-4a64-aea3-52506492a3d4", 62 | "ServerAddresses": [ "http://localhost:8848/" ], 63 | "UserName": "test2", 64 | "Password": "123456", 65 | "AccessKey": "", 66 | "SecretKey": "", 67 | "EndPoint": "acm.aliyun.com" 68 | } 69 | } 70 | ``` 71 | 72 | 3. Use via .NET Core's Way 73 | 74 | ```cs 75 | [ApiController] 76 | [Route("api/[controller]")] 77 | public class ConfigController : ControllerBase 78 | { 79 | private readonly IConfiguration _configuration; 80 | private readonly AppSettings _settings; 81 | private readonly AppSettings _sSettings; 82 | private readonly AppSettings _mSettings; 83 | 84 | public ConfigController( 85 | IConfiguration configuration, 86 | IOptions options, 87 | IOptionsSnapshot sOptions, 88 | IOptionsMonitor _mOptions 89 | ) 90 | { 91 | _logger = logger; 92 | _configuration = configuration; 93 | _settings = options.Value; 94 | _sSettings = sOptions.Value; 95 | _mSettings = _mOptions.CurrentValue; 96 | } 97 | 98 | [HttpGet] 99 | public string Get() 100 | { 101 | // .... 102 | 103 | return "ok"; 104 | } 105 | 106 | } 107 | ``` 108 | 109 | ### Service Registration and Discovery 110 | 111 | 1. Service Registration 112 | 113 | Configure in `Program.cs` 114 | 115 | ```cs 116 | public class Startup 117 | { 118 | public Startup(IConfiguration configuration) 119 | { 120 | Configuration = configuration; 121 | } 122 | 123 | public IConfiguration Configuration { get; } 124 | 125 | public void ConfigureServices(IServiceCollection services) 126 | { 127 | // ... 128 | 129 | services.AddNacosAspNetCore(Configuration); 130 | } 131 | 132 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 133 | { 134 | // ... 135 | } 136 | } 137 | ``` 138 | 139 | Modify `appsettings.json` 140 | 141 | ```JSON 142 | "nacos": { 143 | "ServerAddresses": [ "http://localhost:8848" ], 144 | "DefaultTimeOut": 15000, 145 | "Namespace": "", 146 | "ListenInterval": 1000, 147 | "ServiceName": "App1" 148 | } 149 | ``` 150 | 151 | 2. Service Discovery 152 | 153 | ```cs 154 | [Route("api/[controller]")] 155 | [ApiController] 156 | public class ValuesController : ControllerBase 157 | { 158 | private readonly INacosServerManager _serverManager; 159 | 160 | public ValuesController(INacosServerManager serverManager) 161 | { 162 | _serverManager = serverManager; 163 | } 164 | 165 | [HttpGet("test")] 166 | public async Task Test() 167 | { 168 | // need to know the service name. 169 | // at this time only support random way. 170 | var baseUrl = await _serverManager.GetServerAsync("App2"); 171 | 172 | if(string.IsNullOrWhiteSpace(baseUrl)) 173 | { 174 | return "empty"; 175 | } 176 | 177 | var url = $"{baseUrl}/api/values"; 178 | 179 | using (HttpClient client = new HttpClient()) 180 | { 181 | var result = await client.GetAsync(url); 182 | return await result.Content.ReadAsStringAsync(); 183 | } 184 | } 185 | } 186 | ``` 187 | -------------------------------------------------------------------------------- /src/Nacos.Microsoft.Extensions.Configuration/Impl/MsConfigServerHttpAgent.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Microsoft.Extensions.Configuration 2 | { 3 | using Nacos.Config; 4 | using Nacos.Config.Http; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Net.Http; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | 12 | public class MsConfigServerHttpAgent : HttpAgent, IDisposable 13 | { 14 | private readonly NacosOptions _options; 15 | private readonly ServerListManager _serverListMgr; 16 | private readonly Nacos.Security.SecurityProxy _securityProxy; 17 | 18 | private readonly string _namespaceId; 19 | private readonly Timer _timer; 20 | private long _securityInfoRefreshIntervalMills = 5000; 21 | 22 | public MsConfigServerHttpAgent(NacosOptions options) 23 | { 24 | _options = options; 25 | _serverListMgr = new ServerListManager(_options); 26 | _namespaceId = _options.Namespace; 27 | 28 | _serverListMgr = new ServerListManager(_options); 29 | _securityProxy = new Security.SecurityProxy(_options); 30 | 31 | _securityProxy.LoginAsync(_serverListMgr.GetServerUrls()).ConfigureAwait(false).GetAwaiter().GetResult(); 32 | 33 | _timer = new Timer(async x => 34 | { 35 | await _securityProxy.LoginAsync(_serverListMgr.GetServerUrls()); 36 | }, null, 0, _securityInfoRefreshIntervalMills); 37 | } 38 | 39 | public override string AbstGetName() => _serverListMgr.GetName(); 40 | 41 | public override string AbstGetNamespace() => _serverListMgr.GetNamespace(); 42 | 43 | public override string AbstGetTenant() => _serverListMgr.GetTenant(); 44 | 45 | public void Dispose() 46 | { 47 | Console.WriteLine("ms config timer dispose"); 48 | _timer?.Dispose(); 49 | } 50 | 51 | public override async Task ReqApiAsync(HttpMethod httpMethod, string path, Dictionary headers, Dictionary paramValues, int timeout) 52 | { 53 | using (HttpClient client = new HttpClient()) 54 | { 55 | client.Timeout = TimeSpan.FromSeconds(timeout); 56 | 57 | var requestMessage = new HttpRequestMessage 58 | { 59 | Method = httpMethod 60 | }; 61 | 62 | var currentServerAddr = _serverListMgr.GetCurrentServerAddr(); 63 | 64 | var requestUrl = GetUrl(currentServerAddr, path); 65 | 66 | if (paramValues != null && paramValues.Any()) 67 | { 68 | if (httpMethod == HttpMethod.Post) 69 | { 70 | requestMessage.RequestUri = new Uri(requestUrl); 71 | requestMessage.Content = new FormUrlEncodedContent(paramValues); 72 | } 73 | else 74 | { 75 | var query = HttpAgentCommon.BuildQueryString(paramValues); 76 | requestMessage.RequestUri = new Uri($"{requestUrl}?{query}"); 77 | } 78 | } 79 | 80 | HttpAgentCommon.BuildHeader(requestMessage, headers); 81 | HttpAgentCommon.BuildSpasHeaders(requestMessage, paramValues, _options.AccessKey, _options.SecretKey); 82 | 83 | InjectSecurityInfo(requestMessage, paramValues); 84 | 85 | var responseMessage = await client.SendAsync(requestMessage); 86 | 87 | if (responseMessage.StatusCode == System.Net.HttpStatusCode.InternalServerError 88 | || responseMessage.StatusCode == System.Net.HttpStatusCode.BadGateway 89 | || responseMessage.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable) 90 | { 91 | System.Diagnostics.Trace.TraceError("[NACOS ConnectException] currentServerAddr: {0}, httpCode: {1}", _serverListMgr.GetCurrentServerAddr(), responseMessage.StatusCode); 92 | } 93 | else 94 | { 95 | _serverListMgr.UpdateCurrentServerAddr(currentServerAddr); 96 | return responseMessage; 97 | } 98 | 99 | throw new System.Net.Http.HttpRequestException($"no available server, currentServerAddr : {currentServerAddr}"); 100 | } 101 | } 102 | 103 | private void InjectSecurityInfo(HttpRequestMessage requestMessage, Dictionary paramValues) 104 | { 105 | if (!string.IsNullOrWhiteSpace(_securityProxy.GetAccessToken())) 106 | { 107 | requestMessage.Headers.TryAddWithoutValidation(ConstValue.ACCESS_TOKEN, _securityProxy.GetAccessToken()); 108 | } 109 | 110 | if (!string.IsNullOrWhiteSpace(_namespaceId) && paramValues != null && !paramValues.ContainsKey("tenant")) 111 | { 112 | requestMessage.Headers.TryAddWithoutValidation("tenant", _namespaceId); 113 | } 114 | } 115 | 116 | private string GetUrl(string serverAddr, string relativePath) 117 | { 118 | return $"{serverAddr}{relativePath}"; 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/Nacos/Config/Http/ServerHttpAgent.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.Config.Http 2 | { 3 | using Microsoft.Extensions.Logging; 4 | using Microsoft.Extensions.Options; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Net.Http; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | 12 | public class ServerHttpAgent : HttpAgent, IDisposable 13 | { 14 | private readonly ILogger _logger; 15 | private readonly NacosOptions _options; 16 | private readonly IHttpClientFactory _clientFactory; 17 | private readonly ServerListManager _serverListMgr; 18 | private readonly Nacos.Security.SecurityProxy _securityProxy; 19 | private readonly string _namespaceId; 20 | private readonly Timer _timer; 21 | private long _securityInfoRefreshIntervalMills = 5000; 22 | 23 | public ServerHttpAgent( 24 | ILoggerFactory loggerFactory, 25 | IOptions optionsAccs, 26 | IHttpClientFactory clientFactory) 27 | { 28 | _logger = loggerFactory.CreateLogger(); 29 | _options = optionsAccs.Value; 30 | _clientFactory = clientFactory; 31 | _namespaceId = _options.Namespace; 32 | 33 | _serverListMgr = new ServerListManager(_options); 34 | _securityProxy = new Security.SecurityProxy(_options); 35 | 36 | _securityProxy.LoginAsync(_serverListMgr.GetServerUrls()).ConfigureAwait(false).GetAwaiter().GetResult(); 37 | 38 | _timer = new Timer(async x => 39 | { 40 | await _securityProxy.LoginAsync(_serverListMgr.GetServerUrls()); 41 | }, null, 0, _securityInfoRefreshIntervalMills); 42 | } 43 | 44 | public override string AbstGetName() => _serverListMgr.GetName(); 45 | 46 | public override string AbstGetNamespace() => _serverListMgr.GetNamespace(); 47 | 48 | public override string AbstGetTenant() => _serverListMgr.GetTenant(); 49 | 50 | public override async Task ReqApiAsync(HttpMethod httpMethod, string path, Dictionary headers, Dictionary paramValues, int timeout) 51 | { 52 | var client = _clientFactory.CreateClient(ConstValue.ClientName); 53 | client.Timeout = TimeSpan.FromMilliseconds(timeout); 54 | 55 | var requestMessage = new HttpRequestMessage 56 | { 57 | Method = httpMethod 58 | }; 59 | 60 | var currentServerAddr = _serverListMgr.GetCurrentServerAddr(); 61 | 62 | var requestUrl = GetUrl(currentServerAddr, path); 63 | 64 | if (paramValues != null && paramValues.Any()) 65 | { 66 | if (httpMethod == HttpMethod.Post) 67 | { 68 | requestMessage.RequestUri = new Uri(requestUrl); 69 | requestMessage.Content = new FormUrlEncodedContent(paramValues); 70 | } 71 | else 72 | { 73 | var query = HttpAgentCommon.BuildQueryString(paramValues); 74 | requestMessage.RequestUri = new Uri($"{requestUrl}?{query}"); 75 | } 76 | } 77 | 78 | HttpAgentCommon.BuildHeader(requestMessage, headers); 79 | HttpAgentCommon.BuildSpasHeaders(requestMessage, paramValues, _options.AccessKey, _options.SecretKey); 80 | 81 | InjectSecurityInfo(requestMessage, paramValues); 82 | 83 | var responseMessage = await client.SendAsync(requestMessage); 84 | 85 | if (responseMessage.StatusCode == System.Net.HttpStatusCode.InternalServerError 86 | || responseMessage.StatusCode == System.Net.HttpStatusCode.BadGateway 87 | || responseMessage.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable) 88 | { 89 | _logger.LogError("[NACOS ConnectException] currentServerAddr: {0}, httpCode: {1}", _serverListMgr.GetCurrentServerAddr(), responseMessage.StatusCode); 90 | } 91 | else 92 | { 93 | _serverListMgr.UpdateCurrentServerAddr(currentServerAddr); 94 | return responseMessage; 95 | } 96 | 97 | throw new System.Net.Http.HttpRequestException($"no available server, currentServerAddr : {currentServerAddr}"); 98 | } 99 | 100 | private void InjectSecurityInfo(HttpRequestMessage requestMessage, Dictionary paramValues) 101 | { 102 | if (!string.IsNullOrWhiteSpace(_securityProxy.GetAccessToken())) 103 | { 104 | requestMessage.Headers.TryAddWithoutValidation(ConstValue.ACCESS_TOKEN, _securityProxy.GetAccessToken()); 105 | } 106 | 107 | if (!string.IsNullOrWhiteSpace(_namespaceId) && paramValues != null && !paramValues.ContainsKey("tenant")) 108 | { 109 | requestMessage.Headers.TryAddWithoutValidation("tenant", _namespaceId); 110 | } 111 | } 112 | 113 | private string GetUrl(string serverAddr, string relativePath) 114 | { 115 | return $"{serverAddr}{relativePath}"; 116 | } 117 | 118 | public void Dispose() 119 | { 120 | _timer?.Dispose(); 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /nacos-sdk-csharp.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28803.156 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C473C3A7-1B44-4E1F-83C0-745AD0566FE9}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{8176B7FC-151E-4EFF-A693-F60A39109595}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nacos", "src\Nacos\Nacos.csproj", "{6D3AEDBE-B3CF-4CF8-94ED-49E347D740CE}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nacos.Tests", "tests\Nacos.Tests\Nacos.Tests.csproj", "{F4B027C1-BBCB-450F-BC4D-76F05F2FB3EB}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nacos.AspNetCore", "src\Nacos.AspNetCore\Nacos.AspNetCore.csproj", "{52E524B7-960D-4FB0-AD79-FF3A4395B613}" 15 | EndProject 16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{25B1A184-1541-4F4E-A151-24A47CC08F34}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App1", "samples\App1\App1.csproj", "{60EDEA1E-549B-44EA-8954-FA2AB85966A8}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App2", "samples\App2\App2.csproj", "{2D5914EC-97B4-4AAD-BCBA-6F9FF00F2AAE}" 21 | EndProject 22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App3", "samples\App3\App3.csproj", "{17A6F76B-FEC2-419A-8C68-416032D3FBA1}" 23 | EndProject 24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nacos.Microsoft.Extensions.Configuration", "src\Nacos.Microsoft.Extensions.Configuration\Nacos.Microsoft.Extensions.Configuration.csproj", "{0E5305FF-6ADA-42E7-A5D1-C576E185F932}" 25 | EndProject 26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MsConfigApp", "samples\MsConfigApp\MsConfigApp.csproj", "{D8A28493-0320-4C40-8EBB-0D0AE5015307}" 27 | EndProject 28 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nacos.Microsoft.Extensions.Configuration.Tests", "tests\Nacos.Microsoft.Extensions.Configuration.Tests\Nacos.Microsoft.Extensions.Configuration.Tests.csproj", "{11BEE2DE-B793-4868-B65C-FF915125A7DD}" 29 | EndProject 30 | Global 31 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 32 | Debug|Any CPU = Debug|Any CPU 33 | Release|Any CPU = Release|Any CPU 34 | EndGlobalSection 35 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 36 | {6D3AEDBE-B3CF-4CF8-94ED-49E347D740CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {6D3AEDBE-B3CF-4CF8-94ED-49E347D740CE}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {6D3AEDBE-B3CF-4CF8-94ED-49E347D740CE}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {6D3AEDBE-B3CF-4CF8-94ED-49E347D740CE}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {F4B027C1-BBCB-450F-BC4D-76F05F2FB3EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {F4B027C1-BBCB-450F-BC4D-76F05F2FB3EB}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {F4B027C1-BBCB-450F-BC4D-76F05F2FB3EB}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {F4B027C1-BBCB-450F-BC4D-76F05F2FB3EB}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {52E524B7-960D-4FB0-AD79-FF3A4395B613}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {52E524B7-960D-4FB0-AD79-FF3A4395B613}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {52E524B7-960D-4FB0-AD79-FF3A4395B613}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {52E524B7-960D-4FB0-AD79-FF3A4395B613}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {60EDEA1E-549B-44EA-8954-FA2AB85966A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 49 | {60EDEA1E-549B-44EA-8954-FA2AB85966A8}.Debug|Any CPU.Build.0 = Debug|Any CPU 50 | {60EDEA1E-549B-44EA-8954-FA2AB85966A8}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {60EDEA1E-549B-44EA-8954-FA2AB85966A8}.Release|Any CPU.Build.0 = Release|Any CPU 52 | {2D5914EC-97B4-4AAD-BCBA-6F9FF00F2AAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 53 | {2D5914EC-97B4-4AAD-BCBA-6F9FF00F2AAE}.Debug|Any CPU.Build.0 = Debug|Any CPU 54 | {2D5914EC-97B4-4AAD-BCBA-6F9FF00F2AAE}.Release|Any CPU.ActiveCfg = Release|Any CPU 55 | {2D5914EC-97B4-4AAD-BCBA-6F9FF00F2AAE}.Release|Any CPU.Build.0 = Release|Any CPU 56 | {17A6F76B-FEC2-419A-8C68-416032D3FBA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 57 | {17A6F76B-FEC2-419A-8C68-416032D3FBA1}.Debug|Any CPU.Build.0 = Debug|Any CPU 58 | {17A6F76B-FEC2-419A-8C68-416032D3FBA1}.Release|Any CPU.ActiveCfg = Release|Any CPU 59 | {17A6F76B-FEC2-419A-8C68-416032D3FBA1}.Release|Any CPU.Build.0 = Release|Any CPU 60 | {0E5305FF-6ADA-42E7-A5D1-C576E185F932}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 61 | {0E5305FF-6ADA-42E7-A5D1-C576E185F932}.Debug|Any CPU.Build.0 = Debug|Any CPU 62 | {0E5305FF-6ADA-42E7-A5D1-C576E185F932}.Release|Any CPU.ActiveCfg = Release|Any CPU 63 | {0E5305FF-6ADA-42E7-A5D1-C576E185F932}.Release|Any CPU.Build.0 = Release|Any CPU 64 | {D8A28493-0320-4C40-8EBB-0D0AE5015307}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 65 | {D8A28493-0320-4C40-8EBB-0D0AE5015307}.Debug|Any CPU.Build.0 = Debug|Any CPU 66 | {D8A28493-0320-4C40-8EBB-0D0AE5015307}.Release|Any CPU.ActiveCfg = Release|Any CPU 67 | {D8A28493-0320-4C40-8EBB-0D0AE5015307}.Release|Any CPU.Build.0 = Release|Any CPU 68 | {11BEE2DE-B793-4868-B65C-FF915125A7DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 69 | {11BEE2DE-B793-4868-B65C-FF915125A7DD}.Debug|Any CPU.Build.0 = Debug|Any CPU 70 | {11BEE2DE-B793-4868-B65C-FF915125A7DD}.Release|Any CPU.ActiveCfg = Release|Any CPU 71 | {11BEE2DE-B793-4868-B65C-FF915125A7DD}.Release|Any CPU.Build.0 = Release|Any CPU 72 | EndGlobalSection 73 | GlobalSection(SolutionProperties) = preSolution 74 | HideSolutionNode = FALSE 75 | EndGlobalSection 76 | GlobalSection(NestedProjects) = preSolution 77 | {6D3AEDBE-B3CF-4CF8-94ED-49E347D740CE} = {C473C3A7-1B44-4E1F-83C0-745AD0566FE9} 78 | {F4B027C1-BBCB-450F-BC4D-76F05F2FB3EB} = {8176B7FC-151E-4EFF-A693-F60A39109595} 79 | {52E524B7-960D-4FB0-AD79-FF3A4395B613} = {C473C3A7-1B44-4E1F-83C0-745AD0566FE9} 80 | {60EDEA1E-549B-44EA-8954-FA2AB85966A8} = {25B1A184-1541-4F4E-A151-24A47CC08F34} 81 | {2D5914EC-97B4-4AAD-BCBA-6F9FF00F2AAE} = {25B1A184-1541-4F4E-A151-24A47CC08F34} 82 | {17A6F76B-FEC2-419A-8C68-416032D3FBA1} = {25B1A184-1541-4F4E-A151-24A47CC08F34} 83 | {0E5305FF-6ADA-42E7-A5D1-C576E185F932} = {C473C3A7-1B44-4E1F-83C0-745AD0566FE9} 84 | {D8A28493-0320-4C40-8EBB-0D0AE5015307} = {25B1A184-1541-4F4E-A151-24A47CC08F34} 85 | {11BEE2DE-B793-4868-B65C-FF915125A7DD} = {8176B7FC-151E-4EFF-A693-F60A39109595} 86 | EndGlobalSection 87 | GlobalSection(ExtensibilityGlobals) = postSolution 88 | SolutionGuid = {A1C5215E-0E70-4C04-B21E-5209BCF32472} 89 | EndGlobalSection 90 | GlobalSection(MonoDevelopProperties) = preSolution 91 | Policies = $0 92 | $0.DotNetNamingPolicy = $1 93 | $1.DirectoryNamespaceAssociation = PrefixedHierarchical 94 | $0.TextStylePolicy = $2 95 | $2.FileWidth = 80 96 | $2.TabsToSpaces = True 97 | $2.scope = text/plain 98 | $0.VersionControlPolicy = $3 99 | description = nacos sdk csharp 100 | EndGlobalSection 101 | EndGlobal 102 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | 332 | # Mac OS 333 | .DS_Store -------------------------------------------------------------------------------- /src/Nacos.AspNetCore/StatusReportBgTask.cs: -------------------------------------------------------------------------------- 1 | namespace Nacos.AspNetCore 2 | { 3 | using System; 4 | using System.Linq; 5 | using System.Net; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Microsoft.AspNetCore.Hosting.Server; 9 | using Microsoft.AspNetCore.Hosting.Server.Features; 10 | using Microsoft.AspNetCore.Http.Features; 11 | using Microsoft.Extensions.Hosting; 12 | using Microsoft.Extensions.Logging; 13 | using Microsoft.Extensions.Options; 14 | 15 | public class StatusReportBgTask : IHostedService, IDisposable 16 | { 17 | private readonly ILogger _logger; 18 | private readonly INacosNamingClient _client; 19 | private readonly IFeatureCollection _features; 20 | private NacosAspNetCoreOptions _options; 21 | 22 | private Timer _timer; 23 | private bool _reporting; 24 | private Uri uri = null; 25 | 26 | public StatusReportBgTask( 27 | ILoggerFactory loggerFactory, 28 | INacosNamingClient client, 29 | IServer server, 30 | IOptionsMonitor optionsAccs) 31 | { 32 | _logger = loggerFactory.CreateLogger(); 33 | _client = client; 34 | _options = optionsAccs.CurrentValue; 35 | _features = server.Features; 36 | } 37 | 38 | public Task StartAsync(CancellationToken cancellationToken) 39 | { 40 | uri = GetUri(_features, _options); 41 | 42 | _logger.LogInformation("Report instance ({0}:{1}) status....", uri.Host, uri.Port); 43 | 44 | _timer = new Timer(async x => 45 | { 46 | if (_reporting) 47 | { 48 | _logger.LogInformation($"Latest manipulation is still working ..."); 49 | return; 50 | } 51 | _reporting = true; 52 | await ReportAsync(); 53 | _reporting = false; 54 | }, null, TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(10)); 55 | 56 | return Task.CompletedTask; 57 | } 58 | 59 | private async Task ReportAsync() 60 | { 61 | bool flag = false; 62 | 63 | try 64 | { 65 | // send heart beat will register instance 66 | flag = await _client.SendHeartbeatAsync(new SendHeartbeatRequest 67 | { 68 | Ephemeral = false, 69 | ServiceName = _options.ServiceName, 70 | GroupName = _options.GroupName, 71 | BeatInfo = new BeatInfo 72 | { 73 | ip = uri.Host, 74 | port = uri.Port, 75 | serviceName = _options.ServiceName, 76 | scheduled = true, 77 | weight = _options.Weight, 78 | cluster = _options.ClusterName, 79 | metadata = _options.Metadata, 80 | }, 81 | NameSpaceId = _options.Namespace 82 | }); 83 | } 84 | catch (Exception ex) 85 | { 86 | _logger.LogWarning(ex, "Send heart beat to Nacos error"); 87 | } 88 | 89 | _logger.LogDebug("report at {0}, status = {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), flag); 90 | } 91 | 92 | public async Task StopAsync(CancellationToken cancellationToken) 93 | { 94 | _logger.LogInformation("Unregistering from Nacos"); 95 | 96 | var removeRequest = new RemoveInstanceRequest 97 | { 98 | ServiceName = _options.ServiceName, 99 | Ip = uri.Host, 100 | Port = uri.Port, 101 | GroupName = _options.GroupName, 102 | NamespaceId = _options.Namespace, 103 | ClusterName = _options.ClusterName, 104 | Ephemeral = false 105 | }; 106 | 107 | for (int i = 0; i < 3; i++) 108 | { 109 | try 110 | { 111 | var flag = await _client.RemoveInstanceAsync(removeRequest); 112 | _logger.LogDebug("remove instance, status = {0}", flag); 113 | break; 114 | } 115 | catch (Exception ex) 116 | { 117 | _logger.LogError(ex, "Unregistering error, count = {0}", i + 1); 118 | } 119 | } 120 | 121 | _timer?.Change(Timeout.Infinite, 0); 122 | } 123 | 124 | public void Dispose() 125 | { 126 | _timer?.Dispose(); 127 | } 128 | 129 | private Uri GetUri(IFeatureCollection features, NacosAspNetCoreOptions config) 130 | { 131 | var port = config.Port <= 0 ? 80 : config.Port; 132 | 133 | // 1. config 134 | if (!string.IsNullOrWhiteSpace(config.Ip)) 135 | { 136 | // it seems that nacos don't return the scheme 137 | // so here use http only. 138 | return new Uri($"http://{config.Ip}:{port}"); 139 | } 140 | 141 | var address = string.Empty; 142 | 143 | // 2. IServerAddressesFeature 144 | if (features != null) 145 | { 146 | var addresses = features.Get(); 147 | address = addresses?.Addresses?.FirstOrDefault(); 148 | 149 | if (address != null) 150 | { 151 | var url = ReplaceAddress(address); 152 | return new Uri(url); 153 | } 154 | } 155 | 156 | // 3. ASPNETCORE_URLS 157 | address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS"); 158 | if (!string.IsNullOrWhiteSpace(address)) 159 | { 160 | var url = ReplaceAddress(address); 161 | return new Uri(url); 162 | } 163 | 164 | // 4. --urls 165 | var cmdArgs = Environment.GetCommandLineArgs(); 166 | if (cmdArgs != null && cmdArgs.Any()) 167 | { 168 | var cmd = cmdArgs.FirstOrDefault(x => x.StartsWith("--urls", StringComparison.OrdinalIgnoreCase)); 169 | address = cmd.Split('=')[1]; 170 | 171 | var url = ReplaceAddress(address); 172 | return new Uri(url); 173 | } 174 | 175 | // 5. current ip address third 176 | address = $"http://{GetCurrentIp()}:{port}"; 177 | 178 | return new Uri(address); 179 | } 180 | 181 | private string ReplaceAddress(string address) 182 | { 183 | var ip = GetCurrentIp(); 184 | 185 | if (address.Contains("*")) 186 | { 187 | address = address.Replace("*", ip); 188 | } 189 | else if (address.Contains("+")) 190 | { 191 | address = address.Replace("+", ip); 192 | } 193 | else if (address.Contains("localhost", StringComparison.OrdinalIgnoreCase)) 194 | { 195 | address = address.Replace("localhost", ip, StringComparison.OrdinalIgnoreCase); 196 | } 197 | else if (address.Contains("0.0.0.0", StringComparison.OrdinalIgnoreCase)) 198 | { 199 | address = address.Replace("0.0.0.0", ip, StringComparison.OrdinalIgnoreCase); 200 | } 201 | 202 | return address; 203 | } 204 | 205 | private string GetCurrentIp() 206 | { 207 | var instanceIp = "127.0.0.1"; 208 | 209 | try 210 | { 211 | IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); 212 | 213 | foreach (var ipAddr in Dns.GetHostAddresses(Dns.GetHostName())) 214 | { 215 | if (ipAddr.AddressFamily.ToString() == "InterNetwork") 216 | { 217 | instanceIp = ipAddr.ToString(); 218 | break; 219 | } 220 | } 221 | } 222 | catch 223 | { 224 | } 225 | 226 | return instanceIp; 227 | } 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /src/Nacos/DependencyInjection/ServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace Microsoft.Extensions.DependencyInjection 2 | { 3 | using Microsoft.Extensions.Configuration; 4 | using Microsoft.Extensions.DependencyInjection.Extensions; 5 | using Nacos; 6 | using System; 7 | using System.Net.Http; 8 | 9 | public static class ServiceCollectionExtensions 10 | { 11 | public static IServiceCollection AddNacos(this IServiceCollection services, Action configure) 12 | { 13 | if (services == null) 14 | { 15 | throw new ArgumentNullException(nameof(services)); 16 | } 17 | 18 | services.AddOptions(); 19 | services.Configure(configure); 20 | 21 | services.AddHttpClient(ConstValue.ClientName) 22 | .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseProxy = false }); 23 | 24 | services.TryAddSingleton(); 25 | services.TryAddSingleton(); 26 | services.AddSingleton(); 27 | services.AddSingleton(); 28 | 29 | return services; 30 | } 31 | 32 | public static IServiceCollection AddNacos(this IServiceCollection services, IConfiguration configuration, string sectionName = "nacos") 33 | { 34 | if (services == null) 35 | { 36 | throw new ArgumentNullException(nameof(services)); 37 | } 38 | 39 | services.Configure(configuration.GetSection(sectionName)); 40 | 41 | services.AddHttpClient(ConstValue.ClientName) 42 | .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseProxy = false }); 43 | 44 | services.TryAddSingleton(); 45 | services.TryAddSingleton(); 46 | services.AddSingleton(); 47 | services.AddSingleton(); 48 | 49 | return services; 50 | } 51 | 52 | public static IServiceCollection AddNacos(this IServiceCollection services, Action configure, Action httpClientAction) 53 | { 54 | if (services == null) 55 | { 56 | throw new ArgumentNullException(nameof(services)); 57 | } 58 | 59 | services.AddOptions(); 60 | services.Configure(configure); 61 | 62 | services.AddHttpClient(ConstValue.ClientName) 63 | .ConfigureHttpClient(httpClientAction) 64 | .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseProxy = false }); 65 | 66 | services.TryAddSingleton(); 67 | services.TryAddSingleton(); 68 | services.AddSingleton(); 69 | services.AddSingleton(); 70 | 71 | return services; 72 | } 73 | 74 | public static IServiceCollection AddNacos(this IServiceCollection services, IConfiguration configuration, Action httpClientAction, string sectionName = "nacos") 75 | { 76 | if (services == null) 77 | { 78 | throw new ArgumentNullException(nameof(services)); 79 | } 80 | 81 | services.Configure(configuration.GetSection(sectionName)); 82 | 83 | services.AddHttpClient(ConstValue.ClientName) 84 | .ConfigureHttpClient(httpClientAction) 85 | .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseProxy = false }); 86 | 87 | services.TryAddSingleton(); 88 | services.TryAddSingleton(); 89 | services.AddSingleton(); 90 | services.AddSingleton(); 91 | 92 | return services; 93 | } 94 | 95 | public static IServiceCollection AddNacosNaming(this IServiceCollection services, Action configure, Action httpClientAction = null) 96 | { 97 | if (services == null) 98 | { 99 | throw new ArgumentNullException(nameof(services)); 100 | } 101 | 102 | services.AddOptions(); 103 | services.Configure(configure); 104 | 105 | var clientBuilder = services.AddHttpClient(ConstValue.ClientName) 106 | .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseProxy = false }); 107 | 108 | if (httpClientAction != null) 109 | { 110 | clientBuilder.ConfigureHttpClient(httpClientAction); 111 | } 112 | 113 | services.AddSingleton(); 114 | 115 | return services; 116 | } 117 | 118 | public static IServiceCollection AddNacosNaming(this IServiceCollection services, IConfiguration configuration, Action httpClientAction = null, string sectionName = "nacos") 119 | { 120 | if (services == null) 121 | { 122 | throw new ArgumentNullException(nameof(services)); 123 | } 124 | 125 | services.Configure(configuration.GetSection(sectionName)); 126 | 127 | var clientBuilder = services.AddHttpClient(ConstValue.ClientName) 128 | .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseProxy = false }); 129 | 130 | if (httpClientAction != null) 131 | { 132 | clientBuilder.ConfigureHttpClient(httpClientAction); 133 | } 134 | 135 | services.AddSingleton(); 136 | 137 | return services; 138 | } 139 | 140 | public static IServiceCollection AddNacosConfig(this IServiceCollection services, Action configure, Action httpClientAction = null) 141 | { 142 | if (services == null) 143 | { 144 | throw new ArgumentNullException(nameof(services)); 145 | } 146 | 147 | services.AddOptions(); 148 | services.Configure(configure); 149 | 150 | var clientBuilder = services.AddHttpClient(ConstValue.ClientName) 151 | .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseProxy = false }); 152 | 153 | if (httpClientAction != null) 154 | { 155 | clientBuilder.ConfigureHttpClient(httpClientAction); 156 | } 157 | 158 | services.TryAddSingleton(); 159 | services.TryAddSingleton(); 160 | services.AddSingleton(); 161 | 162 | return services; 163 | } 164 | 165 | public static IServiceCollection AddNacosConfig(this IServiceCollection services, IConfiguration configuration, Action httpClientAction = null, string sectionName = "nacos") 166 | { 167 | if (services == null) 168 | { 169 | throw new ArgumentNullException(nameof(services)); 170 | } 171 | 172 | services.Configure(configuration.GetSection(sectionName)); 173 | 174 | var clientBuilder = services.AddHttpClient(ConstValue.ClientName) 175 | .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler() { UseProxy = false }); 176 | 177 | if (httpClientAction != null) 178 | { 179 | clientBuilder.ConfigureHttpClient(httpClientAction); 180 | } 181 | 182 | services.TryAddSingleton(); 183 | services.TryAddSingleton(); 184 | services.AddSingleton(); 185 | 186 | return services; 187 | } 188 | } 189 | } 190 | --------------------------------------------------------------------------------