【问题标题】:Using reverse proxy localhost not working on 5-node cluster in Azure Service Fabric使用反向代理 localhost 在 Azure Service Fabric 中的 5 节点群集上不起作用
【发布时间】:2018-12-15 09:55:11
【问题描述】:

我在 Azure Service Fabric 集群中有一个 API Gateway 服务,我将其用作我的 Service Fabric 服务的网关。

这一切在我的机器上运行良好,在 Azure 的 1 节点集群中也是如此。

但是,当我在 5 节点集群中运行它时,我得到了错误

System.Net.Http.HttpRequestException: 
An error occurred while sending the request. ---> 
System.Net.WebException: Unable to connect to the remote server 
System.Net.Sockets.SocketException: 
No connection could be made because the target machine actively refused it 127.0.0.1:19081

我知道 19081 是默认的反向代理端口。我在任何地方都没有看到任何关于专门解除阻塞此端口的说明,所以我假设它是打开的。

我很困惑为什么 127.0.0.1 可以在 1 节点集群中工作,但不能在 5 节点集群中工作。

我认为问题可能是我调用的服务并不总是与负载均衡器向其发送请求的 API 网关位于同一节点上。

我该如何解决这个问题?

我的 Price Service Fabric 服务有一个实例,可以在任何节点上。

我不能选择拥有多个价格服务实例。

有关信息,这是我使用的链接:

You Tube Video

我要回去看看我是否遗漏了什么。

这是我用来获取数据的代码:

var proxyUrl = $"http://My Service Fabric URL:{this._configSettings.ReverseProxyPort}/{url.Replace("fabric:/", "")}/api/{Area}/{method}{parameters}";
            Log.Information($"xUrl: {proxyUrl}");

            var response = await this._httpClient.GetAsync(proxyUrl).ConfigureAwait(false);

            var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                Log.Information($"{this.GetType().Name}: Error Response: {response.StatusCode}");

                return this.StatusCode((int) response.StatusCode);
            }

            Log.Information(this.GetType().Name + ": Called API");

按照下面的建议,我现在有了获取地址的代码:

   var resolver = ServicePartitionResolver.GetDefault();
        var partition = await resolver.ResolveAsync(_serviceContext.ServiceName, ServicePartitionKey.Singleton, CancellationToken.None).ConfigureAwait(false);
        var endpoints = JObject.Parse(partition.GetEndpoint().Address)["Endpoints"];
        var address = endpoints[""].ToString().TrimEnd('/');

        var proxyUrl = $"{address}:{this._configSettings.ReverseProxyPort}/{url.Replace("fabric:/", "")}/api/{Area}/{method}{parameters}";

这给出了http://localhost:8081

这如何适应反向代理端口?

干杯

保罗

【问题讨论】:

    标签: azure asp.net-web-api load-balancing azure-service-fabric


    【解决方案1】:

    您收到此错误是因为侦听此端口的服务部署在另一个节点上。有两种方法可以实现您的目标:

    1. 使用服务结构集群的全名,例如http://mycluster.eastus.cloudapp.azure.com:19081/
    2. 使用命名服务来解析服务的端点:

    解析代码示例:

    var resolver = ServicePartitionResolver.GetDefault();
    var endpoints = resolver.ResolveAsync(service.ServiceName, ServicePartitionKey.Singleton, CancellationToken.None).Result;
    

    【讨论】:

    • 如果我使用选项 2,这如何适合 httpclient?我添加了用于获取数据的代码
    • @Paul 是的,你可以使用endpoints.GetEndpoint().Address获取地址
    • {"Endpoints":{"":"http:\/\/localhost:8081"}} 就是这样,嗯!我该如何利用这个?抱歉,这似乎是一个愚蠢的问题!
    • 我在上面的代码中使用了选项 2,但只使用了 url 并删除了反向代理端口,感谢您的帮助!
    猜你喜欢
    • 2018-11-17
    • 2023-04-05
    • 2019-08-29
    • 1970-01-01
    • 2019-01-16
    • 2017-05-06
    • 2021-01-11
    • 2016-12-17
    • 2017-09-08
    相关资源
    最近更新 更多