Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deepmancer/rabbitmq-rpc
RabbitMQ RPC is an async, ready-to-use Python framework for event-driven microservices.
https://github.com/deepmancer/rabbitmq-rpc
event-driven event-sourcing eventbus events message-broker message-bus message-queue microservice microservices-architecture python rabbitmq rabbitmq-client rpc-client rpc-framework
Last synced: 3 months ago
JSON representation
RabbitMQ RPC is an async, ready-to-use Python framework for event-driven microservices.
- Host: GitHub
- URL: https://github.com/deepmancer/rabbitmq-rpc
- Owner: deepmancer
- License: apache-2.0
- Created: 2024-07-15T23:22:27.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-08-16T11:43:47.000Z (6 months ago)
- Last Synced: 2024-10-11T20:02:08.795Z (4 months ago)
- Topics: event-driven, event-sourcing, eventbus, events, message-broker, message-bus, message-queue, microservice, microservices-architecture, python, rabbitmq, rabbitmq-client, rpc-client, rpc-framework
- Language: Python
- Homepage:
- Size: 62.5 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ππ‘ RabbitMQ RPC Client
![]()
![]()
![]()
`rabbitmq_rpc` is a powerful Python package that simplifies the implementation of RPC (Remote Procedure Call) patterns in event-driven microservices. Built on top of the `aio-pika` library, it abstracts the complexities of asynchronous communication with RabbitMQ, providing a seamless and efficient experience for developers.
---
## β¨ Features
- **π Asynchronous RPC Client:** Fully built on `aio-pika`, enabling non-blocking inter-service communication.
- **π Distributed Environment Ready:** Effortlessly connects services across containers and different URLs.
- **π Event Registration & Handling:** Easily define, register, and handle events with custom event handlers.
- **π οΈ Customizable RPC Protocol:** Supports any subclass of `aio_pika.patterns.RPC` for tailored RPC interactions.
- **π Thread-Safe Connection:** Utilizes a singleton design pattern to maintain a single instance of the RPC client across threads.
- **β±οΈ Retry & Timeout Mechanism:** Built-in support for retrying failed calls and handling timeouts with `with_retry_and_timeout`.
- **π οΈ No Server-side Implementation Required:** Just a running RabbitMQ serverβno need for additional RPC server implementations.## π¦ Installation
Get started by installing `rabbitmq_rpc` using pip:
```sh
pip install git+https://github.com/deepmancer/rabbitmq-rpc.git
```## π οΈ Quick Start
### π― Registering Events
In `Service 1`, define event handlers and register them with the `RPCClient` using the `register_event` method:
```python
import asyncio
from rabbitmq_rpc import RPCClientasync def handler_addition(x, y):
return x + yasync def handle_subtraction(x, y):
return x - yasync def handle_multiplication(x, y):
return x * yasync def handle_division(x, y):
return x / yasync def main():
# Initialize RPC client
rpc_client = await RPCClient.create(
host='localhost',
port=5920,
user='rabbitmq_user',
password='rabbitmq_password',
vhost='/',
ssl=False,
)# Register event handlers
await rpc_client.register_event('service1.addition', handler_addition)
await rpc_client.register_event('service1.subtraction', handle_subtraction)
await rpc_client.register_event('service1.multiplication', handle_multiplication)
await rpc_client.register_event('service1.division', handle_division)
# Keep listening for events
await asyncio.Future()if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```### π Calling Events
In `Service 2`, invoke the events defined in `Service 1`:
```python
import asyncio
from rabbitmq_rpc import RPCClientasync def main():
# Initialize RPC client
rpc_client = await RPCClient.create(
host='localhost',
port=5920,
user='rabbitmq_user',
password='rabbitmq_password',
vhost='/',
ssl=False,
)
# Call service1 events
add_result = await rpc_client.call('service1.addition', data={"x": 1, "y": 2})
print(f"Addition Result: {add_result}")sub_result = await rpc_client.call('service1.subtraction', data={"x": 1, "y": 2})
print(f"Subtraction Result: {sub_result}")mul_result = await rpc_client.call('service1.multiplication', data={"x": 1, "y": 2})
print(f"Multiplication Result: {mul_result}")# Call with timeout and retry mechanism
div_result = await rpc_client.call('service1.division', data={"x": 5, "y": 2}, timeout=10, retry_count=3)
print(f"Division Result: {div_result}")# Send event without waiting for a response
rpc_client.send('service1.multiplication', data={"x": 1, "y": 2})if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
```### π‘οΈ Error Handling
`rabbitmq_rpc` provides custom exceptions to handle various connection and RPC-related issues:
- `ConnectionError`
- `RPCError`
- `EventRegistrationError`
- `EventPublishError`
- `EventSubscribeError`### π Disconnecting
Gracefully disconnect from RabbitMQ when you're done:
```python
await rpc_client.close()
```## π License
This project is licensed under the Apache License 2.0. For more details, see the [LICENSE](https://github.com/deepmancer/rabbitmq-rpc/blob/main/LICENSE) file.
---
**Elevate your microservices communication with `rabbitmq_rpc` today!** ππ‘