Service Fabric反向代理端口可配置性

时间:2017-05-16 22:09:28

标签: azure azure-service-fabric

我试图编写一个封装来获取服务结构的本地反向代理的uri,并且我很难决定如何处理端口的可配置性(称为&# 34; HttpApplicationGatewayEndpoint"在服务清单中或" reverseProxyEndpointPort"在arm模板中)。我认为这样做的最佳方式是调用" GetClusterManifestAsync"来自Fabric客户端并从那里解析它,但由于一些原因,我也不是它的粉丝。例如,该调用返回一个字符串xml blob,它不会对清单模式的更改进行保护。我还没有找到一种方法来查询集群管理器以找出我当前所处的节点类型,因此如果出于某种愚蠢的原因,集群有多个节点类型,并且每个节点类型都有不同的反向代理港口(这里只是一名防御性编码员),可能会失败。动态发现端口号似乎是一项非常大的努力,而且我之前肯定错过了Fabric api中的内容,所以有关如何解决这个问题的任何建议吗?

编辑:

我从示例项目中看到它从服务中的配置包中获取端口号。我宁愿不必这样做,因为那时我将不得不为每个服务写一大堆样板,而这些服务需要用它来读取配置并传递它。由于这在运行时或多或少是常量,所以在我看来,这样可以这样处理并从结构客户端获取某个地方?

1 个答案:

答案 0 :(得分:3)

在对象浏览器中度过了一段时间之后,我能够找到正确完成此操作所需的各种部分。

public class ReverseProxyPortResolver
{
    /// <summary>
    /// Represents the port that the current fabric node is configured
    /// to use when using a reverse proxy on localhost
    /// </summary>
    public static AsyncLazy<int> ReverseProxyPort = new AsyncLazy<int>(async ()=>
    {
        //Get the cluster manifest from the fabric client & deserialize it into a hardened object
        ClusterManifestType deserializedManifest;
        using (var cl = new FabricClient())
        {
            var manifestStr = await cl.ClusterManager.GetClusterManifestAsync().ConfigureAwait(false);
            var serializer = new XmlSerializer(typeof(ClusterManifestType));

            using (var reader = new StringReader(manifestStr))
            {
                deserializedManifest = (ClusterManifestType)serializer.Deserialize(reader);
            }
        }

        //Fetch the setting from the correct node type
        var nodeType = GetNodeType();
        var nodeTypeSettings = deserializedManifest.NodeTypes.Single(x => x.Name.Equals(nodeType));
        return int.Parse(nodeTypeSettings.Endpoints.HttpApplicationGatewayEndpoint.Port);
    });

    private static string GetNodeType()
    {
        try
        {
            return FabricRuntime.GetNodeContext().NodeType;
        }
        catch (FabricConnectionDeniedException)
        {
            //this code was invoked from a non-fabric started application
            //likely a unit test
            return "NodeType0";
        }

    }
}

本调查中的新闻是,任何服务结构xml的所有模式都在名为System.Fabric.Management.ServiceModel的程序集中被删除。