Yarp网关代理地址的自定义操作

上次测试了下Yarp这个微软自己家的网关,性能确实比ocelot要好不少。所以继续测试其他功能,在配置的时候,如果我有设置两个ClusterId分别要代理到两个不同的地址上去,如下的配置

  "ReverseProxy": {
    "Routes": {
      "route2": {
        "ClusterId": "cluster_product",
        "Match": {
          "Path": "/upm/{*all}"
        },
        "Transforms": [
          {}
        ]
      },
      "routeBaidu": {
        "ClusterId": "cluster_customer",
        "Match": {
          "Path": "/crm/{**catch-all}"
        }
      }
    },
    "Clusters": {
      "cluster_product": {
        "Destinations": {
          "first_destination": {
            "Address": "http://localhost:5001"
          },
          "two_destination": {
            "Address": "http://localhost:5001"
          }
        }
      },
      "cluster_customer": {
        "Destinations": {
          "baidu": {
            "Address": "http://localhost:5002"
          }
        }
      }
    }
  }

比如我想实现

请求A: http://localhost:5003/upm/user/getuser?userid=123代理到http://localhost:5001/api/user/getuser?userid=123  这个地址

请求B: http://localhost:5003/crm/user/getuser?id=123代理到 http://localhost:5002/api/user/getuser?userid=123这个地址

可是按照上面的配置如果不做什么其他改动的话,他都会分别代理到

http://localhost:5001/upm/user/getuser?userid=123

http://localhost:5002/crm/user/getuser?userid=123

 这样两个地址的,这是不我想要的地址。

后来查资料发现可以通过实现ITransformProvider来进行请求地址的自定义实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yarp.ReverseProxy.Transforms.Builder;
using Yarp.ReverseProxy.Transforms;

namespace YarpGateWay {
    public class CutomerTransformProvider : ITransformProvider {
        public void Apply(TransformBuilderContext context) {
            context.AddRequestTransform(transformContext => {
                var pathArr = transformContext.Path.Value.Split("/").Where(b=>!string.IsNullOrEmpty(b)).Select(b=>b).ToList();
                string apiPath = "";
                switch (pathArr[0]) {
                    case "upm":
                        pathArr[0] = "api";
                        break;
                    case "crm":
                        pathArr[0] = "api";
                        break;
                    default:
                        break;
                }
                transformContext.Path = "/"+string.Join("/", pathArr);
                //transformContext.ProxyRequest.RequestUri = new Uri($"{transformContext.DestinationPrefix}/{apiPath}/{pathArr[1]}");
                return new ValueTask();
            });
        }

        public void ValidateCluster(TransformClusterValidationContext context) {
            //throw new NotImplementedException();
        }

        public void ValidateRoute(TransformRouteValidationContext context) {
            //throw new NotImplementedException();
        }
    }
}

然后在startup中服务注册下

        public void ConfigureServices(IServiceCollection services) {
            // Add the reverse proxy to capability to the server
            var proxyBuilder = services.AddReverseProxy();
            // Initialize the reverse proxy from the "ReverseProxy" section of configuration
            proxyBuilder.LoadFromConfig(_configuration.GetSection("ReverseProxy"))
                .AddTransforms<CutomerTransformProvider>();//自定义转换请求的地址
        }

这样就可以实现我要的代理请求了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值