Consul 带有ocelot和CONSOR的动态服务名称

Consul 带有ocelot和CONSOR的动态服务名称,consul,service-discovery,api-gateway,ocelot,Consul,Service Discovery,Api Gateway,Ocelot,我正在使用Ocelot和API网关与领事和服务发现。 我使用动态名称在领事馆注册服务,如:service.name.1234和service.name.5678 这些服务是稳定的,根本不需要扩展 由于我正在使用Ocelot,我希望能够将请求路由到所需的服务,但由于名称是动态的,我需要使用查询字符串参数作为服务名称 例如: 应重定向到名为service.name.1234的容器 使用这两种产品有没有办法达到这个目的?或者其他产品 谢谢我一直在寻找相同的解决方案,但在GitHub上只找到了一个,这对

我正在使用Ocelot和API网关与领事和服务发现。 我使用动态名称在领事馆注册服务,如:service.name.1234和service.name.5678

这些服务是稳定的,根本不需要扩展

由于我正在使用Ocelot,我希望能够将请求路由到所需的服务,但由于名称是动态的,我需要使用查询字符串参数作为服务名称

例如: 应重定向到名为service.name.1234的容器

使用这两种产品有没有办法达到这个目的?或者其他产品


谢谢

我一直在寻找相同的解决方案,但在GitHub上只找到了一个,这对我帮助很大

因此,您需要创建自定义中间件来重写Ocelot的下行路线:

public static async Task InvokeAsync(HttpContext httpContext, Func<Task> next)
    {
        
        var downstreamRoute = httpContext.Items.DownstreamRoute();

        var yourServiceName = //get query string parameter from httpContext;

        //rewrite any parameter that you want
        httpContext.Items.UpsertDownstreamRoute(
            new DownstreamRoute(
                downstreamRoute.Key,
                downstreamRoute.UpstreamPathTemplate,
                downstreamRoute.UpstreamHeadersFindAndReplace,
                downstreamRoute.DownstreamHeadersFindAndReplace,
                downstreamRoute.DownstreamAddresses,
                tenantServiceName,
                ...
            ));
    }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // some other code

    var configuration = new OcelotPipelineConfiguration
            {
                PreQueryStringBuilderMiddleware = async (ctx, next) =>
                {
                    await RouteContextRetrieverMiddleware.InvokeAsync(ctx, next);

                    await next.Invoke();
                }
            };

            app.UseOcelot(configuration).GetAwaiter().GetResult();
}