【问题标题】:Using ID in URL for load balancing with HAProxy使用 URL 中的 ID 与 HAProxy 进行负载平衡
【发布时间】:2018-11-11 21:45:22
【问题描述】:

我知道可以根据 url 参数使连接保持粘性: https://serverfault.com/questions/495049/using-url-parameters-for-load-balancing-with-haproxy?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

是否也可以根据url路径中的ID来做?

如果我的网址是:/objects/:objectId

我可以使用 :objectId 使连接保持粘性吗?

编辑

我能够使用以下配置进行负载平衡,使请求在 url 路径上保持粘性:

global
    #daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers

backend servers
    balance roundrobin
    stick-table type string size 200k expire 30m
    stick on path
    server server1 127.0.0.1:8000
    server server2 127.0.0.1:8001

listen stats
    bind 127.0.0.1:9000
    mode            http
    log             global

    maxconn 10

    stats enable
    stats hide-version
    stats refresh 5s
    stats show-node
    stats auth admin:password
    stats uri  /haproxy?stats

现在的问题是,如果其中一台服务器宕机,stick-table 不会更新。我怎样才能做到这一点,如果其中一台服务器无法访问,则指向该服务器的粘贴表中的条目将被删除?

最终答案

好的,我能够弄清楚这一点。下面的配置使请求停留在 url 路径上,HAProxy 将每 250 毫秒向 /health 发送一个 HTTP GET,如果它不返回 200,它将认为服务器已关闭,这将从棒中删除所有条目-表。

global
    daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers


backend servers
    balance roundrobin
    stick-table type string size 200k expire 30m
    option httpchk GET /health
    http-check expect status 200
    stick on path,word(2,/)  if { path_beg /objects/ }
    server server1 127.0.0.1:8000 check inter 250
    server server2 127.0.0.1:8001 check inter 250

listen stats
    bind 127.0.0.1:9000
    mode            http
    log             global

    maxconn 10

    stats enable
    stats hide-version
    stats refresh 5s
    stats show-node
    stats auth admin:password
    stats uri  /haproxy?stats

【问题讨论】:

    标签: load-balancing haproxy sticky-session


    【解决方案1】:

    使用这个:

    stick on path,word(2,/)  if { path_beg /objects/ }
    

    【讨论】:

    • 如果object_id 出现在不同路径的不同位置怎么办?喜欢 URL:/v1/tasks/:task_id/objects/:object_id
    • word(<index>,<delimiters>)
    猜你喜欢
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多