简体   繁体   English

集群式无状态Web应用程序-如何处理并发请求?

[英]Clustered, Stateless web application - how to handle concurrent requests?

We have a spring mvc web application that will serve many users in their actions. 我们有一个spring mvc Web应用程序,它将为许多用户提供服务。 Our data source is a mySQL DB. 我们的数据源是一个mySQL DB。

The web app is located on a cluster of tomcat server. 该Web应用程序位于tomcat服务器的群集上。

Users do not have a session while interacting with the web application. 用户与Web应用程序交互时没有会话。

The protocol is a simple REST API between users and the web app. 该协议是用户和Web应用之间的简单REST API。

We would like to ignore users requests if another request handling is still in progress. 如果其他请求处理仍在进行中,我们想忽略用户的请求。 for example: 例如:

user1 requests for action1...
action1 handling begins in webapp.
user1 requests for action1 again (before the handling is completed)
server declines the 2nd request since action1 handling is still in progress..
action1 handling completed.
result for action1 is returned to user1's client.
(now further actions is accepted by the web app)

How is it possible to achieve this? 如何做到这一点? if we used a single node of web app we could manage it simply in memory, but since we use a cluster and no shared memory\\cache it wont work. 如果我们使用Web应用程序的单个节点,则可以在内存中简单地对其进行管理,但是由于我们使用的是群集并且没有共享的内存\\缓存,因此它将无法正常工作。

Another alternative we thought about is using a locking in the DB, for example create a constraint for the userID in a dedicated table (called userLock) and before each handling we simply insert into this table, and finalizing by removing the entry from it, now if an illegal request is made, a constraint exception is thrown and not handled) 我们考虑过的另一种选择是在数据库中使用锁定,例如在专用表中为userID创建约束(称为userLock),在每次处理之前,我们只需将其插入该表中,然后通过从其中删除条目来完成如果提出了非法请求,则会抛出约束异常,并且不会对其进行处理)

are there any other alternatives to this "semaphore" behavior? 除了这种“信号量”行为,还有其他选择吗?

Hazelcast provides distributed implementation of java.util.concurrent.locks.Lock which could be of use to you. Hazelcast提供了java.util.concurrent.locks.Lock分布式实现,可能对您有用。

    HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
    Lock lock = hazelcastInstance.getLock( "myLock" );
    lock.lock();
    try { 
       // do something here
    } finally {
      lock.unlock();
    }

More options are listed in this answer. 答案中列出了更多选项。

您可以使用某些排队系统,JMS或其他http://en.m.wikipedia.org/wiki/Java_Message_Service

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何限制Web API中的并发请求数? - How to limit the number of concurrent requests in web api? servlet在glassfish部署中可以处理多少个并发请求? - How many concurrent requests can a servlet handle in glassfish deployment? 如何在Web应用程序中调用Java并发线程 - How to call Java concurrent threads in web application Web应用程序,集群的tomcat和mongoDB-如何实现持久性队列 - Web application, clustered tomcats and mongoDB - how to achieve a persistance queue Java并发锁在群集环境中部署的Spring Web App中失败 - Java Concurrent Locks failing in Spring Web App deployed in Clustered Environment 如何在一个应用程序中处理多个同时出现的用户请求 - how to handle multiple simultaneous user requests in an application 使用Wicket制作一个主要是无状态的Web应用程序是否很困难? - Is it difficult to make a mainly stateless web application with Wicket? 如何在真实条件下模拟Web应用程序的120个并发用户? - How to simulate 120 concurrent users of a web application with real conditions? 如何同时提出休息请求 - How to make concurrent rest requests 如何在Spring Web应用程序中预处理所有请求? - How to preprocess all requests in Spring Web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM