1

我想使用 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"
    }
}

这样的事情甚至可能吗?

4

1 回答 1

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 - 循环可用服务并发送请求。算法状态不分布在 Ocelot 的集群中。
  • NoLoadBalancer - 从配置或服务发现中获取第一个可用服务。
  • CookieStickySessions - 使用 cookie 将所有请求粘贴到特定服务器

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

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

于 2020-06-29T13:21:51.327 回答