1. 程式人生 > >Ocelot入門實踐

Ocelot入門實踐

執行 lec fig pst pla program base 分享交流 模板

博主是第一次寫技術文檔,一是對這兩年工作以來的一些技術和經驗進行整理,二也是希望能和大家多多分享交流,如有寫的不對的地方望大家多多指正。進入正題


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!打完收工

Ocelot入門實踐