基于shenyu 2.7.0.2的python客户端
Project description
import logging
import os
from contextlib import asynccontextmanager
import uvicorn
from fastapi import FastAPI
from shenyu_register.config import SHENYU_CONFIG
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
HTTP_PORT = int(os.getenv("PORT", os.getenv("UVICORN_PORT", "8000")))
# ShenYu:在代码中改配置,例如 enabled / server_lists / app_name 等
SHENYU_CONFIG.enabled = True
SHENYU_CONFIG.app_name = "python-demo"
SHENYU_CONFIG.context_path = "/python-demo"
SHENYU_CONFIG.server_lists = "http://127.0.0.1:9095"
SHENYU_CONFIG.heartbeat_interval_seconds = 10
SHENYU_CONFIG.admin_username = "admin"
SHENYU_CONFIG.admin_password = "123456"
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
- 启动阶段 ``register`` 若抛错:记录日志后向上抛出,**不会**执行 ``yield``,应用不完成启动;此时未成功注册,**不需要** ``shutdown``。
- 启动成功并 ``yield`` 之后:无论之后是正常停服还是其它原因结束 lifespan,都会在 ``finally`` 里调用 ``shutdown``(离线上报)。
- ``shutdown`` 内部若再抛错:在 ``finally`` 里捕获并记日志,**避免**掩盖其它清理逻辑或影响进程退出。
"""
try:
SHENYU_CONFIG.register(service_port=HTTP_PORT)
except Exception:
logger.exception("ShenYu 启动注册失败,应用将不会完成启动")
raise
try:
yield
finally:
try:
SHENYU_CONFIG.shutdown()
except Exception:
logger.exception("ShenYu shutdown(心跳停止 / 离线上报)失败,可在 Admin 侧检查或手工下线实例")
app = FastAPI(title="Shenyu Client Demo", version="0.1.0", lifespan=lifespan)
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI"}
@app.get("/health")
def health():
return {"status": "ok"}
if __name__ == "__main__":
uvicorn.run("example:app", host="0.0.0.0", port=HTTP_PORT, reload=False)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shenyu_client_python-1.0.0.tar.gz.
File metadata
- Download URL: shenyu_client_python-1.0.0.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d28d91eaa263183fcd9c5cc216aa1dfcc7ac7642714c09cd987b445074063af
|
|
| MD5 |
89a37a453d0b50fcbe918d25416f84cf
|
|
| BLAKE2b-256 |
e3686615cdd163d2a436ecc5ad8176902518ffbd44fc7d5c30009c5823ba1e7d
|
File details
Details for the file shenyu_client_python-1.0.0-py3-none-any.whl.
File metadata
- Download URL: shenyu_client_python-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c09c03b2bbb73b2b1e46a9200f6a2f3b4e2cd29cf63a8ca145352e5307ad18f
|
|
| MD5 |
91e818ef6b9a108662ca89b9a4c224fb
|
|
| BLAKE2b-256 |
5e714116dfd0ff87a849d4a7f1c10e839d4b165dd5a6be721c8f0ddaf393dae0
|