master
wanggaofeng 8 months ago
parent 054bf82e2d
commit 2c99ea9c8b

@ -9,6 +9,18 @@
</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">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

@ -17,21 +17,56 @@ namespace OcelotStudy.Getway
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
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)
.AddJsonFile("ocelot.json")
.AddEnvironmentVariables();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
.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
{
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": {
"commandName": "Project",
"commandLineArgs": "config=a.json",
"launchBrowser": true,
"launchUrl": "Index.html",
"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