forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonConfigUtils.cs
More file actions
242 lines (219 loc) · 7.12 KB
/
JsonConfigUtils.cs
File metadata and controls
242 lines (219 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
namespace Blog.Core.Common.Helper
{
/// <summary>
/// Json 配置文件通用类
/// </summary>
public static class JsonConfigUtils
{
#region 变量
/// <summary>
/// 锁
/// </summary>
private static object __Lock__ = new object();
#endregion
/// <summary>
/// 读取配置文件的信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fileName">要读取json的名称</param>
/// <param name="key">要读取的json节点名称</param>
/// <returns></returns>
public static T GetAppSettings<T>(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<T>(config.GetSection(key))
.BuildServiceProvider()
.GetService<IOptions<T>>()
.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;
}
}
#region Nacos 配置清单
public class JsonConfigSettings
{
// 从nacos 读取到的系统配置信息
public static IConfiguration Configuration { get; set; }
/// <summary>
/// 配置文件名称常量
/// </summary>
private static string AppSettingsFileName = $"appsettings{ GetAppSettingsConfigName() }json";
/// <summary>
/// 根据环境变量定向配置文件名称
/// </summary>
/// <returns></returns>
private static string GetAppSettingsConfigName()
{
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != null
&& Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != "")
{
return $".{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.";
}
else
{
return ".";
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static List<string> NacosServerAddresses
{
get
{
return JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").ServerAddresses;
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static int NacosDefaultTimeOut
{
get
{
return JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").DefaultTimeOut;
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static string NacosNamespace
{
get
{
return JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").Namespace;
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static string NacosServiceName
{
get
{
return JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").ServiceName;
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static int ListenInterval
{
get
{
return JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").ListenInterval;
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static string NacosIp
{
get
{
return JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").Ip;
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static int NacosPort
{
get
{
return int.Parse(JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").Port);
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static bool NacosRegisterEnabled
{
get
{
return JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").RegisterEnabled;
}
}
/// <summary>
/// 获取Nacos配置
/// </summary>
public static Dictionary<string, string> NacosMetadata
{
get
{
return JsonConfigUtils.GetAppSettings<NacosConfigDTO>(Configuration, AppSettingsFileName, "nacos").Metadata;
}
}
#endregion
#region Nacos配置
/// <summary>
/// Nacos配置实体
/// </summary>
public class NacosConfigDTO
{
/// <summary>
/// 服务IP地址
/// </summary>
public List<string> ServerAddresses { get; set; }
/// <summary>
/// 默认超时时间
/// </summary>
public int DefaultTimeOut { get; set; }
/// <summary>
/// 监听间隔
/// </summary>
public int ListenInterval { get; set; }
/// <summary>
/// 服务命名空间
/// </summary>
public string Namespace { get; set; }
/// <summary>
/// 服务名称
/// </summary>
public string ServiceName { get; set; }
/// <summary>
/// IP地址
/// </summary>
public string Ip { get; set; }
/// <summary>
/// 端口
/// </summary>
public string Port { get; set; }
/// <summary>
/// 服务命名空间
/// </summary>
public bool RegisterEnabled { get; set; }
/// <summary>
/// 其他配置
/// </summary>
public Dictionary<string, string> Metadata { get; set; }
}
#endregion
}
}