Explorar o código

fix: 修复端口导致的服务访问不了

天空之城 hai 3 semanas
pai
achega
e7d19e964a
Modificáronse 6 ficheiros con 29 adicións e 19 borrados
  1. 3 2
      .env.uat
  2. 1 1
      alien_contract/Dockerfile
  3. 1 1
      alien_contract/main.py
  4. 1 0
      alien_gateway/config.py
  5. 21 13
      alien_gateway/main.py
  6. 2 2
      alien_store/Dockerfile

+ 3 - 2
.env.uat

@@ -15,8 +15,9 @@ REDIS_PASSWORD=my_password_123
 #REDIS_SENTINEL_USERNAME=default
 REDIS_SENTINEL_PASSWORD=
 
-# 下游服务地址
-STORE_BASE_URL="http://127.0.0.1:8001"# alien_store 服务地址
+# 下游服务地址(容器部署时由 docker run -e 覆盖为容器名:容器端口)
+STORE_BASE_URL="http://127.0.0.1:8001"# alien_store 服务地址(本地开发用)
+CONTRACT_BASE_URL="http://127.0.0.1:8002"# alien_contract 服务地址(本地开发用)
 
 # 阿里云短信配置
 ALIYUN_SMS_SIGN_NAME_CONTRACT="爱丽恩严大连商务科技"

+ 1 - 1
alien_contract/Dockerfile

@@ -17,6 +17,6 @@ RUN poetry source add --priority=primary tsinghua https://pypi.tuna.tsinghua.edu
 
 COPY . .
 
-EXPOSE 8005
+EXPOSE 8002
 
 CMD ["uvicorn", "alien_contract.main:app", "--host", "0.0.0.0", "--port", "8002"]

+ 1 - 1
alien_contract/main.py

@@ -19,4 +19,4 @@ async def health():
 if __name__ == "__main__":
     import uvicorn
 
-    uvicorn.run(app, host="0.0.0.0", port=8005)
+    uvicorn.run(app, host="0.0.0.0", port=8002)

+ 1 - 0
alien_gateway/config.py

@@ -34,6 +34,7 @@ class Settings(BaseSettings):
     REDIS_CONNECT_TIMEOUT: float = float(os.getenv("REDIS_CONNECT_TIMEOUT", "1.0"))
     # 下游服务地址
     STORE_BASE_URL: str = os.getenv("STORE_BASE_URL")  # alien_store 服务地址
+    CONTRACT_BASE_URL: str = os.getenv("CONTRACT_BASE_URL")  # alien_contract 服务地址
 
     # 阿里云短信配置
     ALIYUN_SMS_SIGN_NAME_CONTRACT: str = os.getenv("ALIYUN_SMS_SIGN_NAME_CONTRACT")

+ 21 - 13
alien_gateway/main.py

@@ -88,18 +88,10 @@ def _clean_headers(headers):
     return {k: v for k, v in headers.items() if k.lower() not in HOP_BY_HOP_HEADERS}
 
 
-@app.api_route("/api/store/{full_path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
-async def proxy_to_store(full_path: str, request: Request):
-    """
-    简易网关:监听 33333 端口,将 /api/store/* 转发到 alien_store 服务。
-    """
-    target_url = f"{settings.STORE_BASE_URL}/api/store/{full_path}"
+async def _proxy(request: Request, target_url: str, service_tag: str) -> Response:
+    """通用反向代理:转发请求体、头部、查询参数并回写下游响应。"""
     client_ip = request.client.host if request.client else "-"
-
-    # 读取请求体
     body = await request.body()
-
-    # 过滤头部
     headers = _clean_headers(dict(request.headers))
 
     try:
@@ -112,10 +104,12 @@ async def proxy_to_store(full_path: str, request: Request):
                 params=request.query_params,
             )
     except Exception as exc:
-        logger.error("proxy to store failed ip=%s url=%s err=%s", client_ip, target_url, exc)
+        logger.error(
+            "proxy to %s failed ip=%s url=%s err=%s",
+            service_tag, client_ip, target_url, exc,
+        )
         raise HTTPException(status_code=HTTP_502_BAD_GATEWAY, detail="Upstream unavailable")
 
-    # 返回下游响应
     return Response(
         content=resp.content,
         status_code=resp.status_code,
@@ -123,6 +117,20 @@ async def proxy_to_store(full_path: str, request: Request):
         media_type=resp.headers.get("content-type"),
     )
 
+
+@app.api_route("/api/store/{full_path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
+async def proxy_to_store(full_path: str, request: Request):
+    """监听 43333 端口,转发 /api/store/* 到 alien_store 服务。"""
+    target_url = f"{settings.STORE_BASE_URL}/api/store/{full_path}"
+    return await _proxy(request, target_url, "store")
+
+
+@app.api_route("/api/contract/{full_path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"])
+async def proxy_to_contract(full_path: str, request: Request):
+    """转发 /api/contract/* 到 alien_contract 服务。"""
+    target_url = f"{settings.CONTRACT_BASE_URL}/api/contract/{full_path}"
+    return await _proxy(request, target_url, "contract")
+
 if __name__ == "__main__":
     import uvicorn
-    uvicorn.run(app, host="0.0.0.0", port=33333)
+    uvicorn.run(app, host="0.0.0.0", port=43333)

+ 2 - 2
alien_store/Dockerfile

@@ -17,6 +17,6 @@ RUN poetry source add --priority=primary tsinghua https://pypi.tuna.tsinghua.edu
 
 COPY . .
 
-EXPOSE 48001
+EXPOSE 8001
 
-CMD ["uvicorn", "alien_store.main:app", "--host", "0.0.0.0", "--port", "48001"]
+CMD ["uvicorn", "alien_store.main:app", "--host", "0.0.0.0", "--port", "8001"]