Nacos 2.x async serving & config SDK with auto registration and discovery extensions
Project description
nacos-serving-python v0.1.0
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).
Project details
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 nacos_serving_python-0.1.0.tar.gz.
File metadata
- Download URL: nacos_serving_python-0.1.0.tar.gz
- Upload date:
- Size: 49.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b744c8ac2bde8bc730e6c727ae869ce3d16886e4ad3e688eafb008ebb4e235a8
|
|
| MD5 |
1685acbae289140bf0ef76a9ff445e85
|
|
| BLAKE2b-256 |
65ef460f649f6cc33874c44bdb63b25e67699006ac4c6261dd373349b15a666b
|
File details
Details for the file nacos_serving_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nacos_serving_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 68.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
169700c80b2f0a8dadbc524c1cb1c139de1b29a22e9623aecbd404e6c7a93762
|
|
| MD5 |
c89155316ffd1a6ba473119954b905cf
|
|
| BLAKE2b-256 |
06e2237b0a02a55f0566418f5cf94ae34aca30748b1420fcaff541265df07b8f
|