具有负载平衡功能的多实例 Web 服务 - 保存请求数据
我会尽力清楚地描述我的问题。 假设我在 Amazon EC2 上运行 Java REST Web 服务。 HTTP 请求使用字符串“testString”访问我的一项服务。我将它保存在某个通用类的内存中,假设我将它添加到字符串列表(“stringsList”)中。
我的问题是:如果我的网络服务有多个实例,那么该字符串在每个实例上都可用吗? 如果用户将生成一个调用“stringsList.getString('testString')”的 HTTP 请求, 它肯定会得到字符串吗?或者它可能会错过它,因为它是可用的,但在 我的网络服务的不同实例?
我希望能够将我的 Web 服务作为具有负载平衡和多个功能的可扩展服务来运行 实例,但仍然能够保存一些特定的 HTTP 请求信息(也许这个 这正是 REST 应该是无状态的原因,因此如果我想这样做,我的设计就是错误的 ? )。 我想要的网络服务是这样的:
我会尝试提供详细信息,尽管我不太确定:
- 如果我通过 SSH 到我的 EC2 实例运行 Web 服务,并且只有一个类似 这个,不会有问题吧?
- 如果我做同样的事情,但是对于很多实例,并且通过 Amazon Elastic 负载均衡?
- 如果2的答案是可能会错过,但我仍然想实现这一点,有什么替代方法?
也许在 Amazon EC2 上而不是在不同的服务上执行此操作不是一个好主意?如果有,是哪一个?
- 将其放入数据库并不是我想要的,我只想将其在内存中保存几个小时以供我需要。
谢谢,
丽然
I will try to describe my question clearly.
Suppose I ran a Java REST web service on Amazon EC2.
A HTTP request gets to one of my services with a string "testString". I save it in the memory on some general class, let's say I add it to a list ('stringsList') of strings.
My question is: If I have multiple instances of my web service, will this string be available on each one of these instances ?
If a user will produce a HTTP request that calls "stringsList.getString('testString')",
it will get the string for sure ? Or it can miss it, because it is available, but on a
different instance of my web service ?
I want to be able to ran my web service as a scalable one with load balancing, and multiple
instances, but still to be able to save some specific HTTP requests information (maybe this
is exactly why the REST should be stateless, hence my design is wrong if I wish to do this
? ).
The web service I aim for is something like this:
Scalable web service (from Heroku)
I will try to give details, although I'm not so sure:
- If I ran the web service through SSH to my EC2 instance, and there is only one like
this, it will not be a problem, right ? - If I do the same, but for many instances, and through Amazon Elastic load balancing ?
- If the answer to 2 is that there can be a miss, but I still want to achieve this, what is the alternative way ?
Maybe it's not a good idea to do this on Amazon EC2, but on a different service ? If so, which one ?
- To put it in a database is not what I want, I want to save it only for a few hours in the memory for what I need.
Thanks,
Liran
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Memcache 似乎是解决这个问题的自然选择。然而,它不仅可以在 Google App Engine 上使用。如果您愿意,您还可以在 EC2 上运行自己的 Memcache 服务器。
Memcache seems like the natural choice for this problem. However it is not only available on Google App Engine. You can also run you own Memcache server on EC2 if you so desire.
亚马逊上没有“共享内存”概念,并且该字符串并非在所有实例上都可用。这是简单的数据库,但你说你不想使用数据库。
可以始终将用户定向到他们第一次定向到在负载均衡器上使用 cookie 的同一个实例,但这听起来并不能解决您的问题,我猜该字符串不仅仅适用于设置它的用户。
Google App Engine 可能更适合。它支持您使用的所有 servlet 实例以及所有可访问的 servlet 实例的 Memcache相同的内存缓存。
There's no "shared memory" concept on Amazon and, no, that string will not be available on all instances. The is the simple database, but you say you don't want to use a DB.
It is possible to always direct a user to the same instance they were first directed to using cookies on the load balancer, but it doesn't sound like this solves your problem, I guessing that string isn't just for the user that sets it.
Google App Engine might be better suited. It supports Memcache across the servlet instances you use and all the servlet instances can access the same memcache.