微服务网关从零搭建——(二)搭建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 } } }
参数说明参见上一篇结尾处。
修改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); }); } }
添加 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(); } } }
注意事项:
1.appsettings.json 和 configuration.json 均需要设置
2.services.AddOcelot(Configuration).AddConsul();
此处必须增加 服务发现的AddConsul
到此带有consul的网关搭建完成