日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.Net Core 商城微服务项目系列(二):使用Ocelot + Consul构建具备服务注册和发现功能的网关...

發布時間:2023/11/29 asp.net 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .Net Core 商城微服务项目系列(二):使用Ocelot + Consul构建具备服务注册和发现功能的网关... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.服務注冊

在上一篇的鑒權和登錄服務中分別通過NuGet引用Consul這個包,同時新增AppBuilderExtensions類:

public static class AppBuilderExtensions{public static IApplicationBuilder RegisterConsul(this IApplicationBuilder app,IApplicationLifetime lifetime,ServiceEntity serviceEntity){var consulClient = new ConsulClient(x => x.Address = new Uri($"http://{serviceEntity.ConsulIP}:{serviceEntity.ConsulPort}"));//請求注冊的Consul地址var httpCheck = new AgentServiceCheck(){DeregisterCriticalServiceAfter=TimeSpan.FromSeconds(5),//服務啟動多久后注冊Interval=TimeSpan.FromSeconds(10),//健康檢查時間間隔,或者成為心跳間隔HTTP=$"http://{serviceEntity.IP}:{serviceEntity.Port}/api/health",//健康檢查地址Timeout=TimeSpan.FromSeconds(5)};//Register service with consulvar registration = new AgentServiceRegistration(){Checks = new[] {httpCheck},ID=Guid.NewGuid().ToString(),Name=serviceEntity.ServiceName,Address=serviceEntity.IP,Port=serviceEntity.Port,Tags = new[] { $"urlprefix-/{serviceEntity.ServiceName}"} //添加urlprefix-/servicename格式的tag標簽,以便Fabio識別 };consulClient.Agent.ServiceRegister(registration).Wait();//服務啟動時注冊,內部實現其實就是使用Consul API進行注冊(HttpClient發起)lifetime.ApplicationStopping.Register(() =>{consulClient.Agent.ServiceDeregister(registration.ID).Wait();//服務停止時取消注冊 });return app;}}public class ServiceEntity{public string IP { get; set; }public int Port { get; set; }public string ServiceName { get; set; }public string ConsulIP { get; set; }public int ConsulPort { get; set; }}

通過這個類可以提供服務注冊的基本參數。

修改Startup啟動項中的Configure方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}#region Consul 服務注冊ServiceEntity serviceEntity = new ServiceEntity{IP = "127.0.0.1", //服務運行地址Port = Convert.ToInt32(Configuration["Consul:ServicePort"]), //服務運行端口ServiceName = Configuration["Consul:Name"], //服務標識,Ocelot中會用到ConsulIP = Configuration["Consul:IP"], //Consul運行地址ConsulPort = Convert.ToInt32(Configuration["Consul:Port"]) //Consul運行端口(默認8500) };app.RegisterConsul(lifetime, serviceEntity);#endregionapp.UseIdentityServer();//app.UseAuthentication(); app.UseStaticFiles();app.UseMvcWithDefaultRoute();}

看下配置文件需要新增的東西:

{"Service": {"Name": "MI.Service","Port": "7001","DocName": "Account Service","Version": "v1","Title": "Account Service API"},"Identity": {"IP": "localhost","Port": "7000","Scheme": "Bearer"},"ConnectionStrings": {"SqlConnection": "server=.;uid=sa;pwd=sa;database=MI"},"Consul": {"Name": "MI.Service.Account","ServiceProt": "7001","IP": "localhost","Port": "8500"} }

藍色標識的Consul部分是我們這里需要用到的,這里我把項目名稱當作服務注冊標識。

然后還需要為兩個服務添加兩個方法,一個是用來做健康檢查的,一個是用來測試的:

[Route("api/Health")]public class HealthController : Controller{[HttpGet]public IActionResult Get() => Ok("ok");} public class MiUserController : Controller{public MIContext _context;public MiUserController(MIContext _context){this._context = _context;}public string Index(){return "Successful";}。。。。。。 }

?

通過“consul agent -dev”命令運行Consul,訪問127.0.0.1:8500我們可以看到Consul的UI界面:

這里可以看到我們已經注冊的兩個服務。

?

2.服務發現

新建API項目MI.Ocelot,通過NuGet引用Ocelot和Ocelot.Provider.Consul兩個包,并修改啟動項注冊Ocelot和Consul:

public void ConfigureServices(IServiceCollection services){//services.AddMvc(); services.AddOcelot(Configuration).AddConsul();}// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.UseOcelot();//app.UseMvc(); }

然后添加配置文件consul.json:

{"ReRoutes": [{"UseServiceDiscovery": true, //啟用服務發現"DownstreamPathTemplate": "/Account/{url}", //下游轉發路由"DownstreamScheme": "http", //標識頭"ServiceName": "MI.Service.Account", //服務注冊標識"LoadBalancer": "RoundRobin", //服務均衡:輪詢"UpstreamPathTemplate": "/Account/{url}", //上游請求路由"UpstreamHttpMethod": [ "Get", "Post" ], //請求的方法類型"ReRoutesCaseSensitive": false //不區分大小寫 },{"UseServiceDiscovery": true,"DownstreamPathTemplate": "/Identity/{url}","DownstreamScheme": "http","ServiceName": "MI.Service.IdentityServer","LoadBalancer": "RoundRobin","UpstreamPathTemplate": "/Identity/{url}","UpstreamHttpMethod": [ "Get", "Post" ],"ReRoutesCaseSensitive": false}],"GlobalConfiguration": {//"BaseUrl": "http://localhost:7003","ServiceDiscoveryProvider": {"Host": "127.0.0.1", // Consul Service IP"Port": 8500, // Consul Service Port"Type": "PollConsul","PollingInterval": 100 //健康檢查時間端 }} }

在Program中啟用這個配置文件:

public static IWebHost BuildWebHost(string[] args) =>WebHost.CreateDefaultBuilder(args).UseStartup<Startup>().ConfigureAppConfiguration((hostingContext,builder)=> {builder.AddJsonFile("consul.json");}).Build();

到此,網關配置完畢。現在我將網關項目MI.Gateway部署在7003端口,登錄服務MI.Service.Account部署在7001端口,鑒權服務部署在7000端口,我會通過訪問網關服務來請求登錄服務:

這里的流程是這樣的,Ocelot通過“/Account/MiUser/Index”匹配到了“/Account/{url}”這個路由,進而拿到了“MI.Service.Account”這個服務注冊標識,然后通過Consul拿到了對應的地址,并轉發了請求,同時返回結果。

?

到此,具備服務注冊和發現的簡單網關服務就搭建完畢了,后面有時間會繼續優化,添加限流、熔斷,同時身份驗證會在Ocelot中進行,而不是再去訪問單獨的鑒權服務。

?

轉載于:https://www.cnblogs.com/weiBlog/p/9833807.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的.Net Core 商城微服务项目系列(二):使用Ocelot + Consul构建具备服务注册和发现功能的网关...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。