Introducing Kafka to Spring Boot

[ad_1]

Until now in our earlier chapters, we’ve got used the official libraries and the APIs offered by Apache Kafka. We now have built-in these libraries in plain outdated Java and outlined our use-cases. However now going ahead in all our upcoming chapters, we will likely 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 are able to 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 scale back the event time and run the appliance unbiased of infrastructure and framework.
  • To keep away from complicated XML configurations offered by Spring.
  • To supply a versatile technique to outline Java beans and database transactions.
  • To supply highly effective batch processing and handle REST APIs.
  • To ease dependency administration and embody Embedded Servlet Container.

Having mentioned that, let’s simply rapidly soar 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 shopper as a part of our Spring Boot. Now, with a view to rapidly begin with our setup, we have to open Spring Initializr and go the next dependencies:

Spring Initializr Kafka

  • Spring Net – to incorporate Spring MVC part and embedded Tomcat.
  • Spring for Apache Kafka – to host and handle Kafka shopper.
  • Lombok – a comfort library that helps us cut back boilerplate code equivalent 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 now have already outlined our dependencies. Let’s outline a few of the values to go the Kafka particulars and override the default config in utility.yml:

server:
  port: 8080
spring:
  kafka:
    client:
      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 considered one of our matter. To begin with, we are going to 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 numerous comfort strategies to ship knowledge to Kafka matters.

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

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

Lastly, with a view to automate our course of, we’ve got annotated our sendMessage with @PostConstruct annotation in order that this methodology may be referred to as instantly after our utility is began/initiated.

That’s it after this we are able to see that the “Howdy World” message is getting printed in take a look at matter.

Receiving the identical Kafka Message

Now, as soon as we’ve got printed the message into the Kafka matter, we should learn the identical message and print it in our console log. With a purpose to that we are able to merely outline our client methodology and annotate with @KafkaListener. The @KafkaListener annotation is used to designate a bean methodology as a listener for a listener container.

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

As we are able to 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 applied 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 offered by Spring and construct our personal use-cases.

[ad_2]

Leave a Comment

Your email address will not be published. Required fields are marked *