在URL中使用ID通过HAProxy进行负载平衡 [英] Using ID in URL for load balancing with HAProxy
问题描述
我知道可以根据url使连接变为粘性参数: https://serverfault.com/questions/495049/using-url-parameters-for-load-balance-with-haproxy?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
I know it is possible to make connections sticky based on url a parameter: 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
是否还可以根据网址路径中的ID进行操作?
Is it also possible to do it based on an ID in the url path?
如果我的网址是:/objects/:objectId
If my url is: /objects/:objectId
我可以以某种方式使用:objectId来使连接保持粘性吗?
Can I somehow use that :objectId to make the connection sticky?
编辑
我能够使用以下配置进行负载平衡,使请求在URL路径上保持粘性:
I was able to load balance making the request sticky on the url path using the configuration below:
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中的条目将被删除?
The problem now is that if one of the servers go down the stick-table is not updated. How can I make it so that if one of the servers is not reachable the entries in the stick-table that point to that server are deleted?
最终答案
好的,我能够弄清楚.下面的配置使请求停留在url路径上,并且HAProxy将每隔250ms向/health发送一次HTTP GET,如果它未返回200,它将认为服务器已关闭,并且将从该停留中删除所有条目,桌子.
Ok, I was able to figure that out. The configuration below makes the requests stick on the url path and HAProxy will make an HTTP GET to /health at every 250ms and if it doesn't returns 200 it will consider the server to be down and that will remove all entries from the stick-table.
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
推荐答案
使用此:
stick on path,word(2,/) if { path_beg /objects/ }
这篇关于在URL中使用ID通过HAProxy进行负载平衡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!