1. 程式人生 > >微服務閘道器從零搭建——(二)搭建api閘道器(不帶驗證)

微服務閘道器從零搭建——(二)搭建api閘道器(不帶驗證)

環境準備

建立空的core2.1 api專案  演示使用名稱APIGateWay  過程參考上一篇

完成後在appsettings.json 新增節點

"Setting": {
"Port": "5000"
}

搭建過程

新增檔案configuration.json

{
  "ReRoutes": [
    // API:demo1
    {
      "UseServiceDiscovery": true,
      "DownstreamPathTemplate": "/api/{url}",
      "DownstreamScheme
": "http", "ServiceName": "demoAPi", "LoadBalancerOptions": { "Type": "RoundRobin" }, "UpstreamPathTemplate": "/demo1/{url}", "UpstreamHttpMethod": [ "Get", "Post" ], "ReRoutesCaseSensitive": false // non case sensitive } //, //// API:demo2 //
{ // "UseServiceDiscovery": true, // "DownstreamPathTemplate": "/api/{url}", // "DownstreamScheme": "http", // "ServiceName": "demoAPi2", // "LoadBalancerOptions": { // "Type": "RoundRobin" // }, // "UpstreamPathTemplate": "/demo2/{url}", // "UpstreamHttpMethod": [ "Get", "Post" ],
// "ReRoutesCaseSensitive": false // non case sensitive //} ], "GlobalConfiguration": { "ServiceDiscoveryProvider": { "Host": "localhost", // Consul Service IP "Port": 8500 // Consul Service Port } } }
configuration.json

引數說明參見上一篇結尾處。

修改Program.cs 如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace APIGateWay
{
    public class Program
    {
        public static string StartPort;
        public static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.Build();
            StartPort = config.GetSection("Setting")["Port"];
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls($"http://*:{StartPort}")
                .ConfigureAppConfiguration((hostingContext, builder) =>
                {
                    builder.AddJsonFile("configuration.json", false, true);
                });
    }
}
Program

新增 Ocelot.Provider.Consul nuget引用

修改startup.cs檔案為

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;

namespace APIGateWay
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOcelot(Configuration).AddConsul();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // 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.UseMvc();//no need
            app.UseOcelot().Wait();
        }
    }
}
Startup

 

注意事項:

1.appsettings.json 和 configuration.json 均需要設定

 

 2.services.AddOcelot(Configuration).AddConsul();

此處必須增加 服務發現的AddConsul

 

到此帶有consul的閘道器搭建完成