Producers and consumers
Produce and consume with Spring Boot, Quarkus and .NET. Share the work in a consumer group, and use the HTTP Bridge when you cannot link a client.
The console producer proved the cluster. Applications need a client, a consumer group and a committed offset. This lab wires the same payments topic to three stacks we use on OpenShift, and to the HTTP Bridge for a caller that can only speak REST.
The key is the account, as topic design decided. The value is JSON for this lab. Schema registry replaces that once more than one team writes the record.
Prerequisites
- The
labcluster and thepaymentstopic - A bootstrap address
lab-kafka-bootstrap.kafka.svc:9092from inside the cluster - JDK 21 for the Java examples, the .NET 8 SDK for the C# example
Spring Boot
spring:
kafka:
bootstrap-servers: lab-kafka-bootstrap.kafka.svc:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
acks: all
consumer:
group-id: posting
auto-offset-reset: earliest
enable-auto-commit: false
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
properties:
spring.json.trusted.packages: "com.example.payments"
enable-auto-commit: false so the offset moves when your code says the payment was posted, not when the batch was received. acks: all waits for the in-sync replicas. One producer call, with the account as the key:
kafka.send("payments", payment.account(), payment).join();
A @KafkaListener on group posting handles the record and lets the container commit when the method returns. Throwing is how you refuse to commit. The delivery guarantees lab is what you do inside that method so a retry does not post twice.
Quarkus
kafka.bootstrap.servers=lab-kafka-bootstrap.kafka.svc:9092
mp.messaging.outgoing.payments.connector=smallrye-kafka
mp.messaging.outgoing.payments.topic=payments
mp.messaging.outgoing.payments.acks=all
mp.messaging.outgoing.payments.key.serializer=org.apache.kafka.common.serialization.StringSerializer
mp.messaging.incoming.posted.connector=smallrye-kafka
mp.messaging.incoming.posted.topic=payments
mp.messaging.incoming.posted.group.id=posting
mp.messaging.incoming.posted.auto.offset.reset=earliest
mp.messaging.incoming.posted.enable.auto.commit=false
@Incoming("posted")
@Acknowledgment(Acknowledgment.Strategy.POST_PROCESSING)
public void posted(Payment payment) {
ledger.postIfNew(payment);
}
POST_PROCESSING commits after posted returns. ledger.postIfNew is your idempotent write.
.NET
var producerConfig = new ProducerConfig {
BootstrapServers = "lab-kafka-bootstrap.kafka.svc:9092",
Acks = Acks.All,
EnableIdempotence = true
};
using var producer = new ProducerBuilder<string, string>(producerConfig).Build();
await producer.ProduceAsync("payments", new Message<string, string> {
Key = payment.Account, Value = JsonSerializer.Serialize(payment)
});
var consumerConfig = new ConsumerConfig {
BootstrapServers = "lab-kafka-bootstrap.kafka.svc:9092",
GroupId = "posting",
AutoOffsetReset = AutoOffsetReset.Earliest,
EnableAutoCommit = false
};
using var consumer = new ConsumerBuilder<string, string>(consumerConfig).Build();
consumer.Subscribe("payments");
var record = consumer.Consume(TimeSpan.FromSeconds(10));
if (record is not null && ledger.PostIfNew(record.Message.Value))
consumer.Commit(record);
The Confluent .NET client is the usual choice. EnableIdempotence stops the producer itself creating duplicates on a retry. It does not stop two different producer instances, or a consumer that commits late. That is still the ledger's unique key.
Groups, offsets and the bridge
All three consumers above use the group posting. Six partitions and two pods means about three partitions each. Scale the Deployment to two and watch the rebalance. Offsets live in __consumer_offsets, keyed by group, topic and partition. auto.offset.reset=earliest applies only when that group has no offset yet. A group that has committed will not rewind because you redesigned the config.
Some callers will not take a Kafka library. The Bridge exposes produce and consume over HTTP. Deploy it beside the cluster:
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaBridge
metadata:
name: bridge
namespace: kafka
spec:
replicas: 1
bootstrapServers: lab-kafka-bootstrap:9092
http:
port: 8080
curl -s -X POST http://bridge-bridge-service.kafka.svc:8080/topics/payments \
-H 'Content-Type: application/vnd.kafka.json.v2+json' \
-d '{"records":[{"key":"a1","value":{"id":"p1","account":"a1","amount":100}}]}'
The bridge is a client. It has the same delivery questions as the other three, and it is not a shortcut around security. Put it behind the same network policy as the brokers.
What's next
- Make
postIfNewreal in delivery guarantees. - Replace the JSON string with a schema in schema registry.
- The group you just created is the one DC/DR will fail over, offsets included.