(更新時間)2021年5月28日 商城高并發秒殺系統(.NET Core版) 06-注冊中心組件的封裝

      網友投稿 680 2022-05-30

      一:consul注冊發現封裝

      ///

      /// consul服務發現實現 /// public class ConsulServiceDiscovery : AbstractServiceDiscovery { public ConsulServiceDiscovery(IOptions options) : base(options) { } /*public ConsulServiceDiscovery(IOptions options) { this.serviceDiscoveryOptions = options.Value; }*/ /*public List Discovery(string serviceName) { // 1.2、從遠程服務器取 CatalogService[] queryResult = RemoteDiscovery(serviceName); var list = new List(); foreach (var service in queryResult) { list.Add(new ServiceNode { Url = service.ServiceAddress + ":" + service.ServicePort }); } return list; }*/ protected override CatalogService[] RemoteDiscovery(string serviceName) { // 1、創建consul客戶端連接 2s 1、使用單例全局共享 2、使用數據緩存(進程:字典,集合) 3、使用連接池 var consulClient = new ConsulClient(configuration => { //1.1 建立客戶端和服務端連接 configuration.Address = new Uri(serviceDiscoveryOptions.DiscoveryAddress); }); // 2、consul查詢服務,根據具體的服務名稱查詢 var queryResult = consulClient.Catalog.Service(serviceName).Result; // 3、判斷請求是否失敗 if (!queryResult.StatusCode.Equals(HttpStatusCode.OK)) { throw new FrameException($"consul連接失敗:{queryResult.StatusCode}"); } return queryResult.Response; } }

      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

      ///

      /// consul服務注冊實現 /// public class ConsulServiceRegistry : IServiceRegistry { // 服務注冊選項 public readonly ServiceRegistryOptions serviceRegistryOptions; public ConsulServiceRegistry(IOptions options) { this.serviceRegistryOptions = options.Value; } /// /// 注冊服務 /// public void Register() { // 1、創建consul客戶端連接 var consulClient = new ConsulClient(configuration => { //1.1 建立客戶端和服務端連接 configuration.Address = new Uri(serviceRegistryOptions.RegistryAddress); }); // 2、獲取 var uri = new Uri(serviceRegistryOptions.ServiceAddress); // 3、創建consul服務注冊對象 var registration = new AgentServiceRegistration() { ID = string.IsNullOrEmpty(serviceRegistryOptions.ServiceId) ? Guid.NewGuid().ToString() : serviceRegistryOptions.ServiceId, Name = serviceRegistryOptions.ServiceName, Address = uri.Host, Port = uri.Port, Tags = serviceRegistryOptions.ServiceTags, Check = new AgentServiceCheck { // 3.1、consul健康檢查超時間 Timeout = TimeSpan.FromSeconds(10), // 3.2、服務停止5秒后注銷服務 DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5), // 3.3、consul健康檢查地址 HTTP = $"{uri.Scheme}://{uri.Host}:{uri.Port}{serviceRegistryOptions.HealthCheckAddress}", // 3.4 consul健康檢查間隔時間 Interval = TimeSpan.FromSeconds(10), } }; // 4、注冊服務 consulClient.Agent.ServiceRegister(registration).Wait(); Console.WriteLine($"服務注冊成功:{serviceRegistryOptions.ServiceAddress}"); // 5、關閉連接 consulClient.Dispose(); } /// /// 注銷服務 /// /// public void Deregister() { // 1、創建consul客戶端連接 var consulClient = new ConsulClient(configuration => { //1.1 建立客戶端和服務端連接 configuration.Address = new Uri(serviceRegistryOptions.RegistryAddress); }); // 2、注銷服務 consulClient.Agent.ServiceDeregister(serviceRegistryOptions.ServiceId).Wait(); Console.WriteLine($"服務注銷成功:{serviceRegistryOptions.ServiceAddress}"); // 3、關閉連接 consulClient.Dispose(); } }

      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

      二:配置選項

      ///

      /// 服務發現選項 /// public class ServiceDiscoveryOptions { public ServiceDiscoveryOptions() { // 默認地址 this.DiscoveryAddress = "http://localhost:8500"; } /// /// 服務發現地址 /// public string DiscoveryAddress { set; get; } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      ///

      /// 節點注冊選項 /// public class ServiceRegistryOptions { public ServiceRegistryOptions() { this.ServiceId = Guid.NewGuid().ToString(); this.RegistryAddress = "http://localhost:8500"; this.HealthCheckAddress = "/HealthCheck"; } // 服務ID public string ServiceId { get; set; } // 服務名稱 public string ServiceName { get; set; } // 服務地址http://localhost:5001 public string ServiceAddress { get; set; } // 服務標簽(版本) public string[] ServiceTags { set; get; } /*// 服務地址(可以選填 === 默認加載啟動路徑(localhost)) public string ServiceAddress { set; get; } // 服務端口號(可以選填 === 默認加載啟動路徑端口) public int ServicePort { set; get; } // Https 或者 http public string ServiceScheme { get; set; }*/ // 服務注冊地址 public string RegistryAddress { get; set; } // 服務健康檢查地址 public string HealthCheckAddress { get; set; } }

      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

      三:consul相關擴展

      ///

      /// 服務發現IOC容器擴展 /// public static class ServiceDiscoveryServiceCollectionExtensions { // consul服務發現 public static IServiceCollection AddServiceDiscovery(this IServiceCollection services) { // 1、注冊consul服務發現 AddServiceDiscovery(services, options => { }); return services; } public static IServiceCollection AddServiceDiscovery(this IServiceCollection services, Action options) { // 2、注冊到IOC容器 services.Configure(options); // 3、注冊consul服務發現 services.AddSingleton(); return services; } }

      1

      (更新時間)2021年5月28日 商城高并發秒殺系統(.NET Core版) 06-注冊中心組件的封裝

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      ///

      /// 微服務注冊擴展 /// public static class ServiceRegistryApplicationBuilderExtensions { public static IApplicationBuilder UseRegistry(this IApplicationBuilder app) { // 1、從IOC容器中獲取Consul服務注冊配置 var serviceNode = app.ApplicationServices.GetRequiredService>().Value; // 2、獲取應用程序生命周期 var lifetime = app.ApplicationServices.GetRequiredService(); // 2.1 獲取服務注冊實例 var serviceRegistry = app.ApplicationServices.GetRequiredService(); // 3、獲取服務地址 var features = app.Properties["server.Features"] as FeatureCollection; var address = features.Get().Addresses.First(); var uri = new Uri(address); // 4、注冊服務 serviceNode.Id = Guid.NewGuid().ToString(); //serviceNode.Address = $"{uri.Scheme}://{uri.Host}"; serviceNode.Address = uri.Host; serviceNode.Port = uri.Port; serviceNode.HealthCheckAddress = $"{uri.Scheme}://{uri.Host}:{uri.Port}{serviceNode.HealthCheckAddress}"; serviceRegistry.Register(serviceNode); // 5、服務器關閉時注銷服務 lifetime.ApplicationStopping.Register(() => { serviceRegistry.Deregister(serviceNode); }); return app; } }

      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

      ///

      /// 服務注冊IOC容器擴展 /// public static class ServiceRegistryServiceCollectionExtensions { // consul服務注冊 public static IServiceCollection AddServiceRegistry(this IServiceCollection services) { AddServiceRegistry(services, optons => { }); return services; } // consul服務注冊 public static IServiceCollection AddServiceRegistry(this IServiceCollection services, Action options) { // 1、配置選項到IOC services.Configure(options); // 2、注冊consul注冊 services.AddSingleton(); // 3、注冊開機自動注冊服務 services.AddHostedService(); return services; } }

      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

      四:相關依賴的類文件

      ///

      /// 服務url /// public class ServiceUrl { public string Url { set; get; } }

      1

      2

      3

      4

      5

      6

      7

      ///

      /// 服務啟動時自動注冊 /// public class ServiceRegistryIHostedService : IHostedService { private readonly IServiceRegistry serviceRegistry; public ServiceRegistryIHostedService(IServiceRegistry serviceRegistry) { this.serviceRegistry = serviceRegistry; } public Task StartAsync(CancellationToken cancellationToken) { /* // 1、從IOC容器中獲取Consul服務注冊配置 var serviceNode = app.ApplicationServices.GetRequiredService>().Value; // 2、獲取應用程序生命周期 var lifetime = app.ApplicationServices.GetRequiredService(); // 2.1 獲取服務注冊實例 var serviceRegistry = app.ApplicationServices.GetRequiredService();*/ // 3、獲取服務地址 /*var features = app.Properties["server.Features"] as FeatureCollection; var address = features.Get().Addresses.First();*/ //var uri = new Uri(address); // 4、注冊服務 /* serviceNode.Id = Guid.NewGuid().ToString(); //serviceNode.Address = $"{uri.Scheme}://{uri.Host}"; serviceNode.Address = uri.Host; serviceNode.Port = uri.Port; serviceNode.HealthCheckAddress = $"{uri.Scheme}://{uri.Host}:{uri.Port}{serviceNode.HealthCheckAddress}";*/ return Task.Run(() => serviceRegistry.Register()); /*// 5、服務器關閉時注銷服務 lifetime.ApplicationStopping.Register(() => { serviceRegistry.Deregister(serviceNode); });*/ } /// /// 服務停止時注銷 /// /// /// public Task StopAsync(CancellationToken cancellationToken) { serviceRegistry.Deregister(); return Task.CompletedTask; } }

      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

      ///

      /// 服務注冊節點 /// public class ServiceRegistryConfig { public ServiceRegistryConfig() { this.Id = Guid.NewGuid().ToString(); this.Name = ""; this.Address = this.RegistryAddress = "http://localhost:8500"; this.HealthCheckAddress = ""; } // 服務ID public string Id { get; set; } // 服務名稱 public string Name { get; set; } // 服務標簽(版本) public string[] Tags { set; get; } // 服務地址(可以選填 === 默認加載啟動路徑(localhost)) public string Address { set; get; } // 服務端口號(可以選填 === 默認加載啟動路徑端口) public int Port { set; get; } // 服務注冊地址 public string RegistryAddress { get; set; } // 服務健康檢查地址 public string HealthCheckAddress { get; set; } }

      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

      ///

      /// 服務Node,將所有服務抽象為Node /// public class ServiceNode { public string Url { set; get; } }

      1

      2

      3

      4

      5

      6

      7

      ///

      /// 服務發現配置 /// public class ServiceDiscoveryConfig { /// /// 服務注冊地址 /// public string RegistryAddress { set; get; } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      ///

      /// 服務注冊 /// public interface IServiceRegistry { /// /// 注冊服務 /// void Register(); /// /// 撤銷服務 /// void Deregister(); }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      ///

      /// 服務發現 /// public interface IServiceDiscovery { /// /// 服務發現 /// /// 服務名稱 /// List Discovery(string serviceName); }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      ///

      /// 抽象服務發現,主要是緩存功能 /// public abstract class AbstractServiceDiscovery : IServiceDiscovery { // 字典緩存 private readonly Dictionary> CacheConsulResult = new Dictionary>(); protected readonly ServiceDiscoveryOptions serviceDiscoveryOptions; public AbstractServiceDiscovery(IOptions options) { this.serviceDiscoveryOptions = options.Value; // 1、創建consul客戶端連接 var consulClient = new ConsulClient(configuration => { //1.1 建立客戶端和服務端連接 configuration.Address = new Uri(serviceDiscoveryOptions.DiscoveryAddress); }); // 2、consul 先查詢服務 var queryResult = consulClient.Catalog.Services().Result; if (!queryResult.StatusCode.Equals(HttpStatusCode.OK)) { throw new FrameException($"consul連接失敗:{queryResult.StatusCode}"); } // 3、獲取服務下的所有實例 foreach (var item in queryResult.Response) { QueryResult result = consulClient.Catalog.Service(item.Key).Result; if (!queryResult.StatusCode.Equals(HttpStatusCode.OK)) { throw new FrameException($"consul連接失敗:{queryResult.StatusCode}"); } var list = new List(); foreach (var service in result.Response) { list.Add(new ServiceNode { Url = service.ServiceAddress + ":" + service.ServicePort }); } CacheConsulResult.Add(item.Key, list); } } public List Discovery(string serviceName) { // 1、從緩存中查詢consulj結果 if (CacheConsulResult.ContainsKey(serviceName)) { return CacheConsulResult[serviceName]; } else { // 1.2、從遠程服務器取 CatalogService[] queryResult = RemoteDiscovery(serviceName); var list = new List(); foreach (var service in queryResult) { list.Add(new ServiceNode { Url = service.ServiceAddress + ":" + service.ServicePort }); } // 1.3 將結果添加到緩存 CacheConsulResult.Add(serviceName, list); return list; } } /// /// 遠程服務發現 /// /// /// protected abstract CatalogService[] RemoteDiscovery(string serviceName); }

      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

      .NET

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:在shiro基礎上整合jwt,可在項目中直接使用呦
      下一篇:由生產者/消費者問題看JAVA多線程
      相關文章
      亚洲欧美成人综合久久久| 久久久亚洲欧洲日产国码是AV| 亚洲电影国产一区| 丁香五月亚洲综合深深爱| 亚洲欧洲精品成人久久曰| 亚洲人成电影在线观看网| 亚洲免费一级视频| 亚洲最大成人网色香蕉| 久久精品国产亚洲AV久| 亚洲一区二区三区播放在线| 亚洲国产成人精品久久| 亚洲国产精品一区二区久| 激情内射亚洲一区二区三区爱妻 | 成人亚洲国产精品久久| 国产亚洲视频在线观看| 在线亚洲v日韩v| 亚洲人成无码久久电影网站| 中文字幕亚洲激情| 亚洲精品无码MV在线观看| 狠狠色婷婷狠狠狠亚洲综合| 亚洲中久无码永久在线观看同| 亚洲欧洲无码AV电影在线观看| 亚洲成AV人片一区二区密柚| 久久亚洲精品中文字幕无码| 少妇中文字幕乱码亚洲影视| 亚洲最新中文字幕| 国产成人精品日本亚洲专| 亚洲日韩av无码中文| 综合一区自拍亚洲综合图区 | 国产一区二区三区亚洲综合| 亚洲精品成人网久久久久久| 亚洲无码日韩精品第一页| 亚洲区小说区图片区QVOD| 亚洲情a成黄在线观看动漫尤物| 亚洲高清无在码在线电影不卡| 激情亚洲一区国产精品| 亚洲a∨国产av综合av下载| 国产成人99久久亚洲综合精品| 亚洲爆乳无码一区二区三区| 亚洲第一成年网站大全亚洲| 国产亚洲精品影视在线|