using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
namespace Admin.Core.Common
{
///
/// Json 配置文件通用类
///
public static class JsonConfigUtils
{
#region 变量
///
/// 锁
///
private static object __Lock__ = new object();
#endregion
///
/// 读取配置文件的信息
///
///
/// 要读取json的名称
/// 要读取的json节点名称
///
public static T GetAppSettings(IConfiguration config, string AppSettingsFileName, string key) where T : class, new()
{
lock (__Lock__)
{
if (config == null)
{
config = new ConfigurationBuilder()
.Add(new JsonConfigurationSource
{
Path = AppSettingsFileName,
Optional = false,
ReloadOnChange = true
})
.Build();
}
var appconfig = new ServiceCollection()
.AddOptions()
.Configure(config.GetSection(key))
.BuildServiceProvider()
.GetService>()
.Value;
return appconfig;
}
}
public static string GetJson(string jsonPath, string key)
{
IConfiguration config = new ConfigurationBuilder().AddJsonFile(jsonPath).Build(); //json文件地址
string s = config.GetSection(key).Value; //json某个对象
return s;
}
}
///
/// 配置文件管理器
///
public interface IConfigurationManager
{
T GetAppConfig(string key, T defaultValue = default(T));
}
///
/// 配置读取 根据环境变量
///
public class ConfigurationManager : IConfigurationManager
{
private readonly IConfigurationRoot config;
public ConfigurationManager(IConfigurationRoot _config)
{
config = _config;
}
public T GetAppConfig(string key, T defaultValue = default(T))
{
T value = default(T);
try
{
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", true, false)
.AddJsonFile($"appsettings.{env}.json", true, false)
.AddEnvironmentVariables();
var configuration = builder.Build();
value = (T)Convert.ChangeType(configuration[key], typeof(T));
if (value == null)
value = defaultValue;
}
catch
{
value = defaultValue;
}
return value;
}
}
#region Nacos 配置清单
public class JsonConfigSettings
{
// 从nacos 读取到的系统配置信息
public static IConfiguration Configuration { get; set; }
///
/// 配置文件名称常量
///
private static string AppSettingsFileName = $"appsettings{ GetAppSettingsConfigName() }json";
///
/// 根据环境变量定向配置文件名称
///
///
private static string GetAppSettingsConfigName()
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != null
&& Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != "")
{
return $".{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.";
}
else
{
return ".";
}
}
///
/// 获取Nacos配置
///
public static List NacosServerAddresses
{
get
{
return JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").ServerAddresses;
}
}
///
/// 获取Nacos配置
///
public static int NacosDefaultTimeOut
{
get
{
return JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").DefaultTimeOut;
}
}
///
/// 获取Nacos配置
///
public static string NacosNamespace
{
get
{
return JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").Namespace;
}
}
///
/// 获取Nacos配置
///
public static string NacosServiceName
{
get
{
return JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").ServiceName;
}
}
///
/// 获取Nacos配置
///
public static int ListenInterval
{
get
{
return JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").ListenInterval;
}
}
///
/// 获取Nacos配置
///
public static string NacosIp
{
get
{
return JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").Ip;
}
}
///
/// 获取Nacos配置
///
public static int NacosPort
{
get
{
return int.Parse(JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").Port);
}
}
///
/// 获取Nacos配置
///
public static bool NacosRegisterEnabled
{
get
{
return JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").RegisterEnabled;
}
}
///
/// 获取Nacos配置
///
public static Dictionary NacosMetadata
{
get
{
return JsonConfigUtils.GetAppSettings(Configuration, AppSettingsFileName, "nacos").Metadata;
}
}
#endregion
#region Nacos配置
///
/// Nacos配置实体
///
public class NacosConfigDTO
{
///
/// 服务IP地址
///
public List ServerAddresses { get; set; }
///
/// 默认超时时间
///
public int DefaultTimeOut { get; set; }
///
/// 监听间隔
///
public int ListenInterval { get; set; }
///
/// 服务命名空间
///
public string Namespace { get; set; }
///
/// 服务名称
///
public string ServiceName { get; set; }
///
/// IP地址
///
public string Ip { get; set; }
///
/// 端口
///
public string Port { get; set; }
///
/// 服务命名空间
///
public bool RegisterEnabled { get; set; }
///
/// 其他配置
///
public Dictionary Metadata { get; set; }
}
#endregion
}
}