简体   繁体   English

如何跨Tomcat的多个实例共享静态变量数据?

[英]How to share static variable data across multiple instances of Tomcat?

My company has outgrown its single-instance Tomcat server and so needs to run multiple instances. 我的公司已超出其单实例Tomcat服务器,因此需要运行多个实例。 However, in the code there are many static variables that keep track of various data including objects. 但是,在代码中有许多静态变量可以跟踪包括对象在内的各种数据。 (Sure, let the jabs and jeers begin.:) I have considered moving the static variables to be stored in Redis, but that could potentially be a lot of work. (当然,让刺戳和嘲笑开始。:)我已经考虑过移动静态变量来存储在Redis中,但这可能是很多工作。 Is there an easier way to share mutable static-variable information across instances of Tomcat? 是否有更简单的方法在Tomcat实例之间共享可变的静态变量信息?

Update: per suggestion by @jake, a note on the nature of the variables. 更新:根据@jake的建议,关于变量性质的说明。 These variables mostly store aggregate information for the service as a whole, such as info on the users logged in within the past hour, various features accessed, etc. 这些变量主要存储整个服务的聚合信息,例如过去一小时内登录的用户信息,访问的各种功能等。

You are talking about to have a cluster. 你正在谈论拥有一个集群。 So you need a set of new paradigms. 所以你需要一套新的范例。 You can see this image as an example: Distributed system overview http://a3ab771892fd198a96736e50.javacodegeeks.netdna-cdn.com/wp-content/uploads/2012/11/session-repicate1.png 您可以将此图像作为示例: 分布式系统概述http://a3ab771892fd198a96736e50.javacodegeeks.netdna-cdn.com/wp-content/uploads/2012/11/session-repicate1.png

Refer to this link to read more about clustering and load balancing . 请参阅此链接以了解有关群集和负载平衡的更多信息。

"[..] many static variables" keeping track of mutable information across the webapp sounds terrifying, frankly. 坦率地说,“[...]许多静态变量”跟踪webapp上的可变信息听起来很可怕。 If concurrency isn't being handled well, this is likely a source of all kinds of errors/problems. 如果并发处理不当,这可能是各种错误/问题的根源。 But more importantly even if concurrency is being done properly, that alone could be contributing to resource contention performance problems that are making your app outgrow its single-instance life. 但更重要的是,即使并发正在得当,单独可以促进资源 ,它正在你的应用长大了单一实例的生命争的性能问题 In this case even if you address the implementation by replacing with some type of more robust data store (which you probably should), the added network latency will likely exacerbate that problem substantially. 在这种情况下,即使您通过替换某些类型的更强大的数据存储(您可能应该这样做)来解决实现问题,增加的网络延迟也可能会大大加剧该问题。

I would suggest reviewing what those variables are and determine if each of them really actually does need to be shared across the entire distributed application. 我建议检查这些变量是什么,并确定它们中的每一个是否确实需要在整个分布式应用程序中共享。 If they turn out to be things that are just cached to avoid recalculations or re-fetching from a database, then maybe you can avoid putting those in a centralized datastore, and do something simpler like just putting them in application scope as @EJP recommended. 如果它们被证明是为了避免重新计算或从数据库重新获取而被缓存的东西,那么也许你可以避免将它们放在一个集中的数据存储区中,并做一些更简单的事情,比如将它们放在@EJP推荐的应用范围内。

We'd need more specific use scenarios about some of those variables to make better recommendations. 我们需要有关这些变量的更具体的使用场景来提出更好的建议。

You cannot do this as different tomcat instances are basically different process (possibly on different machines altogether) having separate heap space. 你不能这样做,因为不同的tomcat实例基本上是不同的进程(可能在不同的机器上),具有单独的堆空间。
This is the same problem as storing session variables (Although Tomcat allows replicating session variable across instances) as you scale up replicating across instances becomes considerable overhead (and performance bottleneck) . 这与存储会话变量的问题相同(尽管Tomcat允许跨实例复制会话变量), 因为扩展实例的复制会产生相当大的开销(和性能瓶颈)

So even if you could manage to replicate everything you are only delaying your problem for later and not solving it. 所以,即使你能够设法复制一切,你只是为了以后推迟你的问题,而不是解决它。 For scaling any type server side storage is discouraged. 对于扩展任何类型的服务器端存储是不鼓励的。 If absolutely necessary distributed cache is the right direction . 如果绝对必要的分布式缓存是正确的方向 Or you could store everything in DB (added overhead of DB calls). 或者您可以将所有内容存储在DB中(增加了DB调用的开销)。

Reference 参考
In Java, are static class members shared among programs? 在Java中,是程序之间共享的静态类成员吗?
session in cookis pattern 在cookis模式中的会话

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM