<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服務(wù)注冊(cè),服務(wù)發(fā)現(xiàn)

       

      Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Ocelot+Polly緩存、限流、熔斷、降級(jí)

      Consul+Ocelot+Polly在.NetCore中使用(.NET5)-網(wǎng)關(guān)Ocelot+Consul

      微服務(wù)網(wǎng)關(guān)Ocelot加入IdentityServer4鑒權(quán)-.NetCore(.NET5)中使用

      Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服務(wù)注冊(cè),服務(wù)發(fā)現(xiàn)

      一、簡(jiǎn)介

      環(huán)境 .NET5,Consul_v1.10.2

      在微服務(wù)中利用Consul可以實(shí)現(xiàn)服務(wù)的注冊(cè),服務(wù)發(fā)現(xiàn),治理,健康檢查等。 

       

      Web調(diào)站點(diǎn)需要調(diào)用多個(gè)服務(wù),如果沒(méi)有Consul,可能就是Web中存了全部服務(wù)的ip地址,如果其中一個(gè)服務(wù)更換了地址,web也要跟著修改配置,所以加入了Consul,web直接通過(guò)Consul就能一直取到各個(gè)服務(wù)的最新的地址了。

      二、Consul搭建

      這里使用Docker安裝 ,確保安裝了Docker,執(zhí)行下面命令。

      docker run -d -p 8500:8500 --restart=always --name=consul consul:latest agent -server -bootstrap -ui -node=1 -client='0.0.0.0'

      如果是windows環(huán)境,到官網(wǎng)https://www.consul.io 下載exe文件。然后cmd命令切換到consul.exe目錄,執(zhí)行consul.exe agent -dev 即可啟動(dòng)。

      安裝完,訪問(wèn)http:ip:8500,看到如下界面則安裝成功。

       

      三、服務(wù)注冊(cè)

      安裝NuGet包 --Consul

      appsettings.json增加Consul信息

      復(fù)制代碼
      {
        "Logging": {
          "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
          }
        },
        "AllowedHosts": "*",
        "urls": "http://*:5200",
        "Consul": {
          "consulAddress": "http://172.16.2.84:8500",
          "serviceName": "api",
          "currentIp": "172.16.2.9",
          "currentPort": "5200"
        }
      
      }
      復(fù)制代碼

      增加ConsulRegister.cs

      復(fù)制代碼
       /// <summary>
          /// Consul注冊(cè)
          /// </summary>
          public static class ConsulRegister
          {
              //服務(wù)注冊(cè)
              public static IApplicationBuilder UseConsul(this IApplicationBuilder app, IConfiguration configuration)
              {
                  // 獲取主機(jī)生命周期管理接口
                  var lifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();
      
                  ConsulClient client = new ConsulClient(c =>
                  {
                      c.Address = new Uri(configuration["Consul:consulAddress"]);
                      c.Datacenter = "dc1";
                  });
                  string ip = configuration["ip"];
                  string port = configuration["port"];
                  string currentIp = configuration["Consul:currentIP"];
                  string currentPort = configuration["Consul:currentPort"];
      
                  ip = string.IsNullOrEmpty(ip) ? currentIp : ip; //當(dāng)前程序的IP
                  port = string.IsNullOrEmpty(port) ? currentPort : port; //當(dāng)前程序的端口
                  string serviceId = $"service:{ip}:{port}";//服務(wù)ID,一個(gè)服務(wù)是唯一的
                  //服務(wù)注冊(cè)
                  client.Agent.ServiceRegister(new AgentServiceRegistration()
                  {
                      ID = serviceId, //唯一的
                      Name = configuration["Consul:serviceName"], //組名稱(chēng)-Group
                      Address = ip, //ip地址
                      Port = int.Parse(port), //端口
                      Tags = new string[] { "api站點(diǎn)" },
                      Check = new AgentServiceCheck()
                      {
                          Interval = TimeSpan.FromSeconds(10),//多久檢查一次心跳
                          HTTP = $"http://{ip}:{port}/Health/Index",
                          Timeout = TimeSpan.FromSeconds(5),//超時(shí)時(shí)間
                          DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5) //服務(wù)停止多久后注銷(xiāo)服務(wù)
                      }
      
                  }).Wait();
                  //應(yīng)用程序終止時(shí),注銷(xiāo)服務(wù)
                  lifetime.ApplicationStopping.Register(() =>
                  {
                      client.Agent.ServiceDeregister(serviceId).Wait();
                  });
                  return app;
              }
          }
      復(fù)制代碼

      在Startup.cs中 Configure(IApplicationBuilder app, IWebHostEnvironment env)方法后面加上  app.UseConsul(Configuration);

      復(fù)制代碼
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
              {
                  if (env.IsDevelopment())
                  {
                      app.UseDeveloperExceptionPage();
                  }
                  else
                  {
                      app.UseExceptionHandler("/Home/Error");
                      // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                      app.UseHsts();
                  }
                  app.UseHttpsRedirection();
                  app.UseStaticFiles();
      
                  app.UseRouting();
      
                  app.UseAuthorization();
      
                  app.UseEndpoints(endpoints =>
                  {
                      endpoints.MapControllerRoute(
                          name: "default",
                          pattern: "{controller=Home}/{action=Index}/{id?}");
                  });
                  //Consul注冊(cè)
                  app.UseConsul(Configuration);
              }
      復(fù)制代碼

      增加健康檢查接口

      上面Consul注冊(cè)處有一個(gè)Check  Http的是心跳健康檢查的地址,需要提供一個(gè)接口。

      新建HealthController.cs

      復(fù)制代碼
       /// <summary>
          /// consul健康檢查
          /// </summary>
          public class HealthController : Controller
          {
              public IActionResult Index()
              {
                  return Ok();
              }
          }
      復(fù)制代碼

       

      這樣就配置好了,啟動(dòng)項(xiàng)目時(shí)就會(huì)把服務(wù)注冊(cè)到Consul,我這里用發(fā)布文件同時(shí)啟動(dòng)三個(gè)做負(fù)載。

      dotnet ConsulAndOcelot.Demo.ServerB.dll --urls="http://*5201" --ip="172.16.2.9" --port=5201
      
      dotnet ConsulAndOcelot.Demo.ServerB.dll --urls="http://*5202" --ip="172.16.2.9" --port=5202
      
      dotnet ConsulAndOcelot.Demo.ServerB.dll --urls="http://*5203" --ip="172.16.2.9" --port=5203

       

       

       

       

       

       

      啟動(dòng)后,再看一下Consul界面,可以發(fā)現(xiàn)服務(wù)已成功注冊(cè)到Consul。

       

       

      四、服務(wù)發(fā)現(xiàn)

      另外建一個(gè).NetCore程序。

      安裝Nuget包 --Consul

      appsettings.json 配置Consul信息

      復(fù)制代碼
      {
        "Logging": {
          "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
          }
        },
        "AllowedHosts": "*",
        "Consul": {
          "consulAddress": "http://172.16.2.84:8500",
          "serviceName": "platform",
          "apiServiceName": "api"
        }
      }
      復(fù)制代碼

      新建ConsulHelper.cs類(lèi)

      復(fù)制代碼
        /// <summary>
          /// Consul幫助類(lèi)
          /// </summary>
          public class ConsulHelper
          {
              private IConfiguration _configuration;
              public ConsulHelper(IConfiguration configuration)
              {
                  _configuration = configuration;
              }
              /// <summary>
              /// 根據(jù)服務(wù)名稱(chēng)獲取服務(wù)地址
              /// </summary>
              /// <param name="serviceName"></param>
              /// <returns></returns>
              public string GetDomainByServiceName(string serviceName)
              {
                  string domain = string.Empty;
                  //Consul客戶(hù)端
                  using (ConsulClient client = new ConsulClient(c =>
                  {
                      c.Address = new Uri(_configuration["Consul:consulAddress"]);
                      c.Datacenter = "dc1";
      
                  })
                  )
                  {
                      //根據(jù)服務(wù)名獲取健康的服務(wù)
                      var queryResult = client.Health.Service(serviceName, string.Empty, true);
                      var len = queryResult.Result.Response.Length;
                      //平均策略-多個(gè)負(fù)載中隨機(jī)獲取一個(gè)
                      var node = queryResult.Result.Response[new Random().Next(len)];
                      domain = $"http://{node.Service.Address}:{node.Service.Port}";
                  }
                  return domain;
              }
      
              /// <summary>
              /// 獲取api域名
              /// </summary>
              /// <returns></returns>
              public string GetApiDomain()
              {
                  return GetDomainByServiceName(_configuration["Consul:apiServiceName"]);
              }
          }
      復(fù)制代碼

      把ConsulHelper注入到IOC容器,Startup.cs中。ConfigureServices(IServiceCollection services)方法加上

       public void ConfigureServices(IServiceCollection services)
              {
                  services.AddControllersWithViews();
                  services.AddTransient<ConsulHelper>();
              }

      驗(yàn)證

      復(fù)制代碼
       public class HomeController : Controller
          {
              private readonly ILogger<HomeController> _logger;
              private readonly ConsulHelper _consulHelper;
      
              public HomeController(ILogger<HomeController> logger, ConsulHelper consulHelper)
              {
                  _logger = logger;
                  _consulHelper = consulHelper;
              }
      
              public IActionResult Index()
              {
                  ///獲取api服務(wù)地址
                  var domain = _consulHelper.GetApiDomain();
                  ViewBag.domain = domain;
                  return View();
              }
      }
      復(fù)制代碼

      執(zhí)行結(jié)果,通過(guò)Consul獲得了服務(wù)地址,刷新后會(huì)隨獲取到三個(gè)負(fù)載中的一個(gè)。

       

       

      Consul只負(fù)責(zé)服務(wù)發(fā)現(xiàn),沒(méi)有自帶負(fù)載均衡策略。用上面的平均策略就可以了,如果想要做成輪詢(xún)策略的也可以,不過(guò)會(huì)增加一些開(kāi)銷(xiāo),可以給每組服務(wù)定義一個(gè)

      靜態(tài)自增變量index=0,然后獲取的時(shí)候,index%服務(wù)數(shù)取余,然后index++,這樣就是0%3=0,1%3=1,2%3=2,3%3=0一直循環(huán),獲取List[index]服務(wù),index>10000的時(shí)候重置為0,這樣就能循環(huán)的調(diào)用了。Conul后面配合Ocelot網(wǎng)關(guān)使用,Ocelot中會(huì)自帶幾種負(fù)載策略。

      源碼地址:https://github.com/weixiaolong325/Ocelot-Consul-Polly-Id4.Demo

      引用:Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服務(wù)注冊(cè),服務(wù)發(fā)現(xiàn) - 包子wxl - 博客園 (cnblogs.com)

      posted @ 2023-01-31 16:35  盛開(kāi)的雨季  閱讀(116)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 亚洲区综合区小说区激情区| 亚洲国产精品成人综合色在| 亚洲免费人成网站在线观看| 浮妇高潮喷白浆视频| 亚洲精品国产美女久久久| 国产欧洲欧洲久美女久久| 亚洲免费人成在线视频观看| 免费看国产曰批40分钟| 久久夜色精品国产亚av| 一区二区三区四区五区自拍| 99在线视频免费观看| 日本亚洲一区二区精品| 中文字幕亚洲精品乱码| 久久久成人毛片无码| 欧美福利电影A在线播放| 日本大片在线看黄a∨免费| 欧美高清狂热视频60一70| 扒开双腿猛进入喷水高潮叫声| 久久精品国产中文字幕| 中文字幕无码不卡一区二区三区| 麻豆成人av不卡一二三区| 99久热在线精品视频| 国产a在视频线精品视频下载| 国产在线国偷精品产拍| 韩国 日本 亚洲 国产 不卡| 被黑人伦流澡到高潮HNP动漫 | 伊人成人在线视频免费| 国产精品国产三级国产试看| 亚洲av网一区天堂福利| 最新国产精品亚洲| 蜜桃臀av一区二区三区| 九九久久精品国产免费看小说 | 亚洲aⅴ天堂av天堂无码麻豆| 女子spa高潮呻吟抽搐| 色爱综合另类图片av| 高清无码18| 日本人一区二区在线观看| 人妻系列无码专区免费| 手游| 无码人妻少妇色欲av一区二区 | AV人摸人人人澡人人超碰|