Event broker built on top of kafka and rabbitmq; used to handle micro services message exchange.
Project description
# Franz
A lite wrapper around [Kafka](https://kafka.apache.org/) and [RabbitMQ](https://www.rabbitmq.com/).
# Usage
## Installation
- `pip install franz` (recommended)
- `pip -e git+git@github.com:eshares/franz.git@master#egg=franz`
- Change `@master` to a version or commit if required.
## RabbitMQ
### Sending a message
```python
import random
import time
import franz
class FranzData(franz.FranzEvent):
def serialize(self):
return {'data': time.time()}
with franz.RabbitProducer(exchange='topic_link') as p:
while True:
key = random.choice(['hello.world', 'hello.bob'])
p.send_message(key, FranzData())
time.sleep(1)
```
### Consuming messages
```python
import franz
def callback(ch, method, properties, body):
print('[+] {} from {}'.format(body, method.routing_key))
with franz.RabbitConsumer('hello.*', exchange='topic_link') as c:
c.consume_messages(callback)
```
## Kafka
### Sending a message
```python
import franz
from myapp.models import SomeModel # SomeModel must inherit `franz.FranzEvent`
instance = SomeModel.objects.get(pk=1)
producer = franz.KafkaProducer()
producer.send_message('TopicA', instance)
```
### Consuming messages
```python
import franz
consumer = franz.KafkaConsumer('TopicA')
for message in consumer:
print(message.value)
```
### Kafka/Docker Resources
- [Docker image](https://github.com/spotify/docker-kafka)
- [Helpful article](https://howtoprogram.xyz/2016/07/21/using-apache-kafka-docker/)
- Create topic
```
./kafka-topics.sh --create --topic test --replication-factor 1 --partitions 1 --zookeeper 0.0.0.0:2181
```
- Consuming
```
./kafka-console-consumer.sh --topic test --from-beginning --zookeeper 0.0.0.0:2181
```
- Producing
```
./kafka-console-producer.sh --topic test --broker-list 0.0.0.0:9092
```
# Testing
```bash
# setup versions
pyenv install 3.5.3
pyenv install 3.6.3
pyenv install 3.7.0
pyenv virtualenv franz 3.5.3
pyenv local franz 3.5.3 3.6.3 3.7.0
pip install -r requirements-dev.txt
# run tests (python 3.5, 3.6, 3.7)
tox
```
A lite wrapper around [Kafka](https://kafka.apache.org/) and [RabbitMQ](https://www.rabbitmq.com/).
# Usage
## Installation
- `pip install franz` (recommended)
- `pip -e git+git@github.com:eshares/franz.git@master#egg=franz`
- Change `@master` to a version or commit if required.
## RabbitMQ
### Sending a message
```python
import random
import time
import franz
class FranzData(franz.FranzEvent):
def serialize(self):
return {'data': time.time()}
with franz.RabbitProducer(exchange='topic_link') as p:
while True:
key = random.choice(['hello.world', 'hello.bob'])
p.send_message(key, FranzData())
time.sleep(1)
```
### Consuming messages
```python
import franz
def callback(ch, method, properties, body):
print('[+] {} from {}'.format(body, method.routing_key))
with franz.RabbitConsumer('hello.*', exchange='topic_link') as c:
c.consume_messages(callback)
```
## Kafka
### Sending a message
```python
import franz
from myapp.models import SomeModel # SomeModel must inherit `franz.FranzEvent`
instance = SomeModel.objects.get(pk=1)
producer = franz.KafkaProducer()
producer.send_message('TopicA', instance)
```
### Consuming messages
```python
import franz
consumer = franz.KafkaConsumer('TopicA')
for message in consumer:
print(message.value)
```
### Kafka/Docker Resources
- [Docker image](https://github.com/spotify/docker-kafka)
- [Helpful article](https://howtoprogram.xyz/2016/07/21/using-apache-kafka-docker/)
- Create topic
```
./kafka-topics.sh --create --topic test --replication-factor 1 --partitions 1 --zookeeper 0.0.0.0:2181
```
- Consuming
```
./kafka-console-consumer.sh --topic test --from-beginning --zookeeper 0.0.0.0:2181
```
- Producing
```
./kafka-console-producer.sh --topic test --broker-list 0.0.0.0:9092
```
# Testing
```bash
# setup versions
pyenv install 3.5.3
pyenv install 3.6.3
pyenv install 3.7.0
pyenv virtualenv franz 3.5.3
pyenv local franz 3.5.3 3.6.3 3.7.0
pip install -r requirements-dev.txt
# run tests (python 3.5, 3.6, 3.7)
tox
```
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
franz-0.0.11.tar.gz
(6.5 kB
view details)
File details
Details for the file franz-0.0.11.tar.gz
.
File metadata
- Download URL: franz-0.0.11.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5620600b411103b0c79526336b8eaef773b5eb1d8e9e0fb4bdc01ebd6bf9c2a7 |
|
MD5 | 33bf0648155c8654a1b58cbd0d96b911 |
|
BLAKE2b-256 | aad1ab06bf664b057f112579ca80c683936785a76870d378033ae5a0f2bfbace |