zoukankan      html  css  js  c++  java
  • Asp.Net Core + Ocelot 网关搭建:路由简单配置

    前言

      Ocelot是一个基于中间件的网关实现,功能有很多。从浅入深简单学习并记录一下吧。本篇就是一个简单的路由配置实现。

    DEMO 搭建

      首先建立三个项目。Api.User,Api.Article,Api.GateWay.ApiGateWay项目中引入Ocelot Nuget包.添加配置文件Ocelot.json.

    {
      "ReRoutes": [
        {
          "DownstreamPathTemplate": "/{all}",
          "DownstreamScheme": "http",
          "DownstreamHostAndPorts": [
            {
              "Host": "localhost",
              "Port": 60001
            }
          ],
          "UpstreamPathTemplate": "/user/{all}",
          "UpstreamHttpMethod": ["GET","POST"]
        },
        {
          "DownstreamPathTemplate": "/{all}",
          "DownstreamScheme": "http",
          "DownstreamHostAndPorts": [
            {
              "Host": "localhost",
              "Port": 60002
            }
          ],
          "UpstreamPathTemplate": "/article/{all}",
          "UpstreamHttpMethod": ["GET","POST"]
        }
      ],
      "GlobalConfiguration": {
        "BaseUrl": "https://localhost:60003/"
      }
    }
    

      启动的时候将配置文件加进去,并且Startup中添加相应的中间件:services.AddOcelot(),app.UseOcelot();

     public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                 .ConfigureAppConfiguration((hostingContext, config) =>
                 {
                     config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath)
                           .AddJsonFile("ocelot.json", optional: true, reloadOnChange: true)
                           .AddEnvironmentVariables();
                 }).UseStartup<Startup>();
    

      启动三个项目,运行效果如下:

    总结

      这样就能实现将网关做为统一入口,统一管理了。后续在加上各种Ocelot的功能实现。

  • 相关阅读:
    spring读取配置文件内容并自动注入
    xshell免费下载安装使用
    cas sso原理
    sql两列相除,保留n位小数
    mysql 报zone什么的错误
    mysql union出错: "Every derived table must have its own alias"
    mysql jdbc操作
    sql 对某列取值进行if判断
    Python深入:02浅拷贝深拷贝
    Python基础:22__slots__类属性
  • 原文地址:https://www.cnblogs.com/panzi/p/9817159.html
Copyright © 2011-2022 走看看