HAProxy是可提供高可用性、负载均衡以及基于TCP和HTTP应用的代理.
1、更新系统报
通过在终端中运行以下命令,确保所有系统包都是最新的
sudo apt update
sudo apt upgrade

2、安装Haproxy
sudo apt install haproxy

设置开机自动启动haproxy服务
sudo systemctl enable haproxy
sudo systemctl start haproxy
sudo systemctl status haproxy

CGroup: /system.slice/haproxy.service
├─755 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-m>
版本查看
haproxy -v

3、下载两个tomcat模拟业务
Apache Tomcat访问地址:
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.91/bin/apache-tomcat-8.5.91.tar.gz
分别配置和启动两个Tomcat:
Tomcat1的http端口号18080
Tomcat2的http端口号18081
4、配置Haproxy
sudo nano /etc/haproxy/haproxy.cfg
在末尾添加
frontend tomcat_server1
bind *:80
default_backend tomcat_backend_server1
option forwardfor
backend tomcat_backend_server1
balance roundrobin
server backend01 127.0.0.1:18080 check
server backend02 127.0.0.1:18081 check

按Ctrl+X快捷键退出,选择Y,保存,重启haproxy服务
sudo systemctl restart haproxy
5、验证效果

