-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using proto message envelope for kafka transport
- Loading branch information
Showing
10 changed files
with
238 additions
and
220 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ https: | |
**/data-zoo-data | ||
**/data-zoo-logs | ||
**/bin | ||
.factorypath |
17 changes: 0 additions & 17 deletions
17
...threat-detection-backend/src/main/java/com/akto/threat/backend/service/HealthService.java
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
apps/threat-detection/src/main/java/com/akto/threat/detection/kafka/KafkaProtoProducer.java
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,48 @@ | ||
package com.akto.threat.detection.kafka; | ||
|
||
import com.akto.kafka.KafkaConfig; | ||
import com.google.protobuf.Message; | ||
import java.time.Duration; | ||
import java.util.Properties; | ||
import org.apache.kafka.clients.producer.*; | ||
import org.apache.kafka.common.serialization.StringSerializer; | ||
|
||
public class KafkaProtoProducer { | ||
private KafkaProducer<String, byte[]> producer; | ||
public boolean producerReady; | ||
|
||
public KafkaProtoProducer(KafkaConfig kafkaConfig) { | ||
this.producer = generateProducer( | ||
kafkaConfig.getBootstrapServers(), | ||
kafkaConfig.getProducerConfig().getLingerMs(), | ||
kafkaConfig.getProducerConfig().getBatchSize()); | ||
} | ||
|
||
public void send(String topic, Message message) { | ||
byte[] messageBytes = message.toByteArray(); | ||
this.producer.send(new ProducerRecord<>(topic, messageBytes)); | ||
} | ||
|
||
public void close() { | ||
this.producerReady = false; | ||
producer.close(Duration.ofMillis(0)); // close immediately | ||
} | ||
|
||
private KafkaProducer<String, byte[]> generateProducer(String brokerIP, int lingerMS, int batchSize) { | ||
if (producer != null) | ||
close(); // close existing producer connection | ||
|
||
int requestTimeoutMs = 5000; | ||
Properties kafkaProps = new Properties(); | ||
kafkaProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerIP); | ||
kafkaProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, | ||
"org.apache.kafka.common.serialization.ByteArraySerializer"); | ||
kafkaProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
kafkaProps.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize); | ||
kafkaProps.put(ProducerConfig.LINGER_MS_CONFIG, lingerMS); | ||
kafkaProps.put(ProducerConfig.RETRIES_CONFIG, 0); | ||
kafkaProps.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, requestTimeoutMs); | ||
kafkaProps.put(ProducerConfig.DELIVERY_TIMEOUT_MS_CONFIG, lingerMS + requestTimeoutMs); | ||
return new KafkaProducer<String, byte[]>(kafkaProps); | ||
} | ||
} |
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
Oops, something went wrong.