[英]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();
}
您可以使用某些排队系统,JMS或其他http://en.m.wikipedia.org/wiki/Java_Message_Service
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.