HAProxy 示例
HAProxy 是在 Linux 中运行的免费负载均衡器。它非常强大,支持对出厂功能进行监控。
有关其配置的信息,请参阅:
HAProxy 版本 2.0.28 - 配置手册,适用于 ThingWorx 9.4 及更早版本。
HAProxy 版本 2.7.9 - 配置手册,适用于 ThingWorx 9.5 及更新版本。
全局设置
全局设置可应用在前端和后端服务器上且通常可以忽略。
例如:
#--------------------------------------------------------------------- #
Global settings
#---------------------------------------------------------------------
global
# setup logging and force level to debug. Logs require rsyslog be setup.
chroot /var/lib/haproxy
log /dev/log local0 debug
# maximum number of connections allowed
maxconn 10000
# turn on stats unix socket
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
# user and group haproxy will run as
user haproxy
group haproxy
默认设置
这些设置适用于后端服务器的默认配置。
log
将使用在全局设置中配置的日志记录。
mode
用于确定请求的解析方式。在本示例中,我们将使用 http 处理。
option
例如:
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
default-server init-addr last,libc,none
log global
mode http
option httplog
option redispatch
option forwardfor
timeout connect 10s
timeout client 60s
timeout server 60s
timeout tunnel 1h
前端设置
此为前端,将检查所有应用程序。它所包含的路由规则可确定用于处理传入请求的后端服务器。
bind
指示将接收传入请求的端口。
capture
在 HAProxy http 请求日志记录中包括会话 ID cookie。
acl
根据所检查的条件创建变量。例如,-i -m beg <string> 将检查以 <string> 开头的 URI。
use_backend
根据变量的 true 或 false 值,路由到特定后端服务器组。
default
如果其不符合其他规则,则使用后端。
例如:
#---------------------------------------------------------------------
# FrontEnd Configuration
#---------------------------------------------------------------------
frontend ft_web
bind *:8080 name web
# log the session cookie if passed
capture cookie JSESSIONID= len 32
## path based routing to connection server
acl path_cxserver path -i -m beg /Thingworx/WS
acl path_tunnelserver path -i -m beg /Thingworx/WSTunnelServer
acl path_tunnelclient path -i -m beg /Thingworx/WSTunnelClient
use_backend cxserver if path_cxserver or path_tunnelserver or path_tunnelclient

# default traffic to platform
default_backend platform
ThingWorx Flow 的示例
#---------------------------------------------------------------------
# FrontEnd Configuration
#---------------------------------------------------------------------
frontend ft_web
bind *:8443 ssl crt /certs/haproxy.pem name sslweb
# log the session cookie if passed
capture cookie JSESSIONID= len 32
##path based routing to ThingWorx Flow
acl p_flow1 path -i -m beg /Thingworx/Composer/apps/flow
acl p_flow2 path -i -m beg /Thingworx/Flow
acl p_flow3 path -i -m beg /Thingworx/Triggers
acl p_flow4 path -i -m beg /Thingworx/Lookups
acl p_flow5 path -i -m beg /Thingworx/Oauths
acl p_flow6 path -i -m beg /Thingworx/Subsystems/EventProcessingSubsystem/Subscriptions
acl p_flow7 path -i -m beg /enterprise/v1/fetchconfig
use_backend flow if p_flow1 or p_flow2 or p_flow3 or p_flow4 or p_flow5 or p_flow6 or p_flow7
后端平台设置
后端是一组服务器,可处理从前端传递的请求以及任何特定配置。
balance
负载平衡算法。默认设置为循环,这将遍历下一个可用服务器以获取下一个可用的连接。
cookie
插入负载均衡器使用的 cookie,以将未来请求路由到同一服务器。
例如:
#---------------------------------------------------------------------
# BackEnd Platform Configuration
#---------------------------------------------------------------------
backend platform
balance roundrobin
# sticky sessions
cookie SERVER insert indirect nocache
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
# health check
option httpchk GET /Thingworx/health
# configure platform instances
server platform1 10.0.10.10:8080 check inter 1000 fastinter 1000 cookie twx1
server platform2 10.0.10.11:8080 check inter 1000 fastinter 1000 cookie twx2
后端 Connection Server 设置
示例:
#---------------------------------------------------------------------
# BackEnd Connection Server Configuration
#---------------------------------------------------------------------
backend cxserver
balance source
#hash-type consistent
option httpchk GET /
# configure connection server instances
server cxserver1 10.0.10.20:8080 check port 9009
server cxserver2 10.0.10.21:8080 check port 9009
正在监控
HAProxy 提供了可用来查看流量的监控用户界面。您可以配置传入端口和可选登录凭据。
#---------------------------------------------------------------------
#HAProxy Monitoring Config
#---------------------------------------------------------------------
listen stats
bind *:1936
mode http
option forwardfor
option httpclose
stats enable
stats uri /
stats refresh 5s
stats show-legends
stats realm Haproxy\ Statistics
完整示例
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# setup logging and force level to debug. Logs require rsyslog be setup.
chroot /var/lib/haproxy
log /dev/log local0 debug
# maximum number of connections allowed
maxconn 10000
# turn on stats unix socket
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
# user and group haproxy will run as
user haproxy
group haproxy
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
default-server init-addr last,libc,none
log global
mode http
option httplog
option redispatch
option forwardfor
timeout connect 10s
timeout client 60s
timeout server 60s
timeout tunnel 1h
#---------------------------------------------------------------------
# FrontEnd Configuration
#---------------------------------------------------------------------
frontend ft_web
bind *:8080 name web
# log the session cookie if passed
capture cookie JSESSIONID= len 32
## path based routing to connection server
acl path_cxserver path -i -m beg /Thingworx/WS
acl path_tunnelserver path -i -m beg /Thingworx/WSTunnelServer
acl path_tunnelclient path -i -m beg /Thingworx/WSTunnelClient
use_backend cxserver if path_cxserver or path_tunnelserver or path_tunnelclient
# default traffic to platform
default_backend platform
#---------------------------------------------------------------------
# BackEnd Platform Configuration
#---------------------------------------------------------------------
backend platform
balance roundrobin
# sticky sessions
cookie SERVER insert indirect nocache
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
# health check
option httpchk GET /Thingworx/health
# configure platform instances
server platform1 10.0.10.10:8080 check inter 1000 fastinter 1000" cookie twx1
server platform2 10.0.10.11:8080 check inter 1000 fastinter 1000" cookie twx2
#---------------------------------------------------------------------
# BackEnd Connection Server Configuration
#---------------------------------------------------------------------
backend cxserver
balance source
#hash-type consistent
option httpchk GET /
# configure connection server instances
server cxserver1 10.0.10.20:8080 check port 9009
server cxserver2 10.0.10.21:8080 check port 9009
#---------------------------------------------------------------------
#HAProxy Monitoring Config
#---------------------------------------------------------------------
listen stats
bind *:1936
mode http
option forwardfor
option httpclose
stats enable
stats uri /
stats refresh 5s
stats show-legends
stats realm Haproxy\ Statistics
这对您有帮助吗?