-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Software Architecture in Go: Extensibility - Part 3
Extract Kafka to start adding new Message Brokers, rename some types to make them more intentional.
- Loading branch information
1 parent
ca983d5
commit e553966
Showing
9 changed files
with
188 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
cmdinternal "github.com/MarioCarrion/todo-api/cmd/internal" | ||
"github.com/MarioCarrion/todo-api/internal" | ||
"github.com/MarioCarrion/todo-api/internal/envvar" | ||
"github.com/MarioCarrion/todo-api/internal/kafka" | ||
"github.com/MarioCarrion/todo-api/internal/service" | ||
) | ||
|
||
// KafkaMessageBroker represents Kafka as a Message Broker. | ||
type KafkaMessageBroker struct { | ||
producer *cmdinternal.KafkaProducer | ||
publisher service.TaskMessageBrokerPublisher | ||
} | ||
|
||
// NewMessageBroker initializes a new Kafka Broker. | ||
func NewMessageBroker(conf *envvar.Configuration) (MessageBrokerPublisher, error) { //nolint: ireturn | ||
producer, err := cmdinternal.NewKafkaProducer(conf) | ||
if err != nil { | ||
return nil, internal.WrapErrorf(err, internal.ErrorCodeUnknown, "internal.NewKafkaProducer") | ||
} | ||
|
||
return &KafkaMessageBroker{ | ||
producer: producer, | ||
publisher: kafka.NewTask(producer.Producer, producer.Topic), | ||
}, nil | ||
} | ||
|
||
// Publisher returns the Kafka broker. | ||
func (m *KafkaMessageBroker) Publisher() service.TaskMessageBrokerPublisher { //nolint: ireturn | ||
return m.publisher | ||
} | ||
|
||
// Close closes the broker. | ||
func (m *KafkaMessageBroker) Close() error { | ||
m.producer.Producer.Close() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
services: | ||
rest-server-common: | ||
build: | ||
context: . | ||
dockerfile: ./dockerfiles/rest-server.Dockerfile | ||
ports: | ||
- 9234:9234 | ||
command: rest-server -env /api/env.example | ||
environment: | ||
DATABASE_HOST: postgres | ||
ELASTICSEARCH_URL: http://elasticsearch:9200 | ||
JAEGER_ENDPOINT: http://jaeger:14268/api/traces | ||
MEMCACHED_HOST: memcached:11211 | ||
VAULT_ADDRESS: http://vault:8300 | ||
depends_on: | ||
postgres: | ||
condition: service_healthy | ||
vault: | ||
condition: service_started | ||
prometheus: | ||
condition: service_started | ||
jaeger: | ||
condition: service_started | ||
elasticsearch: | ||
condition: service_healthy | ||
memcached: | ||
condition: service_healthy | ||
elasticsearch-indexer-common: | ||
build: | ||
context: . | ||
command: elasticsearch-indexer -env /api/env.example | ||
environment: | ||
ELASTICSEARCH_URL: http://elasticsearch:9200 | ||
JAEGER_ENDPOINT: http://jaeger:14268/api/traces | ||
VAULT_ADDRESS: http://vault:8300 | ||
depends_on: | ||
elasticsearch: | ||
condition: service_healthy | ||
jaeger: | ||
condition: service_started | ||
vault: | ||
condition: service_started |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
services: | ||
rest-server-kafka: | ||
extends: | ||
file: compose.common.yml | ||
service: rest-server-common | ||
build: | ||
args: | ||
TAG: kafka | ||
environment: | ||
KAFKA_HOST: kafka:29092 | ||
KAFKA_TOPIC: tasks | ||
depends_on: | ||
kafka: | ||
condition: service_healthy | ||
elasticsearch-indexer-kafka: | ||
extends: | ||
file: compose.common.yml | ||
service: elasticsearch-indexer-common | ||
build: | ||
dockerfile: ./dockerfiles/elasticsearch-indexer-kafka.Dockerfile | ||
environment: | ||
KAFKA_HOST: kafka:29092 | ||
KAFKA_TOPIC: tasks | ||
depends_on: | ||
kafka: | ||
condition: service_healthy | ||
zookeeper: | ||
image: confluentinc/cp-zookeeper:7.6.2 | ||
environment: | ||
ZOOKEEPER_CLIENT_PORT: 2181 | ||
ZOOKEEPER_TICK_TIME: 2000 | ||
ports: | ||
- 2181:2181 | ||
healthcheck: | ||
test: ["CMD-SHELL", "nc -z localhost 2181 || exit 1"] | ||
interval: 20s | ||
timeout: 1s | ||
retries: 5 | ||
kafka: | ||
image: confluentinc/cp-kafka:7.6.2 | ||
depends_on: | ||
zookeeper: | ||
condition: service_healthy | ||
ports: | ||
- 9092:9092 | ||
environment: | ||
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092 | ||
KAFKA_BROKER_ID: 1 | ||
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT | ||
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT | ||
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 | ||
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 | ||
healthcheck: | ||
test: kafka-topics --bootstrap-server kafka:29092 --list | ||
interval: 30s | ||
timeout: 10s | ||
retries: 5 | ||
kafka_setup: | ||
image: confluentinc/cp-kafka:7.6.2 | ||
restart: no | ||
depends_on: | ||
kafka: | ||
condition: service_healthy | ||
entrypoint: ["/bin/sh", "-c"] | ||
command: | | ||
" | ||
# blocks until kafka is reachable | ||
kafka-topics --bootstrap-server kafka:29092 --list | ||
echo -e 'Creating kafka topics' | ||
kafka-topics --bootstrap-server kafka:29092 --create --if-not-exists --topic tasks --replication-factor 1 --partitions 1 | ||
echo -e 'Successfully created the following topics:' | ||
kafka-topics --bootstrap-server kafka:29092 --list | ||
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.