ocelot簡單使用


ocelot是一個基於.netcore的網關工具,使用方法,有些場景為什么不用nginx而使用ocelot,
比如:ocelot可以直接做權限驗證、基本上不用安裝專門的網關工具。

1、創建三個.netcore webapi項目

 

 2、Gate項目下創建

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/User",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ],
      "UpstreamPathTemplate": "/webapi1/User",
      "UpstreamHttpMethod": [ "Get" ],
    "QoSOptions": {
      "ExceptionsAllowedBeforeBreaking":3,
      "DurationOfBreak":60000,
      "TimeoutValue": 1000
}
}, { "DownstreamPathTemplate": "/api/User", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5002 } ], "UpstreamPathTemplate": "/webapi2/User", "UpstreamHttpMethod": [ "Get" ],
    "QoSOptions": {
      "ExceptionsAllowedBeforeBreaking":3,
      "DurationOfBreak":60000,
      "TimeoutValue": 1000
}
} ] }

啟動端口設為6000

修改StartUp.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
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;
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;

namespace WebApplicationGate
{
    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.AddMvc(options => options.EnableEndpointRouting = false);

            services.AddOcelot(new ConfigurationBuilder()
                .AddJsonFile("configuration.json")
                .Build());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public async void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            await app.UseOcelot();

            app.UseMvc();
        }
    }
}

3、在項目1中創建一個controller,修改啟動端口為5001

namespace WebApplication1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        [HttpGet]
        public IEnumerable<string> GetUserName()
        {
            return new string[] { "張三" };
        }
    }
}

4、同樣在項目2中創建一個,修改啟動端口為5002

namespace WebApplication1.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        [HttpGet]
        public IEnumerable<string> GetUserName()
        {
            return new string[] { "李四" };
        }
    }
}

5、啟動項目,訪問

http://localhost:6000/webapi1/User

自動跳轉為

https://localhost:5001/api/User

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM