【问题标题】:Configuring Load Balancer to Route to different pages of instance?将负载均衡器配置为路由到实例的不同页面?
【发布时间】:2019-03-29 13:53:50
【问题描述】:
  • 我在实例 x 前面有一个 AWS 负载均衡器。
  • 实例 x 运行于 端口 3000。实例 x 有两个页面,即 x/abc 和 x/zyx。
  • 目前 x 的负载均衡器有两个监听器,即80 -> 30008080 -> 3000。然后 ping /

要求:我有两台服务器要与实例 x 通信。服务器 1 想向 x/abc 和服务器 2 发送 http 请求 想向 x/zyx 发送 http 请求。

如何配置 LB 以路由到特定页面,例如x/abc 和 x/zyx? 或者以不同的方式编写我的请求?

代码 1:服务器 1 想要向 x/abc 发出 http 请求

// url is the DNS of load balancer... this should go to x/abc(?)
request({
    url: "LoadBalancer-11122232.us-west-2.elb.amazonaws.com:80",
    method: "POST",
    json: true,  
    body: tweetJSON
}

代码 2:服务器 2 想要向 x/zyx 发出 http 请求

// url is the DNS of load balancer... this should go to x/abc 
// DO I EVEN NEED TWO DIFFERENT PORT LISTENERS(?)
    request({
        url: "LoadBalancer-11122232.us-west-2.elb.amazonaws.com:8080",
        method: "POST",
        json: true,  
        body: tweetJSON
    }

【问题讨论】:

  • 感觉好像还有更多,我听到了这个问题,但想知道为什么?服务器 1 和服务器 2 有什么区别?不同端点/路由的意义是什么?是什么阻止您在调用 url 中指定路由?你最终想要达到什么结果?
  • 好问题。仅供参考,这是我第一次使用 AWS LB 等。服务器 1 是推文流,服务器 2 是用户查询 UI,服务器 x 进行计算分析。因此,服务器 1 将查询发送到服务器 x(发送到 x/query-request),服务器 2 将推文流发送到服务器 x/stream-endpoint。所以服务器 x 是中间人(从一个服务器获取用户查询并从另一台服务器获取推文流),代码分为两条路线。但是我认为 ELB 管理两个节点服务器之间的流量......不是三路? - 所以设计错了? ...
  • 现在我正在考虑这样做:服务器 1 将查询传送到服务器 2,服务器 2 将流发送到服务器 x。一个定向流量......所以像 1 -> 2 -> x 而不是 1 -> x ->
  • 这取决于您和您的架构。你所做的并没有错。请参阅我的答案以获取解决方案,该解决方案向完整的 URL 发出请求,包括路由。

标签: node.js amazon-web-services httprequest amazon-elb


【解决方案1】:

您无需将负载均衡器配置为将请求路由到不同的端点,这更像是反向代理的工作,例如 Nginx

负载均衡器提供单个端点来调用,并将来自客户端的请求转发到多个相同服务器之一。它的目标是在许多服务器之间共享高负载。

在您的情况下,您仍然可以混合使用负载均衡器,但就路由而言,我建议您完整地处理 URL:

代码 1:服务器 1 想向 x/abc 发出 http 请求

// url is the DNS of load balancer plus the route (/abc)
request({
    url: "https://LoadBalancer-11122232.us-west-2.elb.amazonaws.com/abc",
    method: "POST",
    json: true,  
    body: tweetJSON
}

代码 2:服务器 2 想要向 x/zyx 发出 http 请求

// url is the DNS of load balancer plus the route (/zyx)
request({
    url: "https://LoadBalancer-11122232.us-west-2.elb.amazonaws.com/zyx",
    method: "POST",
    json: true,  
    body: tweetJSON
}

如果您需要阻止客户端访问后端 url,则需要某种形式的身份验证来识别服务器 2。

【讨论】:

    【解决方案2】:

    通常您使用负载平衡器来平衡两个节点服务器之间的流量。

    所以你有两个非常简单的选择。一种是不使用经典的 Amazon 负载均衡器 (ELB),您可以改用 ALB(应用程序负载均衡器)。

    如需设置,请关注these Amazon instructions

    特别注意最后一节

    在侦听器选项卡上,使用箭头查看规则 侦听器,然后选择添加规则。指定规则如下:

    对于 Target group name,选择您的第二个目标组 已创建。

    对于路径模式,指定用于基于路径的确切模式 路由(例如,/img/*)。

    欲了解更多信息,see Listener Rules.

    更便宜的选择可能是使用 Nginx 推出您自己的负载均衡器。您可以使用 Nginx 配置的 AMI 启动 EC2 实例。

    然后您将编辑您的 Nginx 配置,使其看起来像这样:

    # The Node.js application serving ABC
    
    upstream node_abc {
      server ip.address.node.instance1:3000;
    }
    
    # The Node.js application serving XYZ 
    upstream node_xyz {
      server ip.address.node.instance2:3000;
    }
    
    # Accept connections from clients
    server {
      listen 80;
      charset utf-8;
      location /abc {
          proxy_pass http://node_abc;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      }
      location /xyz {
        proxy_set_header X-Forwarded-Proto $scheme;
      }
    }
    

    尽管更好,而且我认为您真正想要的是使用 nginx 作为适当的负载平衡反向代理。为此,您将运行同一个 node.js 应用程序的两个副本,其中每个副本都可以响应路由 /abc 或 /xyz,并为页面提供服务。然后使用这样的配置:

    #Proper Load Balanced Nginx setup
    
    upstream node_server {
      server ip.address.node.instance1:3000;
      server ip.address.node.instance2:3000;
    }
    
    # Accept connections from clients
    
    server {
      listen 80;
      charset utf-8;
      location / {
          proxy_pass http://node_server;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      }
    

    如果您执行最后一项配置,那么您不必担心在您的页面上重写任何复杂的 url。

    您将受益于不同服务器上的两个独立节点实例。因此,如果您的一个节点实例出现故障,那么您的负载均衡器将使用另一个节点实例。 (向响应 200 OK 的 nodejs 应用添加 /_health 路由)

    您可以轻松地进行 A/B 测试和蓝绿部署,您只需在更新另一个实例之前使用新代码更新一个实例。

    Nginx 可以配置不同的负载均衡策略,默认是循环。如果您需要在用户开始会话后将其发送到同一节点实例,您还可以添加粘性会话。

    【讨论】:

      猜你喜欢
      • 2016-01-15
      • 2021-03-30
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      相关资源
      最近更新 更多