无法在AWS中使用粘性会话?

时间:2017-07-19 15:00:07

标签: amazon-web-services amazon-elb sticky-session

我在EC2上的tomcat实例中运行了一个Web应用程序,我不能在我的生活中获得粘性会话来处理负载均衡器。我已经按照http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-sticky-sessions.html中的所有步骤,尝试使用基于应用程序(使用JSESSIONID cookie)和基于时间(使用86400秒)的方法,但无论哪种方式,它都不起作用, 。同一个用户不断在所有不同的节点上反弹..每次他们点击网页上的新链接时,它都会将它们反弹回主屏幕再次登录。

我不知道它为什么不起作用。我也不太了解这些东西(哎呀,直到2个小时前我甚至不知道'粘性会话'是什么意思 - 不,我不能让其他人去做)所以我'我不确定从哪里开始调试它。

编辑:看看我的cookies ..似乎每次加载网站时,AWSELB cookie都会被删除并重写,而不是像它应该的那样持续存在。

编辑2:我的负载均衡器的XML配置:

{
    "LoadBalancerDescriptions": [
        {
            "Subnets": [
                "subnet-5c83aa39",
                "subnet-6a778830",
                "subnet-c41cdde8"
            ],
            "CanonicalHostedZoneNameID": "Z35SXDOTRQ7X7K",
            "CanonicalHostedZoneName": "<ELB_NAME>-<redacted>.us-east-1.elb.amazonaws.com",
            "ListenerDescriptions": [
                {
                    "Listener": {
                        "InstancePort": 5432,
                        "LoadBalancerPort": 5432,
                        "Protocol": "TCP",
                        "InstanceProtocol": "TCP"
                    },
                    "PolicyNames": []
                },
                {
                    "Listener": {
                        "InstancePort": 8888,
                        "LoadBalancerPort": 8888,
                        "Protocol": "HTTP",
                        "InstanceProtocol": "HTTP"
                    },
                    "PolicyNames": [
                        "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500995555135"
                    ]
                }
            ],
            "HealthCheck": {
                "HealthyThreshold": 2,
                "Interval": 10,
                "Target": "HTTP:44554/",
                "Timeout": 8,
                "UnhealthyThreshold": 4
            },
            "VPCId": "vpc-721bec0b",
            "BackendServerDescriptions": [],
            "Instances": [
                {
                    "InstanceId": "i-0ca9c244ed930d58f"
                },
                {
                    "InstanceId": "i-0cf47dc916f3f3443"
                },
                {
                    "InstanceId": "i-09ce2f24abc50259f"
                }
            ],
            "DNSName": "<ELB_NAME>-<redacted>.us-east-1.elb.amazonaws.com",
            "SecurityGroups": [
                "sg-f5ff638b"
            ],
            "Policies": {
                "LBCookieStickinessPolicies": [
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500564994754",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500565103581",
                        "CookieExpirationPeriod": 0
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500566463445",
                        "CookieExpirationPeriod": 28800
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500566440580",
                        "CookieExpirationPeriod": 0
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500476922828",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500481383343",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500479370743",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500475843862",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500995555135",
                        "CookieExpirationPeriod": 86400
                    }
                ],
                "AppCookieStickinessPolicies": [
                    {
                        "PolicyName": "AWSConsole-AppCookieStickinessPolicy-<ELB_NAME>-1500477157435",
                        "CookieName": "JSESSIONID"
                    },
                    {
                        "PolicyName": "AWSConsole-AppCookieStickinessPolicy-<ELB_NAME>-1500472642494",
                        "CookieName": "JSESSIONID"
                    },
                    {
                        "PolicyName": "AWSConsole-AppCookieStickinessPolicy-<ELB_NAME>-1500473344752",
                        "CookieName": "JSESSIONID"
                    },
                    {
                        "PolicyName": "AWSConsole-AppCookieStickinessPolicy-<ELB_NAME>-1500473227851",
                        "CookieName": "JSESSIONID"
                    }
                ],
                "OtherPolicies": []
            },
            "LoadBalancerName": "<ELB_NAME>",
            "CreatedTime": "2017-07-18T15:32:33.890Z",
            "AvailabilityZones": [
                "us-east-1a",
                "us-east-1b",
                "us-east-1c"
            ],
            "Scheme": "internet-facing",
            "SourceSecurityGroup": {
                "OwnerAlias": "558554867759",
                "GroupName": "<redacted>"
            }
        }
    ]
}

编辑3:任何人?请我绝望,我需要这个工作......

2 个答案:

答案 0 :(得分:0)

首先,这个粘性的东西是如何工作的! ELB只是第一次添加一个cookie AWSELB(或者可能是其他名称;你可以只查找所有的cookie,看看哪个不是你的)。此cookie还包含此请求应该服务的信息。

现在,来自同一浏览器的所有请求都将具有该cookie,而ELB知道转发的位置。这里没有什么是行不通的。

让我们来做:

  1. 你能给我发送你正在尝试的网站/ webapp吗?如果可以公开访问。因为即使我可以调试它。

  2. 确保允许使用Cookie;虽然看起来像你的问题,这不是一个问题。

  3. 您在编辑粘性上设置的有效期是多少!确保在ELB上 - &gt;点击Edit Stickiness - &gt;选择单选按钮&#34;启用负载均衡器生成的cookie粘性&#34;并且最重要的是将有效期留空。

  4. 如果您正在使用HTTPS,请对HTTPS重复步骤3.

  5. 让我知道它是否有效;我在这里只在下一个小时。

    不确定为什么你的XML显示了很多LBCookieStickinessPolicies !!

答案 1 :(得分:0)

您使用不同的配置创建了许多不同的策略,但只有一个与侦听器关联,而且一个是基于时间的策略。

"LoadBalancerDescriptions": [
    {
        "Subnets": [
            "subnet-5c83aa39",
            "subnet-6a778830",
            "subnet-c41cdde8"
        ],
        "CanonicalHostedZoneNameID": "Z35SXDOTRQ7X7K",
        "CanonicalHostedZoneName": "<ELB_NAME>-<redacted>.us-east-1.elb.amazonaws.com",
        "ListenerDescriptions": [
            {
                "Listener": {
                    "InstancePort": 5432,
                    "LoadBalancerPort": 5432,
                    "Protocol": "TCP",
                    "InstanceProtocol": "TCP"
                },
                "PolicyNames": []
            },
            {
                "Listener": {
                    "InstancePort": 8888,
                    "LoadBalancerPort": 8888,
                    "Protocol": "HTTP",
                    "InstanceProtocol": "HTTP"
                },
                "PolicyNames": [
 here ---->         "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500995555135"
                ]
            }
        ],

该文档指出了如何将策略与侦听器关联起来:http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-sticky-sessions.html#enable-sticky-sessions-duration

尝试仅将一个使用JSESSIONID的AppCookieStickinessPolicies与监听器相关联