1. SPRING CLOUD STREAM
WITH KAFKA
by David Kiss
(
)
http://kaviddiss.com/2018/03/03/spring-cloud-
stream-kafka/
Navigate : Space / Arrow Keys | - Menu | - Fullscreen | - Overview | - Blackout | - Speaker | - HelpM F O B S ?
1 / 60
2. OVERVIEW
This sample project demonstrates how to build
applications using
, , ,
and .
real-
time streaming event-driven
architecture Spring Boot Spring Cloud Stream Apache
Kafka Lombok
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
2 / 60
3. By end of this tutorial you'll have a simple Spring Boot
based Greetings microservice running that
1. takes a message from a REST api
2. writes it to a Kafka topic
3. reads it from the topic
4. outputs it to the console
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
3 / 60
5. WHAT IS SPRING CLOUD STREAMING?
Spring Cloud Stream is a framework built upon Spring
Boot for building message-driven microservices.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
5 / 60
6. WHAT IS KAFKA?
Kafka is a popular high performant and horizontally
scalable messaging platform originally developed by
LinkedIn.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
6 / 60
8. Download Kafka from and untar it:here
> tar -xzf kafka_2.11-1.0.0.tgz
> cd kafka_2.11-1.0.0
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
8 / 60
9. Start Zookeeper and Kafka
On Windows:
On Linux or Mac:
> binwindowszookeeper-server-start.bat configzookeeper.propert
> binwindowskafka-server-start.bat configserver.properties
> bin/zookeeper-server-start.sh config/zookeeper.properties
> bin/kafka-server-start.sh config/server.properties
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
9 / 60
10. If Kafka is not running and fails to start after your
computer wakes up from hibernation, delete the
<TMP_DIR>/kafka-logs folder and then start Kafka
again.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
10 / 60
11. WHAT IS LOMBOK?
Lombok is a java framework that automatically
generates getters, setters, toString(), builders, loggers,
etc. in the code.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
11 / 60
13. Go to to create a maven project:https://start.spring.io
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
13 / 60
14. 1. Add necessary dependencies: Spring Cloud
Stream, Kafka, Devtools (for hot redeploys
during development, optional), Actuator (for
monitoring application, optional), Lombok (make
sure to also have the Lombok plugin installed in your
IDE)
2. Click the Generate Project button to download the
project as a zip le
3. Extract zip le and import the maven project to your
favourite IDE
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
14 / 60
15. Notice the maven dependencies in the pom.xml le:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
</dependency>
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
15 / 60
16. <!-- Also install the Lombok plugin in your IDE -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- hot reload - press Ctrl+F9 in IntelliJ after a code change
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
16 / 60
17. ... also the <dependencyManagement> section:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boo
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>$
{spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
17 / 60
22. In order for our application to be able to communicate
with Kafka, we'll need to de ne an outbound stream to
write messages to a Kafka topic, and an inbound stream
to read messages from a Kafka topic.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
22 / 60
23. Spring Cloud provides a convenient way to do this by
simply creating an interface that de nes a separate
method for each stream.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
23 / 60
24. The inboundGreetings() method de nes the
inbound stream to read from Kafka and
outboundGreetings() method de nes the
outbound stream to write to Kafka.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
24 / 60
25. During runtime Spring will create a java proxy based
implementation of the GreetingsStreams interface
that can be injected as a Spring Bean anywhere in the
code to access our two streams.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
25 / 60
26. CONFIGURE SPRING CLOUD STREAM
Our next step is to con gure Spring Cloud Stream to
bind to our streams in the GreetingsStreams
interface.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
26 / 60
27. This can be done by creating a @Configuration class
com.kaviddiss.streamkafka.config.StreamsConfig
with below code:
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
27 / 60
29. Binding the streams is done using the
@EnableBinding annotation where the
GreatingsService interface is passed to.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
29 / 60
30. CONFIGURATION PROPERTIES FOR KAFKA
By default, the con guration properties are stored in
the
src/main/resources/application.properties
le.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
30 / 60
31. However I prefer to use the YAML format as it's less
verbose and allows to keep both common and
environment-speci c properties in the same le.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
31 / 60
32. For now, let's rename application.properties to
application.yaml and paste below con g snippet
into the le:
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
32 / 60
34. The above con guration properties con gure the
address of the Kafka server to connect to, and the
Kafka topic we use for both the inbound and outbound
streams in our code. They both must use the same
Kafka topic!
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
34 / 60
35. The contentType properties tell Spring Cloud Stream
to send/receive our message objects as Strings in the
streams.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
35 / 60
36. CREATE THE MESSAGE OBJECT
Create a simple
com.kaviddiss.streamkafka.model.Greetings
class with below code that will represent the message
object we read from and write to the greetings
Kafka topic:
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
36 / 60
37. package com.kaviddiss.streamkafka.model;
// lombok autogenerates getters, setters, toString() and a builde
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter @Setter @ToString @Builder
public class Greetings {
private long timestamp;
private String message;
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
37 / 60
38. Notice how the class doesn't have any getters and
setters thanks to the Lombok annotations. The
@ToString will generate a toString() method
using the class' elds and the @Builder annotation
will allow us creating Greetings objects using uent
builder (see below).
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
38 / 60
39. CREATE SERVICE LAYER TO WRITE TO KAFKA
Let's create the
com.kaviddiss.streamkafka.service.GreetingsSer
class with below code that will write a Greetings
object to the greetings Kafka topic:
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
39 / 60
43. The @Service annotation will con gure this class as a
Spring Bean and inject the GreetingsService
dependency via the constructor.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
43 / 60
44. The @Slf4j annotation will generate an SLF4J logger
eld that we can use for logging.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
44 / 60
45. In the sendGreeting() method we use the injected
GreetingsStream object to send a message
represented by the Greetings object.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
45 / 60
46. CREATE REST API
Now we'll be creating a REST api endpoint that will
trigger sending a message to Kafka using the
GreetingsService Spring Bean:
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
46 / 60
50. The @RestController annotation tells Spring that
this is a Controller bean (the C from MVC). The
greetings() method de nes an HTTP GET
/greetings endpoint that takes a message request
param and passes it to the sendGreeting() method
in GreetingsService.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
50 / 60
51. LISTENING ON THE GREETINGS KAFKA TOPIC
Let's create a
com.kaviddiss.streamkafka.service.GreetingsLis
class that will listen to messages on the greetings
Kafka topic and log them on the console:
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
51 / 60
53. @Component
@Slf4j
public class GreetingsListener {
@StreamListener(GreetingsStreams.INPUT)
public void handleGreetings(@Payload Greetings greetings) {
log.info("Received greetings: {}", greetings);
}
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
53 / 60
54. The @Component annotation similarly to @Service
and @RestController de nes a Spring Bean.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
54 / 60
55. GreetingsListener has a single method,
handleGreetings() that will be invoked by Spring
Cloud Stream with every new Greetings message
object on the greetings Kafka topic. This is thanks to
the @StreamListener annotation con gured for the
handleGreetings() method.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
55 / 60
56. RUNNING THE APPLICATION
The last piece of the puzzle is the
com.kaviddiss.streamkafka.StreamKafkaApplicati
class that was auto-generated by the Spring Initializer:
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
56 / 60
58. No need to make any changes here. You can either run
this class as a Java application from your IDE, or run the
application from the command line using the Spring
Boot maven plugin:
> mvn spring-boot:run
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
58 / 60
59. Once the application is running, go to
in the
browser and check your console.
http://localhost:8080/greetings?message=hello
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
59 / 60
60. SUMMARY
I hope you enjoyed this tutorial. Feel free to ask any
questions and leave your feedback.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]
60 / 60