Skip to content

Commit 02f6346

Browse files
authored
Merge pull request #75 from checkr/zz/update-docker-compose
Improve the experience of running kafka locally
2 parents 19f7275 + 73b9f8c commit 02f6346

4 files changed

Lines changed: 51 additions & 22 deletions

File tree

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,27 @@ omctl push --directory ./demo_templates --url http://localhost:9998
379379
kind: Behavior
380380
expect:
381381
kafka:
382-
topic: hello_kafka_in
382+
topic: hello_kafka_in_2
383383
actions:
384384
- publish_kafka:
385385
topic: hello_kafka_out
386386
payload_from_file: './files/colors.json' # the path is relative to OPENMOCK_TEMPLATES_DIR
387387
```
388388
389+
If you started the example from docker-compose, you can test the above kafka mocks by using a kt docker container.
390+
391+
```bash
392+
# Exec into the container
393+
docker-compose exec kt bash
394+
395+
# Run some kt commands inside the container
396+
# Notice that the container is within the docker-compose network, and it connects to "kafka:9092"
397+
398+
$ kt topic
399+
$ echo '{"123":"hi"}' | kt produce -topic hello_kafka_in -literal
400+
$ kt consume -topic hello_kafka_out -offsets all=newest:newest
401+
```
402+
389403
### Example: Mock AMQP (e.g. RabbitMQ)
390404
```yaml
391405
# demo_templates/amqp.yaml

docker-compose.yml

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
version: '3.4'
22
services:
33
zookeeper:
4-
image: confluentinc/cp-zookeeper:3.3.2
5-
container_name: openmock-zookeeper
4+
image: confluentinc/cp-zookeeper:4.1.1 # kafka 1.1.1
65
environment:
7-
ZOOKEEPER_CLIENT_PORT: 2181
8-
ZOOKEEPER_TICK_TIME: 2000
9-
ports:
10-
- "2181:2181"
11-
extra_hosts:
12-
- "moby:127.0.0.1"
6+
- ZOOKEEPER_CLIENT_PORT=2181
7+
- ZOOKEEPER_TICK_TIME=2000
8+
- KAFKA_HEAP_OPTS=-Xmx256m -Xms256m
9+
healthcheck:
10+
test: ["CMD-SHELL", "echo ruok | nc -w 2 localhost 2181"]
11+
interval: 10s
12+
timeout: 10s
13+
retries: 5
1314

1415
kafka:
15-
image: confluentinc/cp-kafka:3.3.2
16-
container_name: openmock-kafka
17-
links:
18-
- zookeeper
16+
image: confluentinc/cp-kafka:4.1.1 # kafka 1.1.1
1917
environment:
20-
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
21-
KAFKA_ADVERTISED_LISTENERS: "PLAINTEXT://localhost:9092"
22-
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
23-
KAFKA_BROKER_ID: 1
18+
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
19+
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092
20+
- KAFKA_BROKER_ID=1
21+
- KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
22+
- KAFKA_HEAP_OPTS=-Xmx512m -Xms512m
23+
depends_on:
24+
- zookeeper
2425
ports:
2526
- "9092:9092"
26-
extra_hosts:
27-
- "moby:127.0.0.1"
27+
healthcheck:
28+
test: ["CMD-SHELL", "kafka-topics --zookeeper zookeeper:2181 --list"]
29+
interval: 30s
30+
timeout: 10s
31+
retries: 5
32+
start_period: 10s
2833

2934
rabbitmq:
3035
image: rabbitmq:3.6.6-management
@@ -58,8 +63,18 @@ services:
5863
restart: "on-failure"
5964
environment:
6065
OPENMOCK_REDIS_TYPE: "redis"
66+
OPENMOCK_KAFKA_ENABLED: "true"
67+
OPENMOCK_KAFKA_SEED_BROKERS: "kafka:9092"
68+
depends_on:
69+
- kafka
6170
volumes:
6271
- ./demo_templates:/data/templates
6372
ports:
6473
- "9999:9999"
6574
- "9998:9998"
75+
76+
kt:
77+
image: evpavel/kt
78+
command: "sleep 1d"
79+
environment:
80+
KT_BROKERS: "kafka:9092"

kafka.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (om *OpenMock) startKafka() {
9494
if err := om.configKafka(); err != nil {
9595
logrus.WithFields(logrus.Fields{
9696
"err": err,
97-
}).Errorf("failed to config kafka")
97+
}).Fatal("failed to config kafka")
9898
return
9999
}
100100
for kafka, ms := range om.repo.KafkaMocks {
@@ -109,7 +109,7 @@ func (om *OpenMock) startKafka() {
109109
logrus.WithFields(logrus.Fields{
110110
"err": err,
111111
"topic": kafka.Topic,
112-
}).Errorf("failed to create a consumer")
112+
}).Fatal("failed to create a consumer")
113113
return
114114
}
115115
logrus.Infof("consumer started for topic:%s", kafka.Topic)

openmock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type OpenMock struct {
2323
AdminHTTPHost string `env:"OPENMOCK_ADMIN_HTTP_HOST" envDefault:"0.0.0.0"`
2424
KafkaEnabled bool `env:"OPENMOCK_KAFKA_ENABLED" envDefault:"false"`
2525
KafkaClientID string `env:"OPENMOCK_KAFKA_CLIENT_ID" envDefault:"openmock"`
26-
KafkaSeedBrokers []string `env:"OPENMOCK_KAFKA_SEED_BROKERS" envDefault:"kafka:9092,localhost:9092" envSeparator:","`
26+
KafkaSeedBrokers []string `env:"OPENMOCK_KAFKA_SEED_BROKERS" envDefault:"kafka:9092" envSeparator:","`
2727
AMQPEnabled bool `env:"OPENMOCK_AMQP_ENABLED" envDefault:"false"`
2828
AMQPURL string `env:"OPENMOCK_AMQP_URL" envDefault:"amqp://guest:guest@rabbitmq:5672"`
2929
RedisType string `env:"OPENMOCK_REDIS_TYPE" envDefault:"memory"`

0 commit comments

Comments
 (0)