【问题标题】:Redirect to other address if main address is unavailable in Ocelot Gateway API如果 Ocelot Gateway API 中的主地址不可用,则重定向到其他地址
【发布时间】:2020-10-19 14:59:50
【问题描述】:

我想使用 Ocelot Gateway Api 连接到服务器,但是如果服务器不可用(例如 PC 离线),我想使用本地服务来代替。 例如,如果第一个 localhost:7001 失败,则使用 localhost:7002

{
    "Routes": [
        {
            "DownstreamPathTemplate": "/catalog",
            "DownstreamScheme": "http",
            "DownstreamHostAndPorts": [
                {
                    "Host": "localhost",
                    "Port": 7001
                },
                {
                    "Host": "localhost",
                    "Port": 7002
                }
            ],
            "UpstreamPathTemplate": "/catalog-api"
        }
    ],
    "GlobalConfiguration": {
            "BaseUrl": "http://localhost:7000"
    }
}

这样的事情可能吗?

【问题讨论】:

    标签: asp.net gateway ocelot


    【解决方案1】:

    是的,完全有可能。 Ocelot 原生支持负载均衡。实际上,通过指定多条下游路径,Ocelot 会自动将负载均衡到活跃连接数最少的服务上。

    要更好地控制负载平衡器功能,您可以指定 loadBalancerType 属性:

    {
        "Routes": [
            {
                "DownstreamPathTemplate": "/catalog",
                "DownstreamScheme": "http",
                "LoadBalancerOptions": {
                    "Type": "<<load balancer type here>>"
                },
                "DownstreamHostAndPorts": [
                    {
                        "Host": "localhost",
                        "Port": 7001
                    },
                    {
                        "Host": "localhost",
                        "Port": 7002
                    }
                ],
                "UpstreamPathTemplate": "/catalog-api"
            }
        ]
    }
    

    来自文档:

    可用的负载均衡器类型有:

    • LeastConnection - 跟踪哪些服务正在处理请求并将新请求发送到现有请求最少的服务。这 算法状态不会分布在 Ocelot 的集群中。
    • RoundRobin - 循环通过可用服务并发送请求。算法状态不是分布在一个集群中 豹猫的。
    • NoLoadBalancer - 从配置或服务发现中获取第一个可用服务。
    • CookieStickySessions - 使用 cookie 将所有请求粘贴到特定服务器

    由此看来,您想要的似乎自相矛盾地称为“NoLoadBalancer”。

    参考:https://ocelot.readthedocs.io/en/latest/features/loadbalancer.html

    【讨论】:

    • 你好。感谢您的回答。 NoLoadBalancer 不符合我的要求,但我创建了自己的 CustomLoadBalancer。谢谢你给我指路!
    猜你喜欢
    • 2023-03-27
    • 2016-01-17
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多