master
wanggaofeng 8 months ago
parent 054bf82e2d
commit 2c99ea9c8b

@ -9,6 +9,18 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Update="ocelot.Empty.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="ocelot.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="ocelot.Production.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="ocelot.Staging.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="ocelot.json"> <Content Update="ocelot.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>

@ -17,21 +17,56 @@ namespace OcelotStudy.Getway
CreateHostBuilder(args).Build().Run(); CreateHostBuilder(args).Build().Run();
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args)
Host.CreateDefaultBuilder(args) {
.ConfigureAppConfiguration((hostingContext, config) => var builder = Host.CreateDefaultBuilder(args);
{ builder
//Ìí¼ÓÅäÖÃÎļþµÄʹÓà .ConfigureAppConfiguration((hostingContext, config) =>
config {
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) config
.AddJsonFile("appsettings.json", true, true) .SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true) .AddJsonFile("appsettings.json", true, true)
.AddJsonFile("ocelot.json") .AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true)
.AddEnvironmentVariables(); .AddEnvironmentVariables()
}) .AddCommandLine(args);
.ConfigureWebHostDefaults(webBuilder =>
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<Startup>(); 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;
}
} }
} }

@ -19,6 +19,7 @@
}, },
"OcelotStudy.Getway": { "OcelotStudy.Getway": {
"commandName": "Project", "commandName": "Project",
"commandLineArgs": "config=a.json",
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "Index.html", "launchUrl": "Index.html",
"applicationUrl": "http://localhost:5000", "applicationUrl": "http://localhost:5000",

@ -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"
}
}

@ -0,0 +1,5 @@
{
//使Ocelot
"Routes": [],
"GlobalConfiguration": {}
}

@ -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"
}
}

@ -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"
}
}
Loading…
Cancel
Save