nacos-serving-python v0.1.3
Overview
nacos-serving-python focuses on seamless Service Registration & Discovery plus Config access for Python web apps and HTTP clients.
Core capabilities:
- Auto register Flask / Django / FastAPI in three ways:
a) CLI (module run)
b) Import-triggered
c) WSGI middleware injection - Built‑in service discovery for urllib / requests / httpx / aiohttp so you can call
http://<service-name>/pathdirectly.
Built atop the async Nacos v2 SDK (gRPC push + structured params).
Installation & Usage
# 1. install the library
pip install nacos-serving-python
# 2. cd to your project root and run
# 3. run the auto-registration CLI
python -m nacos.auto.registration --nacos-server 127.0.0.1:8848 --service-name demoservice app.py
Quick Start (Conceptual)
- Build a client config (server address, namespace, credentials)
- Create Config & Naming services (async)
- Optionally: auto register your web service (one of 3 methods)
- Use logical hostnames via adapters (urllib / requests / httpx / aiohttp)
- Graceful shutdown: deregister ephemeral instance
(Full code samples removed for brevity—see demo/ & Chinese README.)
Auto Registration (3 Methods)
| Method | How | Use Case | Intrusive |
|---|---|---|---|
| CLI | python -m nacos.auto.registration app.py ... |
Zero code change | None |
| Import | import nacos.auto.registration.enabled |
Simple & explicit | Low |
| WSGI Middleware | from nacos.auto.middleware.wsgi import inject_wsgi_middleware then inject_wsgi_middleware(app) |
Custom lifecycle / delayed | Low |
Example (Import Trigger – Flask)
import nacos.auto.registration.enabled
from flask import Flask
app = Flask(__name__)
@app.route("/health")
def health(): return "OK"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5001)
Example (WSGI Middleware – Flask)
from flask import Flask
from nacos.auto.middleware.wsgi import inject_wsgi_middleware
app = Flask(__name__)
inject_wsgi_middleware(app)
app.run()
Configuration File (nacos.yaml)
Placed at project root (nacos.yaml or application.yaml). See demo/*/nacos.yaml.
CLI Startup Parameters
Invoke:
python -m nacos.auto.registration app.py [options]
| Parameter | Example | Meaning |
|---|---|---|
| --nacos-server | 127.0.0.1:8848 | Nacos server address (host:port[,host:port...]) |
| --namespace | public | Namespace ID |
| --service-name | demo.api | Logical service name |
| --service-port | 8000 | Explicit service port (auto-detect if omitted) |
| --service-ip | 192.168.1.10 | Explicit bind IP (auto-detect if omitted) |
| --service-group | DEFAULT_GROUP | Service group |
| --service-cluster | default | Cluster name (topology segmentation) |
| --service-weight | 1.0 | Load balancing weight |
| --metadata key=val | version=1.0.0 | Repeatable: attach metadata entries |
| --register-on-startup | (flag) | Register immediately at startup |
| --register-on-request | (flag) | Lazy register on first request |
| --no-auto-register | (flag) | Disable auto registration (override defaults) |
| --retry-times | 3 | Registration retry count |
| --retry-interval | 2 | Retry interval seconds |
| --heartbeat-interval | 5 | Heartbeat interval seconds |
| --heartbeat-timeout | 5 | Heartbeat timeout seconds |
| --graceful-shutdown | (flag) | Enable graceful shutdown |
| --shutdown-timeout | 10 | Graceful shutdown max wait seconds |
| --deregister-on-exit | (flag) | Explicitly deregister on exit |
| --log-level | INFO | Logging level |
| --log-file | /path/file.log | Custom log file path |
| --empty-protection | true/false | Keep previous instance list when NACOS returns empty |
| --help | Show full help |
Environment variables may override core fields (examples):
| Env | Maps To |
|---|---|
| NACOS_SERVER | --nacos-server |
| NACOS_NAMESPACE | --namespace |
| NACOS_SERVICE_NAME | --service-name |
| NACOS_SERVICE_PORT | --service-port |
| NACOS_SERVICE_IP | --service-ip |
If both CLI and env present: CLI takes precedence.
Service Discovery (HTTP Clients)
Adapters allow logical hostname usage:
- urllib:
from nacos.auto.discovery.ext.urllib import urlopen - requests:
from nacos.auto.discovery.ext import requests as nacos_requests - httpx:
from nacos.auto.discovery.ext.httpx import AsyncClient - aiohttp:
from nacos.auto.discovery.ext.aiohttp import get_session
Fallback steps (manual):
- List healthy instances
- Pick one (LB strategy)
- Compose
http://ip:port/path
Load Balancing & Resilience:
- Strategy: round-robin or random (implementation)
- Filters: healthy + enabled
- Retry: next instance on failure
- Cache: subscription-driven updates
- Empty protection: optional guard against transient zero instance returns
Configuration Field Summary (nacos.yaml)
| Section | Key | Description |
|---|---|---|
| nacos | server | Nacos server address list |
| nacos | namespace | Namespace ID |
| service | name | Logical service name |
| service | ip / port | Explicit service endpoint (auto-detected if omitted) |
| service | group | Service group (default DEFAULT_GROUP) |
| service | cluster | Cluster name |
| service | weight | Load balancing weight |
| service | metadata | Arbitrary key-value routing tags |
| registration | auto_register | Enable auto registration driver |
| registration | register_on_startup | Register immediately at startup |
| registration | register_on_request | Lazy register on first request |
| registration | retry_times / retry_interval | Retry strategy |
| discovery | empty_protection | Keep last snapshot if server returns empty |
| heartbeat | interval / timeout | Ephemeral heartbeat config |
| shutdown | graceful / timeout / deregister | Graceful exit behavior |
| logging | level / file | Logging control |
Migration (Legacy SDK → This)
| Legacy | New | Change |
|---|---|---|
| Sync calls | Async await | Non-blocking |
| Flat params | Data classes | Structured |
| Polling | gRPC push | Lower latency |
| Custom discovery | Built-in adapters | Less boilerplate |
| Manual heartbeat | Managed ephemeral | Simplified lifecycle |
Best Practices
| Scenario | Recommendation |
|---|---|
| Env isolation | Use namespaces per env |
| Canary / gray | Route via metadata.version |
| Security | Enable TLS + external credential store |
| Fallback | Cache last good instance |
| Heavy callbacks | Offload to queues / thread pools |
| Observability | Track resolution latency & errors |
Troubleshooting
| Symptom | Cause | Action |
|---|---|---|
| No auto registration | Trigger not used | Use CLI/import/middleware |
| 404 logical hostname | Adapter not imported | Import adapter module |
| Empty instance list | All offline or perms | Check Nacos console & creds |
| Config not updating | Listener missing | Register listener early |
| Residual instance | No graceful shutdown | Enable deregister-on-exit |
Enable debug:
ClientConfigBuilder().log_level("DEBUG")
Versioning
0.x = rapid iteration (breaking possible).
Pin dependency: nacos-serving-python>=0.1.0,<1.0.0.
Contributing
Fork → Branch → Code / Tests → Docs → PR (with context & rollback plan).
Metadata
Release files for nacos-serving-python 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| nacos_serving_python-0.1.3.tar.gz | 49.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nacos_serving_python-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 117.5 kB
Release files / nacos_serving_python-0.1.3.tar.gz
| Download URL | nacos_serving_python-0.1.3.tar.gz |
|---|---|
| Size | 49.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
50b37415fc27247e403cd6e3af7d3e5434feccb2c898b49ec26e82e05f085cd3
|
|
BLAKE2b-256 checksum How to use checksums |
b7b0eeff6b892f6cc5d5fdc6117003f1394dd9cd1b46d45c4bc7e072e7c6eb54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / nacos_serving_python-0.1.3-py3-none-any.whl
| Download URL | nacos_serving_python-0.1.3-py3-none-any.whl |
|---|---|
| Size | 68.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ecb9a96e5d06cb837faa4ea77e2f5695fe78aa707f517882de50435b5f68156d
|
|
BLAKE2b-256 checksum How to use checksums |
ef901b9750374f3e748b678d007b01b4653d7020380db6b37683390cd01cfc79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for nacos-serving-python 0.1.3
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.