24.4. 将 JBoss EAP 配置为前端负载平衡器
您可以配置 JBoss EAP 和 undertow
子系统,充当前端负载平衡器,以代理到后端 JBoss EAP 服务器的请求。由于 Undertow 使用异步 IO,负责连接的 IO 线程是唯一与请求相关的线程。同样的线程也用于连接后端服务器。
您可以使用以下协议:
-
通过纯文本(
http)的
HTTP,支持 HTTP/1 和 HTTP/2(h2c
) -
通过安全连接(
https
)的 HTTP,支持 HTTP/1 和 HTTP/2(h2)
-
AJP(
ajp
)
您可以定义 静态负载平衡器 并在配置中指定后端主机,或者使用 mod_cluster 前端 动态更新主机。
有关将 Undertow 配置为使用 HTTP/2 的说明,请参阅配置 HTTP/2。
24.4.1. 使用 mod_cluster 将 Undertow 配置为负载平衡器
您可以使用内置的 mod_cluster 前端负载平衡器来负载平衡其他 JBoss EAP 实例。
此流程假定您在受管域中运行,并且已配置了以下配置:
将充当负载平衡器的 JBoss EAP 服务器。
此服务器使用
load-balancer
配置文件,它绑定到load-balancer-sockets
套接字绑定组。注意load-balancer
配置集已预先配置了套接字绑定 mod-cluster Undertow 过滤器,并在默认主机中引用过滤器以将此服务器用作前端负载平衡器。
两台 JBoss EAP 服务器,用作后端服务器。
-
这些服务器在集群中运行,并使用
ha
配置文件,它绑定到ha-sockets
套接字绑定组。
-
这些服务器在集群中运行,并使用
- 将负载平衡部署到后端服务器的 distributable 应用。
配置 mod_cluster Front-end Load Balancer
下列步骤对受管域中的服务器进行负载平衡,但可以调整它们以应用到一组单机服务器。务必更新管理 CLI 命令值以适合您的环境。
设置 mod_cluster 公告安全密钥。
添加广播安全密钥可让负载平衡器和服务器在发现期间进行身份验证。
使用以下管理 CLI 命令,设置 mod_cluster 公告安全密钥:
/profile=ha/subsystem=modcluster/proxy=default:write-attribute(name=advertise-security-key, value=mypassword)
更新 mod_cluster 负载平衡器的安全密钥。
使用以下管理 CLI 命令,为 mod_cluster 过滤器设置安全密钥:
/profile=load-balancer/subsystem=undertow/configuration=filter/mod-cluster=load-balancer:write-attribute(name=security-key,value=mypassword)
建议由 mod_cluster 使用的管理和公告套接字绑定仅公开给内部网络,而非公共 IP 地址。
负载平衡器 JBoss EAP 服务器现在可以对两个后端 JBoss EAP 服务器进行负载平衡。
多个 mod_cluster 配置
mod_cluster
子系统支持多个命名代理配置,允许使用反向代理在非默认 下注册服务器
。此外,这允许单个应用服务器节点注册到不同的代理服务器组。
以下示例将 ajp-listener
、server 和主机添加到 undertow
服务器。它还添加新的 mod_cluster
配置,以使用广播机制注册主机。
/socket-binding-group=standard-sockets/socket-binding=ajp-other:add(port=8010) /subsystem=undertow/server=other-server:add /subsystem=undertow/server=other-server/ajp-listener=ajp-other:add(socket-binding=ajp-other) /subsystem=undertow/server=other-server/host=other-host:add(default-web-module=root-other.war) /subsystem=undertow/server=other-server/host=other-host /location=other:add(handler=welcome-content) /subsystem=undertow/server=other-server/host=other-host:write-attribute(name=alias,value=[localhost])) /socket-binding-group=standard-sockets/socket-binding=modcluster-other:add(multicast-address=224.0.1.106,multicast-port=23364) /subsystem=modcluster/proxy=other:add(advertise-socket=modcluster-other,balancer=other-balancer,connector=ajp-other) reload
24.4.2. 在负载均衡器中启用等级的会话关联
您必须在负载平衡器中启用等级会话关联,才能在 distribut-web
子系统中具有多个排序的路由。有关 distributable-web
子系统和不同关联性选项的更多信息,请参阅 JBoss EAP 开发指南中 适用于分布式 Web 会话配置的 distribut-web 子系统。
分隔节点路由的默认分隔符为:
如果要使用其他值,您可以配置 affinity
资源的 分隔符
属性。
流程
为负载均衡器启用排序的会话关联:
/subsystem=undertow/configuration=filter/mod-cluster=load-balancer/affinity=ranked:add
可选:配置
关联性
资源的分隔符
属性:/subsystem=undertow/configuration=filter/mod-cluster=load-balancer/affinity=ranked:write-attribute(name=delimiter,value=':')
24.4.3. 将 Undertow 配置为静态负载平衡器
若要使用 Undertow 配置静态负载平衡器,您需要在 undertow
子系统中配置代理处理程序。要在 Undertow 中配置代理处理程序,您需要对将充当静态负载平衡器的 JBoss EAP 实例进行以下操作:
- 添加反向代理处理程序
- 定义每个远程主机的出站套接字绑定
- 将每个远程主机添加到反向代理处理程序
- 添加反向代理位置
下例演示了如何将 JBoss EAP 实例配置为静态负载平衡器。JBoss EAP 实例位于 lb.example.com
,并在另外两个服务器之间负载平衡:server 1.example.com
和 server2.example.com
。负载均衡器将反向代理到位置 /app
,并将使用 AJP 协议。
添加反向代理处理程序:
/subsystem=undertow/configuration=handler/reverse-proxy=my-handler:add
为每个远程主机定义出站套接字绑定:
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=remote-host1/:add(host=server1.example.com, port=8009) /socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=remote-host2/:add(host=server2.example.com, port=8009)
将每个远程主机添加到反向代理处理程序:
/subsystem=undertow/configuration=handler/reverse-proxy=my-handler/host=host1:add(outbound-socket-binding=remote-host1, scheme=ajp, instance-id=myroute1, path=/test) /subsystem=undertow/configuration=handler/reverse-proxy=my-handler/host=host2:add(outbound-socket-binding=remote-host2, scheme=ajp, instance-id=myroute2, path=/test)
添加反向代理位置:
/subsystem=undertow/server=default-server/host=default-host/location=\/test:add(handler=my-handler)
访问 lb.example.com:8080/app
时,您现在会看到从 server1.example.com 和
进行代理的内容。
server2.
example.com