Java Spring Boot REST Server with Redis and Kafka

🕓 40 minutes

What you’ll learn

How to setup your application for :

  • connecting to Redis,
  • connecting to Kafka and publishing messages to its topic,
  • getting data from REST API,
  • providing data to REST API.

In this tutorial, we will create a simple java component with the Java Spring Boot scaffolder. We want to expose a single REST endpoint for getting user authorization roles. As roles are stored in Redis key-value store, we need a client configuration for our component. Any access to information about user roles should be logged in a Kafka topic, so we need a Kafka client configuration too.

clientAuthorizationService

Project source

This example project can be cloned from: [email protected]:innobank/aia-spring-auth-service.git

Prerequisites

  • Prepare your local development environment for CodeNOW with Spring Boot.
  • Run Kafka and Redis locally.
    • You can run PostgreSQL directly or using docker compose.
    • The configuration file for docker-compose can be downloaded from the link that can be found in the section Docker compose and third-party tools of the Java Spring Boot Local Development tutorial.
  • Create a new component

Steps

Open your IDE, import the created component and start coding:

  • Define the message payload. Here is an example of UserAuthorizationResponse, which is a simple POJO with user roles:

    • generate getters and setters with your IDE

      01 package io.codenow.client.authorization.service.model;
      02
      03 import java.util.Set;
      04
      05 public class UserAuthorizationResponse {
      06
      07 private Set<String> roles;
      08
      09 //getters and setters...
      10 }
  • Next prepare the configuration for the kafka logging client:

    • Go to the Kafka administration console (http://localhost:9000 if using kafdrop from our Local development manual) and create a new topic client-logging

    • Add maven dependency to your pom.xml

      01 <dependency>
      02 <groupId>org.springframework.kafka</groupId>
      03 <artifactId>spring-kafka</artifactId>
      04 <version>2.4.5.RELEASE</version>
      05 </dependency>
  • For more details about Spring Boot Kafka, see: https://spring.io/projects/spring-kafka

  • Now add this code below to your Application.class to create bean configuration for kafka:

    01 @Value("${kafka.broker.url}") private String kafkaBrokerUrl;
    02
    03 @Bean
    04 RouterFunction<ServerResponse> routerFunction() {
    05 return route(GET("/swagger/index.html"), req ->
    06 ServerResponse.temporaryRedirect(URI.create("/swagger-ui/")).build());
    07 }
    08
    09 @Bean
    10 WebFilter traceResponseFilter() {
    11 return new TraceResponseFilter();
    12 }
    13
    14 @Bean
    15 public ProducerFactory<String, String> producerFactory() {
    16 return new DefaultKafkaProducerFactory<>(producerConfigs());
    17 }
    18
    19 @Bean
    20 public Map<String, Object> producerConfigs() {
    21 Map<String, Object> props = new HashMap<>();
    22 props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBrokerUrl);
    23 props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    24 props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    25 // See https://kafka.apache.org/documentation/#producerconfigs for more properties
    26 return props;
    27 }
    28
    29 @Bean
    30 public KafkaTemplate<String, String> kafkaTemplate() {
    31 return new KafkaTemplate<String, String>(producerFactory());
    32 }
  • Next prepare the configuration for the Redis client:

    • For more details about Spring Boot Redis, see: https://spring.io/projects/spring-data-redis

    • Add maven dependency to your pom.xml.

    • These dependencies are the starter for using Redis key-value data store with Spring Data Redis and the Lettuce client

      01 <dependency>
      02 <groupId>org.springframework.boot</groupId>
      03 <artifactId>spring-boot-starter-data-redis</artifactId>
      04 <version>2.3.3.RELEASE</version>
      05 </dependency>
  • Create a new configuration class for redis RedisConfig.class:

    01 import io.lettuce.core.RedisClient;
    02 import io.lettuce.core.RedisURI;
    03 import io.lettuce.core.api.StatefulRedisConnection;
    04 import io.lettuce.core.api.sync.RedisCommands;
    05 import org.springframework.beans.factory.annotation.Value;
    06 import org.springframework.context.annotation.Bean;
    07 import org.springframework.context.annotation.Configuration;
    08 import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
    09
    10 @Configuration
    11 @EnableRedisRepositories
    12 public class RedisConfig {
    13
    14 @Value("${spring.redis.url}")
    15 private String uri;
    16
    17 @Bean
    18 public RedisCommands connectionFactory() {
    19
    20 RedisURI redisURI = RedisURI.create(uri);
    21 RedisClient redisClient = RedisClient.create(redisURI);
    22 StatefulRedisConnection<String, String> redisConnection = redisClient.connect();
    23 return redisConnection.sync();
    24 }
    25 }
  • Create a new controller and put all the parts together

    • For more details about Spring Boot controller, see: https://spring.io/guides/gs/rest-service/

      01 import io.lettuce.core.api.sync.RedisCommands;
      02 import org.example.service.model.UserAuthorizationResponse;
      03 import org.slf4j.Logger;
      04 import org.slf4j.LoggerFactory;
      05 import org.springframework.beans.factory.annotation.Autowired;
      06 import org.springframework.beans.factory.annotation.Value;
      07 import org.springframework.kafka.core.KafkaTemplate;
      08 import org.springframework.web.bind.annotation.*;
      09
      10 import java.util.List;
      11 import java.util.TreeSet;
      12
      13 @RestController
      14 @RequestMapping("/user")
      15 public class UserAuthorizationController {
      16 Logger log = LoggerFactory.getLogger(UserAuthorizationController.class);
      17
      18 @Autowired
      19 private RedisCommands<String,String> commands;
      20
      21 private String kafkaTopicName;
      22 private String kafkaTopicKey;
      23 private KafkaTemplate<String, String> kafkaTemplate;
      24
      25 public UserAuthorizationController(@Value("${kafka.topic.name}") String kafkaTopicName,
      26 KafkaTemplate<String, String> kafkaTemplate,
      27 @Value("${kafka.topic.key}") String kafkaTopicKey)
      28 {
      29 this.kafkaTopicName = kafkaTopicName;
      30 this.kafkaTopicKey = kafkaTopicKey;
      31 this.kafkaTemplate = kafkaTemplate;
      32 }
      33
      34 @GetMapping("/{username}")
      35 private UserAuthorizationResponse user(@PathVariable String username) {
      36 log.info("Get data for username: {}", username);
      37 kafkaTemplate.send(kafkaTopicName, kafkaTopicKey, username);
      38
      39 final UserAuthorizationResponse response = new UserAuthorizationResponse();
      40 List<String> privileges = commands.lrange(username, 0L, 1000L);
      41 response.setRoles(new TreeSet<>(privileges));
      42 return response;
      43 }
      44 }
  • Last but not least, append the configuration for Kafka and Redis to config/application.yaml

    • Note that this configuration depends on your local development setup for Kafka and Redis and can be different case-by-case

    • Make sure you follow yaml syntax (especially whitespaces)

      01 spring:
      01 redis:
      02 url: redis://localhost:6379
      03
      04 kafka:
      05 broker:
      06 servers: localhost:29092
      07 topic:
      08 name: client-logging
      09 key: client-authorization-service
  • Try to build and run the application in your IDE. After startup, you should be able to access your new controller’s swagger: http://localhost:8080/swagger/index.html

ClientAuthorizationServiceSwagger

Deploy to CodeNOW

If your code works in the local development, you are ready to push your changes to GIT and try to build and deploy your new component version to the CodeNOW environment.

What’s next?

See our other developer tutorials: