Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

128 linhas
4.1KB

  1. using dotnet_etcd;
  2. using Etcdserverpb;
  3. using Google.Protobuf;
  4. using HealthMonitor.Model.Config;
  5. using Microsoft.Extensions.Logging;
  6. using Microsoft.Extensions.Options;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net.Sockets;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace HealthMonitor.Service.Etcd
  14. {
  15. public class EtcdService
  16. {
  17. private readonly ServiceConfig _configService;
  18. private readonly ILogger<EtcdService> _logger;
  19. private readonly EtcdClient _etcdClient;
  20. public EtcdService(IOptions<ServiceConfig> _optConfigService, ILogger<EtcdService> logger)
  21. {
  22. _configService = _optConfigService.Value;
  23. _logger = logger;
  24. _etcdClient = new EtcdClient(_configService.EtcdServerAddress);
  25. }
  26. public async Task<string> GetValAsync(string key)
  27. {
  28. return await _etcdClient.GetValAsync(key);
  29. }
  30. public async Task<RangeResponse> GetResponseAsync(string key)
  31. {
  32. return await _etcdClient.GetAsync(key);
  33. }
  34. public async Task<RangeResponse> GetValWithPrefixAsync(string keyWithPREFIX)
  35. {
  36. return await _etcdClient.GetRangeAsync(keyWithPREFIX);
  37. }
  38. //
  39. public async Task PutValAsync(string key, string value, long? ttl = null, bool? keepalived = false)
  40. {
  41. try
  42. {
  43. PutRequest request = new()
  44. {
  45. Key = ByteString.CopyFromUtf8(key),
  46. Value = ByteString.CopyFromUtf8(value),
  47. };
  48. if (ttl != null)
  49. {
  50. long leaseId = _etcdClient.LeaseGrant(new LeaseGrantRequest { TTL = (long)ttl }).ID;
  51. request = new PutRequest
  52. {
  53. Key = ByteString.CopyFromUtf8(key),
  54. Value = ByteString.CopyFromUtf8(value),
  55. Lease = leaseId
  56. };
  57. if ((bool)keepalived!)
  58. {
  59. //await _etcdClient.PutAsync(request);
  60. _ = _etcdClient.LeaseKeepAlive(leaseId, CancellationToken.None);
  61. //await _etcdClient.LeaseKeepAlive(leaseId, CancellationToken.None);
  62. request = new PutRequest
  63. {
  64. Key = ByteString.CopyFromUtf8(key),
  65. Value = ByteString.CopyFromUtf8(value),
  66. Lease = leaseId
  67. };
  68. //await _etcdClient.PutAsync(request);
  69. //return;
  70. }
  71. }
  72. await _etcdClient.PutAsync(request);
  73. }
  74. catch (Exception ex)
  75. {
  76. _logger.LogInformation($"{nameof(EtcdService)} --- {nameof(PutValAsync)} 出错\n{ex.Message}\n{ex.StackTrace}");
  77. }
  78. // await _etcdClient.PutAsync(key, value,null);
  79. }
  80. public async Task PutValAsync(string key, string value, DateTime deadline)
  81. {
  82. PutRequest request = new()
  83. {
  84. Key = ByteString.CopyFromUtf8(key),
  85. Value = ByteString.CopyFromUtf8(value),
  86. };
  87. await _etcdClient.PutAsync(request, null, deadline);
  88. // await _etcdClient.PutAsync(key, value,null);
  89. }
  90. public async Task DeleteKeyValAsync(string key)
  91. {
  92. await _etcdClient.DeleteAsync(key);
  93. }
  94. public async Task DeleteKeyValWithPrefixAsync(string keyWithPREFIX)
  95. {
  96. await _etcdClient.DeleteRangeAsync(keyWithPREFIX);
  97. }
  98. public async Task WacthKeysWithPrefixAsync(string keyWithPREFIX, Action<WatchEvent[]> action)
  99. {
  100. await _etcdClient.WatchRangeAsync(keyWithPREFIX, action);
  101. }
  102. public async Task WacthKeysWithPrefixResponseAsync(string keyWithPREFIX, Action<WatchResponse> action)
  103. {
  104. await _etcdClient.WatchRangeAsync(keyWithPREFIX, action);
  105. }
  106. }
  107. }