.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 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; }
    }

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

修改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);
            #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部分是我們這里需要用到的,這里我把項目名稱當作服務注冊標識。

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

    [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中進行,而不是再去訪問單獨的鑒權服務。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM