简体   繁体   English

需要来自同一客户端的所有请求才能进入同一实例

[英]Need all requests from the same client to go to the same instance

I have a GWT based Java web application deployed to Google App Engine, in which the servelet reads and changes state held in memory. 我有一个部署到Google App Engine的基于GWT的Java Web应用程序,其中小服务器读取并更改保存在内存中的状态。 The client code might send requests to change this state and, subsequently, to change or read the same state. 客户端代码可能会发送请求以更改此状态,然后再更改或读取同一状态。 So it's important that all requests from the same instance of the client page go to the same instance of the application's version. 因此,将来自客户端页面同一实例的所有请求都转到应用程序版本的同一实例非常重要。

Since I don't expect a lot of traffic, I don't mind limiting the maximum number of instances to 1. But I'd like that one instance to exist more or less permanently. 由于我不希望有太多流量,因此我不介意将实例的最大数量限制为1。但是我希望一个实例或多或少地永久存在。 (If a user takes more than, say, an hour between requests, I don't mind if their data is lost.) (如果用户两次请求之间花费的时间超过一个小时,那么我不介意其数据是否丢失。)

In detail, the way I'm managing state is that I have a static variable that points to a hash table, the hash table maps strings to states. 详细地说,我管理状态的方式是我有一个指向哈希表的静态变量,哈希表将字符串映射到状态。 On the first request from the client, a new unique string is created and a new state and a new entry is made in the hash table. 在客户端发出的第一个请求中,将创建一个新的唯一字符串,并在哈希表中创建一个新状态和一个新条目。 The string is returned in the response. 该字符串在响应中返回。 On subsequent requests, the client sends the string so that the servelet can find the state that it needs to mutate or read. 在后续请求中,客户端发送字符串,以便servelet可以找到它需要突变或读取的状态。 I can't keep the state in a database because it is very complex and not at all serializable. 我无法将状态保留在数据库中,因为它非常复杂并且根本无法序列化。

What are the ways to ensure that all requests from a given client instance go to the same server instance? 有什么方法可以确保来自给定客户端实例的所有请求都发送到同一服务器实例?

What are the ways to ensure that all requests from a given client instance go to the same server instance? 有什么方法可以确保来自给定客户端实例的所有请求都发送到同一服务器实例?

There are no ways, by design. 设计是没有办法的。 If you want to persist state between requests reliably, use the datastore, with memcache as a cache. 如果要可靠地在请求之间保持状态,请使用数据存储,将memcache用作缓存。

Adding: You can also use cookies if your data storage is meager, obfuscating/encrypting them as needed. 添加:如果您的数据存储量很少,也可以使用cookie,并根据需要对其进行混淆/加密。

App Engine assumes that applications hold no essential state between requests. App Engine假定应用程序在请求之间不保留任何基本状态。 That makes spinning up/shutting down instances a non-issue. 这使得旋转/关闭实例不再是问题。

I second Dave's answer, GAE is not exactly the right fit for what you desire. 我同意Dave的回答,GAE并非完全适合您的需求。

However there could be ways around it, but in only a few specific cases: if you're using the standard GAE environment with manual scaling and a subsequent request is always based on URLs embedded in the response to the previous request. 但是, 可能有一些解决方法,但是仅在少数特定情况下:如果您使用具有手动缩放功能的标准GAE环境,并且后续请求始终基于对先前请求的响应中嵌入的URL。

You could craft the URLs in a response to a request according to the targeted routing rules such that subsequent requests hit the same instance. 您可以根据目标路由规则在响应请求的过程中构建URL,以使后续请求到达同一实例。 From Targeted routing : 目标路由

  • If you are still using backends or have manually-scaled services , you can target and send a request to a instance by including the instance ID. 如果您仍在使用后端或具有手动扩展的服务 ,则可以通过包含实例ID来定向实例并将请求发送到实例。 The instance ID is an integer in the range from 0 up to the total number of instances that are running, and can be specified as follows: 实例ID是一个整数,范围是0到正在运行的实例总数,可以指定如下:

Sends a request to a specific service and version within a specific instance: 向特定实例内的特定服务和版本发送请求:

 https://INSTANCE_ID-dot-VERSION_ID-dot-SERVICE_ID-dot-MY_PROJECT_ID.appspot.com http://INSTANCE_ID.VERSION_ID.SERVICE_ID.MY_CUSTOM_DOMAIN 

Note: Targeting an instance is not supported in services that are configured for auto scaling or basic scaling. 注意:在为自动缩放或基本缩放配置的服务中,不支持以实例为目标。 The instance ID must be an integer in the range from 0, up to the total number of instances running. 实例ID必须是0到运行实例总数之间的整数。 Regardless of your scaling type or instance class, it is not possible to send a request to a specific instance without targeting a service or version within that instance. 无论您使用的是缩放类型还是实例类,都无法将请求发送到特定实例而不将实例或服务定位到该实例中。

To determine the instance ID you could use the modules API , for example: 要确定实例ID,可以使用模块API ,例如:

// Get the instance handling the current request.
int currentInstance = modulesApi.getCurrentInstance();

Note that if the targeted instance goes down you will keep getting errors permanently (that instance will not come back), so you might want to think at a fall-back solution for going somehow to a non-instance-based based page from where you could hitch a flow on another instance. 请注意,如果目标实例出现故障,您将继续永久获得错误(该实例将不会再出现),因此您可能需要考虑使用一种后备解决方案,以某种方式从您所在的位置转到基于非实例的页面可能会在另一个实例上发生故障。

But such solution is not available in the GAE flex environment. 但是,这种解决方案在GAE flex环境中不可用。 From Targeted routing : 目标路由

Note: In the flexible environment, targeting an instance is not supported. 注意:在灵活的环境中,不支持定位实例。 It is not possible to send requests directly to a specific instance. 无法将请求直接发送到特定实例。

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

相关问题 Google AppEngine将所有请求发送到同一实例 - Google AppEngine sending all requests to same instance Go:如何在所有页面上设置相同的Cookie? - Go : How to Set same Cookie on all pages? 验证来自同一项目中容器引擎的App Engine请求 - Authenticating App Engine requests from Container Engine within the same project 如何从Servlet调用在同一AppEngine实例上运行的资源网址 - how to call a resource url running on the same AppEngine instance from a servlet 对同一GCS / blobstore URL的多个上载请求 - Multiple upload requests to the same GCS/blobstore URL 从Google Datastore获取与列表相同类型的所有实体? - Get all entities of same kind as list from Google Datastore? 为什么对新(动态)实例的请求要比对常驻实例的请求多? - Why do more requests go to new (dynamic) instances than to resident instance? 难以将实体实例从服务器传输到客户端 - Difficulty to transfer Entity instance from server to client AppEngine-Go模块是否必须共享相同的代码库 - Do AppEngine-Go modules have to share the same code base 是否可以通过GAE套接字API将相同的IP用于多个请求? - Is there any way to use same IP for several requests with GAE Sockets API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM