SlideShare a Scribd company logo
1 of 60
Download to read offline
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
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
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
LET'S GET STARTED!
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

4 / 60
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
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
INSTALLING KAFKA
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

7 / 60
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
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
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
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
MAVEN DEPENDENCIES
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

12 / 60
Go to to create a maven project:https://start.spring.io
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

13 / 60
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
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
<!-- 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
... 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
...
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-dependencies</art
<version>$
{spring-cloud-stream.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

18 / 60
... and the <repository> section:
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

19 / 60
DEFINE THE KAFKA STREAMS
package com.kaviddiss.streamkafka.stream;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

20 / 60
public interface GreetingsStreams {
String INPUT = "greetings-in";
String OUTPUT = "greetings-out";
@Input(INPUT)
SubscribableChannel inboundGreetings();
@Output(OUTPUT)
MessageChannel outboundGreetings();
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

21 / 60
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
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
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
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
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
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
package com.kaviddiss.streamkafka.config;
import com.kaviddiss.streamkafka.stream.GreetingsStreams;
import org.springframework.cloud.stream.annotation.EnableBinding;
@EnableBinding(GreetingsStreams.class)
public class StreamsConfig {
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

28 / 60
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
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
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
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
spring:
cloud:
stream:
kafka:
binder:
brokers: localhost:9092
bindings:
greetings-in:
destination: greetings
contentType: application/json
greetings-out:
destination: greetings
contentType: application/json
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

33 / 60
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
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
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
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
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
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
package com.kaviddiss.streamkafka.service;
import com.kaviddiss.streamkafka.model.Greetings;
import com.kaviddiss.streamkafka.stream.GreetingsStreams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

40 / 60
@Service
@Slf4j
public class GreetingsService {
private final GreetingsStreams greetingsStreams;
public GreetingsService(GreetingsStreams greetingsStreams) {
this.greetingsStreams = greetingsStreams;
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

41 / 60
public void sendGreeting(final Greetings greetings) {
log.info("Sending greetings {}", greetings);
MessageChannel messageChannel = greetingsStreams.outboundG
messageChannel.send(MessageBuilder
.withPayload(greetings)
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeU
.build());
}
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

42 / 60
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
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
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
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
package com.kaviddiss.streamkafka.web;
import com.kaviddiss.streamkafka.model.Greetings;
import com.kaviddiss.streamkafka.service.GreetingsService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

47 / 60
@RestController
public class GreetingsController {
private final GreetingsService greetingsService;
public GreetingsController(GreetingsService greetingsService)
this.greetingsService = greetingsService;
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

48 / 60
@GetMapping("/greetings")
@ResponseStatus(HttpStatus.ACCEPTED)
public void greetings(@RequestParam("message") String message
Greetings greetings = Greetings.builder()
.message(message)
.timestamp(System.currentTimeMillis())
.build();
greetingsService.sendGreeting(greetings);
}
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

49 / 60
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
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
package com.kaviddiss.streamkafka.service;
import com.kaviddiss.streamkafka.model.Greetings;
import com.kaviddiss.streamkafka.stream.GreetingsStreams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

52 / 60
@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
The @Component annotation similarly to @Service
and @RestController de nes a Spring Bean.
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

54 / 60
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
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
package com.kaviddiss.streamkafka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplicati
@SpringBootApplication
public class StreamKafkaApplication {
public static void main(String[] args) {
SpringApplication.run(StreamKafkaApplication.class, args)
}
}
[ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]

57 / 60
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
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
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

More Related Content

What's hot

Spring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsugSpring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsugToshiaki Maki
 
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
Game of Streams: How to Tame and Get the Most from Your Messaging PlatformsGame of Streams: How to Tame and Get the Most from Your Messaging Platforms
Game of Streams: How to Tame and Get the Most from Your Messaging PlatformsVMware Tanzu
 
Microservices - java ee vs spring boot and spring cloud
Microservices - java ee vs spring boot and spring cloudMicroservices - java ee vs spring boot and spring cloud
Microservices - java ee vs spring boot and spring cloudBen Wilcock
 
Spring Cloud: Why? How? What?
Spring Cloud: Why? How? What?Spring Cloud: Why? How? What?
Spring Cloud: Why? How? What?Orkhan Gasimov
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introductionJonathan Holloway
 
Angular Owin Katana TypeScript
Angular Owin Katana TypeScriptAngular Owin Katana TypeScript
Angular Owin Katana TypeScriptJustin Wendlandt
 
Microservices with Java, Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring CloudMicroservices with Java, Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring CloudEberhard Wolff
 
Microservices with Netflix OSS and Spring Cloud - Dev Day Orange
Microservices with Netflix OSS and Spring Cloud -  Dev Day OrangeMicroservices with Netflix OSS and Spring Cloud -  Dev Day Orange
Microservices with Netflix OSS and Spring Cloud - Dev Day Orangeacogoluegnes
 
Building Distributed Systems with Netflix OSS and Spring Cloud
Building Distributed Systems with Netflix OSS and Spring CloudBuilding Distributed Systems with Netflix OSS and Spring Cloud
Building Distributed Systems with Netflix OSS and Spring CloudMatt Stine
 
Managing your Docker image continuously with Concourse CI
Managing your Docker image continuously with Concourse CIManaging your Docker image continuously with Concourse CI
Managing your Docker image continuously with Concourse CIToshiaki Maki
 
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07Toshiaki Maki
 
Spring Boot on Amazon Web Services with Spring Cloud AWS
Spring Boot on Amazon Web Services with Spring Cloud AWSSpring Boot on Amazon Web Services with Spring Cloud AWS
Spring Boot on Amazon Web Services with Spring Cloud AWSVMware Tanzu
 
Simplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring CloudSimplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring CloudRamnivas Laddad
 
OWIN and Katana Project - Not Only IIS - NoIIS
OWIN and Katana Project - Not Only IIS - NoIISOWIN and Katana Project - Not Only IIS - NoIIS
OWIN and Katana Project - Not Only IIS - NoIISBilal Haidar
 
What’s New in Spring Batch?
What’s New in Spring Batch?What’s New in Spring Batch?
What’s New in Spring Batch?VMware Tanzu
 
Serverless with Spring Cloud Function, Knative and riff #SpringOneTour #s1t
Serverless with Spring Cloud Function, Knative and riff #SpringOneTour #s1tServerless with Spring Cloud Function, Knative and riff #SpringOneTour #s1t
Serverless with Spring Cloud Function, Knative and riff #SpringOneTour #s1tToshiaki Maki
 
Spring ❤️ Kotlin #jjug
Spring ❤️ Kotlin #jjugSpring ❤️ Kotlin #jjug
Spring ❤️ Kotlin #jjugToshiaki Maki
 
Cloud Foundry for Spring Developers
Cloud Foundry for Spring DevelopersCloud Foundry for Spring Developers
Cloud Foundry for Spring DevelopersGunnar Hillert
 
実例で学ぶ、明日から使えるSpring Boot Tips #jsug
実例で学ぶ、明日から使えるSpring Boot Tips #jsug実例で学ぶ、明日から使えるSpring Boot Tips #jsug
実例で学ぶ、明日から使えるSpring Boot Tips #jsugToshiaki Maki
 

What's hot (20)

Spring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsugSpring Cloud Function & Project riff #jsug
Spring Cloud Function & Project riff #jsug
 
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
Game of Streams: How to Tame and Get the Most from Your Messaging PlatformsGame of Streams: How to Tame and Get the Most from Your Messaging Platforms
Game of Streams: How to Tame and Get the Most from Your Messaging Platforms
 
Microservices - java ee vs spring boot and spring cloud
Microservices - java ee vs spring boot and spring cloudMicroservices - java ee vs spring boot and spring cloud
Microservices - java ee vs spring boot and spring cloud
 
Spring Cloud: Why? How? What?
Spring Cloud: Why? How? What?Spring Cloud: Why? How? What?
Spring Cloud: Why? How? What?
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Angular Owin Katana TypeScript
Angular Owin Katana TypeScriptAngular Owin Katana TypeScript
Angular Owin Katana TypeScript
 
Microservices with Java, Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring CloudMicroservices with Java, Spring Boot and Spring Cloud
Microservices with Java, Spring Boot and Spring Cloud
 
Microservices with Netflix OSS and Spring Cloud - Dev Day Orange
Microservices with Netflix OSS and Spring Cloud -  Dev Day OrangeMicroservices with Netflix OSS and Spring Cloud -  Dev Day Orange
Microservices with Netflix OSS and Spring Cloud - Dev Day Orange
 
Building Distributed Systems with Netflix OSS and Spring Cloud
Building Distributed Systems with Netflix OSS and Spring CloudBuilding Distributed Systems with Netflix OSS and Spring Cloud
Building Distributed Systems with Netflix OSS and Spring Cloud
 
Managing your Docker image continuously with Concourse CI
Managing your Docker image continuously with Concourse CIManaging your Docker image continuously with Concourse CI
Managing your Docker image continuously with Concourse CI
 
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07
 
Spring Boot on Amazon Web Services with Spring Cloud AWS
Spring Boot on Amazon Web Services with Spring Cloud AWSSpring Boot on Amazon Web Services with Spring Cloud AWS
Spring Boot on Amazon Web Services with Spring Cloud AWS
 
Introduction to OWIN
Introduction to OWINIntroduction to OWIN
Introduction to OWIN
 
Simplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring CloudSimplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring Cloud
 
OWIN and Katana Project - Not Only IIS - NoIIS
OWIN and Katana Project - Not Only IIS - NoIISOWIN and Katana Project - Not Only IIS - NoIIS
OWIN and Katana Project - Not Only IIS - NoIIS
 
What’s New in Spring Batch?
What’s New in Spring Batch?What’s New in Spring Batch?
What’s New in Spring Batch?
 
Serverless with Spring Cloud Function, Knative and riff #SpringOneTour #s1t
Serverless with Spring Cloud Function, Knative and riff #SpringOneTour #s1tServerless with Spring Cloud Function, Knative and riff #SpringOneTour #s1t
Serverless with Spring Cloud Function, Knative and riff #SpringOneTour #s1t
 
Spring ❤️ Kotlin #jjug
Spring ❤️ Kotlin #jjugSpring ❤️ Kotlin #jjug
Spring ❤️ Kotlin #jjug
 
Cloud Foundry for Spring Developers
Cloud Foundry for Spring DevelopersCloud Foundry for Spring Developers
Cloud Foundry for Spring Developers
 
実例で学ぶ、明日から使えるSpring Boot Tips #jsug
実例で学ぶ、明日から使えるSpring Boot Tips #jsug実例で学ぶ、明日から使えるSpring Boot Tips #jsug
実例で学ぶ、明日から使えるSpring Boot Tips #jsug
 

Similar to Spring Cloud Stream with Kafka

Spark Streaming Info
Spark Streaming InfoSpark Streaming Info
Spark Streaming InfoDoug Chang
 
Cloud Foundry Meetup Stuttgart 2017 - Spring Cloud Development
Cloud Foundry Meetup Stuttgart 2017 - Spring Cloud DevelopmentCloud Foundry Meetup Stuttgart 2017 - Spring Cloud Development
Cloud Foundry Meetup Stuttgart 2017 - Spring Cloud DevelopmentAndreas Falk
 
Kafka Connect & Streams - the ecosystem around Kafka
Kafka Connect & Streams - the ecosystem around KafkaKafka Connect & Streams - the ecosystem around Kafka
Kafka Connect & Streams - the ecosystem around KafkaGuido Schmutz
 
Shipping to Server and Cloud with Docker
Shipping to Server and Cloud with DockerShipping to Server and Cloud with Docker
Shipping to Server and Cloud with DockerAtlassian
 
Aws Lambda in Swift - NSLondon - 3rd December 2020
Aws Lambda in Swift - NSLondon - 3rd December 2020Aws Lambda in Swift - NSLondon - 3rd December 2020
Aws Lambda in Swift - NSLondon - 3rd December 2020Andrea Scuderi
 
Apache Kafka - Scalable Message Processing and more!
Apache Kafka - Scalable Message Processing and more!Apache Kafka - Scalable Message Processing and more!
Apache Kafka - Scalable Message Processing and more!Guido Schmutz
 
Kafka Connect & Kafka Streams/KSQL - the ecosystem around Kafka
Kafka Connect & Kafka Streams/KSQL - the ecosystem around KafkaKafka Connect & Kafka Streams/KSQL - the ecosystem around Kafka
Kafka Connect & Kafka Streams/KSQL - the ecosystem around KafkaGuido Schmutz
 
Cloud Native Programing with Docker and Kubernetes
Cloud Native Programing with Docker and KubernetesCloud Native Programing with Docker and Kubernetes
Cloud Native Programing with Docker and KubernetesBallerina
 
Apache Kafka - Scalable Message Processing and more!
Apache Kafka - Scalable Message Processing and more!Apache Kafka - Scalable Message Processing and more!
Apache Kafka - Scalable Message Processing and more!Guido Schmutz
 
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMwareEvent Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMwareHostedbyConfluent
 
K8sfor dev parisoss-summit-microsoft-5-decembre-short
K8sfor dev parisoss-summit-microsoft-5-decembre-shortK8sfor dev parisoss-summit-microsoft-5-decembre-short
K8sfor dev parisoss-summit-microsoft-5-decembre-shortGabriel Bechara
 
Cloud native Kafka | Sascha Holtbruegge and Margaretha Erber, HiveMQ
Cloud native Kafka | Sascha Holtbruegge and Margaretha Erber, HiveMQCloud native Kafka | Sascha Holtbruegge and Margaretha Erber, HiveMQ
Cloud native Kafka | Sascha Holtbruegge and Margaretha Erber, HiveMQHostedbyConfluent
 
Real-Time Log Analysis with Apache Mesos, Kafka and Cassandra
Real-Time Log Analysis with Apache Mesos, Kafka and CassandraReal-Time Log Analysis with Apache Mesos, Kafka and Cassandra
Real-Time Log Analysis with Apache Mesos, Kafka and CassandraJoe Stein
 
Deploy made easy (even on Friday)
Deploy made easy (even on Friday)Deploy made easy (even on Friday)
Deploy made easy (even on Friday)Riccardo Bini
 
Best Practices for Middleware and Integration Architecture Modernization with...
Best Practices for Middleware and Integration Architecture Modernization with...Best Practices for Middleware and Integration Architecture Modernization with...
Best Practices for Middleware and Integration Architecture Modernization with...Claus Ibsen
 
Kafka Connect & Kafka Streams/KSQL - powerful ecosystem around Kafka core
Kafka Connect & Kafka Streams/KSQL - powerful ecosystem around Kafka coreKafka Connect & Kafka Streams/KSQL - powerful ecosystem around Kafka core
Kafka Connect & Kafka Streams/KSQL - powerful ecosystem around Kafka coreGuido Schmutz
 
How to Dockerize your Sitecore module
How to Dockerize your Sitecore moduleHow to Dockerize your Sitecore module
How to Dockerize your Sitecore moduleMihály Árvai
 

Similar to Spring Cloud Stream with Kafka (20)

Spark Streaming Info
Spark Streaming InfoSpark Streaming Info
Spark Streaming Info
 
Cloud Foundry Meetup Stuttgart 2017 - Spring Cloud Development
Cloud Foundry Meetup Stuttgart 2017 - Spring Cloud DevelopmentCloud Foundry Meetup Stuttgart 2017 - Spring Cloud Development
Cloud Foundry Meetup Stuttgart 2017 - Spring Cloud Development
 
Kafka Connect & Streams - the ecosystem around Kafka
Kafka Connect & Streams - the ecosystem around KafkaKafka Connect & Streams - the ecosystem around Kafka
Kafka Connect & Streams - the ecosystem around Kafka
 
CI/CD on AWS
CI/CD on AWSCI/CD on AWS
CI/CD on AWS
 
Shipping to Server and Cloud with Docker
Shipping to Server and Cloud with DockerShipping to Server and Cloud with Docker
Shipping to Server and Cloud with Docker
 
Aws Lambda in Swift - NSLondon - 3rd December 2020
Aws Lambda in Swift - NSLondon - 3rd December 2020Aws Lambda in Swift - NSLondon - 3rd December 2020
Aws Lambda in Swift - NSLondon - 3rd December 2020
 
Apache Kafka - Scalable Message Processing and more!
Apache Kafka - Scalable Message Processing and more!Apache Kafka - Scalable Message Processing and more!
Apache Kafka - Scalable Message Processing and more!
 
Kafka Connect & Kafka Streams/KSQL - the ecosystem around Kafka
Kafka Connect & Kafka Streams/KSQL - the ecosystem around KafkaKafka Connect & Kafka Streams/KSQL - the ecosystem around Kafka
Kafka Connect & Kafka Streams/KSQL - the ecosystem around Kafka
 
server side Swift
server side Swift server side Swift
server side Swift
 
Cloud Native Programing with Docker and Kubernetes
Cloud Native Programing with Docker and KubernetesCloud Native Programing with Docker and Kubernetes
Cloud Native Programing with Docker and Kubernetes
 
Apache Kafka - Scalable Message Processing and more!
Apache Kafka - Scalable Message Processing and more!Apache Kafka - Scalable Message Processing and more!
Apache Kafka - Scalable Message Processing and more!
 
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMwareEvent Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
 
K8sfor dev parisoss-summit-microsoft-5-decembre-short
K8sfor dev parisoss-summit-microsoft-5-decembre-shortK8sfor dev parisoss-summit-microsoft-5-decembre-short
K8sfor dev parisoss-summit-microsoft-5-decembre-short
 
Cloud native Kafka | Sascha Holtbruegge and Margaretha Erber, HiveMQ
Cloud native Kafka | Sascha Holtbruegge and Margaretha Erber, HiveMQCloud native Kafka | Sascha Holtbruegge and Margaretha Erber, HiveMQ
Cloud native Kafka | Sascha Holtbruegge and Margaretha Erber, HiveMQ
 
Apache Kafka - Strakin Technologies Pvt Ltd
Apache Kafka - Strakin Technologies Pvt LtdApache Kafka - Strakin Technologies Pvt Ltd
Apache Kafka - Strakin Technologies Pvt Ltd
 
Real-Time Log Analysis with Apache Mesos, Kafka and Cassandra
Real-Time Log Analysis with Apache Mesos, Kafka and CassandraReal-Time Log Analysis with Apache Mesos, Kafka and Cassandra
Real-Time Log Analysis with Apache Mesos, Kafka and Cassandra
 
Deploy made easy (even on Friday)
Deploy made easy (even on Friday)Deploy made easy (even on Friday)
Deploy made easy (even on Friday)
 
Best Practices for Middleware and Integration Architecture Modernization with...
Best Practices for Middleware and Integration Architecture Modernization with...Best Practices for Middleware and Integration Architecture Modernization with...
Best Practices for Middleware and Integration Architecture Modernization with...
 
Kafka Connect & Kafka Streams/KSQL - powerful ecosystem around Kafka core
Kafka Connect & Kafka Streams/KSQL - powerful ecosystem around Kafka coreKafka Connect & Kafka Streams/KSQL - powerful ecosystem around Kafka core
Kafka Connect & Kafka Streams/KSQL - powerful ecosystem around Kafka core
 
How to Dockerize your Sitecore module
How to Dockerize your Sitecore moduleHow to Dockerize your Sitecore module
How to Dockerize your Sitecore module
 

Recently uploaded

OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024Shane Coughlan
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)Max Lee
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfSrushith Repakula
 
What need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java DevelopersWhat need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java DevelopersEmilyJiang23
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfDeskTrack
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesNeo4j
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionWave PLM
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Gáspár Nagy
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdfkalichargn70th171
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfVictor Lopez
 
Naer Toolbar Redesign - Usability Research Synthesis
Naer Toolbar Redesign - Usability Research SynthesisNaer Toolbar Redesign - Usability Research Synthesis
Naer Toolbar Redesign - Usability Research Synthesisparimabajra
 
Odoo vs Shopify: Why Odoo is Best for Ecommerce Website Builder in 2024
Odoo vs Shopify: Why Odoo is Best for Ecommerce Website Builder in 2024Odoo vs Shopify: Why Odoo is Best for Ecommerce Website Builder in 2024
Odoo vs Shopify: Why Odoo is Best for Ecommerce Website Builder in 2024Primacy Infotech
 
IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024vaibhav130304
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAShane Coughlan
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1KnowledgeSeed
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAlluxio, Inc.
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAlluxio, Inc.
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Soroosh Khodami
 

Recently uploaded (20)

OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
 
Lessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdfLessons Learned from Building a Serverless Notifications System.pdf
Lessons Learned from Building a Serverless Notifications System.pdf
 
What need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java DevelopersWhat need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java Developers
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdf
 
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product UpdatesGraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
GraphSummit Stockholm - Neo4j - Knowledge Graphs and Product Updates
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion Production
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
 
Naer Toolbar Redesign - Usability Research Synthesis
Naer Toolbar Redesign - Usability Research SynthesisNaer Toolbar Redesign - Usability Research Synthesis
Naer Toolbar Redesign - Usability Research Synthesis
 
Odoo vs Shopify: Why Odoo is Best for Ecommerce Website Builder in 2024
Odoo vs Shopify: Why Odoo is Best for Ecommerce Website Builder in 2024Odoo vs Shopify: Why Odoo is Best for Ecommerce Website Builder in 2024
Odoo vs Shopify: Why Odoo is Best for Ecommerce Website Builder in 2024
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
AI Hackathon.pptx
AI                        Hackathon.pptxAI                        Hackathon.pptx
AI Hackathon.pptx
 
IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024
 
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCAOpenChain Webinar: AboutCode and Beyond - End-to-End SCA
OpenChain Webinar: AboutCode and Beyond - End-to-End SCA
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 

Spring Cloud Stream with Kafka

  • 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
  • 4. LET'S GET STARTED! [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  4 / 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
  • 7. INSTALLING KAFKA [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  7 / 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
  • 12. MAVEN DEPENDENCIES [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  12 / 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
  • 19. ... and the <repository> section: <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>http://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  19 / 60
  • 20. DEFINE THE KAFKA STREAMS package com.kaviddiss.streamkafka.stream; import org.springframework.cloud.stream.annotation.Input; import org.springframework.cloud.stream.annotation.Output; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.SubscribableChannel; [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  20 / 60
  • 21. public interface GreetingsStreams { String INPUT = "greetings-in"; String OUTPUT = "greetings-out"; @Input(INPUT) SubscribableChannel inboundGreetings(); @Output(OUTPUT) MessageChannel outboundGreetings(); } [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  21 / 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
  • 28. package com.kaviddiss.streamkafka.config; import com.kaviddiss.streamkafka.stream.GreetingsStreams; import org.springframework.cloud.stream.annotation.EnableBinding; @EnableBinding(GreetingsStreams.class) public class StreamsConfig { } [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  28 / 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
  • 33. spring: cloud: stream: kafka: binder: brokers: localhost:9092 bindings: greetings-in: destination: greetings contentType: application/json greetings-out: destination: greetings contentType: application/json [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  33 / 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
  • 40. package com.kaviddiss.streamkafka.service; import com.kaviddiss.streamkafka.model.Greetings; import com.kaviddiss.streamkafka.stream.GreetingsStreams; import lombok.extern.slf4j.Slf4j; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.support.MessageBuilder; import org.springframework.stereotype.Service; import org.springframework.util.MimeTypeUtils; [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  40 / 60
  • 41. @Service @Slf4j public class GreetingsService { private final GreetingsStreams greetingsStreams; public GreetingsService(GreetingsStreams greetingsStreams) { this.greetingsStreams = greetingsStreams; } [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  41 / 60
  • 42. public void sendGreeting(final Greetings greetings) { log.info("Sending greetings {}", greetings); MessageChannel messageChannel = greetingsStreams.outboundG messageChannel.send(MessageBuilder .withPayload(greetings) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeU .build()); } } [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  42 / 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
  • 47. package com.kaviddiss.streamkafka.web; import com.kaviddiss.streamkafka.model.Greetings; import com.kaviddiss.streamkafka.service.GreetingsService; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  47 / 60
  • 48. @RestController public class GreetingsController { private final GreetingsService greetingsService; public GreetingsController(GreetingsService greetingsService) this.greetingsService = greetingsService; } [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  48 / 60
  • 49. @GetMapping("/greetings") @ResponseStatus(HttpStatus.ACCEPTED) public void greetings(@RequestParam("message") String message Greetings greetings = Greetings.builder() .message(message) .timestamp(System.currentTimeMillis()) .build(); greetingsService.sendGreeting(greetings); } } [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  49 / 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
  • 52. package com.kaviddiss.streamkafka.service; import com.kaviddiss.streamkafka.model.Greetings; import com.kaviddiss.streamkafka.stream.GreetingsStreams; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.stream.annotation.StreamListener import org.springframework.messaging.handler.annotation.Payload; import org.springframework.stereotype.Component; [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  52 / 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
  • 57. package com.kaviddiss.streamkafka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplicati @SpringBootApplication public class StreamKafkaApplication { public static void main(String[] args) { SpringApplication.run(StreamKafkaApplication.class, args) } } [ GitPitch @ github/davidkiss/spring-cloud-streams-kafka-demo ]  57 / 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