zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    2018徐州网络赛H. Ryuji doesn't want to study
    P3388 【模板】割点
    数列分块入门 1 LOJ6277
    P2261 [CQOI2007]余数求和
    模拟微信小程序页面Page方法
    .Net Newtonsoft.Json 转json时将枚举转为字符串
    .NET CORE 中使用AutoMapper进行对象映射
    .NetMvc从http或本地下载pdf文件
    js给多级复杂动态变量赋值
    微信三方平台开发上传base64格式图片至临时素材
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/12593765.html
Copyright © 2011-2022 走看看