简体   繁体   English

在Elastic Beanstalk中维护会话

[英]Maintaining session in Elastic Beanstalk

As the number of instances changes from time to time in Amazon Elastic Beanstalk. 随着Amazon Elastic Beanstalk中实例数量的不断变化。 If an application is built in PHP how the session will be maintained among several instances? 如果应用程序是用PHP构建的,那么如何在多个实例之间维护会话? How can we make sure not to delete the Parent instance (The first instance when the app was start 我们如何确保不删除Parent实例(应用程序启动时的第一个实例)

This post on the AWS blog describes how to use DynamoDB to do session state handling using the AWS SDK for PHP. AWS博客上的这篇文章描述了如何使用DynamoDB使用AWS SDK for PHP进行会话状态处理。

http://aws.typepad.com/aws/2012/04/scalable-session-handling-in-php-using-amazon-dynamodb.html http://aws.typepad.com/aws/2012/04/scalable-session-handling-in-php-using-amazon-dynamodb.html

If an application is built in PHP how the session will be maintained among several instances? 如果应用程序是用PHP构建的,那么如何在多个实例之间维护会话?

No. Elastic Beanstalk will not maintain session among several instances. 不,Elastic Beanstalk不会在多个实例之间维护会话。 You must maintain session among several instances by yourself. 您必须自己在多个实例之间维护会话。 If you enable Elastic Load Balancing Session Stickiness, it will only make sure to forward your user to the same instance. 如果启用Elastic Load Balancing会话粘性,则只会确保将用户转发到同一实例。

How can we make sure not to delete the Parent instance (The first instance when the app was start 我们如何确保不删除Parent实例(应用程序启动时的第一个实例)

Elastic Beanstalk Auto Scaling is use Default Termination Policy which will terminate oldest instance first. Elastic Beanstalk Auto Scaling使用默认终止策略 ,它将首先终止最旧的实例。 You can modify Elastic Beanstalk Auto Scaling Termination Policy to avoid terminating oldest instance first. 您可以修改Elastic Beanstalk Auto Scaling终止策略,以避免首先终止最旧的实例。

From personal experience I would suggest moving sessions to the database (RDS|DynamoDB|SimpleDB) or cache server (AWS started supporting Redis recently). 根据个人经验,我建议将会话移动到数据库(RDS | DynamoDB | SimpleDB)或缓存服务器(AWS最近开始支持Redis)。 It would take a lot of headache when application starts to scale or get terminated by unsuccesful health check. 当应用程序开始扩展或通过不成功的健康检查终止时,将需要很多头痛。

Here is a tutorial for how to configure persistent sessions using DynamoDB and Tomcat. 这是一个如何使用DynamoDB和Tomcat配置持久会话的教程。 https://packageprogrammer.wordpress.com/2014/01/13/aws-elastic-beanstalk-and-dynamodb-session-manager-for-tomcat/ https://packageprogrammer.wordpress.com/2014/01/13/aws-elastic-beanstalk-and-dynamodb-session-manager-for-tomcat/

AWS Project: https://github.com/aws/aws-dynamodb-session-tomcat AWS Project: https//github.com/aws/aws-dynamodb-session-tomcat

Manage Tomcat Session State with DynamoDB: http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-tomcat-session-manager.html 使用DynamoDB管理Tomcat会话状态: http ://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-tomcat-session-manager.html

Key Excerpt from the links above. 关键摘录自上面的链接。 Note that you may have change version numbers since the tutorial uses Tomcat 7 and 1.0.1 of aws-dynamodb-session-tomcat 请注意,您可能有更改版本号,因为本教程使用了aws-dynamodb-session-tomcat的Tomcat 7和1.0.1

Configuration for Tomcat Tomcat的配置

Download AmazonDynamoDBSessionManagerForTomcat-2.0.4.jar from aws-dynamodb-session-tomcat releases the and copy to the lib directory of your Tomcat installation Edit context.xml to use the .jar with code: aws-dynamodb-session-tomcat下载AmazonDynamoDBSessionManagerForTomcat-2.0.4.jar释放并复制到Tomcat安装的lib目录编辑context.xml以使用带有代码的.jar:

<?xml version='1.0' encoding='utf-8'?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <Manager className="com.amazonaws.services.dynamodb.sessionmanager.DynamoDBSessionManager"
             createIfNotExist="true" />
</Context>

Configuration for Elastic Beanstalk Elastic Beanstalk的配置

Download AmazonDynamoDBSessionManagerForTomcat-2.0.4.jar from aws-dynamodb-session-tomcat releases the and copy into .ebextensions directory of your WAR Edit context.xml with the code below and copy into .ebextensions directory of your WAR In the .ebextensions directory (Remember it must be inside /WEB-INF directory, /web-app for grails applications), create a .config file to write the command to execute the replacement of context.xml and copy the jar inside Beanstalk instances. aws-dynamodb-session-tomcat下载AmazonDynamoDBSessionManagerForTomcat-2.0.4.jar,使用以下代码释放并复制到WAR Edit context.xml的.ebextensions目录中,并复制到WAR的.ebextensions目录中.ebextensions目录中(记住它必须在/ WEB-INF目录下,/ web-app用于grails应用程序),创建一个.config文件来编写命令来执行context.xml的替换并复制jarstalk实例中的jar。

container_commands:
  01-copy-aws-library:
    command: "cp WEB-INF/.ebextensions/AmazonDynamoDBSessionManagerForTomcat-2.0.4.jar.jar /usr/share/tomcat8/lib/"
  02-replace-tomcat-context:
    command: "cp WEB-INF/.ebextensions/context.xml /etc/tomcat8/context.xml"

Now, just upload and deploy the war with the changes. 现在,只需使用更改上传和部署战争。 Try login and go into AWS Console->Services->DynamoDB, look for a table with name Tomcat_SessionState and voilà! 尝试登录并进入AWS Console-> Services-> DynamoDB,查找名为Tomcat_SessionState和voilà的表! You have affinity and persistence! 你有亲和力和坚持!

Better option to use Redis on Elastic Cache in order to store session. 更好的选择在Elastic Cache上使用Redis来存储会话。 One t1.micro node available on free tier and maintainable by Amazon, so you can get stable, cheep and fast storage for sessions, shared between nodes in autoscalling environment. 一个t1.micro节点可在免费层上使用并可由亚马逊维护,因此您可以获得稳定,快速和快速的会话存储,在自动调节环境中的节点之间共享。 How to configure PHP for keeping sessions in Redis I guess you can find in internet. 如何配置PHP以保持Redis中的会话我想你可以在互联网上找到。

Option with DynamoDB can be expensive, because you will pay for each call to DynamoDB services, also you need take into account that AWS services exposed by HTTP(S) can throttle requests. 使用DynamoDB的选项可能很昂贵,因为您将为每次调用DynamoDB服务付费,同时您还需要考虑HTTP(S)公开的AWS服务可以限制请求。

I think the only way to manage this is to store your sessions in DynamoDB or similar. 我认为管理这个的唯一方法是将您的会话存储在DynamoDB或类似的中。 We had the exact same problem when we deployed a java based app on Tomcat in the Elastic Bean Environment. 当我们在Elastic Bean环境中在Tomcat上部署基于java的应用程序时,我们遇到了完全相同的问题。 When the load balancer used to scale down instances, any user attached to that instance would automatically log out. 当负载均衡器用于缩减实例时,连接到该实例的任何用户都将自动注销。 Moving sessions to DynamoDB solved this problem for us. 将会话移动到DynamoDB为我们解决了这个问题。

When using Elastic Beanstalk with a typical Java webapp, I think you will definitely want to enable session stickiness. 将Elastic Beanstalk与典型的Java webapp一起使用时,我认为您肯定希望启用会话粘性。 Otherwise each HTTP request from a user's browser could be routed to a different server. 否则,来自用户浏览器的每个HTTP请求都可以路由到不同的服务器。

To get around the issue of the user's session being destroyed when the server they are "stuck" to gets shut down you would need to look into Tomcat session replication. 为了解决当用户被“卡住”的服务器被关闭时用户会话被破坏的问题,你需要查看Tomcat会话复制。 This isn't something that Elastic Beanstalk comes with out of the box unfortunately, so in order to setup session replication you would have to create a custom Elastic Beanstalk AMI for your application to use. 不幸的是,这不是Elastic Beanstalk开箱即用的,因此为了设置会话复制,您必须为应用程序创建自定义Elastic Beanstalk AMI。 Also, you would have to use an implementation of Tomcat session replication that does not rely on multicast, since multicast isn't available on AWS, or any other cloud environment that I know of. 此外,您必须使用不依赖于多播的Tomcat会话复制的实现,因为在AWS或我知道的任何其他云环境中无法使用多播。 An example of an implementation that doesn't rely on multicast would be one that uses a database (such as Amazon RDS) or memcached server (such as Amazon Elastic Cache) to make the sessions available across multiple Tomcat instances. 不依赖于多播的实现的示例将是使用数据库(诸如Amazon RDS)或memcached服务器(诸如Amazon Elastic Cache)以使得会话跨多个Tomcat实例可用的实现。

Also note that the Elastic Beanstalk UI only allows you to enable load balancer-generated HTTP cookies. 另请注意,Elastic Beanstalk UI仅允许您启用负载均衡器生成的HTTP Cookie。 However after Elastic Beanstalk has created the load balancer, you can go into the EC2 console and modify the load balancer's settings to switch it to application-generated HTTP cookies, and then tell it to use the "JSESSIONID" cookie. 但是,在Elastic Beanstalk创建了负载均衡器之后,您可以进入EC2控制台并修改负载均衡器的设置,将其切换为应用程序生成的HTTP cookie,然后告诉它使用“JSESSIONID”cookie。

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

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