Sunday, April 30, 2023
HomeProgrammingIntroducing Kafka to Spring Boot

Introducing Kafka to Spring Boot


Until now in our earlier chapters, we’ve used the official libraries and the APIs supplied by Apache Kafka. We have now built-in these libraries in plain outdated Java and outlined our use-cases. However now going ahead in all our upcoming chapters, we can be utilizing Spring Boot and attempt to add the related libraries to combine Kafka.

Introducing Spring Boot

Spring Boot is an open-source framework developed and maintained by firm referred to as Pivotal. It gives a platform for Java builders to develop a stand-alone, auto-configurable, production-grade spring utility the place we will deal with our enterprise implementation than the low-level logic. Spring Boot can also be opinionated because it decides the default values for our configuration. It additionally takes care of the underlying packages and libraries to put in for the options or the dependencies we require.

Spring Boot is designed to serve the next objectives:

  • To host and develop a production-grade code fairly simply.
  • To cut back the event time and run the applying impartial of infrastructure and framework.
  • To keep away from advanced XML configurations supplied by Spring.
  • To offer a versatile technique to outline Java beans and database transactions.
  • To offer highly effective batch processing and handle REST APIs.
  • To ease dependency administration and embody Embedded Servlet Container.

Having mentioned that, let’s simply rapidly bounce into our improvement and begin defining our dependencies to publish and devour Kafka messages.

Kafka Dependencies for Spring Boot

In our earlier chapters, we have already got our Kafka cluster occasion configured and hosted. We’ll outline or configure our Kafka consumer as a part of our Spring Boot. Now, to be able to rapidly begin with our setup, we have to open Spring Initializr and move the next dependencies:

  • Spring Net – to incorporate Spring MVC part and embedded Tomcat.
  • Spring for Apache Kafka – to host and handle Kafka consumer.
  • Lombok – a comfort library that helps us scale back boilerplate code comparable to getters, setters and constructors.

The generated challenge has the next dependencies in pom.xml:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.kafka</groupId>
	<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optionally available>true</optionally available>
</dependency>

Now, as per the objectives of Spring Boot, let’s rapidly auto-configure the default opinionated parts of Spring Boot and begin our utility. We have now already outlined our dependencies. Let’s outline a few of the values to move the Kafka particulars and override the default config in utility.yml:

server:
  port: 8080
spring:
  kafka:
    shopper:
      bootstrap-servers: localhost:9092
      auto-offset-reset: earliest
      key-deserializer: org.apache.kafka.widespread.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.widespread.serialization.StringDeserializer
    producer:
      bootstrap-servers: localhost:9092
      key-serializer: org.apache.kafka.widespread.serialization.StringSerializer
      value-serializer: org.apache.kafka.widespread.serialization.StringSerializer

Right here we’re defining the Kafka dealer particulars as a part of bootstrap-servers and the serializer and deserializer to print/intercept the messages.

Sending a Easy Kafka Message

Let’s implement our logic to publish few fast pattern messages to one in all our matter. To start with, we’ll create a subject in Kafka by executing the next command:

bin/kafka-topics.sh 
    --create 
    --zookeeper localhost:2181 
    --replication-factor 3 
    --partitions 3 
    --topic take a look at 
    --config min.insync.replicas=2

Now we have to merely autowire the KafkaTemplate bean and use it to ship messages. The KafkaTemplate wraps a producer and gives varied comfort strategies to ship knowledge to Kafka subjects.

@Autowired
non-public KafkaTemplate<String, String> kafkaTemplate;

@PostConstruct
public void sendMessage() {
	String message = "Hiya World";
	log.information(String.format("Message despatched -> %s", message));
	this.kafkaTemplate.ship("take a look at", message);
}

Lastly, to be able to automate our course of, we’ve annotated our sendMessage with @PostConstruct annotation in order that this technique will be referred to as instantly after our utility is began/initiated.

That’s it after this we will see that the “Hiya World” message is getting revealed in take a look at matter.

Receiving the identical Kafka Message

Now, as soon as we’ve revealed the message into the Kafka matter, we should learn the identical message and print it in our console log. With a view to that we will merely outline our shopper technique and annotate with @KafkaListener. The @KafkaListener annotation is used to designate a bean technique as a listener for a listener container.

@KafkaListener(subjects = "take a look at",	groupId = "group-id")
public void devour(String message)	{
	log.information(String.format("Message acquired -> %s", message));
}

As we will see that the Spring Boot is utilizing the above configurations outlined as a part of utility.yml and intercept or decode the messages based mostly upon the serializers and the deserializers outlined.

Conclusion

As a part of this chapter, we rapidly carried out a Spring code to publish or devour messages from Kafka very simply. In our subsequent chapter we’re going to deep-dive into every of the parts supplied by Spring and construct our personal use-cases.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments