如何在App Engine上实现服务器关联或粘性会话? [英] How do I implement server affinity or sticky sessions on App Engine?

查看:122
本文介绍了如何在App Engine上实现服务器关联或粘性会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序希望:


  1. 自动扩展性


    • 当流量增加时,我希望App Engine启动应用的新实例

    • 当实例闲置时,我希望App Engine关闭它们。 >

  2. 客户端/服务器关联性




    • After一个初始的客户端 - >服务器HTTP请求,我希望客户端
      能够连接到同一个应用服务器,这样应用服务器
      可以维护一堆客户端状态

    • 状态可能会经常更新(为了支持实时交互),
      ,所以基于memcache + datastore的持久性是不可取的。

    • 服务器可能需要做出决定基于多个客户的状态,例如
      实时多人游戏




  3. 我该如何做到这一点?

    解决方案

    您可以使用App Engine后端(长时间运行,可配置,可寻址的持久性服务器)实现这些目标:


    $ b

    Python实现




    1. 将后端配置为 public dynamic

       #backends.yaml 

      后端:
      - name:foo
      实例:20
      选项:public,dynamic


    2. 以通常的方式部署您的应用程序:

        appcfg.py更新。 

      请记得部署您的后端:

        appcfg.py后端。更新


    3. 对于初始连接,请让客户端使用
      非实例具体的后端主机名,例如:

        foo.your_app_id.appspot.com 

      在启动新的后端实例后,App Engine会将您的请求路由到可用的后端
      实例。


    4. 在服务器上的请求处理代码中,使用后端
      API来确定哪个实例处理请求,
      返回客户端特定于实例的URL。

        from google.appengine.api导入后端

      导入webapp2

      类GetPersistentUrlHandler(webapp2 .RequestHandler):

      def get(self):
      返回当前后端的特定于实例的URL。

      my_url = backends.get_url (instance = backends.get_instance())
      self.response.write(my_url)

      app = webapp2.WSGIApplication([
      ('/ get_peristent_url ,GetPersistentUrlHandler),
      ],debug = True)


    5. 到特定实例的
      后端URL:

        http://3.foo.your_app_id.appspot.com 

      注意:使用https时,请务必用 -dot替换子域名点 -
      为了避免SSL证书问题。


        https: 




      局限性




      1. 后端不会永久存在,并且可能意外关机而无需事先通知

      2. 您的应用程序可以拥有的后端数量目前有限


      My application wishes to have:

      1. Automatic scalability

        • I want App Engine to spin up new instances of my app when traffic increases
        • When instances become idle, I want App Engine to shut them down
      2. Client/server affinity

        • After an initial client->server HTTP request, I want clients to be able to connect to the same appserver, so that the appserver can maintain a bunch of client state
        • State may be updated frequently (in order to support real-time interactions), so memcache+datastore based persistence is not desirable.
        • The server may need to make decisions based on the state of multiple clients, e.g. real time multi-player game

      How can I accomplish this?

      解决方案

      You can achieve these goals using App Engine backends (long-running, configurable, addressable, persistent servers):

      Python implementation

      1. Configure a backend to be both public and dynamic

        # backends.yaml
        
        backends:
        - name: foo
          instances: 20
          options: public, dynamic
        

      2. In addition to deploying your app in usual way:

        appcfg.py update .
        

        remember to deploy you backend:

        appcfg.py backends . update
        

      3. For the initial connection, have your client use the non-instance specific backend hostname, e.g.:

        foo.your_app_id.appspot.com
        

        App Engine will route your request to available backend instance, after optionally starting a new backend instance.

      4. In the request handling code on the server, use the backends API to determine which instance is handling the request and return to the client a instance specific URL.

        from google.appengine.api import backends
        
        import webapp2
        
        class GetPersistentUrlHandler(webapp2.RequestHandler):
        
          def get(self):
            """Return the current backend's instance-specific URL."""
        
            my_url = backends.get_url(instance=backends.get_instance())
            self.response.write(my_url)
        
        app = webapp2.WSGIApplication([
          ('/get_peristent_url', GetPersistentUrlHandler),
        ], debug=True)
        

      5. Client makes subsequent connections to the instance specific backend URL:

        http://3.foo.your_app_id.appspot.com
        

        Note: when using https be sure to replace subdomain dots with -dot- in order to avoid SSL certificate issues.

        https://3-dot-foo.your_app_id.appspot.com
        

      Limitations

      1. Backends do not live forever and may be shutdown unexpectedly and without notice
      2. The number of backends your application can have is currently limited

      这篇关于如何在App Engine上实现服务器关联或粘性会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆