0

我使用以下示例为我的 配置通信侦听器Stateful Service

https://github.com/Microsoft/azure-docs/blob/master/articles/service-fabric/service-fabric-reliable-services-communication-webapi.md

相关片段:

public Task<string> OpenAsync(CancellationToken cancellationToken)
{
    var serviceEndpoint = this.serviceContext.CodePackageActivationContext.GetEndpoint(this.endpointName);
    var protocol = serviceEndpoint.Protocol;
    int port = serviceEndpoint.Port;

    if (this.serviceContext is StatefulServiceContext)
    {
        StatefulServiceContext statefulServiceContext = this.serviceContext as StatefulServiceContext;

        this.listeningAddress = string.Format(
            CultureInfo.InvariantCulture,
            "{0}://+:{1}/{2}{3}/{4}/{5}",
            protocol,
            port,
            string.IsNullOrWhiteSpace(this.appRoot)
                ? string.Empty
                : this.appRoot.TrimEnd('/') + '/',
            statefulServiceContext.PartitionId,
            statefulServiceContext.ReplicaId,
            Guid.NewGuid());
    }
...

服务清单片段:

<Endpoints>
  <Endpoint Protocol="http" Name="ServiceEndpoint" Type="Input" Port="8090" />
  <Endpoint Name="ReplicatorEndpoint" />
</Endpoints>

现在,当部署我的应用程序时,我可以通过各种 guid 在 URL 上获得服务:

http://localhost:8090/ba794109-bba3-4cdf-8434-d718be264087/131407811483781446/614de30b-14a7-4172-a03d-4e28d23cf28d

如果我尝试http://localhost:8090/自行访问,则会收到错误503

有什么方法可以将通用 URL 映射到主分区和副本?或者在有状态服务中是不可能的?在 Stateless 中,您将开箱即用。

4

1 回答 1

2

您所指的“开箱即用”解决方案取决于分区类型。可以通过其服务 URL 访问单例分区:

http://localhost:8090/ApplicationName/ServiceName

这不适用于具有 Named 或 Int64Range 分区的服务,因为 URL 不引用服务的特定分区。这就是为什么您必须调用包含 GUID 的服务 URL。GUID 指的是分区。

为了解决这个问题,您可以使用反向代理。反向代理允许您通过 URL 提供分区信息。您可以通过 URL 调用您的分区服务:

http(s)://<Cluster FQDN | internal IP>:Port/<ServiceInstanceName>/<Suffix path>?PartitionKey=<key>&PartitionKind=<partitionkind>&ListenerName=<listenerName>&TargetReplicaSelector=<targetReplicaSelector>&Timeout=<timeout_in_seconds>

在您的集群中,它可能看起来像:

http://clusterIP:19081/ApplicationName/ServiceName/PartitionKey=1&PartitionKind=Int64Range

请注意,反向代理(当前)在本地开发集群上不可用。出于开发目的,我建议使用带有 GUID 的 URL,或者暂时将分区更改为单例方案。

于 2017-06-01T09:23:51.527 回答