• Stars
    star
    579
  • Rank 76,889 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Demo to show how Apache Kafka can be used for communication between microservices

Microservice Kafka Sample

Deutsche Anleitung zum Starten des Beispiels

This is a sample to show how Kafka can be used for the communication between microservices.

The project creates Docker containers.

It uses three microservices:

  • Order to create orders. This services sends messages to Kafka. It uses the KafkaTemplate.
  • Shipment receives the orders and extract the information needed to ship the items.
  • Invoicing receives the messages, too. It extracts all information to send out an invoice. It uses @KafkaListener just like Shipment.

This is done using a topic order. It has five partitions. Shipment and invoicing each have a separate consumer group. So multiple instances of shipment and invoicing can be run. Each instance would get specific events.

Technologies

  • Spring Boot
  • Spring Kafka
  • Apache httpd
  • Kafka
  • Zookeeper
  • Postgres
  • Docker Compose to link the containers.

How To Run

See How to run for details.

Once you create an order in the order application, after a while the invoice and the shipment should be shown in the other applications.

Remarks on the Code

The microservices are:

The data of an order is copied - including the data of the customer and the items. So if a customer or item changes in the order system this does not influence existing shipments and invoices. It would be odd if a change to a price would also change existing invoices. Also only the information needed for the shipment and the invoice are copied over to the other systems.

The Order microservice uses Spring's KafkaTemplate to send message while the other two microservices use the annotation @KafkaListener on the methods that should be called if a new record comes in. All records are put in the order topic. It has five partitions to allow for scalability.

For tests an embedded Kafka server is used. A @ClassRule starts it. And a method annotated with @BeforeClass configures Spring Kafka to use the embedded Kafka server.

The orders are serialized as JSON. So the Order object of the order microservice is serialized as a JSON data structure. The other two microservices just read the data they need for shipping and invoicing. So the invoicing microservices reads the Invoiceobject and the delivery microservice the Deliveryobject. This avoids code dependencies between the microservices. Order contains all the data for Invoice as well as Delivery. JSON serialization is flexible. So when an Order is deserialized into Invoice and Delivery just the needed data is read. The additional data is just ignored.

There are three Docker container for the microservices. The other Docker containers are for Apache httpd, Kafka, Zookeeper and Postgres.

Incoming http request are handled by the Apache httpd server. It is available at port 8080 of the Docker host e.g. http://localhost:8080. HTTP requests are forwarded to the microservices. Kafka is used for the communication between the microservices. Kafka needs Zookeeper to coordinate instances. Postgres is used by all microservices to store data. Each microservices uses its own database in the Postgres instance so they are decoupled in that regard.

You can scale the listener with e.g. docker-compose scale shipping=2. The logs (docker logs mskafka_shipping_1) will show which partitions the instances listen to and which records they handle.

You can also start a shell on the Kafka server docker exec -it mskafka_kafka_1 /bin/sh and then take a look at the records in the topic using kafka-console-consumer.sh --bootstrap-server kafka:9092 --topic order --from-beginning.

More Repositories

1

microservice

Sample of a Microservice setup for my book. Based on Spring Cloud / Netflix / Java / Docker / Docker Compose / Docker Machine / Vagrant
Java
712
star
2

microservice-kubernetes

Microservices example using Kubernetes
Java
201
star
3

microservice-istio

Example for a microservices system based in Kubernetes and the service mesh Istio
Java
185
star
4

microservice-consul

Sample of a Microservice setup for my book ported to Consul / Apache httpd. Based on Spring Cloud / Netflix / Java / Docker / Docker Compose / Docker Machine
Java
104
star
5

user-registration

Continuous Delivery example: Chef, Docker, Jenkins, Graphite, ELK stack, jBehave, Selenium, Gatling
Ruby
65
star
6

InformatikFAQ

Informatik als Beruf FAQ
51
star
7

SCS-ESI

Demo for Self-contained Systems (SCS) using ESI (Edge Side Includes) with Varnish for Frontend Integration
Java
44
star
8

user-registration-V2

Java
38
star
9

spring-boot-demos

Some demos for Spring Boot
Java
38
star
10

microservice-atom

Demo to show how Atom can be used for communication between microservices
Java
31
star
11

microservice-consul-dns

Microservices Demo. Uses Consul as DNS server, registration is automated using Registrator
Java
21
star
12

microservice-linkerd

Microservices example with Linkerd service mesh
Java
16
star
13

crimson-assurance-demo

Demo for frontend integration
Dockerfile
14
star
14

cheatsheets-DE

Kurze Ãœbersichten / Cheatsheets zu Maven und Docker
14
star
15

microservice-spring

A microservices sample using Spring Boot features
Java
14
star
16

scala-spring

Shows how Scala and Spring can be integrated
Scala
12
star
17

microservice-cloudfoundry

Microservices demo using Cloud Foundry
Java
12
star
18

software-architektur.tv

software-architektur.tv Website
CSS
10
star
19

SCS-jQuery

Demo for Self-contained Systems (SCS) using jQuery for Frontend Integration
Java
9
star
20

microservices-rezepte

Source für Broschüre "Microservices Rezepte"
9
star
21

Spring-Integration-Web-Services-Batch-Demo

Demo of Spring Web Services, Spring Batch and Spring Integration
Java
8
star
22

microservices-primer

Source for booklet "Microservices Primer"
Shell
8
star
23

microservice-dapr

A microservices sample using Dapr
Java
7
star
24

spring-redis-demo

Shows how Spring can be used to access the Redis NoSQL store
Java
6
star
25

bildungsinitiativen

List von verschiedenen Bildungsinitiativen im Bereich Software-Entwicklung
5
star
26

docker-java

Very small Java image for Docker
5
star
27

microservices-vm

Creates a virtual machine with the Microservices demo and the Continuous Delivery demo
Shell
4
star
28

microservices-ueberblick

Source für Broschüre "Microservices Überblick"
4
star
29

lvm-demo

LVM Demo shows principles of ROCA and SCS
3
star
30

Spring-Buch

Repository for the Spring book (German)
Java
2
star
31

ewolff.com

Eberhard Wolff's Homepage
CSS
2
star
32

service-mesh-primer

Source for booklet "Service Mesh Primer"
Makefile
2
star
33

seneca-example

A simple example using the Seneca framework
JavaScript
2
star
34

practical-microservices.com

XSLT
2
star
35

microservices-recipes

Source for booklet "Microservices Recipes"
2
star
36

spring-buch.de

Website für das Spring Buch
HTML
1
star
37

JavaChefVagrantEC2

Ruby
1
star
38

spring-rabbit-demo

Demo how to use Spring AMQP and RabbitMQ
Java
1
star
39

microservices-buch.de

Website für das Microservices Buch
XSLT
1
star
40

redis-docker

Redis installation with Docker
Shell
1
star
41

news-application-heroku

Java
1
star
42

war-demo

Demo for Nanoservices using WARs and Tomcat
Java
1
star
43

microservices-praxisbuch.de

XSLT
1
star
44

continuous-delivery-buch.de

Website für das Continuous Delivery Buch
XSLT
1
star
45

erlang-example

Simple example for an Erlang client / server
Erlang
1
star