服务器记录(249)

最近优化了些服务器做下记录

  1. httpclient的使用

    httpclient不能立即关闭,会占用资源,并发量一大就耗尽。所以.net core 会使用如下:

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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
public class RestClient : IDisposable
{
private readonly ILogger Logger = LoggerManager.GetLogger("ApiClient");
private HttpClient _httpClient = null;
const string TASKAPI20 = "api/v2/task";
const string TASKAPI30 = "api/v3/task";
const string MATRIXAPI20 = "api/v2/matrix";
const string USERAPI20 = "api/v2/user";
const string DEVICEAPI30 = "api/v3/device";
const string DEVICEAPI20 = "api/v2/device";
private string IngestDbUrl { get; set; }
private string CmServerUrl { get; set; }
private bool _disposed;
public RestClient(HttpClient httpClient, string ingesturl, string cmurl)
{
_disposed = false;
_httpClient = httpClient != null? httpClient : new HttpClient();
_httpClient.DefaultRequestHeaders.Connection.Clear();
_httpClient.DefaultRequestHeaders.ConnectionClose = false;
_httpClient.Timeout = TimeSpan.FromSeconds(15);
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_httpClient.DefaultRequestHeaders.Add("sobeyhive-http-system", "INGESTSERVER");
_httpClient.DefaultRequestHeaders.Add("sobeyhive-http-site", "S1");
_httpClient.DefaultRequestHeaders.Add("sobeyhive-http-tool", "INGESTSERVER");
IngestDbUrl = ingesturl;
CmServerUrl = cmurl;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~RestClient()
{
//必须为false
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed) return;
if (disposing)
{
}
if (_httpClient != null)
{
_httpClient.Dispose();
_httpClient = null;
}
_disposed = true;
}
public Dictionary<string, string> GetTokenHeader(string usertoken)
{
return new Dictionary<string, string>() {
{"sobeyhive-http-token", usertoken }
};
}
public Dictionary<string, string> GetCodeHeader(string usertoken)
{
return new Dictionary<string, string>() {
{"sobeyhive-http-secret", RSAHelper.RSAstr()},
{"current-user-code", usertoken }
};
}
public Dictionary<string, string> GetIngestHeader()
{
return new Dictionary<string, string>() {
{"sobeyhive-ingest-signature", Base64SQL.ToBase64String($"ingest_server;{DateTime.Now}")},
};
}
public async Task<TResponse> PostAsync<TResponse>(string url, object body, string method = "POST", NameValueCollection queryString = null, int timeout = 60)
where TResponse : class, new()
{
TResponse response = null;
try
{
string json = JsonHelper.ToJson(body);
HttpClient client = _httpClient;
if (queryString == null)
{
queryString = new NameValueCollection();
}
if (String.IsNullOrEmpty(method))
{
method = "POST";
}
url = CreateUrl(url, queryString);
//Logger.Debug("请求:{0} {1}", method, url);
byte[] strData = Encoding.UTF8.GetBytes(json);
MemoryStream ms = new MemoryStream(strData);
using (StreamContent sc = new StreamContent(ms))
{
sc.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
//foreach (var item in _httpClient.DefaultRequestHeaders)
//{
// Logger.Error("header : " + item.Key + ":" + item.Value.FirstOrDefault());
// foreach(var test in item.Value)
// {
// Logger.Error("test : " + test);
// }
//}
var res = await client.PostAsync(url, sc).ConfigureAwait(true);
byte[] rData = await res.Content.ReadAsByteArrayAsync().ConfigureAwait(true);
string rJson = Encoding.UTF8.GetString(rData);
Logger.Info("url body response:\r\n{0} {1} {2}", url, json, rJson);
response = JsonHelper.ToObject<TResponse>(rJson);
return response;
}
}
catch (System.Exception e)
{
TResponse r = new TResponse();
Logger.Error("请求异常:\r\n{0} {1}", e.ToString(), url);
throw;
}
}
public async Task<string> PostAsync(string url, string body, string method = "POST", NameValueCollection queryString = null, int timeout = 60)
{
string response = null;
try
{
string json = body;
HttpClient client = _httpClient;
if (queryString == null)
{
queryString = new NameValueCollection();
}
if (String.IsNullOrEmpty(method))
{
method = "POST";
}
url = CreateUrl(url, queryString);
//Logger.Debug("请求:{0} {1}", method, url);
byte[] strData = Encoding.UTF8.GetBytes(json);
MemoryStream ms = new MemoryStream(strData);
using (StreamContent sc = new StreamContent(ms))
{
sc.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
var res = await client.PostAsync(url, sc).ConfigureAwait(true);
if (res.Content == null || res.Content.Headers.ContentLength == 0)
{
response = "";
}
else
{
byte[] rData = await res.Content.ReadAsByteArrayAsync().ConfigureAwait(true);
string rJson = Encoding.UTF8.GetString(rData);
//Logger.Debug("应答:\r\n{0}", rJson);
response = rJson;
}
}
}
catch (System.Exception e)
{
response = "ERROR";
Logger.Error("请求异常:\r\n{0} {1}", e.ToString(), url);
}
return response;
}
public async Task<TResponse> PutAsync<TResponse>(string url, object body, Dictionary<string, string> header, NameValueCollection queryString = null)
{
TResponse response = default(TResponse);
try
{
string json = JsonHelper.ToJson(body);
HttpClient client = _httpClient;
if (queryString == null)
{
queryString = new NameValueCollection();
}
url = CreateUrl(url, queryString);
//Logger.Debug("请求:{0} {1}", method, url);
byte[] strData = Encoding.UTF8.GetBytes(json);
MemoryStream ms = new MemoryStream(strData);
using (StreamContent sc = new StreamContent(ms))
{
sc.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
if (header != null)
{
foreach (var item in header)
{
sc.Headers.Add(item.Key, item.Value);
}
}
var res = await client.PutAsync(url, sc).ConfigureAwait(true);
byte[] rData = await res.Content.ReadAsByteArrayAsync().ConfigureAwait(true);
string rJson = Encoding.UTF8.GetString(rData);
//Logger.Debug("应答:\r\n{0}", rJson);
response = JsonHelper.ToObject<TResponse>(rJson);
return response;
}
}
catch (System.Exception e)
{
Logger.Error("请求异常:\r\n{0} {1}", e.ToString(), url);
throw;
}
}
public async Task<TResponse> DeleteAsync<TResponse>(string url, Dictionary<string, string> header, NameValueCollection queryString = null)
where TResponse : class, new()
{
TResponse response = default(TResponse);
try
{
HttpClient client = _httpClient;
if (queryString == null)
{
queryString = new NameValueCollection();
}
url = CreateUrl(url, queryString);
//Logger.Debug("请求:{0} {1}", method, url);
using (var requestMessage = new HttpRequestMessage(HttpMethod.Delete, url))
{
if (header != null)
{
foreach (var item in header)
{
requestMessage.Headers.Add(item.Key, item.Value);
}
}
var backinfo = await client.SendAsync(requestMessage).ConfigureAwait(true);
var rJson = await backinfo.Content.ReadAsStringAsync().ConfigureAwait(true);
Logger.Info("url response:\r\n{0} {1}", url, rJson);
response = JsonHelper.ToObject<TResponse>(rJson);
}
}
catch (System.Exception e)
{
TResponse r = new TResponse();
Logger.Error("请求异常:\r\n{0}", e.ToString(), url);
return r;
}
return response;
}
public async Task<TResponse> GetAsync<TResponse>(string url, NameValueCollection queryString, Dictionary<string, string> header)
where TResponse : class, new()
{
TResponse response = null;
try
{
HttpClient client = _httpClient;
if (queryString != null)
{
url = CreateUrl(url, queryString);
}
//Logger.Debug("请求:{0} {1}", "GET", url);
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, url))
{
if (header != null)
{
foreach (var item in header)
{
requestMessage.Headers.Add(item.Key, item.Value);
}
}
var backinfo = await client.SendAsync(requestMessage).ConfigureAwait(true);
var rJson = await backinfo.Content.ReadAsStringAsync().ConfigureAwait(true);
Logger.Info("url response:\r\n{0} {1}", url, rJson);
response = JsonHelper.ToObject<TResponse>(rJson);
}
}
catch (System.Exception e)
{
TResponse r = new TResponse();
Logger.Error("请求异常:\r\n{0} {1}", e.ToString(), url);
return r;
}
return response;
}
//public async Task<TResponse> GetAsync<TResponse>(string url, NameValueCollection queryString)
// where TResponse : class, new()
//{
// TResponse response = null;
// try
// {
// HttpClient client = _httpClient;
// if (queryString != null)
// {
// url = CreateUrl(url, queryString);
// }
//
// //Logger.Debug("请求:{0} {1}", "GET", url);
// byte[] rData = await client.GetByteArrayAsync(url).ConfigureAwait(true);
// string rJson = Encoding.UTF8.GetString(rData);
// Logger.Info("url response:\r\n{0} {1}", url, rJson);
// response = JsonHelper.ToObject<TResponse>(rJson);
// }
// catch (System.Exception )
// {
// TResponse r = new TResponse();
// //Logger.Error("请求异常:\r\n{0}", e.ToString());
// return r;
// }
// return response;
//}
public async Task<TResponse> PostAsync<TResponse>(string url, object body, Dictionary<string, string> header, string method = null, NameValueCollection queryString = null)
{
TResponse response = default(TResponse);
try
{
string json = JsonHelper.ToJson(body);
HttpClient client = _httpClient;
if (queryString == null)
{
queryString = new NameValueCollection();
}
url = CreateUrl(url, queryString);
if (String.IsNullOrEmpty(method))
{
method = "POST";
}
//Logger.Debug("请求:{0} {1}", method, url);
byte[] strData = Encoding.UTF8.GetBytes(json);
MemoryStream ms = new MemoryStream(strData);
using (StreamContent sc = new StreamContent(ms))
{
sc.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
if (header != null)
{
foreach (var item in header)
{
sc.Headers.Add(item.Key, item.Value);
}
}
var res = await client.PostAsync(url, sc).ConfigureAwait(true);
byte[] rData = await res.Content.ReadAsByteArrayAsync().ConfigureAwait(true);
string rJson = Encoding.UTF8.GetString(rData);
//Logger.Debug("应答:\r\n{0}", rJson);
response = JsonHelper.ToObject<TResponse>(rJson);
return response;
}
}
catch (System.Exception )
{
//Logger.Error("请求异常:\r\n{0}", e.ToString());
throw;
}
}
//public async Task<string> PostAsync(string url, object body, string method, NameValueCollection queryString)
//{
// string response = null;
// try
// {
// string json = JsonHelper.ToJson(body);
// HttpClient client = _httpClient;
// if (queryString == null)
// {
// queryString = new NameValueCollection();
// }
// url = CreateUrl(url, queryString);
// if (String.IsNullOrEmpty(method))
// {
// method = "POST";
// }
// //Logger.Debug("请求:{0} {1}", method, url);
// byte[] strData = Encoding.UTF8.GetBytes(json);
// MemoryStream ms = new MemoryStream(strData);
// using (StreamContent sc = new StreamContent(ms))
// {
// sc.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
// var res = await client.PostAsync(url, sc).ConfigureAwait(true);
// byte[] rData = await res.Content.ReadAsByteArrayAsync().ConfigureAwait(true);
// string rJson = Encoding.UTF8.GetString(rData);
// //Logger.Debug("应答:\r\n{0}", rJson);
// response = rJson;
// return response;
// }
// }
// catch (System.Exception )
// {
// //Logger.Error("请求异常:\r\n{0}", e.ToString());
// throw;
// }
//}
public async Task<TResult> PostWithTokenAsync<TResult>(string url, object body, string token, string userId = null, string method = "Post")
{
//Stopwatch sw = new Stopwatch();
//sw.Start();
string apiUrl = $"{url}";
HttpMethod hm = new HttpMethod(method);
using (var request = new HttpRequestMessage(hm, apiUrl))
{
if (!String.IsNullOrEmpty(token))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
if (!String.IsNullOrEmpty(userId))
{
request.Headers.Add("User", userId);
}
string json = "";
if (body != null)
{
json = Newtonsoft.Json.JsonConvert.SerializeObject(body);
}
request.Content = new StringContent(json);
request.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).ConfigureAwait(true);
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new UnauthorizedAccessException("验证失败");
}
try
{
response.EnsureSuccessStatusCode();
string str = await response.Content.ReadAsStringAsync().ConfigureAwait(true);
//sw.Stop();
//if (sw.ElapsedMilliseconds >= 1000)
//{
//slowLogger.Warn("请求时间超过一秒:POST {0} {1}", apiUrl, sw.ElapsedMilliseconds);
//}
return Newtonsoft.Json.JsonConvert.DeserializeObject<TResult>(str);
}
catch (Exception )
{
//logger.Error("Post 失败:{0}\r\n{1}", url, e.ToString());
string str = await response.Content.ReadAsStringAsync().ConfigureAwait(true);
//logger.Error(str);
throw;
}
}
}
public async Task<TResult> SubmitFormAsync<TResult>(string url, Dictionary<string, string> formData, string method = "Post")
{
HttpMethod hm = new HttpMethod(method);
using (var request = new HttpRequestMessage(hm, url))
{
request.Content = new FormUrlEncodedContent(formData);
var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).ConfigureAwait(true);
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized ||
response.StatusCode == System.Net.HttpStatusCode.Forbidden)
{
throw new UnauthorizedAccessException("验证失败");
}
response.EnsureSuccessStatusCode();
string str = await response.Content.ReadAsStringAsync().ConfigureAwait(true);
return Newtonsoft.Json.JsonConvert.DeserializeObject<TResult>(str);
}
}
public static string CreateUrl(string url, NameValueCollection qs)
{
if (qs != null && qs.Count > 0)
{
StringBuilder sb = new StringBuilder();
List<string> kl = qs.AllKeys.ToList();
foreach (string k in kl)
{
if (sb.Length > 0)
{
sb.Append("&");
}
sb.Append(k).Append("=");
if (!String.IsNullOrEmpty(qs[k]))
{
sb.Append(System.Net.WebUtility.UrlEncode(qs[k]));
}
}
if (url != null)
{
if (url.Contains("?"))
{
url = url + "&" + sb.ToString();
}
else
{
url = url + "?" + sb.ToString();
}
}
}
return url;
}
#region Global
public async Task<List<UserLoginInfo>> GetAllUserLoginInfosAsync()
{
var back = await AutoRetry.RunAsync<ResponseMessage<List<UserLoginInfo>>>(() =>
{
return GetAsync<ResponseMessage<List<UserLoginInfo>>>(
$"{IngestDbUrl}/{USERAPI20}/userlogininfo/all", null, GetIngestHeader()
);
}).ConfigureAwait(true);
if (back != null)
{
return back.Ext;
}
return null;
}
#endregion
#region Task
public async Task<TaskSource> GetTaskSourceByTaskIdAsync(int taskid)
{
var back = await AutoRetry.RunAsync<ResponseMessage<TaskSource>>(() =>
{
return GetAsync<ResponseMessage<TaskSource>>(
$"{IngestDbUrl}/{TASKAPI20}/tasksource/{taskid}",
null, GetIngestHeader());
}).ConfigureAwait(true);
if (back != null)
{
return back.Ext;
}
return TaskSource.emUnknowTask;
}
public async Task<DispatchTask> GetTaskDBAsync(int taskid)
{
var back = await GetAsync<ResponseMessage<DispatchTask>>(
$"{IngestDbUrl}/{TASKAPI30}/db/{taskid}", null, GetIngestHeader()
).ConfigureAwait(true);
if (back != null)
{
return back.Ext;
}
return null;
}
public async Task<TaskContent> ReScheduleTaskChannelAsync(int oldtaskid)
{
var back = await AutoRetry.RunAsync<ResponseMessage<TaskContent>>(() =>
{
return PutAsync<ResponseMessage<TaskContent>>(
$"{IngestDbUrl}/{TASKAPI30}/reschedule/channel/{oldtaskid}", null,
GetIngestHeader());
}).ConfigureAwait(true);
if (back != null && back.IsSuccess())
{
return back.Ext;
}
return null;
}
#endregion
}
  1. 在异步方法中,不要使用 Thread.Sleep;在同步方法中,不要使用Task.Delay ,否则可能出现线程死锁,结果难出来。

  2. 吞吐量(TPS)、QPS(每秒查询率)、并发数、响应时间(RT)
    当时为了增加qps,把所有webapi接口都改成异步请求

// //