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
  • 相关阅读:
    指针,数组,字符串的区别(高质量程序设计指南C++/C语言第7章)
    bitset初始化问题
    书籍
    编译器的工作过程
    C++函数传递指向指针的指针的应用
    程序员面试金典--二叉树的下一个结点
    程序员面试金典--对称的二叉树
    程序员面试金典--按之字形顺序打印二叉树
    程序员面试金典--阶乘尾零
    程序员面试金典--矩阵元素查找
  • 原文地址:https://www.cnblogs.com/zhaogaojian/p/12593765.html
Copyright © 2011-2022 走看看