- 新建网站项目然后添加ocelot 的nuget包
- 新建ocelot.json的网关的配置文件
{ "GlobalConfiguration": { "BaseUrl": "https://api.mybusiness.com" }, "ReRoutes": [ { "DownstreamPathTemplate": "/api/values", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 60907 } ], "UpstreamPathTemplate": "/users", "UpstreamHttpMethod": [ "Get" ], "AuthenticationOptions": { "AuthenticationProviderKey": "finbook", "AllowedScopes": [] } }, { "DownstreamPathTemplate": "/connect/token", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 52619 } ], "UpstreamPathTemplate": "/connect/token", "UpstreamHttpMethod": [ "Post" ] } ] }
- 添加配置文件进入netcore管道
public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, builder) => { builder .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .AddJsonFile("Ocelot.json"); }) .UseStartup<Startup>() .Build(); }
public void ConfigureServices(IServiceCollection services) { services.AddOcelot(); }
- 集成identityserver
services.AddAuthentication() .AddIdentityServerAuthentication(authenticationProviderKey, options=> { options.Authority = "http://localhost:52619/"; options.ApiName = "geteway_api"; options.SupportedTokens = SupportedTokens.Both; options.ApiSecret = "secret"; options.RequireHttpsMetadata = false;//是否是https请求 });
- 参考自大佬文章http://www.jessetalk.cn/2018/03/19/net-core-apigateway-ocelot-docs/