반응형
안녕하세요. 오늘은 Spring Boot Gradle(그래들) 프로젝트에 Redis(레디스) 설정 및 테스트 하는 방법에 대해 알아보겠습니다.
1. build.gradle에 dependencies 부분에 아래 코드를 입력합니다.
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.session:spring-session-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-cache'
2. application.properties에 Redis(레디스) host와 port, password 설정 값을 셋팅합니다.
spring.session.store-type=redis
spring.redis.host=localhost
spring.redis.password=redis1234
spring.redis.port=6379
3. Redis(레디스)를 테스트 할 컨트롤러를 만들어 줍니다.
저는 set, get을 할 수 있는 API를 만들어 주었습니다.
/getRedisKeyValue에는 ket를 받아 value를 리턴 하도록 하였습니다.
package com.gtit.api.controller;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.gtit.api.vo.TestReqVO;
import jakarta.servlet.http.HttpServletRequest;
@RestController
public class RedisTestController {
private final RedisTemplate<String, String> redisTemplate;
public RedisTestController(RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@RequestMapping(value = "/setRedisKeyValue", method = RequestMethod.POST)
public ResponseEntity<?> setRedisKeyValue(HttpServletRequest request) {
ValueOperations<String, String> vop = redisTemplate.opsForValue();
vop.set("name", "홍길동");
vop.set("age", "19");
vop.set("country", "korea");
vop.set("gender", "man");
return new ResponseEntity<>(HttpStatus.CREATED);
}
@RequestMapping(value = "/getRedisKeyValue", method = RequestMethod.GET)
public ResponseEntity<?> getRedisKeyValue(TestReqVO req, HttpServletRequest request) {
ValueOperations<String, String> vop = redisTemplate.opsForValue();
String value = vop.get(req.getKey());
return new ResponseEntity<>(value, HttpStatus.OK);
}
}
4. RedisConfig를 만들어 줍니다.
package com.gtit.api.config;
import java.time.Duration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory();
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
@Bean
public CacheManager cacheManager() {
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory());
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) // Value Serializer 변경
.entryTtl(Duration.ofMinutes(5)); // 캐시 수명 5분
builder.cacheDefaults(configuration);
return builder.build();
}
}
5. postman으로 /setRedisKeyValue API를 호출하여 Redis(레디스)에 값을 셋팅해줍니다.
6. postman으로 /getRedisKeyValue API를 호출하여 age, name등을 호출하여 앞서 셋팅한 값을 리턴하는지 확인 합니다.
ing Boot Gradle(그래들) 프로젝트
Spring Boot Gradle(그래들) 프로젝트
Spring Boot Gradle(그래들) 프로젝트
반응형
'개발 > Redis' 카테고리의 다른 글
[Redis] 레디스 비밀번호 설정 io.lettuce.core.RedisCommandExecutionException: ERR Client sent AUTH, but no password is set (1) | 2024.05.08 |
---|---|
[Redis] 윈도우(Window)에 레디스(Redis) 설치 및 확인하기 (4) | 2024.04.26 |