Skip to main content

注册方式

1. 全路径注册(默认)

register_all_paths=True(默认)时,向 Admin 注册 context_path/**,网关代理整个上下文下所有 HTTP 路径。

2. 按接口注册(注解)

将 register_all_paths 设为 False,在需要暴露的接口上使用 @register_metadata(别名 @shenyu_client),启动时由 SHENYU_CONFIG.register() 批量上报:

from shenyu_register.annotations import register_metadata

SHENYU_CONFIG.register_all_paths = False

@register_metadata("/health", path_desc="健康检查")
@app.get("/health")
def health():
    return {"status": "ok"}

装饰器写在路由装饰器内侧(更靠近 def),与 Flask 用法一致。path 可为相对 context_path 的路径(如 /health),注册时会拼成 /python-demo/health。

也可用代码声明:SHENYU_CONFIG.add_endpoint("/health", path_desc="健康检查")。


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)

Metadata

Release files for shenyu-client-python 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shenyu-client-python 1.0.1
File Size Uploaded Provenance Actions
shenyu_client_python-1.0.1.tar.gz 11.6 kB Without provenance Details for shenyu_client_python-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shenyu-client-python 1.0.1
File Interpreter ABI Platform Actions
shenyu_client_python-1.0.1-py3-none-any.whl Python 3 none any Details for shenyu_client_python-1.0.1-py3-none-any.whl

Total release size: 27.0 kB

Release files / shenyu_client_python-1.0.1.tar.gz

Download URL shenyu_client_python-1.0.1.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3755ddfa6609c324888c9ea8596f0c8169887900716f89aa34b749e6840d8b18
BLAKE2b-256 checksum
How to use checksums
75aabcd737a5787a03aae53bf6977fc785acf29625ee19a970e95bee4bb333da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / shenyu_client_python-1.0.1-py3-none-any.whl

Download URL shenyu_client_python-1.0.1-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a646d9740ce0652dd7b5624d8dd7059202c1a5a3c9c47cb12e75c630c0ee9f4
BLAKE2b-256 checksum
How to use checksums
77f1be68fbd8ad833cb901375bd508415e3ee8539b03ce3e778cecbc8449cd27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for shenyu-client-python 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page