03: Apache Kafka example with toxiproxy & Producer timeout handling

This extends Apache Kafka example with Java Producer & Consumer Tutorial – Part 2. This covers Apache Kafka example with toxiproxy & producer timeout. The producer code needs to be robust to handle any network issues like timeouts & latency issues. In this tutorial a tool named toxiproxy is used to simulate network issues.

Kafka server on Docker

TOXIPROXY WITH KAFKA

TOXIPROXY WITH KAFKA

Step 1: As discussed in part-1, stand-up the Kafka, Zookeeper & Apache Kafka Cluster Visualization (AKHQ) on a Docker container.

Step 2: Kafka is running on localhost:9093

Outputs:

Install toxiproxy

Step 3: Install toxiproxy as described. For example, on Mac OS

and then

Run toxiproxy server

Step 4: Open a new command-line & run the toxiproxy server:

toxiproxy cli to create proxies

Step 5: Open a new command-line & run the toxiproxy client see a list of any proxies.

Outputs:

Step 6: Create a proxy for the kafka service running on localhost:9093.

Outputs:

You can list the proxies running.

Outputs:

Run the ProducerApp.java from Intellij IDEA

Step 7: Please note that the producer connects to the Kafka server running in Docker via the toxiproxy proxy running on localhost:19093 created in the previous step.

If you run the above code the “ProducerApp.java” will successfully publish a message to the topic “my-first-topic“. You can view the message in the UI via a browser with URL http://localhost:9099/ui/docker-kafka-server/topic.

Note that below lines are added to acknowledge if a message can’t be published. This gets used in the later steps when a timeout toxic is added to simulate a network timeout issue with toxiproxy.

Add timeout toxic to the kafka-proxy

Step 8: Let’s add a timeout toxic to the proxy kafka-proxy that was created via toxiproxy-cli in one of the previous steps.

Outputs:

You can list the toxics with inspect.

Outputs:

Step 9: Run the Java application “ProducerApp.java” again to connect to the Kafka server at “localhost:19093” with a timeout toxic. You will notice that the “ProducerApp.java” will be running for a while & gets timed out.

It gets timed out on the line

Outputs:

Remove the toxic whilst the ProducerApp.java is running

Step 9: Run the Java application “ProducerApp.java” again to connect to the Kafka server at “localhost:19093” with a timeout toxic.

Whilst the application is running & blocked on producer.send(..,..), remove the toxic with the below command-line.

Now the message will be published to the Kafka topic “my-first-topic“.

Add latency toxic

Step 10: Add a latency toxic as shown below:

Outputs:

List the toxics:

Outputs:

Step 11: Run the Java application “ProducerApp.java” again to connect to the Kafka server at “localhost:19093” with a latency toxic.

The code will take extra 2 seconds, but the message will be published to the topic.

Finally, remove the proxy

Outputs:


Select by a category

300+ Java Interview Questions - FAQs

Tutorials on Java & Big Data