1.服務註冊json
在上一篇的鑑權和登陸服務中分別經過NuGet引用Consul這個包,同時新增AppBuilderExtensions類:api
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 consul var 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; } }
經過這個類能夠提供服務註冊的基本參數。app
修改Startup啓動項中的Configure方法:ide
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); #endregion app.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部分是咱們這裏須要用到的,這裏我把項目名稱看成服務註冊標識。優化
而後還須要爲兩個服務添加兩個方法,一個是用來作健康檢查的,一個是用來測試的:ui
[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界面:this
這裏能夠看到咱們已經註冊的兩個服務。url
2.服務發現spa
新建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中進行,而不是再去訪問單獨的鑑權服務。