|
| 1 | +# Kafka Docker Setup for Evolution API |
| 2 | + |
| 3 | +This directory contains the Docker Compose configuration for running Apache Kafka locally for development and testing with Evolution API. |
| 4 | + |
| 5 | +## Services |
| 6 | + |
| 7 | +### Zookeeper |
| 8 | +- **Container**: `zookeeper` |
| 9 | +- **Image**: `confluentinc/cp-zookeeper:7.5.0` |
| 10 | +- **Port**: `2181` |
| 11 | +- **Purpose**: Coordination service for Kafka cluster |
| 12 | + |
| 13 | +### Kafka |
| 14 | +- **Container**: `kafka` |
| 15 | +- **Image**: `confluentinc/cp-kafka:7.5.0` |
| 16 | +- **Ports**: |
| 17 | + - `9092` - PLAINTEXT_HOST (localhost access) |
| 18 | + - `29092` - PLAINTEXT (internal container access) |
| 19 | + - `9094` - OUTSIDE (external Docker access) |
| 20 | +- **Purpose**: Message broker for event streaming |
| 21 | + |
| 22 | +## Quick Start |
| 23 | + |
| 24 | +### 1. Start Kafka Services |
| 25 | +```bash |
| 26 | +cd Docker/kafka |
| 27 | +docker-compose up -d |
| 28 | +``` |
| 29 | + |
| 30 | +### 2. Verify Services |
| 31 | +```bash |
| 32 | +# Check if containers are running |
| 33 | +docker-compose ps |
| 34 | + |
| 35 | +# Check Kafka logs |
| 36 | +docker-compose logs kafka |
| 37 | + |
| 38 | +# Check Zookeeper logs |
| 39 | +docker-compose logs zookeeper |
| 40 | +``` |
| 41 | + |
| 42 | +### 3. Test Kafka Connection |
| 43 | +```bash |
| 44 | +# Create a test topic |
| 45 | +docker exec kafka kafka-topics --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1 |
| 46 | + |
| 47 | +# List topics |
| 48 | +docker exec kafka kafka-topics --list --bootstrap-server localhost:9092 |
| 49 | + |
| 50 | +# Produce messages |
| 51 | +docker exec -it kafka kafka-console-producer --topic test-topic --bootstrap-server localhost:9092 |
| 52 | + |
| 53 | +# Consume messages (in another terminal) |
| 54 | +docker exec -it kafka kafka-console-consumer --topic test-topic --from-beginning --bootstrap-server localhost:9092 |
| 55 | +``` |
| 56 | + |
| 57 | +## Evolution API Integration |
| 58 | + |
| 59 | +### Environment Variables |
| 60 | +Configure these variables in your Evolution API `.env` file: |
| 61 | + |
| 62 | +```bash |
| 63 | +# Kafka Configuration |
| 64 | +KAFKA_ENABLED=true |
| 65 | +KAFKA_CLIENT_ID=evolution-api |
| 66 | +KAFKA_BROKERS=localhost:9092 |
| 67 | +KAFKA_GLOBAL_ENABLED=true |
| 68 | +KAFKA_CONSUMER_GROUP_ID=evolution-api-consumers |
| 69 | +KAFKA_TOPIC_PREFIX=evolution |
| 70 | +KAFKA_AUTO_CREATE_TOPICS=true |
| 71 | + |
| 72 | +# Event Configuration |
| 73 | +KAFKA_EVENTS_APPLICATION_STARTUP=true |
| 74 | +KAFKA_EVENTS_INSTANCE_CREATE=true |
| 75 | +KAFKA_EVENTS_MESSAGES_UPSERT=true |
| 76 | +# ... other events as needed |
| 77 | +``` |
| 78 | + |
| 79 | +### Connection Endpoints |
| 80 | +- **From Evolution API**: `localhost:9092` |
| 81 | +- **From other Docker containers**: `kafka:29092` |
| 82 | +- **From external applications**: `host.docker.internal:9094` |
| 83 | + |
| 84 | +## Data Persistence |
| 85 | + |
| 86 | +Data is persisted in Docker volumes: |
| 87 | +- `zookeeper_data`: Zookeeper data and logs |
| 88 | +- `kafka_data`: Kafka topic data and logs |
| 89 | + |
| 90 | +## Network |
| 91 | + |
| 92 | +Services run on the `evolution-net` network, allowing integration with other Evolution API services. |
| 93 | + |
| 94 | +## Stopping Services |
| 95 | + |
| 96 | +```bash |
| 97 | +# Stop services |
| 98 | +docker-compose down |
| 99 | + |
| 100 | +# Stop and remove volumes (WARNING: This will delete all data) |
| 101 | +docker-compose down -v |
| 102 | +``` |
| 103 | + |
| 104 | +## Troubleshooting |
| 105 | + |
| 106 | +### Connection Issues |
| 107 | +1. Ensure ports 2181, 9092, 29092, and 9094 are not in use |
| 108 | +2. Check if Docker network `evolution-net` exists |
| 109 | +3. Verify firewall settings allow connections to these ports |
| 110 | + |
| 111 | +### Performance Tuning |
| 112 | +The configuration includes production-ready settings: |
| 113 | +- Log retention: 7 days (168 hours) |
| 114 | +- Compression: gzip |
| 115 | +- Auto-topic creation enabled |
| 116 | +- Optimized segment and retention settings |
| 117 | + |
| 118 | +### Logs |
| 119 | +```bash |
| 120 | +# View all logs |
| 121 | +docker-compose logs |
| 122 | + |
| 123 | +# Follow logs in real-time |
| 124 | +docker-compose logs -f |
| 125 | + |
| 126 | +# View specific service logs |
| 127 | +docker-compose logs kafka |
| 128 | +docker-compose logs zookeeper |
| 129 | +``` |
| 130 | + |
| 131 | +## Integration with Evolution API |
| 132 | + |
| 133 | +Once Kafka is running, Evolution API will automatically: |
| 134 | +1. Connect to Kafka on startup (if `KAFKA_ENABLED=true`) |
| 135 | +2. Create topics based on `KAFKA_TOPIC_PREFIX` |
| 136 | +3. Start producing events to configured topics |
| 137 | +4. Handle consumer groups for reliable message processing |
| 138 | + |
| 139 | +For more details on Kafka integration, see the main Evolution API documentation. |
0 commit comments