Azure Service Fabric服务之间的Http通信

4

我的情况:

问题:

我试图将我的有状态服务和无状态 WebAPI 之间的通信从 RPC 更改为 HTTP 通信,因为我想避免使用接口在服务之间进行通信。使用 HTTP 通信是否可行?如果可以,如何使我的无状态 WebAPI 在不使用接口的情况下调用我的有状态应用程序中的特定方法?

更新:

感谢 alltej 的帮助,我开始更多地阅读有关 HttpCommunicationListeners 的信息。这个教程(https://learn.microsoft.com/nl-nl/azure/service-fabric/service-fabric-concepts-partitioning)很好地解释了 Http 通信。

在下面的代码片段中:"CreateServiceReplicaListeners()" 调用 CreateInternalListener(),然后调用 "ProcessInternalRequest()",最后调用 "AddUserAsync()"。

    protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return new[] { new ServiceReplicaListener(context => this.CreateInternalListener(context))};
    }

    private ICommunicationListener CreateInternalListener(ServiceContext context)
    {
        EndpointResourceDescription internalEndpoint = context.CodePackageActivationContext.GetEndpoint("ProcessingServiceEndpoint");

        string uriPrefix = String.Format(
            "{0}://+:{1}/{2}/{3}-{4}/",
            internalEndpoint.Protocol,
            internalEndpoint.Port,
            context.PartitionId,
            context.ReplicaOrInstanceId,
            Guid.NewGuid());

        string nodeIP = FabricRuntime.GetNodeContext().IPAddressOrFQDN;

        string uriPublished = uriPrefix.Replace("+", nodeIP);
        return new HttpCommunicationListener(uriPrefix, uriPublished, this.ProcessInternalRequest);
    }

    private async Task ProcessInternalRequest(HttpListenerContext context, CancellationToken cancelRequest)
    {
        string output = null;
        string user = context.Request.QueryString["lastname"].ToString();

        try
        {
            output = await this.AddUserAsync(user);
        }
        catch (Exception ex)
        {
            output = ex.Message;
        }

        using (HttpListenerResponse response = context.Response)
        {
            if (output != null)
            {
                byte[] outBytes = Encoding.UTF8.GetBytes(output);
                response.OutputStream.Write(outBytes, 0, outBytes.Length);
            }
        }
    }

    private async Task<string> AddUserAsync(string user)
    {
        IReliableDictionary<String, String> dictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<String, String>>("dictionary");

        using (ITransaction tx = this.StateManager.CreateTransaction())
        {
            bool addResult = await dictionary.TryAddAsync(tx, user.ToUpperInvariant(), user);

            await tx.CommitAsync();

            return String.Format(
                "User {0} {1}",
                user,
                addResult ? "sucessfully added" : "already exists");
        }
    }
}
1个回答

3

我已经创建了实现ICommunicationListener的HttpCommunicationListener.cs。我可以解析有状态服务的URI,但是如何从我的无状态WebAPI中调用有状态服务的特定方法/函数呢? - TheSugoiBoi
你需要从有状态服务中调用无状态方法,因为你需要在可靠队列中管理事务。你可以在可靠服务的RunAsync方法内部调用它。 - alltej
等等……我需要从有状态服务中调用无状态方法吗?难道不应该是反过来吗?我希望我的无状态WebApi(POST操作)调用我的有状态服务中的“Blah()”任务。 - TheSugoiBoi
从无状态的Web API网关调用有状态服务方法,将请求添加到可靠队列中。在有状态服务的RunAsync方法中,添加代码以处理队列中的数据。那个调用涉及循环进入队列并出列记录。或者,队列中的数据处理可以调用一个无状态服务来实际运行长时间的过程。有状态服务只是传入请求的数据存储。 - alltej
感谢 @Jacky 。如果您对答案满意,请不要忘记点赞。谢谢! - alltej
我已经点赞了,但由于我的声望不到15分,它已被记录但未显示。 - TheSugoiBoi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接