将请求发送到在Service Fabric群集中的特定节点上运行的ASP.Net Core Web API [英] Sending request to ASP.Net Core Web API running on a specific node in a Service Fabric Cluster

查看:95
本文介绍了将请求发送到在Service Fabric群集中的特定节点上运行的ASP.Net Core Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Service Fabric应用程序,其中正在运行包含一堆ASP.NET Core Web API的应用程序.现在,当我在配置有5个节点的本地服务结构群集上运行应用程序时,该应用程序成功运行,并且能够发送公开Web API的发布请求.实际上,我想以相同的群集节点上运行的代码,对特定节点上公开的API发出不同的发布请求.

I am working on a Service Fabric Application, in which I am running my Application that contains a bunch of ASP.NET Core Web APIs. Now when I run my application on my local service fabric cluster that is configured with 5 nodes, the application runs successfully and I am able to send post requests the exposed Web APIs. Actually I want to hit the code running on a same cluster node with different post requests to the exposed APIs on that particular node.

为进一步说明,例如,在节点'0'上公开了一个API,该API接受发布请求并执行作业,并且还有一个API可以中止正在运行的作业.现在,当我请求执行一个作业时,它开始在节点"0"上执行,但是当我尝试中止该作业时,服务结构群集将请求转发到另一个节点,例如节点"1".结果,我无法中止正在运行的作业,因为节点"1"上没有可用的正在运行的作业.我不知道该如何处理.

For further explanation, for example there is an API exposed on Node '0' that accept a post request and execute a Job, and also there is an API that abort the running job. Now when I request to execute a Job, it starts to execute on Node '0' but when I try to abort the Job, the service fabric cluster forward the request to a different node for example say node '1'. In resulting I could not able to abort the running Job because there is no running Job available on Node '1'. I don't know how to handle this situation.

对于状态,我正在使用ASP.Net Core Web API类型的无状态服务,并在本地服务结构群集的5个节点上运行该应用程序.

For states, I am using a Stateless service of type ASP.Net Core Web API and running the app on 5 nodes of my local service fabric cluster.

请提出最佳方法.

推荐答案

您的问题是因为您正在运行API来执行Worker任务.

Your problem is because you are running your APIs to do a Worker task.

您应该使用API​​在后台(Process \ Worker)中安排工作,并向用户返回令牌或操作ID.用户将使用此令牌来请求状态或取消任务.

You should use your API to schedule the work in the Background(Process\Worker) and return to the user a token or operation id. The user will use this token to request the status or cancel the task.

第一步:当您首次调用API时,您可以生成一个GUID(或在DB中插入)并将此消息放入队列(即Service Bus),然后将GUID返回给调用者.

The first step: When you call your API the first time, you could generate a GUID(Or insert in DB) and put this message in a queue(i.e: Service Bus), and then return the GUID to the caller.

第二步:一个工作进程将在您的集群中运行,以侦听此队列中的消息,并在消息到达时处理这些消息.您可以将其设置为单线程服务,以循环方式按消息处理消息,也可以使多线程服务使用一个线程为每个消息处理多个消息.这取决于您想要变得多么复杂:

The second step: A worker process will be running in your cluster listening for messages from this queue and process these messages whenever a message arrives. You can make this a single thread service that process message by message in a loop, or a multi-threaded service that process multiple messages using one Thread for each message. It will depend how complex you want to be:

  • 在单线程侦听器中,要扩展应用程序,您必须跨多个实例,以便多个任务可以并行运行,您可以在SF中使用简单的scale命令执行此操作,并且SF将分发服务实例跨您的可用节点.

  • In a single threaded listener, to scale you application, you have to span multiple instances so that multiple tasks will run in parallel, you can do that in SF with a simple scale command and SF will distribute the service instances across your available nodes.

在多线程版本中,您将必须管理并发性以获得更好的性能,可能必须考虑内存,cpu,磁盘等,否则您可能会冒着在单个节点中负载过多的风险.

In a multi-threaded version you will have to manage the concurrency for better performance, you might have to consider memory, cpu, disk and so on, otherwise you risk having too much load in a single node.

第三步,取消:取消过程很容易,并且有很多方法:

The third step, the cancellation: The cancellation process is easy and there are many approaches:

  • 使用类似的方法并加入取消消息
    • 您的服务将在单独的线程中侦听取消,并取消正在运行的任务(如果正在运行).
    • 使用其他队列发送取消消息更好
    • 如果运行多个侦听器实例,您可能会考虑一个主题而不是一个队列.
    • Using a similar approach and enqueue a cancellation message
      • Your service will listen for the cancellation in a separate thread and cancel the running task(if running).
      • Using a different queue to send the cancellation messages is better
      • If running multiple listener instances you might consider a topic instead of a queue.

      方法很多,很简单,可以结合使用多种方法来更好地控制自己的任务.

      There are many approaches, these are simple, and you might make use of multiple in combination to have a better control of your tasks.

      这篇关于将请求发送到在Service Fabric群集中的特定节点上运行的ASP.Net Core Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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