Get Started Free
‹ Back to courses
course: Spring Framework and Apache Kafka®

Introduction to Spring Boot for Apache Kafka

7 min
Viktor Gamov

Viktor Gamov

Developer Advocate (Presenter)

Introduction to Spring Boot for Apache Kafka

Although Spring has been around for more than a decade, it has kept up with the times and embraces the best modern, cloud-native technologies. For example, it supports a modern version of Java and has cross-language options, such as a Kotlin integration. Spring supports the requirements of the Twelve-Factor App manifesto, meaning, for example, that you can move your applications between different environments without changing the code. And Spring also has a large community, with an extensive number of Java and JVM developers currently using it.

Adding the Spring Boot framework to Spring reduces development time and increases overall productivity. Although its opinionated configurations are actually what save you time, Spring Boot also provides the ability to extend or modify certain options if you need to customize or if its opinions don't fit with the requirements of your system.

And Spring Boot comes with a variety of included "batteries,” for example, integrations with databases and messaging systems are included. In fact, virtually any data source you have in a modern application landscape can interact with your Spring code.

This course focuses on the Spring for Apache Kafka project, which extends opinionated options for combining Spring Boot and Kafka. Spring for Apache Kafka is a top-level project that applies the Spring community's existing practices to the Kafka ecosystem. So, for example, the JmsTemplate used with the Java Message Service is manifested in KafkaTemplate, a thin layer on top of the Kafka producer API. Spring for Apache Kafka also has options that ease the configuration of Kafka with Spring Boot, and you can access them programmatically or declaratively using property files.

Generally, Spring for Apache Kafka provides features that abstract away infrastructure code so you can focus on your programming logic. In addition to the KafkaTemplate mentioned above, it provides MessageListenerContainer, which allows Spring to handle low-level communications, including retries and serialization. Spring for Apache Kafka also extends @KafkaListener, an annotation for POJOs that lets you convert a method into a Kafka consumer, and TopicBuilder, which simplifies topic configuration.

But in the end, should you use Spring or no framework at all? It's really a question about your productivity and where you want to spend your time. Spring has built-in ways of configuring applications, and because they are opinionated, they are easy to communicate with other Spring developers.

The goal of this course is to give you the tools to effectively use Spring with Confluent Cloud.

Use the promo code SPRING101 to get $25 of free Confluent Cloud usage

Get started with Confluent Cloud

Total 13

Related Resources

Be the first to get updates and new content

We will only share developer content and updates, including notifications when new content is added. We will never send you sales emails. 🙂 By subscribing, you understand we will process your personal information in accordance with our Privacy Statement.

Introduction to Spring Boot for Apache Kafka

Hi, this is Viktor Gamov with Confluent. And welcome back to this course about Spring Boot and Spring Kafka and Confluent Cloud. In the lesson number one, we'll walk you through some of the ideas behind of this integration, Spring Kafka Integration, why would one wants to use this? And what kind of things you can, you know, expect from this project. First of all, let me talk about why you want to use Spring? So first of all, Spring is a framework that provides some of the best practices in modern application development. Even though Spring was around for last, I don't know, 15 years? Still going strong and embraces some of the modern piece of technology. The current version of Spring Framework is version five, supports modern version of Java, supports even cross the language things, there is integration with Kotlin. It is quite popular and very big number of developers, Java Developers, JVM Developers use Spring these days. Spring Boot can simplify and reduced time that you spent in development and increase overall productivity because integration in some of the opinionated configurations that Spring Boot provides. It is huge help especially when you starting with something, it's much better to follow some of the predefined and maybe opinionated ways. Some people call it magic, I call it just a well-crafted piece of engineering. So that's why I personally love to use some of the defaults that Spring Boot provides. However, Spring Boot provides the ways how it can extend and modify certain things, if you need to customize or something. Some of the opinions that Spring provides will not fit with your own opinions or requirements of your system. As a modern framework, Spring embraces what's called two factor application manifesto. Essentially it allows to run your application and it change the configuration based on the environments and move the same code between different environments without changing this code. In this case, Spring supports different profiles that allows you to provide different configuration parameters based on your environment. For example, if you're in development, you will use some of the development that database connections, maybe even connection to some local hosts. If you want a QA on production environment, you definitely want to use different systems or different connection to the system that will be very close to your production. Spring comes with the variety of the batteries included: Integration with databases, integration with messaging systems, integration with the APIs, you name it. So, virtually any data source that you have in a modern application landscape will be able to interact with your Spring code. We're gonna be focusing on the project called Spring Kafka, which is sole purpose is provide you opinionated... way how you can be productive of writing your Spring Boot and Kafka Applications. Spring Kafka Project is a top level spring project that you can find this in a spring websites, spring.io/project/spring-kafka. The way how you can find documentation, get the latest artifacts, and all the things you can find there. The goal of this project, to bring the familiarities that the people already have with the Spring Boot Framework or Spring Framework and the Spring Boot into world of Kafka developers. So for example, if people use in the past some integration with messaging systems like a JMS, people tend to use things called JMS Template that allows you to publish messages into JMS queue. Spring Kafka comes with a similar pattern or similar class, similar set of APIs called Kafka Template that allows you to send the messages to Kafka topic. And this is like very foundational API and it's essentially very thin layer on top of the Kafka producer API that come for this. However, it also brings some of the integration with Spring Boot, for example, the way how you can configure this, you can do this programmatically, or can you do this declaratively using the property files. Many aspects that allows the people to write applications that will not depend on some of the intricacies, or like communication between like underlying technologies. How the message will arrive from the message queue and how it will be passed into your code. So in this case, Spring provides with the two things, it provides you MessageListenerContainer that allows Spring to handle some of the low level communication stuff including the retries, including some of the serialization, and you will just focus on just the processing logic, that's how you're supposed to do. Not the plumbing, but rather than actually implementing business logic. Spring Kafka provides integration with KafkaListener. It's KafakListener will be special annotation that you can annotate any POJO or method of the POJO, in this method will become your Kafka consumer. A lot of things that people wants to do with Kafka is they're using admin API and manipulate with topic configuration. Also Spring simplifies the way how we can define your topics, which is defined the bean with type of new topic and using Topic Builder to create the Kafka topic in a very nice and concise fashion. A very common question that I'm getting from the people all over internet, is that should I use Spring? Or should I use just like plain old... API from Kafka and do not worry about any other frameworks? So, it's always question about your productivity and where you want to spend your time. So, Spring already provides certain ways how you can configure your applications, those ways are opinionated and they will define. However, it's very easy to communicate this with other Spring developers, and it's important in the different teams if you're working on the some common project, you can easily explain how the things will be configured for your Kafka project, or you easily learn if you knew how you can do this things with Spring before. So in this case, it's always answer is or choice is yours. Should you use a Framework? Should you use Kafka? Should you use like a Kafka APIs? My goal for this presentation is give you tools and explain how you can be productive with certain frameworks. And hopefully you will pick the right choice and find this information useful. Stick around for our next session that will include exercise showing you how we can speed up and start new Spring Boot project and connect it to Confluent Cloud. My name is Victor Gamov, and as always have a nice day.