利用阿里云 LBS 做负载均衡

上一篇 / 下一篇  2017-06-21 11:12:27

年关将近,不少朋友开始做年会抽奖,几千人的抽奖节目会瞬间涌入大量请求,单台服务器无法胜任,需要多个服务器打配合战。基于阿里云提供的 LBS、ECS、RDS,我们有非常简单的方案。

武器库:

  • 对外域名 example.com
  • ECS1:公网 IP 200.114.5.36,内网 IP 192.1.1.100
  • ECS2:公网 IP 200.114.5.37,内网 IP 192.1.1.101
  • LBS:公网IP 222.1.1.1
  • RDS:账号信息 -h 192.1.1.102 -P 3306 -utest -ptest

步骤:

  1. example.com DNS 解析指向 LBS 222.1.1.1
  2. LBS 后台添加 ECS1 和 ECS2,对外端口 80,对内端口 80,加权轮询策略,会话保持3600秒
  3. ECS1 和 ECS2 中的应用数据库使用 RDS,应用 session 保存在 RDS。图片等存储最好使用 OSS,如果不是 OSS 少数场景可能存在少许问题,这里不展开。

搞定!负载均衡的效果如下:

[lbs@test~]# curl http://100.114.1.12/index.htmlReplyfrom184server
[lbs@test~]# curl http://100.114.1.12/index.htmlserver222OK[lbs@test~]# curl http://100.114.1.12/index.htmlReplyfrom184server
[lbs@test~]# curl http://100.114.1.12/index.htmlReplyfrom184server
[lbs@test~]# curl http://100.114.1.12/index.htmlReplyfrom184server
[lbs@test~]# curl http://100.114.1.12/index.htmlserver222OK[lbs@test~]# curl http://100.114.1.12/index.htmlserver222OK[lbs@test~]# curl http://100.114.1.12/index.htmlserver222OK[lbs@test~]# curl http://100.114.1.12/index.htmlReplyfrom184server
[lbs@test~]# curl http://100.114.1.12/index.htmlserver222OK
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

目前阿里云 LBS 内网负载均衡功能是免费的,用于做实验足够。如果希望对外服务,则需要付费购买 LBS 外网服务。本质就是给你分配一个外网 IP,走外网流量。

附:Session 同步问题

  • LBS 支持会话保持,但并不支持 session 同步。所谓会话保持,是指来自同一个 用户的请求会在一段时间内路由到同一个 ECS;session 同步是指一个用户的 session 数据保持在多个 ECS 中,无论用户请求哪个 ECS, session 数据都存在。

  • 阿里云 LBS 不支持 session 同步的原因: session 同步是应用层的事情,底层软件无法感知 session。默认情况下 php 将 session 数据保存在磁盘文件中,PHP 配置文件中 session.save_path 负责session文件的存放位置。

  • 应用层 session 同步方案:把 session 保存在数据库中,如 RDS 、Memcached、Redis 等。


TAG: 阿里云

 

评分:0

我来说两句

日历

« 2024-03-23  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 80150
  • 日志数: 94
  • 文件数: 1
  • 建立时间: 2017-04-14
  • 更新时间: 2020-11-17

RSS订阅

Open Toolbar