微服務網關從零搭建——(二)搭建api網關(不帶驗證)

環境準備

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

完成後在appsettings.json 添加節點api

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

搭建過程

添加文件configuration.jsonide

{
  "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

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

修改Program.cs 以下:this

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引用url

修改startup.cs文件爲spa

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 均須要設置3d

 

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

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

 

到此帶有consul的網關搭建完成

相關文章
相關標籤/搜索