Spring - Redis
Overview
Spring boot application integration with redis for messaging & data persistence.
Github: https://github.com/gitorko/project56
Redis
Redis can be used as an in-memory data store, database, cache, streaming engine, and message broker.
Code
1package com.demo.project56.config;
2
3import com.demo.project56.service.MessageListener;
4import org.springframework.context.annotation.Bean;
5import org.springframework.context.annotation.Configuration;
6import org.springframework.data.redis.connection.RedisConnectionFactory;
7import org.springframework.data.redis.core.RedisTemplate;
8import org.springframework.data.redis.core.StringRedisTemplate;
9import org.springframework.data.redis.listener.ChannelTopic;
10import org.springframework.data.redis.listener.PatternTopic;
11import org.springframework.data.redis.listener.RedisMessageListenerContainer;
12import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
13import org.springframework.data.redis.serializer.StringRedisSerializer;
14
15@Configuration
16public class RedisConfiguration {
17 @Bean
18 public RedisTemplate<String, Long> redisTemplate(RedisConnectionFactory connectionFactory) {
19 RedisTemplate<String, Long> template = new RedisTemplate<>();
20 template.setConnectionFactory(connectionFactory);
21 template.setDefaultSerializer(new StringRedisSerializer());
22 return template;
23 }
24
25 @Bean
26 RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
27 RedisMessageListenerContainer container = new RedisMessageListenerContainer();
28 container.setConnectionFactory(connectionFactory);
29 container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
30 return container;
31 }
32
33 @Bean
34 MessageListenerAdapter listenerAdapter(MessageListener messageListener) {
35 return new MessageListenerAdapter(messageListener, "receiveMessage");
36 }
37
38 @Bean
39 MessageListener messageConsumer() {
40 return new MessageListener();
41 }
42
43 @Bean
44 StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
45 return new StringRedisTemplate(connectionFactory);
46 }
47
48}
1package com.demo.project56.controller;
2
3import com.demo.project56.domain.Customer;
4import com.demo.project56.repository.CustomerRepository;
5import lombok.RequiredArgsConstructor;
6import lombok.extern.slf4j.Slf4j;
7import org.springframework.data.redis.core.RedisTemplate;
8import org.springframework.data.redis.core.StringRedisTemplate;
9import org.springframework.web.bind.annotation.GetMapping;
10import org.springframework.web.bind.annotation.PathVariable;
11import org.springframework.web.bind.annotation.PostMapping;
12import org.springframework.web.bind.annotation.RequestBody;
13import org.springframework.web.bind.annotation.RestController;
14
15@RestController
16@Slf4j
17@RequiredArgsConstructor
18public class HomeController {
19
20 private final RedisTemplate<String, Long> counterTemplate;
21 private final StringRedisTemplate chatTemplate;
22 private final CustomerRepository customerRepository;
23
24 @GetMapping("/api/send/{message}")
25 public void sendMessage(@PathVariable String message) {
26 log.info("Sending message {}", message);
27 chatTemplate.convertAndSend("chat", message);
28 }
29
30 @GetMapping("/api/inc")
31 public Long incrementCounter() {
32 return counterTemplate.opsForValue().increment("chat");
33 }
34
35 @PostMapping("/api/customer")
36 public Customer saveCustomer(@RequestBody Customer customer) {
37 return customerRepository.save(customer);
38 }
39
40 @GetMapping("/api/customer")
41 public Iterable<Customer> getCustomers() {
42 return customerRepository.findAll();
43 }
44
45 @GetMapping("/api/send-queue/{message}")
46 public void sendToQueue(@PathVariable String message) {
47 log.info("Sending to queue {}", message);
48 chatTemplate.opsForList().leftPush("app-key", message);
49 }
50
51 @GetMapping("/api/get-queue")
52 public String getFromQueue() {
53 return chatTemplate.opsForList().leftPop("app-key");
54 }
55
56}
Postman
Import the postman collection to postman
Setup
1# Project 56
2
3Spring & Redis (Messaging + Data)
4
5[https://gitorko.github.io/spring-redis/](https://gitorko.github.io/spring-redis/)
6
7### Version
8
9Check version
10
11```bash
12$java --version
13openjdk version "21.0.3" 2024-04-16 LTS
14```
15
16### Redis
17
18```bash
19docker run --rm --name my-redis -p 6379:6379 -d redis redis-server --requirepass "password"
20
21```
22
23To bring up Redis and Redis Commander UI
24
25```bash
26docker-compose -f docker/docker-compose-redis.yaml up
27```
28
29Open Redis UI [http://localhost:8081/](http://localhost:8081/)
30
31### Dev
32
33To run the backend in dev mode.
34
35```bash
36./gradlew clean build
37./gradlew bootRun
38```
References
comments powered by Disqus