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

 

posted @ 2020-03-29 18:02  zhaogaojian  阅读(954)  评论(2编辑  收藏  举报