In this repo we are demonstrating on how to use the Redis Pubsub or Kafka communication between application using the gogen framework that also apply the Clean Architecture
For the Gogen Framework Structure, you can refer to here link
The application consist of two parts
- Client : Has a restapi interface to invoke the Redis PubSub or Kafka publisher
- Server : Has a Redis PubSub or Kafka server to consume the request, process it and then return back to client (Redis PubSub or Kafka)
gogen_pubsub
├── application
│ ├── app_client.go
│ └── app_server.go
├── domain_demo
│ ├── controller
│ │ ├── kafkasubscriber
│ │ ├── redissubscriber
│ │ └── restapi
│ ├── gateway
│ │ ├── simpleprint
│ │ ├── kafkapublisher
│ │ └── redispublisher
│ └── usecase
│ ├── onmessagereceived
│ └── runmessagesend
├── main.go
└── shared
└── model
└── payload
└── data.go
-
After you git clone it, make sure to run the
go mod tidy
to download the dependency -
Run kafka cluster. In this case, we use docker-compose
--- version: '3' services: zookeeper: image: confluentinc/cp-zookeeper:7.3.2 container_name: zookeeper environment: ZOOKEEPER_CLIENT_PORT: 2181 ZOOKEEPER_TICK_TIME: 2000 broker: image: confluentinc/cp-kafka:7.3.2 container_name: broker ports: # To learn about configuring Kafka for access across networks see # https://www.confluent.io/blog/kafka-client-cannot-connect-to-broker-on-aws-on-docker-etc/ - "9092:9092" depends_on: - zookeeper environment: KAFKA_BROKER_ID: 1 KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181' KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_INTERNAL://broker:29092 KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1 KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
-
Create a topic (see
domain_demo/controller/kafkasubsrciber/router.go
anddomain_demo/gateway/kafkapublisher/gateway.go
where we mention the topic name :sendMessage002
)$ docker exec broker \ kafka-topics --bootstrap-server broker:9092 \ --create \ --topic sendMessage002
-
Run the server application by
go run main.go server
-
Run the client application by
go run main.go client
-
Invoke this api with curl, postman or use the file
http_runmessagesend.http
underdomain_demo/controller/restapi
POST http://localhost:8000/api/v1/runmessagesend { "message": "hello" }
See the terminal from server side, it will print out the message. Means that the message has been sent by client and received by server
>>> hello
For the server you may comment / uncomment this part (application/app_server.go
)
//primaryDriver := redissubscriber.NewController(log, cfg)
primaryDriver := kafkasubscriber.NewController(log, cfg)
For the client you may comment / uncomment this part (application/app_client.go
)
//datasource := redispublisher.NewGateway(log, appData, cfg)
datasource := kafkapublisher.NewGateway(log, appData, cfg)