diff --git a/OcelotStudy.Getway/Controllers/TestController.cs b/OcelotStudy.Getway/Controllers/TestController.cs new file mode 100644 index 0000000..6252642 --- /dev/null +++ b/OcelotStudy.Getway/Controllers/TestController.cs @@ -0,0 +1,22 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace OcelotStudy.Getway.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TestController : ControllerBase + { + public IActionResult Get() + { + var data = new { Name="Ocelot 娴嬭瘯Api"}; + + return Ok(data) ; + } + } +} diff --git a/OcelotStudy.Getway/OcelotStudy.Getway.csproj b/OcelotStudy.Getway/OcelotStudy.Getway.csproj new file mode 100644 index 0000000..5372733 --- /dev/null +++ b/OcelotStudy.Getway/OcelotStudy.Getway.csproj @@ -0,0 +1,18 @@ + + + + netcoreapp3.1 + + + + + + + + + Always + + + + + diff --git a/OcelotStudy.Getway/Program.cs b/OcelotStudy.Getway/Program.cs new file mode 100644 index 0000000..3452813 --- /dev/null +++ b/OcelotStudy.Getway/Program.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace OcelotStudy.Getway +{ + public class Program + { + public static void Main(string[] args) + { + 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 => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/OcelotStudy.Getway/Properties/launchSettings.json b/OcelotStudy.Getway/Properties/launchSettings.json new file mode 100644 index 0000000..05cfc53 --- /dev/null +++ b/OcelotStudy.Getway/Properties/launchSettings.json @@ -0,0 +1,30 @@ +锘縶 + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:6353", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "OcelotStudy.Getway": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "Index.html", + "applicationUrl": "http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/OcelotStudy.Getway/Startup.cs b/OcelotStudy.Getway/Startup.cs new file mode 100644 index 0000000..4a60701 --- /dev/null +++ b/OcelotStudy.Getway/Startup.cs @@ -0,0 +1,39 @@ +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.Hosting; +using Microsoft.Extensions.Logging; + +using Ocelot.DependencyInjection; +using Ocelot.Middleware; + +namespace OcelotStudy.Getway +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + public void ConfigureServices(IServiceCollection services) + { + services.AddOcelot(); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseStaticFiles(); + app.UseOcelot(); + } + } +} diff --git a/OcelotStudy.Getway/appsettings.Development.json b/OcelotStudy.Getway/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/OcelotStudy.Getway/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/OcelotStudy.Getway/appsettings.json b/OcelotStudy.Getway/appsettings.json new file mode 100644 index 0000000..ce23979 --- /dev/null +++ b/OcelotStudy.Getway/appsettings.json @@ -0,0 +1,11 @@ +{ + "urls": "http://localhost:5000", + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/OcelotStudy.Getway/ocelot.json b/OcelotStudy.Getway/ocelot.json new file mode 100644 index 0000000..185a87b --- /dev/null +++ b/OcelotStudy.Getway/ocelot.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/wwwroot/Index.html b/OcelotStudy.Getway/wwwroot/Index.html new file mode 100644 index 0000000..e8c7c79 --- /dev/null +++ b/OcelotStudy.Getway/wwwroot/Index.html @@ -0,0 +1,18 @@ +锘 + + + + 椤圭洰璇存槑 + + +

+ Ocelot 缃戝叧椤圭洰锛屾墍鏈夊姛鑳藉熀浜庢墜鍔ㄩ厤缃枃浠躲 +

+
+ 娴嬭瘯闇鍚屾椂鍚姩鍒涓や釜鏈嶅姟椤圭洰锛 + 涔嬪悗锛屾湰缃戝叧鍩哄湴鍧鍚庯紝鍔 + http://缃戝叧/api/ServiceA + http://缃戝叧/api/ServiceB +
+ + \ No newline at end of file diff --git a/OcelotStudy.Getway/wwwroot/favicon.ico b/OcelotStudy.Getway/wwwroot/favicon.ico new file mode 100644 index 0000000..cd294c1 Binary files /dev/null and b/OcelotStudy.Getway/wwwroot/favicon.ico differ diff --git a/OcelotStudy.WebApiA/Controllers/ServiceAController.cs b/OcelotStudy.WebApiA/Controllers/ServiceAController.cs new file mode 100644 index 0000000..5fcb01d --- /dev/null +++ b/OcelotStudy.WebApiA/Controllers/ServiceAController.cs @@ -0,0 +1,22 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace OcelotStudy.WebApiA.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ServiceAController : ControllerBase + { + public IActionResult Ping() + { + var data = new { Name="鏈嶅姟A",Describe="娴嬭瘯鏈嶅姟A" }; + + return Ok(data); + } + } +} diff --git a/OcelotStudy.WebApiA/OcelotStudy.WebApiA.csproj b/OcelotStudy.WebApiA/OcelotStudy.WebApiA.csproj new file mode 100644 index 0000000..adaae29 --- /dev/null +++ b/OcelotStudy.WebApiA/OcelotStudy.WebApiA.csproj @@ -0,0 +1,8 @@ +锘 + + + netcoreapp3.1 + + + + diff --git a/OcelotStudy.WebApiA/Program.cs b/OcelotStudy.WebApiA/Program.cs new file mode 100644 index 0000000..b2680fc --- /dev/null +++ b/OcelotStudy.WebApiA/Program.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace OcelotStudy.WebApiA +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/OcelotStudy.WebApiA/Properties/launchSettings.json b/OcelotStudy.WebApiA/Properties/launchSettings.json new file mode 100644 index 0000000..df04834 --- /dev/null +++ b/OcelotStudy.WebApiA/Properties/launchSettings.json @@ -0,0 +1,30 @@ +锘縶 + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:34710", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "OcelotStudy.WebApiA": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/ServiceA/ping", + "applicationUrl": "http://localhost:5101", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/OcelotStudy.WebApiA/Startup.cs b/OcelotStudy.WebApiA/Startup.cs new file mode 100644 index 0000000..cb08b9e --- /dev/null +++ b/OcelotStudy.WebApiA/Startup.cs @@ -0,0 +1,49 @@ +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.Hosting; +using Microsoft.Extensions.Logging; + +namespace OcelotStudy.WebApiA +{ + 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.AddControllers(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/OcelotStudy.WebApiA/appsettings.Development.json b/OcelotStudy.WebApiA/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/OcelotStudy.WebApiA/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/OcelotStudy.WebApiA/appsettings.json b/OcelotStudy.WebApiA/appsettings.json new file mode 100644 index 0000000..dc9ac33 --- /dev/null +++ b/OcelotStudy.WebApiA/appsettings.json @@ -0,0 +1,11 @@ +{ + "urls": "http://localhost:5101", + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/OcelotStudy.WebApiB/Controllers/ServiceBController.cs b/OcelotStudy.WebApiB/Controllers/ServiceBController.cs new file mode 100644 index 0000000..0094b6d --- /dev/null +++ b/OcelotStudy.WebApiB/Controllers/ServiceBController.cs @@ -0,0 +1,22 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace OcelotStudy.WebApiB.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ServiceBController : ControllerBase + { + public IActionResult Ping() + { + var data = new { Name="鏈嶅姟B",Describe="娴嬭瘯鏈嶅姟B" }; + + return Ok(data); + } + } +} diff --git a/OcelotStudy.WebApiB/OcelotStudy.WebApiB.csproj b/OcelotStudy.WebApiB/OcelotStudy.WebApiB.csproj new file mode 100644 index 0000000..d12c450 --- /dev/null +++ b/OcelotStudy.WebApiB/OcelotStudy.WebApiB.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + + + + diff --git a/OcelotStudy.WebApiB/Program.cs b/OcelotStudy.WebApiB/Program.cs new file mode 100644 index 0000000..ea2630e --- /dev/null +++ b/OcelotStudy.WebApiB/Program.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace OcelotStudy.WebApiB +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/OcelotStudy.WebApiB/Properties/launchSettings.json b/OcelotStudy.WebApiB/Properties/launchSettings.json new file mode 100644 index 0000000..30f7e7a --- /dev/null +++ b/OcelotStudy.WebApiB/Properties/launchSettings.json @@ -0,0 +1,30 @@ +锘縶 + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:12569", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "weatherforecast", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "OcelotStudy.WebApiB": { + "commandName": "Project", + "launchBrowser": true, + "launchUrl": "api/ServiceB/ping", + "applicationUrl": "http://localhost:5102", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/OcelotStudy.WebApiB/Startup.cs b/OcelotStudy.WebApiB/Startup.cs new file mode 100644 index 0000000..a5f1ac9 --- /dev/null +++ b/OcelotStudy.WebApiB/Startup.cs @@ -0,0 +1,49 @@ +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.Hosting; +using Microsoft.Extensions.Logging; + +namespace OcelotStudy.WebApiB +{ + 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.AddControllers(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseAuthorization(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } + } +} diff --git a/OcelotStudy.WebApiB/appsettings.Development.json b/OcelotStudy.WebApiB/appsettings.Development.json new file mode 100644 index 0000000..8983e0f --- /dev/null +++ b/OcelotStudy.WebApiB/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/OcelotStudy.WebApiB/appsettings.json b/OcelotStudy.WebApiB/appsettings.json new file mode 100644 index 0000000..e0f3bad --- /dev/null +++ b/OcelotStudy.WebApiB/appsettings.json @@ -0,0 +1,11 @@ +{ + "urls": "http://localhost:5102", + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/OcelotStudy.sln b/OcelotStudy.sln new file mode 100644 index 0000000..12de4f0 --- /dev/null +++ b/OcelotStudy.sln @@ -0,0 +1,37 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31205.134 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OcelotStudy.WebApiA", "OcelotStudy.WebApiA\OcelotStudy.WebApiA.csproj", "{EF25A97D-2330-463A-9AE7-8F02D41F8ABE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OcelotStudy.WebApiB", "OcelotStudy.WebApiB\OcelotStudy.WebApiB.csproj", "{B1E66C70-6D81-4998-A7D6-C2089773E2A3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OcelotStudy.Getway", "OcelotStudy.Getway\OcelotStudy.Getway.csproj", "{006D1F30-AE0D-454A-B402-1EB9AB35AF82}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EF25A97D-2330-463A-9AE7-8F02D41F8ABE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF25A97D-2330-463A-9AE7-8F02D41F8ABE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF25A97D-2330-463A-9AE7-8F02D41F8ABE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF25A97D-2330-463A-9AE7-8F02D41F8ABE}.Release|Any CPU.Build.0 = Release|Any CPU + {B1E66C70-6D81-4998-A7D6-C2089773E2A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B1E66C70-6D81-4998-A7D6-C2089773E2A3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B1E66C70-6D81-4998-A7D6-C2089773E2A3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B1E66C70-6D81-4998-A7D6-C2089773E2A3}.Release|Any CPU.Build.0 = Release|Any CPU + {006D1F30-AE0D-454A-B402-1EB9AB35AF82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {006D1F30-AE0D-454A-B402-1EB9AB35AF82}.Debug|Any CPU.Build.0 = Debug|Any CPU + {006D1F30-AE0D-454A-B402-1EB9AB35AF82}.Release|Any CPU.ActiveCfg = Release|Any CPU + {006D1F30-AE0D-454A-B402-1EB9AB35AF82}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E511B0EB-F326-4865-B034-67F1F5C78631} + EndGlobalSection +EndGlobal