-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add docker compose to setup/run kafka & zookeeper (kafka uses zookeeper to run) * update readme file for settingup & using kafka. * add code to check kafka working smoothly or not. * add checkPayment() which returns sucess 70% of time.
- Loading branch information
1 parent
36b6487
commit 2260f80
Showing
6 changed files
with
138 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { Kafka } = require('kafkajs'); | ||
|
||
// Define your Kafka broker(s) | ||
const kafkaBrokers = ['localhost:9093']; // Update with your Kafka broker(s) information | ||
|
||
// Create a Kafka instance | ||
const kafka = new Kafka({ | ||
clientId: 'kafka-test-client', | ||
brokers: kafkaBrokers, | ||
}); | ||
|
||
// Create a Kafka producer | ||
const producer = kafka.producer(); | ||
|
||
// Create a Kafka consumer | ||
const consumer = kafka.consumer({ groupId: 'kafka-test-group' }); | ||
|
||
// Function to produce a test message | ||
async function produceMessage() { | ||
await producer.connect(); | ||
await producer.send({ | ||
topic: 'test-topic', | ||
messages: [{ value: 'Hello, Kafka!' }], | ||
}); | ||
await producer.disconnect(); | ||
} | ||
|
||
// Function to consume messages | ||
async function consumeMessages() { | ||
await consumer.connect(); | ||
await consumer.subscribe({ topic: 'test-topic', fromBeginning: true }); | ||
|
||
await consumer.run({ | ||
eachMessage: async ({ message }) => { | ||
console.log(`Received message: ${message.value}`); | ||
}, | ||
}); | ||
|
||
// Uncomment the following line if you want to stop the consumer after a certain period | ||
// setTimeout(async () => await consumer.disconnect(), 5000); | ||
} | ||
|
||
// Run the test | ||
async function runTest() { | ||
try { | ||
await produceMessage(); | ||
await consumeMessages(); | ||
} catch (error) { | ||
console.error('Error:', error); | ||
} finally { | ||
// Uncomment the following line if you want to stop the producer after the test | ||
// await producer.disconnect(); | ||
} | ||
} | ||
|
||
// Run the test | ||
runTest(); |
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,7 @@ | ||
/** | ||
* it returns success 70% of time | ||
* @returns {boolean} | ||
*/ | ||
export function checkPayment({txnId, orderId}) { | ||
return Math.random() < 0.7; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
# docker-compose.yml | ||
version: "3.7" | ||
services: | ||
zookeeper: | ||
restart: always | ||
image: docker.io/bitnami/zookeeper:3.8 | ||
ports: | ||
- "2181:2181" | ||
volumes: | ||
- "zookeeper-volume:/bitnami" | ||
environment: | ||
- ALLOW_ANONYMOUS_LOGIN=yes | ||
kafka: | ||
restart: always | ||
image: docker.io/bitnami/kafka:3.3 | ||
ports: | ||
- "9093:9093" | ||
volumes: | ||
- "kafka-volume:/bitnami" | ||
environment: | ||
- KAFKA_BROKER_ID=1 | ||
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181 | ||
- ALLOW_PLAINTEXT_LISTENER=yes | ||
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT | ||
- KAFKA_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9093 | ||
- KAFKA_CFG_ADVERTISED_LISTENERS=CLIENT://kafka:9092,EXTERNAL://localhost:9093 | ||
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=CLIENT | ||
depends_on: | ||
- zookeeper | ||
volumes: | ||
kafka-volume: | ||
zookeeper-volume: |