-
Notifications
You must be signed in to change notification settings - Fork 855
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added functionality to allow user to add a message to a topic partiti…
…on manually (#703)
- Loading branch information
1 parent
0ed94d0
commit 5275899
Showing
15 changed files
with
465 additions
and
14 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,17 @@ | ||
package kafdrop.model; | ||
|
||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Data | ||
@RequiredArgsConstructor | ||
public final class CreateMessageVO { | ||
|
||
private int topicPartition; | ||
|
||
private String key; | ||
|
||
private String value; | ||
|
||
private String topic; | ||
} |
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,65 @@ | ||
package kafdrop.service; | ||
|
||
import java.util.Properties; | ||
import java.util.concurrent.Future; | ||
|
||
|
||
import jakarta.annotation.PostConstruct; | ||
import org.apache.kafka.clients.producer.KafkaProducer; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
import org.apache.kafka.common.serialization.ByteArraySerializer; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import kafdrop.config.KafkaConfiguration; | ||
import kafdrop.model.CreateMessageVO; | ||
import kafdrop.util.Serializers; | ||
|
||
@Service | ||
public final class KafkaHighLevelProducer { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(KafkaHighLevelProducer.class); | ||
private final KafkaConfiguration kafkaConfiguration; | ||
private KafkaProducer<byte[], byte[]> kafkaProducer; | ||
|
||
public KafkaHighLevelProducer(KafkaConfiguration kafkaConfiguration) { | ||
this.kafkaConfiguration = kafkaConfiguration; | ||
} | ||
|
||
@PostConstruct | ||
private void initializeClient() { | ||
if (kafkaProducer == null) { | ||
final var properties = new Properties(); | ||
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class); | ||
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class); | ||
properties.put(ProducerConfig.ACKS_CONFIG, "all"); | ||
properties.put(ProducerConfig.RETRIES_CONFIG, 0); | ||
properties.put(ProducerConfig.LINGER_MS_CONFIG, 1); | ||
properties.put(ProducerConfig.CLIENT_ID_CONFIG, "kafdrop-producer"); | ||
kafkaConfiguration.applyCommon(properties); | ||
|
||
kafkaProducer = new KafkaProducer<>(properties); | ||
} | ||
} | ||
|
||
public RecordMetadata publishMessage(CreateMessageVO message, Serializers serializers) { | ||
initializeClient(); | ||
|
||
final ProducerRecord<byte[], byte[]> record = new ProducerRecord<byte[], byte[]>(message.getTopic(), | ||
message.getTopicPartition(), serializers.getKeySerializer().serializeMessage(message.getKey()), | ||
serializers.getValueSerializer().serializeMessage(message.getValue())); | ||
|
||
Future<RecordMetadata> result = kafkaProducer.send(record); | ||
try { | ||
RecordMetadata recordMetadata = result.get(); | ||
LOG.info("Record published successfully [{}]", recordMetadata); | ||
return recordMetadata; | ||
} catch (Exception e) { | ||
LOG.error("Failed to publish message", e); | ||
throw new KafkaProducerException(e); | ||
} | ||
} | ||
} |
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,16 @@ | ||
package kafdrop.service; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public class KafkaProducerException extends RuntimeException { | ||
|
||
public KafkaProducerException(Throwable exception) { | ||
super(exception); | ||
} | ||
|
||
public KafkaProducerException(String message) { | ||
super(message); | ||
} | ||
} |
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,36 @@ | ||
package kafdrop.util; | ||
|
||
import java.util.HashMap; | ||
|
||
import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig; | ||
import io.confluent.kafka.serializers.KafkaAvroSerializer; | ||
|
||
public class AvroMessageSerializer implements MessageSerializer { | ||
|
||
private final String topicName; | ||
private final KafkaAvroSerializer serializer; | ||
|
||
public AvroMessageSerializer(String topicName, String schemaRegistryUrl, String schemaRegistryAuth) { | ||
this.topicName = topicName; | ||
this.serializer = getSerializer(schemaRegistryUrl, schemaRegistryAuth); | ||
} | ||
|
||
@Override | ||
public byte[] serializeMessage(String value) { | ||
final var bytes = value.getBytes(); | ||
return serializer.serialize(topicName, bytes); | ||
} | ||
|
||
private KafkaAvroSerializer getSerializer(String schemaRegistryUrl, String schemaRegistryAuth) { | ||
final var config = new HashMap<String, Object>(); | ||
config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl); | ||
if (schemaRegistryAuth != null) { | ||
config.put(AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE, "USER_INFO"); | ||
config.put(AbstractKafkaAvroSerDeConfig.USER_INFO_CONFIG, schemaRegistryAuth); | ||
} | ||
final var kafkaAvroSerializer = new KafkaAvroSerializer(); | ||
kafkaAvroSerializer.configure(config, false); | ||
return kafkaAvroSerializer; | ||
} | ||
|
||
} |
Oops, something went wrong.