diff --git a/OcelotStudy.Getway/OcelotStudy.Getway.csproj b/OcelotStudy.Getway/OcelotStudy.Getway.csproj index 5372733..ed0c0c5 100644 --- a/OcelotStudy.Getway/OcelotStudy.Getway.csproj +++ b/OcelotStudy.Getway/OcelotStudy.Getway.csproj @@ -9,6 +9,18 @@ + + Always + + + Always + + + Always + + + Always + Always diff --git a/OcelotStudy.Getway/Program.cs b/OcelotStudy.Getway/Program.cs index 3452813..0f36a75 100644 --- a/OcelotStudy.Getway/Program.cs +++ b/OcelotStudy.Getway/Program.cs @@ -17,21 +17,56 @@ namespace OcelotStudy.Getway CreateHostBuilder(args).Build().Run(); } - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureAppConfiguration((hostingContext, config) => - { - //添加配置文件的使用 - config - .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) - .AddJsonFile("appsettings.json", true, true) - .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) - .AddJsonFile("ocelot.json") - .AddEnvironmentVariables(); - }) - .ConfigureWebHostDefaults(webBuilder => + public static IHostBuilder CreateHostBuilder(string[] args) + { + var builder = Host.CreateDefaultBuilder(args); + builder + .ConfigureAppConfiguration((hostingContext, config) => + { + config + .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) + .AddJsonFile("appsettings.json", true, true) + .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) + .AddEnvironmentVariables() + .AddCommandLine(args); + + LoadOcelotConfig(config, hostingContext); + }); + + return builder; + } + + static IConfigurationBuilder LoadOcelotConfig(IConfigurationBuilder config, HostBuilderContext hostingContext) + { + var configFromCommandLine = config.Build().GetValue("config", string.Empty); + + //命令行参数优化 + if (!string.IsNullOrEmpty(configFromCommandLine)) + { + if (configFromCommandLine.EndsWith(".json")) + { + config.AddJsonFile($"{configFromCommandLine}", true, true); + } + else { - webBuilder.UseStartup(); - }); + if (configFromCommandLine.StartsWith("ocelot.")) + { + config.AddJsonFile($"{configFromCommandLine}.json", false, true); + } + else + { + config.AddJsonFile($"ocelot.{configFromCommandLine}.json", false, true); + } + } + } + else + { + config + .AddJsonFile("ocelot.json", false, true) + .AddJsonFile($"ocelot.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true); + } + + return config; + } } } diff --git a/OcelotStudy.Getway/Properties/launchSettings.json b/OcelotStudy.Getway/Properties/launchSettings.json index 05cfc53..3488c8b 100644 --- a/OcelotStudy.Getway/Properties/launchSettings.json +++ b/OcelotStudy.Getway/Properties/launchSettings.json @@ -19,6 +19,7 @@ }, "OcelotStudy.Getway": { "commandName": "Project", + "commandLineArgs": "config=a.json", "launchBrowser": true, "launchUrl": "Index.html", "applicationUrl": "http://localhost:5000", diff --git a/OcelotStudy.Getway/ocelot.Development.json b/OcelotStudy.Getway/ocelot.Development.json new file mode 100644 index 0000000..185a87b --- /dev/null +++ b/OcelotStudy.Getway/ocelot.Development.json @@ -0,0 +1,32 @@ +{ + "Routes": [ + { + "DownstreamPathTemplate": "/api/ServiceA/ping", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 5101 + } + ], + "UpstreamPathTemplate": "/api/ServiceA", + "UpstreamHttpMethod": [ "Get" ] + }, + { + "DownstreamPathTemplate": "/api/ServiceB/ping", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 5102 + } + ], + "DownstreamPort": 5102, + "UpstreamPathTemplate": "/api/ServiceB", + "UpstreamHttpMethod": [ "Get" ] + } + ], + "GlobalConfiguration": { + "RequestIdKey": "OcRequestId", + "AdministrationPath": "/administration" + } +} diff --git a/OcelotStudy.Getway/ocelot.Empty.json b/OcelotStudy.Getway/ocelot.Empty.json new file mode 100644 index 0000000..e139da2 --- /dev/null +++ b/OcelotStudy.Getway/ocelot.Empty.json @@ -0,0 +1,5 @@ +{ + //娌℃湁浠讳綍鍔熻兘锛屼粎鑳戒娇Ocelot绋嬪簭鍚姩 + "Routes": [], + "GlobalConfiguration": {} +} diff --git a/OcelotStudy.Getway/ocelot.Production.json b/OcelotStudy.Getway/ocelot.Production.json new file mode 100644 index 0000000..185a87b --- /dev/null +++ b/OcelotStudy.Getway/ocelot.Production.json @@ -0,0 +1,32 @@ +{ + "Routes": [ + { + "DownstreamPathTemplate": "/api/ServiceA/ping", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 5101 + } + ], + "UpstreamPathTemplate": "/api/ServiceA", + "UpstreamHttpMethod": [ "Get" ] + }, + { + "DownstreamPathTemplate": "/api/ServiceB/ping", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 5102 + } + ], + "DownstreamPort": 5102, + "UpstreamPathTemplate": "/api/ServiceB", + "UpstreamHttpMethod": [ "Get" ] + } + ], + "GlobalConfiguration": { + "RequestIdKey": "OcRequestId", + "AdministrationPath": "/administration" + } +} diff --git a/OcelotStudy.Getway/ocelot.Staging.json b/OcelotStudy.Getway/ocelot.Staging.json new file mode 100644 index 0000000..185a87b --- /dev/null +++ b/OcelotStudy.Getway/ocelot.Staging.json @@ -0,0 +1,32 @@ +{ + "Routes": [ + { + "DownstreamPathTemplate": "/api/ServiceA/ping", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 5101 + } + ], + "UpstreamPathTemplate": "/api/ServiceA", + "UpstreamHttpMethod": [ "Get" ] + }, + { + "DownstreamPathTemplate": "/api/ServiceB/ping", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 5102 + } + ], + "DownstreamPort": 5102, + "UpstreamPathTemplate": "/api/ServiceB", + "UpstreamHttpMethod": [ "Get" ] + } + ], + "GlobalConfiguration": { + "RequestIdKey": "OcRequestId", + "AdministrationPath": "/administration" + } +}