我如何到达无状态服务的特定副本 [英] How can I reach a specific replica of a stateless service

查看:112
本文介绍了我如何到达无状态服务的特定副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Service Fabric中创建了一个无状态服务.它具有SingletonPartition,但是有多个实例(在我的情况下,InstanceCount为-1​​).

I've created a stateless service within Service Fabric. It has a SingletonPartition, but multiple instances (InstanceCount is -1 in my case).

我想与此服务的特定副本进行通信.要查找所有副本,我使用:

I want to communicate with a specific replica of this service. To find all replica's I use:

var fabricClient = new FabricClient();
var serviceUri = new Uri(SERVICENAME);

Partition partition = (await fabricClient.QueryManager.GetPartitionListAsync(serviceUri)).First();
foreach(Replica replica in await fabricClient.QueryManager.GetReplicaListAsync(partition.PartitionInformation.Id))
{
  // communicate with this replica, but how to construct the proxy?
  //var eventHandlerServiceClient = ServiceProxy.Create<IService>(new Uri(replica.ReplicaAddress));
}

问题在于ServiceProxy没有重载,无法为副本创建一个.还有另一种与特定副本进行通信的方式吗?

The problem is that there is no overload of the ServiceProxy to create one to the replica. Is there another way to communicate with a specific replica?

修改

我们正在构建的场景如下.我们有不同的带有计数器信息的移动部件:1个名为分区状态服务(具有数百个分区),1个int64分区状态服务和1个具有状态的actor.要汇总计数器信息,我们需要联系所有服务分区和参与者实例.

The scenario we are building is the following. We have different moving parts with counter information: 1 named partitioned stateful service (with a couple of hundred partitions), 1 int64 partitioned stateful service, and 1 actor with state. To aggregate the counter information, we need to reach out to all service-partitions and actor-instances.

我们当然可以撤消它,让每个人都将计数发送到单个(分区的)服务.但这会在正常流程中增加网络呼叫(并因此增加开销).

We could of course reverse it and let everyone send there counts to a single (partitioned) service. But that would add a network call in the normal flow (and thus overhead).

相反,我们提出了以下内容.提到的服务和角色被组合为一个可执行文件和一个服务清单.因此,它们处于同一过程中.我们将实例计数为-1的无状态服务添加到上述服务和参与者.所有计数器信息都存储在静态变量中.无状态服务可以读取此计数器信息. 现在,我们只需要联系无状态服务(该服务的节点数上限).

Instead, we came up with the following. The mentioned services&actors are combined into one executable and one servicemanifest. Therefore they are in the same process. We add a stateless service with instancecount -1 to the mentioned services&actors. All counter information is stored inside a static variable. The stateless service can read this counter information. Now, we only need to reach out to the stateless service (which has an upper limit of the number of nodes).

推荐答案

首先要弄清一些术语,副本"仅适用于有状态服务,在该状态服务中,您为服务的每个分区都设置了唯一的副本,并且在它们之间为HA复制状态.无状态服务仅具有实例,所有实例都是相同且相同的.

Just to get some terminology out of the way first, "replica" only applies to stateful services where you have a unique replica set for each partition of a service and replicate state between them for HA. Stateless services just have instances, all of which are equal and identical.

现在要回答您的实际问题:ServiceProxy没有连接到已部署的无状态服务的特定实例的选项.您有以下选择:

Now to answer your actual question: ServiceProxy doesn't have an option to connect to a specific instance of a deployed stateless service. You have the following options:

  • 主副本:连接到有状态服务分区的主副本.
  • 随机实例:连接到无状态服务的随机实例.
  • 随机副本:连接到有状态服务分区的随机副本(无论其角色如何).
  • 随机二级副本-连接到有状态服务分区的随机二级副本.

例如:

ServiceProxy.Create<IMyService>(serviceUri, partitionKey, TargetReplicaSelector.RandomInstance)

那么为什么没有选择连接到特定的无状态服务实例呢?

So why no option to connect to a specific stateless service instance?

好吧,我想解决这个问题,并问您为什么想要连接到特定的无状态服务实例?根据定义,每个无状态实例应相同.如果您要在其中保留某些状态(例如用户会话),那么您现在是有状态的,应该使用有状态的服务.

Well, I would turn this question around and ask why would you want to connect to a specific stateless service instance? By definition, each stateless instance should be identical. If you are keeping some state in there - like user sessions - then now you're stateful and should use stateful services.

您可能会想到智能地确定要连接到哪个实例以进行负载平衡,但是由于它是无状态的,因此只要请求平均分配,任何实例都不应比其他任何实例做更多的工作.为此,服务代理具有随机分发选项.

You might think of intelligently deciding which instance to connect to for load balancing, but again since it's stateless, no instance should be doing more work than any other as long as requests are distributed evenly. And for that, Service Proxy has the random distribution option.

考虑到这一点,如果您仍然有理由寻找特定的无状态服务实例,则可以始终使用不同的通信堆栈(例如HTTP),并执行所需的任何操作.

With that in mind, if you still have some reason to seek out specific stateless service instances, you can always use a different communication stack - like HTTP - and do whatever you want.

这篇关于我如何到达无状态服务的特定副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆