SlideShare a Scribd company logo
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 #jsug
Toshiaki 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 Platforms
VMware 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 cloud
Ben 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 introduction
Jonathan Holloway
 
Angular Owin Katana TypeScript
Angular Owin Katana TypeScriptAngular Owin Katana TypeScript
Angular Owin Katana TypeScript
Justin 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 Cloud
Eberhard 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 Orange
acogoluegnes
 
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
Matt 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 CI
Toshiaki Maki
 
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07
マイクロサービスに必要な技術要素はすべてSpring Cloudにある #DO07
Toshiaki 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 AWS
VMware Tanzu
 
Introduction to OWIN
Introduction to OWINIntroduction to OWIN
Introduction to OWIN
Saran Doraiswamy
 
Simplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring CloudSimplify Cloud Applications using Spring Cloud
Simplify Cloud Applications using Spring Cloud
Ramnivas 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 - NoIIS
Bilal 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 #s1t
Toshiaki Maki
 
Spring ❤️ Kotlin #jjug
Spring ❤️ Kotlin #jjugSpring ❤️ Kotlin #jjug
Spring ❤️ Kotlin #jjug
Toshiaki Maki
 
Cloud Foundry for Spring Developers
Cloud Foundry for Spring DevelopersCloud Foundry for Spring Developers
Cloud Foundry for Spring Developers
Gunnar Hillert
 
実例で学ぶ、明日から使えるSpring Boot Tips #jsug
実例で学ぶ、明日から使えるSpring Boot Tips #jsug実例で学ぶ、明日から使えるSpring Boot Tips #jsug
実例で学ぶ、明日から使えるSpring Boot Tips #jsug
Toshiaki 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 Info
Doug 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 Development
Andreas 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 Kafka
Guido Schmutz
 
CI/CD on AWS
CI/CD on AWSCI/CD on AWS
CI/CD on AWS
Amazon Web Services
 
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
Atlassian
 
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
Andrea 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 Kafka
Guido Schmutz
 
server side Swift
server side Swift server side Swift
server side Swift
NormanSutorius
 
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
Ballerina
 
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, VMware
HostedbyConfluent
 
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
Gabriel 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, HiveMQ
HostedbyConfluent
 
Apache Kafka - Strakin Technologies Pvt Ltd
Apache Kafka - Strakin Technologies Pvt LtdApache Kafka - Strakin Technologies Pvt Ltd
Apache Kafka - Strakin Technologies Pvt Ltd
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
Joe 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 core
Guido Schmutz
 
How to Dockerize your Sitecore module
How to Dockerize your Sitecore moduleHow to Dockerize your Sitecore module
How to Dockerize your Sitecore module
Mihá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

KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Undress Baby
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
Hironori Washizaki
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
SOCRadar
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
pavan998932
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Envertis Software Solutions
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
aymanquadri279
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 

Recently uploaded (20)

KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024SWEBOK and Education at FUSE Okinawa 2024
SWEBOK and Education at FUSE Okinawa 2024
 
socradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdfsocradar-q1-2024-aviation-industry-report.pdf
socradar-q1-2024-aviation-industry-report.pdf
 
What is Augmented Reality Image Tracking
What is Augmented Reality Image TrackingWhat is Augmented Reality Image Tracking
What is Augmented Reality Image Tracking
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise EditionWhy Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
Why Choose Odoo 17 Community & How it differs from Odoo 17 Enterprise Edition
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
What is Master Data Management by PiLog Group
What is Master Data Management by PiLog GroupWhat is Master Data Management by PiLog Group
What is Master Data Management by PiLog Group
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 

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