使用反向代理本地主机不适用于 Azure Service Fabric 中的 5 节点集群
Using reverse proxy localhost not working on 5-node cluster in Azure Service Fabric
我在 Azure Service Fabric 集群中有一个 API 网关服务,我将其用作我的 Service Fabric 服务的网关。
这在我的机器上以及在 Azure 的单节点集群中都运行良好。
但是,当我 运行 它在 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 服务只有一个实例,可以在任何节点上。
我无法拥有多个价格服务实例。
有关信息,这是我使用的link:
我要回过头来看看我是否遗漏了什么。
这是我用来获取数据的代码:
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://mycluster.eastus.cloudapp.azure.com:19081/
- 使用命名服务解析您的服务端点:
解析代码示例:
var resolver = ServicePartitionResolver.GetDefault();
var endpoints = resolver.ResolveAsync(service.ServiceName, ServicePartitionKey.Singleton, CancellationToken.None).Result;
我在 Azure Service Fabric 集群中有一个 API 网关服务,我将其用作我的 Service Fabric 服务的网关。
这在我的机器上以及在 Azure 的单节点集群中都运行良好。
但是,当我 运行 它在 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 服务只有一个实例,可以在任何节点上。
我无法拥有多个价格服务实例。
有关信息,这是我使用的link:
我要回过头来看看我是否遗漏了什么。
这是我用来获取数据的代码:
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://mycluster.eastus.cloudapp.azure.com:19081/
- 使用命名服务解析您的服务端点:
解析代码示例:
var resolver = ServicePartitionResolver.GetDefault();
var endpoints = resolver.ResolveAsync(service.ServiceName, ServicePartitionKey.Singleton, CancellationToken.None).Result;