首页 > 其他分享 >Ocelot与路由共存

Ocelot与路由共存

时间:2023-08-21 17:08:07浏览次数:32  
标签:Ocelot 共存 app UseOcelotWhenRouteMatch IApplicationBuilder context 路由

Ocelot与路由共存

引言

在Asp.Net Core中使用了Ocelot做网关之后,其自身的Api路由就不起作用了,寻了许久的解决方法,终于找到一个,主要是使用MapWhen

判断Ocelot的配置是否符合,是则走转发路由,否则走自身路由,步骤如下:

1.先创建以下类

using Ocelot.Configuration.Repository;
using Ocelot.DownstreamRouteFinder.Finder;
using Ocelot.Middleware;

namespace GateWay.Extensions
{
    public static class OcelotExtensions
    {
        public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app)
            => UseOcelotWhenRouteMatch(app, new OcelotPipelineConfiguration());

        public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app,
            Action<OcelotPipelineConfiguration> pipelineConfigurationAction)
        {
            var pipelineConfiguration = new OcelotPipelineConfiguration();
            pipelineConfigurationAction?.Invoke(pipelineConfiguration);
            return UseOcelotWhenRouteMatch(app, pipelineConfiguration);
        }

        public static IApplicationBuilder UseOcelotWhenRouteMatch(this IApplicationBuilder app, OcelotPipelineConfiguration configuration)
        {
            app.MapWhen(context =>
            {
                // 获取 OcelotConfiguration
                var internalConfigurationResponse =
                    context.RequestServices.GetRequiredService<IInternalConfigurationRepository>().Get();
                if (internalConfigurationResponse.IsError || internalConfigurationResponse.Data.Routes.Count == 0)
                {
                    // 如果没有配置路由信息,不符合分支路由的条件,直接退出
                    return false;
                }

                var internalConfiguration = internalConfigurationResponse.Data;
                var downstreamRouteFinder = context.RequestServices
                    .GetRequiredService<IDownstreamRouteProviderFactory>()
                    .Get(internalConfiguration);

                // 根据请求以及上面获取的Ocelot配置获取下游路由
                var response = downstreamRouteFinder.Get(context.Request.Path, context.Request.QueryString.ToString(),
                    context.Request.Method, internalConfiguration, context.Request.Host.ToString());

                // 如果有匹配路由则满足该分支路由的条件,交给 Ocelot 处理
                return !response.IsError
                       && !string.IsNullOrEmpty(response.Data?.Route?.DownstreamRoute?.FirstOrDefault()
                           ?.DownstreamScheme);
            }, appBuilder => appBuilder.UseOcelot(configuration).Wait());
            return app;
        }
    }
}

2.在Program.cs调用

app.UseOcelotWhenRouteMatch(); // 此处调用
app.UseRouting();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();

标签:Ocelot,共存,app,UseOcelotWhenRouteMatch,IApplicationBuilder,context,路由
From: https://www.cnblogs.com/DriftingLeaf/p/17646493.html

相关文章

  • 微信小程序中的路由及其区别
    wx.navigateTo():保留当前页面,跳转到应用内的某个页面。但是不能跳到tabbar页面wx.redirectTo():关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到tabbar页面wx.switchTab():跳转到tabBar页面,并关闭其他所有非tabBar页面wx.navigateBack()关闭当前页面,返回上......
  • HCSA(Hillstone)——接口与路由技术
    接口技术接口种类lHillstone设备具有多种类型接口,分为物理接口和逻辑接口:(1)物理接口:每一个以太网接口表示一个物理接口。例如ethernet0/1(2)逻辑接口:Vswitchif接口、子接口、VLAN接口、隧道接口、集聚接口、冗余接口l根据接口所处安全域还可以分为二层接口和三层接口IP类型静态IP在Web......
  • Linux路由功能练习(1)
    打开一台Linux虚拟机VM1,并添加2块网卡,一个使用VMware1网卡,一个使用VMware2网卡配置VMware1网卡的IP地址为192.168.136.254/24;VMware2网卡的IP地址为192.168.137.254/24[root@nb~]#ifconfigens33192.168.136.254netmask255.255.255.0[root@nb~]#ifconfigens37192.168.1......
  • Express - 路由
    路由定义采用以下结构:app.METHOD(PATH,HANDLER)METHOD是请求方法,PATH是路径,HANDLER是处理路由的回调函数。路由方法Express支持对应于HTTP方法的以下路由方法:get、post、put、head、delete、options、trace、copy、lock、mkcol、move、purge、propfind、proppatch、unlo......
  • 2-14-Gateway网关-路由断言工厂[15-Gateway网关-路由的过滤器配置]
    所谓的断言工厂与过滤器都是通过yml配置生效以下都将从配置的角度说明如何配置具体哪一个怎么用可以直接访问spring官网查看spring:cloud:gateway:routes:-id:user-serviceuri:lb://userservicepredicates:-Path=/us......
  • react项目搭建-路由封装
    routerv6路由统一管理与添加,对是否登录进行判断。1.使用脚手架创建项目新建一个文件夹,在文件夹内部打开命令行工具。执行命令:npxcreate-react-app项目名字将项目放在开发工具中,打开项目终端运行项目(npmstart/yarnstart)注:npx命令会帮助我们临时安装create-react-ap......
  • webman:配置路由(v1.5.7)
     一,官方文档地址:https://www.workerman.net/doc/webman/route.html二,php代码:config/route.php1234567891011121314151617181920<?php useWebman\Route; //指定默认页面Route::get('/',[app\controller\IndexController::class......
  • 4G工业路由器的功能与选型!详解工作原理、关键参数、典型品牌
    随着工业互联网的发展,4G工业路由器得到越来越广泛的应用。但是如何根据实际需求选择合适的4G工业路由器,是许多用户关心的问题。为此,本文将深入剖析4G工业路由器的工作原理、重要参数及选型要点,并推荐优质的品牌及产品,以提供选型参考。  一、4G工业路由器的工作原理4G......
  • 实现通用路由菜单的一种方式
    将路由数组引入后进行遍历,自行迭代,即可展示需要的菜单选项<a-menu-itemv-for="iteminroutes":key="item.path">{{item.name}}</a-menu-item>在通用布局中使用route-view渲染实际的页面内容,更加动态也更加灵活<a-layout-contentclass="center">......
  • ESXI8.0 安装OpenWrt高大全版本作为旁路由
    安装esir-openwrt的高大全版当然是为了旅游了,本文只讲述我们已经有ESXI服务器,如何在自己的ESXI上安装esir大神openwrt高大全的版本;后面的使用,我相信聪明的你,一看就会了。 一、准备材料:1.镜像下载:01-LeanCodex86_64Firmware|固件-GoogleDrive 无法通过谷歌网盘下......