####安装#####
sudo apt update
sudo apt install haproxy
sudo haproxy -v
sudo systemctl status haproxy
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg-org
####配置站点#####
vim /etc/haproxy/haproxy.cfg
==========================================
frontend www-http
bind *:80
mode http
default_backend myweb1
backend myweb1
mode http
balance roundrobin
server web1 127.0.0.1:5001
server web2 127.0.0.1:5002
=========================================
sudo systemctl restart haproxy
使用fastapi测试:
===============================================
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def index():
return {"message": "Hello World at 5001"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5001)
===============================================
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.get("/")
async def index():
return {"message": "Hello World at 5002"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5002)