简体   繁体   English

从外部调用 Azure Service Fabric

[英]Call Azure Service Fabric from outside

I'm playing with Azure Service Fabric and a console app.我正在使用 Azure Service Fabric 和控制台应用程序。 I simply want my console app to connect to the cluster and do some stuff.我只是想让我的控制台应用程序连接到集群并做一些事情。

The console app try to resolve the service address with the following:控制台应用程序尝试使用以下内容解析服务地址:

    static void Main(string[] args)
    {
        ServicePartitionResolver resolver = null;

        try
        {
            resolver = new ServicePartitionResolver(
                new string[] {
                    "localhost:19000",
                    "localhost:19001"
                });

            Uri serviceUri = new Uri("fabric:/StatefullServiceTEST/MyStatefulService");
            ResolvedServicePartition partition = resolver.ResolveAsync(serviceUri, new ServicePartitionKey(), CancellationToken.None).GetAwaiter().GetResult();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Exception: {ex.Message}");
        }
        Console.WriteLine();
        Console.Write("Press any key to exit...");
        Console.ReadKey();
    }

My problem is that resolver.ResolveAsync throws an exception that doesn't seem to have any connection with Service Fabric:我的问题是resolver.ResolveAsync引发了一个似乎与 Service Fabric 没有任何联系的异常:

Unable to cast COM object of type 'System.__ComObject' to interface type 'IFabricApplicationManagementClient10'.无法将“System.__ComObject”类型的 COM 对象转换为接口类型“IFabricApplicationManagementClient10”。 This operation failed because the QueryInterface call on the COM component for the interface with IID '{67001225-D106-41AE-8BD4-5A0A119C5C01}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).此操作失败,因为 IID 为“{67001225-D106-41AE-8BD4-5A0A119C5C01}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE)) .

Any ideas on this?对此有什么想法吗?

UPDATE更新

I was not so clear explaining my problem and what I want to achive.我不太清楚地解释我的问题以及我想要实现的目标。

I'm playing with Azure Service Fabric (both stateless and stateful services): my question is: what's the best way to call a micro service hosted in Azure Service Fabric?我正在使用 Azure Service Fabric(无状态和有状态服务):我的问题是:调用 Azure Service Fabric 中托管的微服务的最佳方式是什么?

Regards, Attilio问候, 阿提利奥

You can't use ServicePartitionResolver , it is a reliable service feature and must be called from within a service running in your cluster.您不能使用ServicePartitionResolver ,它是一项可靠的服务功能,必须从集群中运行的服务中调用。

I couldn't understand clearly what you want.我无法清楚地理解你想要什么。

If you want to manage the service and get details about it, like query running instances or replicas, add or remove instances, and so on, Use the Fabric Client, below is a quick snippet, check details here and here :如果您想管理服务并获取有关它的详细信息,例如查询正在运行的实例或副本、添加或删除实例等,请使用 Fabric 客户端,下面是一个快速片段,请在此处此处查看详细信息:

` `

using System.Fabric;
using System.Security.Cryptography.X509Certificates;

string clientCertThumb = "71DE04467C9ED0544D021098BCD44C71E183414E";
string serverCertThumb = "A8136758F4AB8962AF2BF3F27921BE1DF67F4326";
string CommonName = "www.clustername.westus.azure.com";
string connection = "clustername.westus.cloudapp.azure.com:19000";

var xc = GetCredentials(clientCertThumb, serverCertThumb, CommonName);
var fc = new FabricClient(xc, connection);`

or,或者,

If you want to communicate to a running service, like an API, you should use a Reverse Proxy to resolve your services via URL, like the below snippet, more details here :如果您想与正在运行的服务(例如 API)进行通信,您应该使用反向代理通过 URL 解析您的服务,如下面的代码片段,更多详细信息请点击此处

http://mycluster.eastus.cloudapp.azure.com:19081/MyApp/MyService

You have to create a public facing service (such as Asp.net Core Web Api) which will expose the functionality of your service inside service fabric to outside world (outside the service fabric cluster).您必须创建一个面向公众的服务(例如 Asp.net Core Web Api),它将服务结构内的服务功能暴露给外部世界(服务结构集群之外)。 FabricClient approach is to be utilzied for calling services from within the service fabric cluster and not outside. FabricClient 方法将用于从服务结构集群内部而不是外部调用服务。

From your Asp.net Core service you will use the FabricClient to access the service hosted, so in general your asp.net core app act as reverse proxy to expose the functionality of actual service.从您的 Asp.net Core 服务中,您将使用 FabricClient 访问托管的服务,因此通常您的 asp.net Core 应用程序充当反向代理来公开实际服务的功能。

You cannot access service in an ASF cluster from the outside using the ServicePartitionResolver .您无法使用ServicePartitionResolver从外部访问 ASF 集群中的服务。

You have to have a public facing endpoint on your cluster, like a stateless service acting as a web api for example.您必须在集群上有一个面向公众的端点,例如充当 Web api 的无状态服务。

From the docs :文档

Services connecting to each other inside a cluster generally can directly access the endpoints of other services because the nodes in a cluster are on the same local network.集群内部相互连接的服务通常可以直接访问其他服务的端点,因为集群中的节点位于同一个本地网络上。 In some environments, however, a cluster may be behind a load balancer that routes external ingress traffic through a limited set of ports.但是,在某些环境中,集群可能位于负载均衡器后面,该负载均衡器通过一组有限的端口路由外部入口流量。 In these cases, services can still communicate with each other and resolve addresses using the Naming Service, but extra steps must be taken to allow external clients to connect to services.在这些情况下,服务仍然可以相互通信并使用命名服务解析地址,但必须采取额外的步骤来允许外部客户端连接到服务。

A Service Fabric cluster in Azure is placed behind an Azure Load Balancer. Azure 中的 Service Fabric 群集位于 Azure 负载均衡器后面。 All external traffic to the cluster must pass through the load balancer .到集群的所有外部流量都必须通过负载均衡器 The load balancer will automatically forward traffic inbound on a given port to a random node that has the same port open.负载均衡器会自动将给定端口上的入站流量转发到打开相同端口的随机节点。 The Azure Load Balancer only knows about ports open on the nodes, it does not know about ports open by individual services. Azure 负载均衡器只知道节点上打开的端口,它不知道各个服务打开的端口。

So, unless your console app is hosted in the cluster as a guest executable , you have some more work to do.因此,除非您的控制台应用程序作为来宾可执行文件托管在集群中,否则您还有更多工作要做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM