https://github.com/ifimust/srsrpy
Really Simple Service Registry - Python Client
https://github.com/ifimust/srsrpy
hatch microservices python
Last synced: 4 months ago
JSON representation
Really Simple Service Registry - Python Client
- Host: GitHub
- URL: https://github.com/ifimust/srsrpy
- Owner: ifIMust
- License: mit
- Created: 2024-08-06T18:57:25.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-12T17:29:17.000Z (almost 2 years ago)
- Last Synced: 2025-06-09T13:23:59.536Z (12 months ago)
- Topics: hatch, microservices, python
- Language: Python
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SRSR Python Client
Python client library for the [srsr service registry](https://github.com/ifIMust/srsr).
Include the client in your Python project to make your service discoverable through the service registry.
## Installation
```bash
# Base package
pip install srsrpy
# With Flask extension
pip install srsrpy[flask]
```
## Quick Start
The simplest way to use the client is with a context manager:
```python
from srsrpy import ServiceRegistryClient
def main():
with ServiceRegistryClient('my-service', service_address='http://localhost:3000'):
# Your service runs here - heartbeats are sent automatically
run_service()
# Automatically deregisters when done
if __name__ == "__main__":
main()
```
The client will:
- Register your service when entering the `with` block
- Send heartbeats automatically in the background
- Deregister your service when exiting (even if an error occurs)
You may provide the service registry address, or the client will try using `http://localhost:4214`.
```python
with ServiceRegistryClient(
'my-service',
registry_address='http://registry:4214',
service_address='http://localhost:3000'
):
run_service()
```
## Configure From Environment
Use the `from_env()` factory method to configure the client entirely through environment variables:
```bash
export SRSR_SERVICE_NAME=my-service
export SRSR_REGISTRY_URL=http://registry:4214 # Optional
export SRSR_SERVICE_ADDRESS=http://localhost:3000 # Optional
```
```python
from srsrpy import ServiceRegistryClient
def main():
with ServiceRegistryClient.from_env():
run_service()
if __name__ == "__main__":
main()
```
## Flask Integration
For Flask applications, install the Flask extension and use it to automatically register your app:
```bash
pip install srsrpy[flask]
```
**The Flask extension is configured exclusively via environment variables:**
```bash
export SRSR_SERVICE_NAME=my-flask-api
export SRSR_REGISTRY_URL=http://registry:4214 # Optional
export SRSR_SERVICE_ADDRESS=http://localhost:5000 # Optional
```
```python
from flask import Flask
from srsrpy.contrib.flask import FlaskServiceRegistry
app = Flask(__name__)
FlaskServiceRegistry(app) # Automatically registers on startup
@app.route('/')
def hello():
return 'Hello from registered service!'
if __name__ == '__main__':
app.run()
```
The extension also works with the application factory pattern:
```python
from flask import Flask
from srsrpy.contrib.flask import FlaskServiceRegistry
registry = FlaskServiceRegistry()
def create_app():
app = Flask(__name__)
registry.init_app(app)
return app
```
## Next Steps
- **[Configuration Guide](doc/CONFIGURATION.md)** - Detailed configuration options, port specifications, environment variables, and heartbeat settings
- **[Advanced Usage](doc/ADVANCED.md)** - Manual registration, signal handlers, error handling, and API reference
- **[Roadmap](doc/ROADMAP.md)** - Planned improvements and future enhancements
## Common Customizations
### Custom Heartbeat Interval
```python
with ServiceRegistryClient(
'my-service',
service_address='http://localhost:3000',
heartbeat_interval=10 # Send heartbeat every 10 seconds (default: 20)
):
run_service()
```
### Error Handling
```python
def on_heartbeat_error(error):
print(f"Heartbeat failed: {error}")
with ServiceRegistryClient(
'my-service',
service_address='http://localhost:3000',
heartbeat_error_handler=on_heartbeat_error
):
run_service()
```
See [ADVANCED.md](doc/ADVANCED.md) for more error handling patterns.