{"id":74335,"date":"2018-03-05T19:00:27","date_gmt":"2018-03-05T17:00:27","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=74335"},"modified":"2018-03-05T11:21:05","modified_gmt":"2018-03-05T09:21:05","slug":"spring-cloud-stream-kafka","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html","title":{"rendered":"Spring Cloud Stream with Kafka"},"content":{"rendered":"<h2>Overview<\/h2>\n<p>This sample project demonstrates how to build\u00a0<a href=\"https:\/\/aws.amazon.com\/streaming-data\/\" rel=\"nofollow\">real-time streaming<\/a>\u00a0applications using\u00a0<a href=\"https:\/\/thenewstack.io\/event-driven-architecture-wave-future\/\" rel=\"nofollow\">event-driven architecture<\/a>,\u00a0<a href=\"https:\/\/thenewstack.io\/event-driven-architecture-wave-future\/\" rel=\"nofollow\">Spring Boot<\/a>,Spring Cloud Stream,\u00a0<a href=\"https:\/\/kafka.apache.org\/\" rel=\"nofollow\">Apache Kafka<\/a>\u00a0and\u00a0<a href=\"https:\/\/projectlombok.org\/\" rel=\"nofollow\">Lombok<\/a>.<\/p>\n<p>By the end of this tutorial you\u2019ll have a simple Spring Boot based Greetings micro-service running that<\/p>\n<ol>\n<li>takes a message from a REST api<\/li>\n<li>writes it to a Kafka topic<\/li>\n<li>reads it from the topic<\/li>\n<li>outputs it to the console<\/li>\n<\/ol>\n<p>Let\u2019s get started!<\/p>\n<p>Btw, you\u2019ll find the source code <a href=\"https:\/\/github.com\/davidkiss\/spring-cloud-streams-kafka-demo\">here<\/a>.<\/p>\n<h2>What is Spring Cloud Streaming?<\/h2>\n<p>Spring Cloud Stream is a framework built upon Spring Boot for building message-driven microservices.<\/p>\n<h3>What is Kafka?<\/h3>\n<p>Kafka is a popular high performant and horizontally scalable messaging platform originally developed by LinkedIn.<\/p>\n<h3>Installing Kafka<\/h3>\n<p>Download Kafka from\u00a0<a href=\"https:\/\/kafka.apache.org\/downloads\" rel=\"nofollow\">here<\/a>\u00a0and untar it:<\/p>\n<pre class=\"brush:java\">&gt;\u00a0tar -xzf kafka_2.11-1.0.0.tgz\r\n&gt; cd kafka_2.11-1.0.0<\/pre>\n<p>Start Zookeeper and Kafka<\/p>\n<p>On Windows:<\/p>\n<pre class=\"brush:java\">&gt;\u00a0bin\\windows\\zookeeper-server-start.bat config\\zookeeper.properties\r\n&gt; bin\\windows\\kafka-server-start.bat config\\server.properties<\/pre>\n<p>On Linux or Mac:<\/p>\n<pre class=\"brush:java\">&gt;\u00a0bin\/zookeeper-server-start.sh config\/zookeeper.properties\r\n&gt; bin\/kafka-server-start.sh config\/server.properties<\/pre>\n<p>If Kafka is not running and fails to start after your computer wakes up from hibernation, delete the\u00a0<code>&lt;TMP_DIR&gt;\/kafka-logs<\/code>folder and then start Kafka again.<\/p>\n<h2>What is Lombok?<\/h2>\n<p>Lombok is a java framework that automatically generates getters, setters, toString(), builders, loggers, etc. in the code.<\/p>\n<h2>Maven dependencies<\/h2>\n<p>Go to\u00a0<a href=\"https:\/\/start.spring.io\/\" rel=\"nofollow\">https:\/\/start.spring.io<\/a>\u00a0to create a maven project:<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-initializr-streamkafka.png\"><img decoding=\"async\" class=\"aligncenter size-large wp-image-74344\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-initializr-streamkafka-1024x519.png\" alt=\"\" width=\"620\" height=\"314\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-initializr-streamkafka-1024x519.png 1024w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-initializr-streamkafka-300x152.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-initializr-streamkafka-768x389.png 768w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-initializr-streamkafka.png 1270w\" sizes=\"(max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<ol>\n<li>Add necessary dependencies:\u00a0<code>Spring Cloud Stream<\/code>,\u00a0<code>Kafka<\/code>,\u00a0<code>Devtools<\/code>\u00a0(for hot redeploys during development, optional),\u00a0<code>Actuator<\/code>\u00a0(for monitoring application, optional),\u00a0<code>Lombok<\/code>\u00a0(make sure to also have the Lombok plugin installed in your IDE)<\/li>\n<li>Click the Generate Project button to download the project as a zip file<\/li>\n<li>Extract zip file and import the maven project to your favourite IDE<\/li>\n<\/ol>\n<p>Notice the maven dependencies in the\u00a0<code>pom.xml<\/code>\u00a0file:<\/p>\n<pre class=\"brush:xml\">&lt;dependency&gt;\r\n      &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;spring-boot-starter-actuator&lt;\/artifactId&gt;\r\n  &lt;\/dependency&gt;\r\n  &lt;dependency&gt;\r\n      &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;spring-cloud-stream&lt;\/artifactId&gt;\r\n  &lt;\/dependency&gt;\r\n  &lt;dependency&gt;\r\n      &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;spring-cloud-starter-stream-kafka&lt;\/artifactId&gt;\r\n  &lt;\/dependency&gt;\r\n  &lt;!-- Also install the Lombok plugin in your IDE --&gt;\r\n  &lt;dependency&gt;\r\n      &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;lombok&lt;\/artifactId&gt;\r\n      &lt;optional&gt;true&lt;\/optional&gt;\r\n  &lt;\/dependency&gt;\r\n\r\n  &lt;!-- hot reload - press Ctrl+F9 in IntelliJ after a code change while application is running --&gt;\r\n  &lt;dependency&gt;\r\n      &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;spring-boot-devtools&lt;\/artifactId&gt;\r\n      &lt;optional&gt;true&lt;\/optional&gt;\r\n  &lt;\/dependency&gt;<\/pre>\n<p>\u2026 also the\u00a0<code>&lt;dependencyManagement&gt;<\/code>\u00a0section:<\/p>\n<pre class=\"brush:xml\">&lt;dependencyManagement&gt;\r\n\u00a0 &lt;dependencies&gt;\r\n\u00a0 \u00a0 &lt;dependency&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;!-- Import dependency management from Spring Boot --&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;artifactId&gt;spring-boot-dependencies&lt;\/artifactId&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;version&gt;${spring-boot.version}&lt;\/version&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;type&gt;pom&lt;\/type&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;scope&gt;import&lt;\/scope&gt;\r\n\u00a0 \u00a0 &lt;\/dependency&gt;\r\n\u00a0 \u00a0 &lt;dependency&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;artifactId&gt;spring-cloud-stream-dependencies&lt;\/artifactId&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;version&gt;${spring-cloud-stream.version}&lt;\/version&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;type&gt;pom&lt;\/type&gt;\r\n\u00a0 \u00a0 \u00a0 &lt;scope&gt;import&lt;\/scope&gt;\r\n\u00a0 \u00a0 &lt;\/dependency&gt;\r\n\u00a0 &lt;\/dependencies&gt;\r\n    &lt;\/dependencyManagement&gt;<\/pre>\n<p>\u2026 and the\u00a0<code>&lt;repository&gt;<\/code>\u00a0section:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\"brush:xml\">&lt;repository&gt;\r\n\u00a0 &lt;id&gt;spring-milestones&lt;\/id&gt;\r\n\u00a0 &lt;name&gt;Spring Milestones&lt;\/name&gt;\r\n\u00a0 &lt;url&gt;http:\/\/repo.spring.io\/libs-milestone&lt;\/url&gt;\r\n\u00a0 &lt;snapshots&gt;\r\n\u00a0 \u00a0 &lt;enabled&gt;false&lt;\/enabled&gt;\r\n\u00a0 &lt;\/snapshots&gt;\r\n&lt;\/repository&gt;<\/pre>\n<h2>Define the Kafka streams<\/h2>\n<pre class=\"brush:java\">package com.kaviddiss.streamkafka.stream;\r\n\r\nimport org.springframework.cloud.stream.annotation.Input;\r\nimport org.springframework.cloud.stream.annotation.Output;\r\nimport org.springframework.messaging.MessageChannel;\r\nimport org.springframework.messaging.SubscribableChannel;\u00a0 \r\n\r\npublic interface GreetingsStreams {\r\n    String INPUT = \"greetings-in\";\r\n    String OUTPUT = \"greetings-out\";\r\n\r\n    @Input(INPUT)\r\n    SubscribableChannel inboundGreetings();\r\n\r\n    @Output(OUTPUT)\r\n    MessageChannel outboundGreetings();\r\n}<\/pre>\n<p>In order for our application to be able to communicate with Kafka, we\u2019ll need to define an outbound stream to write messages to a Kafka topic, and an inbound stream to read messages from a Kafka topic.<\/p>\n<p>Spring Cloud provides a convenient way to do this by simply creating an interface that defines a separate method for each stream.<\/p>\n<p>The\u00a0<code>inboundGreetings()<\/code>\u00a0method defines the inbound stream to read from Kafka and\u00a0<code>outboundGreetings()<\/code>\u00a0method defines the outbound stream to write to Kafka.<\/p>\n<p>During runtime Spring will create a java proxy based implementation of the\u00a0<code>GreetingsStreams<\/code>\u00a0interface that can be injected as a Spring Bean anywhere in the code to access our two streams.<\/p>\n<h2>Configure Spring Cloud Stream<\/h2>\n<p>Our next step is to configure Spring Cloud Stream to bind to our streams in the\u00a0<code>GreetingsStreams<\/code>\u00a0interface. This can be done by creating a\u00a0<code>@Configuration<\/code>\u00a0class\u00a0<code>com.kaviddiss.streamkafka.config.StreamsConfig<\/code>\u00a0with below code:<\/p>\n<pre class=\"brush:java\">package com.kaviddiss.streamkafka.config;\r\n\r\nimport com.kaviddiss.streamkafka.stream.GreetingsStreams;\r\nimport org.springframework.cloud.stream.annotation.EnableBinding;\r\n\r\n@EnableBinding(GreetingsStreams.class)\r\npublic class StreamsConfig {\r\n}<\/pre>\n<p>Binding the streams is done using the\u00a0<code>@EnableBinding<\/code>\u00a0annotation where the\u00a0<code>GreatingsService<\/code>\u00a0interface is passed to.<\/p>\n<h2>Configuration properties for Kafka<\/h2>\n<p>By default, the configuration properties are stored in the\u00a0<code>src\/main\/resources\/application.properties<\/code>\u00a0file.<\/p>\n<p>However I prefer to use the YAML format as it\u2019s less verbose and allows to keep both common and environment-specific properties in the same file.<\/p>\n<p>For now, let\u2019s rename\u00a0<code>application.properties<\/code>\u00a0to\u00a0<code>application.yaml<\/code>\u00a0and paste below config snippet into the file:<\/p>\n<pre class=\"brush:java\">spring:\r\n  cloud:\r\n    stream:\r\n      kafka:\r\n        binder:\r\n          brokers: localhost:9092\r\n      bindings:\r\n        greetings-in:\r\n          destination: greetings\r\n          contentType: application\/json\r\n        greetings-out:\r\n          destination: greetings\r\n          contentType: application\/json<\/pre>\n<p>The above configuration properties configure 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!<\/p>\n<p>The\u00a0<code>contentType<\/code>\u00a0properties tell Spring Cloud Stream to send\/receive our message objects as\u00a0<code>String<\/code>s in the streams.<\/p>\n<h2>Create the message object<\/h2>\n<p>Create a simple\u00a0<code>com.kaviddiss.streamkafka.model.Greetings<\/code>\u00a0class with below code that will represent the message object we read from and write to the\u00a0<code>greetings<\/code>\u00a0Kafka topic:<\/p>\n<pre class=\"brush:java\">package com.kaviddiss.streamkafka.model;\r\n\r\n\/\/ lombok autogenerates getters, setters, toString() and a builder (see https:\/\/projectlombok.org\/):\r\nimport lombok.Builder;\r\nimport lombok.Getter;\r\nimport lombok.Setter;\r\nimport lombok.ToString;\r\n\r\n@Getter @Setter @ToString @Builder\r\npublic class Greetings {\r\n    private long timestamp;\r\n    private String message;\r\n}<\/pre>\n<p>Notice how the class doesn\u2019t have any getters and setters thanks to the Lombok annotations. The\u00a0<code>@ToString<\/code>\u00a0will generate a\u00a0<code>toString()<\/code>\u00a0method using the class\u2019 fields and the\u00a0<code>@Builder<\/code>\u00a0annotation will allow us creating\u00a0<code>Greetings<\/code>\u00a0objects using fluent builder (see below).<\/p>\n<h2>Create service layer to write to Kafka<\/h2>\n<p>Let\u2019s create the\u00a0<code>com.kaviddiss.streamkafka.service.GreetingsService<\/code>\u00a0class with below code that will write a\u00a0<code>Greetings<\/code>object to the\u00a0<code>greetings<\/code>\u00a0Kafka topic:<\/p>\n<pre class=\"brush:java\">package com.kaviddiss.streamkafka.service;\r\n\r\nimport com.kaviddiss.streamkafka.model.Greetings;\r\nimport com.kaviddiss.streamkafka.stream.GreetingsStreams;\r\nimport lombok.extern.slf4j.Slf4j;\r\nimport org.springframework.messaging.MessageChannel;\r\nimport org.springframework.messaging.MessageHeaders;\r\nimport org.springframework.messaging.support.MessageBuilder;\r\nimport org.springframework.stereotype.Service;\r\nimport org.springframework.util.MimeTypeUtils;\r\n\r\n@Service\r\n@Slf4j\r\npublic class GreetingsService {\r\n    private final GreetingsStreams greetingsStreams;\r\n\r\n    public GreetingsService(GreetingsStreams greetingsStreams) {\r\n        this.greetingsStreams = greetingsStreams;\r\n    }\r\n\r\n    public void sendGreeting(final Greetings greetings) {\r\n        log.info(\"Sending greetings {}\", greetings);\r\n\r\n        MessageChannel messageChannel = greetingsStreams.outboundGreetings();\r\n        messageChannel.send(MessageBuilder\r\n                .withPayload(greetings)\r\n                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)\r\n                .build());\r\n    }<\/pre>\n<p>The\u00a0<code>@Service<\/code>\u00a0annotation will configure this class as a Spring Bean and inject the\u00a0<code>GreetingsService<\/code>\u00a0dependency via the constructor.<\/p>\n<p>The\u00a0<code>@Slf4j<\/code>\u00a0annotation will generate an SLF4J logger field that we can use for logging.<\/p>\n<p>In the\u00a0<code>sendGreeting()<\/code>\u00a0method we use the injected\u00a0<code>GreetingsStream<\/code>\u00a0object to send a message represented by the\u00a0<code>Greetings<\/code>\u00a0object.<\/p>\n<h2>Create REST api<\/h2>\n<p>Now we\u2019ll be creating a REST api endpoint that will trigger sending a message to Kafka using the\u00a0<code>GreetingsService<\/code>\u00a0Spring Bean:<\/p>\n<pre class=\"brush:java\">package com.kaviddiss.streamkafka.web;\r\n\r\nimport com.kaviddiss.streamkafka.model.Greetings;\r\nimport com.kaviddiss.streamkafka.service.GreetingsService;\r\nimport org.springframework.http.HttpStatus;\r\nimport org.springframework.web.bind.annotation.GetMapping;\r\nimport org.springframework.web.bind.annotation.RequestParam;\r\nimport org.springframework.web.bind.annotation.ResponseStatus;\r\nimport org.springframework.web.bind.annotation.RestController;\u00a0\r\n\r\n@RestController\r\npublic class GreetingsController {\r\n    private final GreetingsService greetingsService;\r\n\r\n    public GreetingsController(GreetingsService greetingsService) {\r\n        this.greetingsService = greetingsService;\r\n    }\r\n\r\n    @GetMapping(\"\/greetings\")\r\n    @ResponseStatus(HttpStatus.ACCEPTED)\r\n    public void greetings(@RequestParam(\"message\") String message) {\r\n        Greetings greetings = Greetings.builder()\r\n            .message(message)\r\n            .timestamp(System.currentTimeMillis())\r\n            .build();\r\n\r\n        greetingsService.sendGreeting(greetings);\r\n    }\r\n}<\/pre>\n<p>The\u00a0<code>@RestController<\/code>\u00a0annotation tells Spring that this is a Controller bean (the C from MVC). The\u00a0<code>greetings()<\/code>\u00a0method defines an\u00a0<code>HTTP GET \/greetings<\/code>\u00a0endpoint that takes a\u00a0<code>message<\/code>\u00a0request param and passes it to the\u00a0<code>sendGreeting()<\/code>\u00a0method in\u00a0<code>GreetingsService<\/code>.<\/p>\n<h2>Listening on the greetings Kafka topic<\/h2>\n<p>Let\u2019s create a\u00a0<code>com.kaviddiss.streamkafka.service.GreetingsListener<\/code>\u00a0class that will listen to messages on the\u00a0<code>greetings<\/code>Kafka topic and log them on the console:<\/p>\n<pre class=\"brush:java\">package com.kaviddiss.streamkafka.service;\r\n\r\nimport com.kaviddiss.streamkafka.model.Greetings;\r\nimport com.kaviddiss.streamkafka.stream.GreetingsStreams;\r\nimport lombok.extern.slf4j.Slf4j;\r\nimport org.springframework.cloud.stream.annotation.StreamListener;\r\nimport org.springframework.messaging.handler.annotation.Payload;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\n@Slf4j\r\npublic class GreetingsListener {\r\n    @StreamListener(GreetingsStreams.INPUT)\r\n    public void handleGreetings(@Payload Greetings greetings) {\r\n        log.info(\"Received greetings: {}\", greetings);\r\n    }\r\n}<\/pre>\n<p>The\u00a0<code>@Component<\/code>\u00a0annotation similarly to\u00a0<code>@Service<\/code>\u00a0and\u00a0<code>@RestController<\/code>\u00a0defines a Spring Bean.<\/p>\n<p><code>GreetingsListener<\/code>\u00a0has a single method,\u00a0<code>handleGreetings()<\/code>\u00a0that will be invoked by Spring Cloud Stream with every new\u00a0<code>Greetings<\/code>\u00a0message object on the\u00a0<code>greetings<\/code>\u00a0Kafka topic. This is thanks to the\u00a0<code>@StreamListener<\/code>\u00a0annotation configured for the\u00a0<code>handleGreetings()<\/code>\u00a0method.<\/p>\n<h2>Running the application<\/h2>\n<p>The last piece of the puzzle is the\u00a0<code>com.kaviddiss.streamkafka.StreamKafkaApplication<\/code>\u00a0class that was auto-generated by the Spring Initializer:<\/p>\n<pre class=\"brush:java\">package com.kaviddiss.streamkafka;\r\n\r\nimport org.springframework.boot.SpringApplication;\r\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\r\n\r\n@SpringBootApplication\r\npublic class StreamKafkaApplication {\r\n\r\n    public static void main(String[] args) {\r\n        SpringApplication.run(StreamKafkaApplication.class, args);\r\n    }\r\n}<\/pre>\n<p>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:<\/p>\n<pre class=\"brush:java\">&gt;\u00a0mvn spring-boot:run<\/pre>\n<p>Once the application is running, go to\u00a0<a href=\"http:\/\/localhost:8080\/greetings?message=hello\" rel=\"nofollow\">http:\/\/localhost:8080\/greetings?message=hello<\/a>\u00a0in the browser and check your console.<\/p>\n<h2>Summary<\/h2>\n<p>I hope you enjoyed this tutorial. Feel free to ask any questions and leave your feedback.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>Published on Java Code Geeks with permission by David Kiss, partner at our <a href=\"\/\/www.javacodegeeks.com\/join-us\/jcg\/\" target=\"_blank\" rel=\"noopener\">JCG program<\/a>. See the original article here: <a href=\"http:\/\/kaviddiss.com\/2018\/03\/03\/spring-cloud-stream-kafka\/\" target=\"_blank\" rel=\"noopener\">Spring Cloud Stream with Kafka<\/a><\/p>\n<p>Opinions expressed by Java Code Geeks contributors are their own.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Overview This sample project demonstrates how to build\u00a0real-time streaming\u00a0applications using\u00a0event-driven architecture,\u00a0Spring Boot,Spring Cloud Stream,\u00a0Apache Kafka\u00a0and\u00a0Lombok. By the end of this tutorial you\u2019ll have a simple Spring Boot based Greetings micro-service running that takes a message from a REST api writes it to a Kafka topic reads it from the topic outputs it to the console &hellip;<\/p>\n","protected":false},"author":948,"featured_media":240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[1349,30,992],"class_list":["post-74335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-kafka","tag-spring","tag-spring-cloud"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Cloud Stream with Kafka - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Overview This sample project demonstrates how to build\u00a0real-time streaming\u00a0applications using\u00a0event-driven architecture,\u00a0Spring Boot,Spring Cloud\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Cloud Stream with Kafka - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Overview This sample project demonstrates how to build\u00a0real-time streaming\u00a0applications using\u00a0event-driven architecture,\u00a0Spring Boot,Spring Cloud\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-05T17:00:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"David Kiss\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@kaviddiss\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"David Kiss\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html\"},\"author\":{\"name\":\"David Kiss\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/6fcb2b9861b8ea973df5450b2de662c6\"},\"headline\":\"Spring Cloud Stream with Kafka\",\"datePublished\":\"2018-03-05T17:00:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html\"},\"wordCount\":907,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"keywords\":[\"Kafka\",\"Spring\",\"Spring Cloud\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html\",\"name\":\"Spring Cloud Stream with Kafka - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"datePublished\":\"2018-03-05T17:00:27+00:00\",\"description\":\"Overview This sample project demonstrates how to build\u00a0real-time streaming\u00a0applications using\u00a0event-driven architecture,\u00a0Spring Boot,Spring Cloud\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"spring-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/spring-cloud-stream-kafka.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Cloud Stream with Kafka\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/6fcb2b9861b8ea973df5450b2de662c6\",\"name\":\"David Kiss\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c4ea7467fedb1c8d2ad2d88158316ca2d30960ea3eb57d8da000b30f82f57bf3?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c4ea7467fedb1c8d2ad2d88158316ca2d30960ea3eb57d8da000b30f82f57bf3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c4ea7467fedb1c8d2ad2d88158316ca2d30960ea3eb57d8da000b30f82f57bf3?s=96&d=mm&r=g\",\"caption\":\"David Kiss\"},\"description\":\"David is a passionate software engineer specializing in building scalable web-applications using Java, Spring, Hibernate and Apache Camel. He is based in Toronto, Canada and is the founder of David Andras Consulting Ltd.\",\"sameAs\":[\"http:\\\/\\\/kaviddiss.com\\\/\",\"https:\\\/\\\/ca.linkedin.com\\\/in\\\/davidkiss\",\"https:\\\/\\\/x.com\\\/kaviddiss\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/david-kiss\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Cloud Stream with Kafka - Java Code Geeks","description":"Overview This sample project demonstrates how to build\u00a0real-time streaming\u00a0applications using\u00a0event-driven architecture,\u00a0Spring Boot,Spring Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html","og_locale":"en_US","og_type":"article","og_title":"Spring Cloud Stream with Kafka - Java Code Geeks","og_description":"Overview This sample project demonstrates how to build\u00a0real-time streaming\u00a0applications using\u00a0event-driven architecture,\u00a0Spring Boot,Spring Cloud","og_url":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-03-05T17:00:27+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","type":"image\/jpeg"}],"author":"David Kiss","twitter_card":"summary_large_image","twitter_creator":"@kaviddiss","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"David Kiss","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html"},"author":{"name":"David Kiss","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/6fcb2b9861b8ea973df5450b2de662c6"},"headline":"Spring Cloud Stream with Kafka","datePublished":"2018-03-05T17:00:27+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html"},"wordCount":907,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","keywords":["Kafka","Spring","Spring Cloud"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html","url":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html","name":"Spring Cloud Stream with Kafka - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","datePublished":"2018-03-05T17:00:27+00:00","description":"Overview This sample project demonstrates how to build\u00a0real-time streaming\u00a0applications using\u00a0event-driven architecture,\u00a0Spring Boot,Spring Cloud","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","width":150,"height":150,"caption":"spring-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-cloud-stream-kafka.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Cloud Stream with Kafka"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/6fcb2b9861b8ea973df5450b2de662c6","name":"David Kiss","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c4ea7467fedb1c8d2ad2d88158316ca2d30960ea3eb57d8da000b30f82f57bf3?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c4ea7467fedb1c8d2ad2d88158316ca2d30960ea3eb57d8da000b30f82f57bf3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c4ea7467fedb1c8d2ad2d88158316ca2d30960ea3eb57d8da000b30f82f57bf3?s=96&d=mm&r=g","caption":"David Kiss"},"description":"David is a passionate software engineer specializing in building scalable web-applications using Java, Spring, Hibernate and Apache Camel. He is based in Toronto, Canada and is the founder of David Andras Consulting Ltd.","sameAs":["http:\/\/kaviddiss.com\/","https:\/\/ca.linkedin.com\/in\/davidkiss","https:\/\/x.com\/kaviddiss"],"url":"https:\/\/www.javacodegeeks.com\/author\/david-kiss"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/74335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/948"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=74335"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/74335\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=74335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=74335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=74335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}