๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring RedisSerializer ์ข…๋ฅ˜์™€ ํŠน์ง•

by ํ‘์‹œ๋ฐ” 2023. 7. 15.

๐Ÿค” ๋ฐฐ๊ฒฝ

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ์ค‘์— Redis Queue์™€ WebSocket์„ ํ™œ์šฉํ•ด์„œ ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๋Š” ๊ณผ์ •์ด ์žˆ์—ˆ๋‹ค. Spring Data Redis๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Config ํŒŒ์ผ์— RedistTemplate์„ Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ฒŒ ๋˜์—ˆ๊ณ , ๊ด€๋ จํ•ด์„œ Key์™€ Value์— Serializer๋„ ์„ค์ •ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ํ•ด๋‹น ๊ณผ์ •์—์„œ ์•Œ๊ฒŒ ๋œ RedisSerializer ์ข…๋ฅ˜์™€ ํŠน์ง•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

Redis๋Š” ์ธ ๋ฉ”๋ชจ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฏ€๋กœ Redis์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ/์กฐํšŒํ•˜๋Š” ๋™์•ˆ์— ์ง๋ ฌํ™”/์—ญ์ง๋ ฌํ™”๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

์ง๋ ฌํ™”๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

RedisTemplate ์„ค์ •

Spring Data Redis ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด RedisTemplate ๊ฐ์ฒด๋ฅผ Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ  ์ด๋ฅผ ํ™œ์šฉํ•ด์„œ Redis๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ ๋ฐ์ดํ„ฐ Key์™€ Value๋ฅผ ์ง๋ ฌํ™” / ์—ญ์ง๋ ฌํ™”ํ•˜๋Š” ๋ฐฉ์‹์„ ํ•„์š”์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ๋ฐฉ์‹์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.

 

@Configuration
@EnableRedisRepositories
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(redisHost, redisPort);
    }

    @Bean
    public RedisTemplate<?, ?> redisTemplate() {
        RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory());

        redisTemplate.setKeySerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }
}

 

redisTemplate์—๋Š” setKeySerializer, setValueSerializer ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋Š”๋ฐ ํ•ด๋‹น ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ RedisSerializer ๊ตฌํ˜„์ฒด๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ RedisTemplate์—์„œ ์ œ๊ณตํ•˜๋Š” ์ง๋ ฌํ™”/์—ญ์ง๋ ฌํ™” ๋‹ด๋‹น ๊ตฌํ˜„ ํด๋ž˜์Šค๊ฐ€ ์กด์žฌํ•œ๋‹ค.

1. JdkSerializationRedisSerializer (Default)

- ๊ธฐ๋ณธ์œผ๋กœ ์„ค์ •๋œ ์ž๋ฐ” ์ง๋ ฌํ™” ๋ฐฉ์‹์ด๋‹ค.

2. StringRedisSerializer

- ๋ฌธ์ž์—ด(String) ํƒ€์ž…์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

- Class Type์„ ๋ณ„๋„๋กœ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

 

- ์ผ๋ฐ˜์ ์ธ ๊ฐ์ฒด ํƒ€์ž…(Dto, Entity ๋“ฑ)์€ ์บ์ŠคํŒ…์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

 

- ๊ธฐ๋ณธ์ ์œผ๋กœ UTF-8๋กœ ์„ค์ •๋œ๋‹ค.

3. GenericJackson2JsonRedisSerializer

- ๋ณ„๋„ Class Type์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋“  Class Type์„ JSON ํ˜•์‹์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฒ”์šฉ์ ์ธ ํŠน์ง•์ด ์žˆ๋‹ค.

 

- Object์˜ class, package๋ฅผ ๊ฐ™์ด ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ ‘๊ทผํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ต๋‹ค.

(๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ @class๋กœ ์ง€์ •๋œ ๋™์ผํ•œ ํŒจํ‚ค์ง€์— ์žˆ๋Š” Dto๋กœ ๊ฐ€์ ธ์™€์•ผ ํ•œ๋‹ค.)

 

- ๋‚ด๋ถ€์ ์œผ๋กœ ObjectMapper๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ง๋ ฌํ™”, ์—ญ์ง๋ ฌํ™” ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

4. GenericToStringSerializer<T>

- Class Type์„ ์ง€์ •ํ•ด์•ผ ํ•˜๊ณ (Generic), ํŠน์ • Class Type์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ Converter.convert()๋ฅผ ํ™œ์šฉํ•ด์„œ String์œผ๋กœ ๋ณ€ํ™˜ ํ›„, ์ง๋ ฌํ™”ํ•˜๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

 

- ๊ธฐ๋ณธ์ ์œผ๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ํƒ€์ž…๋“ค์˜ ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ์Šคํ”„๋ง์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” Converter, ConverterFactory๋ฅผ ๋“ฑ๋กํ•œ DefaultConversionService์„ ์‚ฌ์šฉํ•œ๋‹ค.

5. Jackson2JsonRedisSerializer<T>

- Class Type์„ ์ง€์ •ํ•ด์•ผ ํ•˜๊ณ (Generic), Redis์— ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•  ๋•Œ JSON ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

 

- Class Type ์ง€์ • ๋•Œ๋ฌธ์— ํŠน์ • ํด๋ž˜์Šค์— ์ข…์†์ ์ด๋ฉฐ, ๋งŒ์•ฝ ์ถ”์ƒ ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถ”์ƒํ™”ํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ถ”๊ฐ€์ ์ธ ๋ณ„๋„ ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค.

 

- ๋‚ด๋ถ€์ ์œผ๋กœ ObjectMapper๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ง๋ ฌํ™”, ์—ญ์ง๋ ฌํ™” ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

6. OxmSerializer

- Marshaller, Unmarshaller๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ง๋ ฌํ™”, ์—ญ์ง๋ ฌํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

(Marshaller๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ XML ๋ฌธ์„œ๋กœ ๋ณ€ํ™˜, Unmarshaller๋Š” XML ๋ฌธ์„œ๋ฅผ ์ž๋ฐ” ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜)

๋Œ“๊ธ€