haproxy配置负载均衡

####安装#####
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)

posted @ 2024-12-14 15:21  河北大学-徐小波  阅读(3)  评论(0编辑  收藏  举报