问题描述
我正在Azure Service Fabric中创建无状态服务.但是,一旦服务启动(或在自定义通信侦听器启动时),我需要获取该服务的所有其他实例/分区的地址.
I'm creating a stateless service inside Azure Service Fabric. However, once service starts (or on start of custom communication listener) I need to get addresses of all other instances/partitions of that service.
我这样做是通过创建新的FabricClient
并调用fabricClient.QueryManager.GetPartitionListAsync(serviceUri)
来实现的.但是,当第一个服务实例开始运行时,我收到消息 Service不存在的FabricServiceNotFoundException
..
I'm doing so by creating new FabricClient
and calling fabricClient.QueryManager.GetPartitionListAsync(serviceUri)
. However I'm getting FabricServiceNotFoundException
with message Service does not exist. when a first service instance starts running.
我在文档中找不到该问题,所以我的问题是:当该服务的新实例启动时,如何获取在Azure Service Fabric中运行的特定服务的所有活动实例的侦听终结点地址的列表跑步吗?
I couldn't find that in documentation, so my question is: how can I get a list of listening endpoint addresses for all active instances of a particular service, running in Azure Service Fabric, when a new instance of that service starts running?
推荐答案
端点地址实际上位于服务副本和实例上-这些实际上是放置在节点上的东西. ServiceManagerClient 称为 ResolveServicePartitionAsync .
Endpoint addresses are actually on service replicas and instances - those are the things that are actually placed on nodes. There is a special method for this in the ServiceManagerClient called ResolveServicePartitionAsync.
在Reliable Services SDK(在Microsoft.ServiceFabric.Services.Client命名空间中)中,我们提供了一个分解器实用程序,使此操作变得容易一些:
In the Reliable Services SDK (in the Microsoft.ServiceFabric.Services.Client namespace) we provide a resolver utility that makes this a bit easier:
ServicePartitionResolver resolver = ServicePartitionResolver.GetDefault();
ResolvedServicePartition partition =
await resolver.ResolveAsync(new Uri("fabric:/MyApp/MyService"), new ServicePartitionKey(), cancellationToken);
ResolvedServicePartition的Endpoints属性是分区中每个副本/实例的列表. Address属性将包含一个JSON对象,该对象是键/值对的列表,其中包含由副本/实例打开的每个侦听器:
The Endpoints property of ResolvedServicePartition is a list of each replica/instance in the partition. The Address property will contain a JSON object, which is a list of key-value pairs containing each listener that's opened by the replica/instance:
{
"Endpoints" :
{ "mylistener1" : "some-address" },
{ "mylistener2" : "some-address" }
...
}
请记住,复制副本/实例出现时没有确定的顺序,因此您可能需要重试几次.此外,副本和实例将在服务的整个生命周期中不时移动,因此您需要使列表保持最新.基本上,您不能真的一次性就将所有这些信息预先设置好,因为它是一个非常动态的系统.
Keep in mind that there's no guaranteed order for when replicas/instances come up, so you may need to retry a few times. Also, replicas and instances will move at times throughout the service's lifetime, so you'll need to keep the list up to date. Basically you can't really just get all this information up front in one go and be set because it's a very dynamic system.
See here for an overview:https://azure.microsoft.com/en-us/documentation/articles/service-fabric-connect-and-communicate-with-services/
此处提供更多详细信息: https://azure.microsoft.com/zh-CN/documentation/articles/service-fabric-reliable-services-communication/
And here for more detailed information: https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-services-communication/
这篇关于在服务启动时获取所有活动的服务实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!