博主是第一次写技术文档,一是对这两年工作以来的一些技术和经验进行整理,二也是希望能和大家多多分享交流,如有写的不对的地方望大家多多指正。进入正题
Ocelot 概念就不说了,大家自行百度,今天做一个Ocelot实例
1.VS新建空白解决方案
2.右键解决方案新建项目Service1,Service2选择Api项目模板
右键解决方案添加项目Gateway选择空项目模板
建立完成后解决方案如下
3.右键解决方案=>设置启动项目
打开Service1 launchSettings.json文件,修改"applicationUrl": "http://localhost:7001" ,"launchBrowser": false,
打开Service2 launchSettings.json文件,修改"applicationUrl": "http://localhost:7002" ,"launchBrowser": false,
打开Gateway launchSettings.json文件,修改"applicationUrl": "http://localhost:7000" ,"launchBrowser": false,
4.打开Service1 中 ValuesController改为如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.AspNetCore.Mvc; 6 7 namespace Service1.Controllers 8 { 9 [Route("api/[controller]")] 10 [ApiController] 11 public class ValuesController : ControllerBase 12 { 13 // GET api/values 14 [HttpGet] 15 public ActionResult<string> Get() 16 { 17 return "这是 Service1 "; 18 } 19 20 } 21 }
打开Service2 中 ValuesController改为如下:
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace Service2.Controllers { [Route("api/[controller]")] [ApiController] public class ValuesController : ControllerBase { // GET api/values [HttpGet] public ActionResult<string> Get() { return "这是 Service2 "; } } }
5. VS =>调试=>开始执行
打开postman api测试工具 请求 http://localhost:7001/api/values
请求 http://localhost:7002/api/values
service准备完毕,接下来接入Ocelot
6.Gateway项目安装nuget包 Install-Package Ocelot
Gateway项目下添加ocelot.json文件,右键属性,如果较新则复制,并进行如下配置
{ "ReRoutes": [ { //Upstream表示上游请求,即客户端请求到API Gateway的请求 "UpstreamPathTemplate": "/Service1/{url}", //请求路径模板 "UpstreamHttpMethod": [ "Get", "Post" ], //请求方法数组 "UseServiceDiscovery": false, //启用服务发现 //Downstream表示下游请求,即API Gateway转发的目标服务地址 "DownstreamPathTemplate": "/api/{url}", //下游请求地址模板 "DownstreamScheme": "http", //请求协议,目前应该是支持http和https "DownstreamHostAndPorts": [ //请求服务地址 { "Host": "localhost", "Port": 7001 } ] }, { "UpstreamPathTemplate": "/Service2/{url}", "UpstreamHttpMethod": [ "Get", "Post" ], "UseServiceDiscovery": false, "DownstreamPathTemplate": "/api/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7002 } ] } ], "GlobalConfiguration": { //"ServiceDiscoveryProvider": { // "Host": "127.0.0.1", // "Port": 8500, // "Type": "PollConsul" //} } }
打开Program.cs,修改如下:
public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, builder) => { builder.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .AddJsonFile("ocelot.json", false, true); }) .UseStartup<Startup>(); }
打开Startup.cs文件,进行如下配置:
public void ConfigureServices(IServiceCollection services) { services.AddOcelot();//添加Ocelot服务 }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseOcelot().Wait();//使用Ocelot服务 }
Ocelot配置完毕,VS=>调试=>开始执行
打开postman工具进行测试
请求 http://localhost:7000/Service1/values
请求 http://localhost:7000/Service2/values
OK!打完收工