Communicate Between Microservices with Apache Kafka

Communicate Between Microservices with Apache Kafka

This tutorial shows you how to add asynchronous messaging between Java microservices using Apache Kafka.

Kanat Aidarov

Awesome post, Jimena!
I have issues with reproducing last step: I could not create a store entity.
I can login using Oauth2 but when I choose ‘Store’ or ‘Store alert’ in
‘Entities’ menu, it says that I am not authorized
( https://ibb.co/TB5DcDF )
My okta account groups policy seems to be set (https://ibb.co/hsyktwx ).
It looks like issue at Angular part, because there are no relevant log messages in spring logs. https://uploads.disquscdn.c…

Matt Raible

Hello Kanat,

Can you please try comparing your code to this tutorial’s GitHub repo? SmartSynchronize is a nice tool for comparing directory contents.

Olfa Moussaoui

please matt make a video about this blog :slight_smile:

Matt Raible

OK. I’ll add it to my list! Expect it sometime in May or June.

Olfa Moussaoui

I appreciate that, thank you so much

Federico Ricchiuto

Very nice article!
Recently I’ve migrated a monolithic app to microservice infrastructure. I have a network problem when I try to connect to kafka.
Basically everything works when I build the docker image and deploy everything through docker compose.
But as you can imagine, during development it’s easier to use a simpler docker compose configuration, including all the infrastructure services (jhipster-registry, keycloak, kafka, etc…) and then launch each microservice separately without using docker (you need also to run each microservice database, with docker).
If I do this, when I launch the spring application (let’s call it microservice-A), then it cannot reach the kafka server, while if the application is “dockerized”, there’s no problem and it works.

Does anybody have an idea on why this happens, and what I can do to solve this problem?
Thanks in advance

Federico Ricchiuto

Okay so I was using the full infrastructure docker-compose configuration, where kafka container was configured a little differently. So I used the kafka.yml docker-compose configuration shipped with the microservice, and it worked this time.
The difference is in this line:

KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092

In order to let a spring application (not dockerized) reach the kafka server, it should be configured as:

KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092

As you can see it points also to localhost, which is not needed in a docker-only configuration.
Hope that this can help somebody else.

Ivan86to

what happens if (in case of exception) the consumer goes closed? Does the service stop consuming messages? Can’t restart it automatically?