URL 使用 OWIN 通信侦听器的有状态服务

URL of Stateful Service using OWIN communication listener

我使用以下示例为我的 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>

现在部署我的应用程序时,我在 URL 上使用各种 guid 获得我的服务:

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

如果我尝试访问 http://localhost:8090/ 本身,我会收到错误 503

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

您所指的 "out of the box" 解决方案取决于分区类型。单例分区可以被它的服务访问 URL:

http://localhost:8090/ApplicationName/ServiceName

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

为了解决这个问题,您可以使用 reverse proxy。反向代理允许您通过 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

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