如何在FastAPI和Eureka上设置相同的端口?

0 投票
0 回答
33 浏览
提问于 2025-04-11 23:48

我想让uvicorn和eureka的配置使用同一个端口。我搜索了很多资料,只找到这个链接 https://github.com/encode/uvicorn/issues/761,但它并没有解决我的问题。这是我的代码:

import py_eureka_client.eureka_client as eureka_client
import uvicorn
from fastapi import FastAPI
from contextlib import asynccontextmanager
from com.controladores import controladorRouter


@asynccontextmanager
async def lifespan(app: FastAPI):
    await eureka_client.init_async(
        eureka_server="http://eureka-primary:8011/eureka/,http://eureka-secondary:8012/eureka/,http://eureka-tertiary:8013/eureka/",
        app_name="msprueba",
        instance_port=8000,
        instance_host="localhost"
    )
    yield

app = FastAPI(lifespan=lifespan)
app.include_router(controladorRouter)

if __name__ == "__main__":
    config = uvicorn.Config("com.main:app", host="localhost", port=0)
    server = uvicorn.Server(config)
    server.run()

我把链接里的代码去掉了,只是为了展示我实际的代码。我真的很难相信这么简单的任务在fastapi中会这么难。别误会,fastapi是个很棒的工具,只是我之前用的是springboot,这个任务在springboot中要简单得多,有谁能帮帮我吗?

0 个回答

暂无回答

撰写回答