
๊ณ ์ฑ๋ฅ ์ธ๋ฉ๋ชจ๋ฆฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํ ์ด์ธ Redis๋ ํ์ํ ๋ฐ์ดํฐ ์บ์ฑ ๋ฐ ๊ฒ์ ์๋๋ฅผ ์ ๊ณตํ๋ฉฐ ๋ง์ ํ๋ก๋์
ํ๊ฒฝ์์ ํ์ฉ๋๊ณ ์๋ค.
์ด์ ๋ฐ๋ง์ถ์ด Spring Data ํ๋ก์ ํธ๋ Redis ์ํธ ์์ฉ์ ๋ํ ๋์ ์์ค์ ์ถ์ํ๋ฅผ ์ ๊ณตํ์ฌ ๊ฐ๋ฐ์์๊ฒ ์ต์ํ์ ๋
ธ๋ ฅ์ผ๋ก Redis์ ์ฑ๋ฅ ์ด์ ์ ํ์ฉํ๋ฉด์ ๊ธฐ๋ฅ์ด ํ๋ถํ ์ ํ๋ฆฌ์ผ์ด์
์ ๊ตฌ์ถํ๋๋ฐ ์ง์คํ ์ ์๋๋ก ํ๋ค.
์ด๋ฒ ํฌ์คํ
์์๋ Spring์์ ์ ๊ณตํ๋ Redis ๊ด๋ จ Auto Configuration์ ๋ํด์ ๋ค๋ค๋ณด๊ณ ์ ํ๋ค. (Lettuce ์์ฃผ๋ก ์ดํด๋ด)
- ์ํ ์ฝ๋ ์์ฑ์ spring boot 3.2.3๊ณผ JDK 21 ๋ฒ์ ์์ ์์ฑํ์๋ค.
- ์ํ ์ฝ๋ ํ์ธ์ ์ํ Redis ์๋ฒ ํ๊ฒฝ ๊ตฌ์ฑ์ spring boot docker compose๋ฅผ ํ์ฉํ์๋ค.
- spring boot docker compose support ํฌ์คํ ์ ์ฐธ๊ณ
Dependency
์ฐ์ Spring Data Redis๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๋ค์๊ณผ ๊ฐ์ dependency๊ฐ ํ์ํ๋ค.
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.3'
id 'io.spring.dependency-management' version '1.1.4'
}
...
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.apache.commons:commons-pool2:2.12.0'
developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
testAndDevelopmentOnly 'org.springframework.boot:spring-boot-docker-compose'
...
}
Groovy- spring boot ์์ ์ง์ํ๋ docker compose ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ถ๊ฐ๋ก spring-boot-docker-compose ์ํฐํฉํธ๋ฅผ dependency๋ก ์ถ๊ฐํ์๋ค.
- ํ
์คํธ ์ฝ๋์์๋ spring boot docker compose๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ๋ค๋ฉด
- testAndDevelopmentOnly ‘org.springframework.boot:spring-boot-docker-compose’ ๋ํ๋์๋ฅผ ์ถ๊ฐํ๋ค.
- ๋จ์ผ ์๋ฒ ํ๊ฒฝ์ Redis์ธ ๊ฒฝ์ฐ connection pool์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ commons-pool2 ๋ํ๋์๋ฅผ ์ถ๊ฐํด์ผ ํ๋ค.
- ๋จ์ผ ์๋ฒ ํ๊ฒฝ์ ๊ฒฝ์ฐ ์ ํ๋ฆฌ์ผ์ด์ ์ด commons-pool2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ์ ์๋ค๋ฉด pool์ ์๋ ํ์ฑํ ๋๋ค.
Spring Redis Auto Configuration
Spring Boot์์ Redis๋ฅผ ์ํ auto configuration์ ์์์ org.springframework.boot:spring-boot-autoconfigure ์ํฐํฉํธ์ org.springframework.boot.autoconfigure.data.redis ํจํค์ง์ ์๋ RedisAutoConfiguration ํด๋์ค๋ค.
RedisAutoConfiguration ํด๋์ค์ ์ง์ ๋ ์ด๋
ธํ
์ด์
์ ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
@AutoConfiguration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})Java- application.yml(. properties)์ Redis ๊ด๋ จ ์ค์ ์ RedisProperties ํด๋์ค์ ๋งตํ๋๋ค.
- LettuceConnectoinConfiguration, JedisConnectionConfiguration ์ด ์ฐ์ ๋์ํ๋ค.
Spring Data Redis Properties
RedisProperties ํด๋์ค๋ spring data redis ์ค์ ํญ๋ชฉ์ ๋ํ ๋งตํ ํด๋์ค๋ค.
RedisProperties ํด๋์ค์ ์ด๋
ธํ
์ด์
์ ๋ค์๊ณผ ๊ฐ์ด ์ง์ ๋์ด ์๋ค.
@ConfigurationProperties(
prefix = "spring.data.redis"
)Java์์ฑ ์ด๋ฆ์ spring.data.redis prefix๊ฐ ์ ์๋จ์ ์ ์ ์๋ค.
์๋ฒ ์ฐ๊ฒฐ์ ์ํ ๊ธฐ๋ณธ ์ค์ ํญ๋ชฉ์ ๋ค์๊ณผ ๊ฐ๋ค.
| ํ๋กํผํฐ | ์ค๋ช | ๋ํดํธ |
|---|---|---|
| spring.data.redis.client-name | ํด๋ผ์ด์ธํธ ์ค์ ์ด๋ฆ์ผ๋ก ์ฐ๊ฒฐํ ๋ ์ค์ ํ ํด๋ผ์ด์ธํธ ์ด๋ฆ | |
| spring.data.redis.client-type | ์ฐ๊ฒฐ์ ์ฌ์ฉํ ํด๋ผ์ด์ธํธ ์ ํ. (lettuce or jedis) ๊ธฐ๋ณธ์ ์ผ๋ก ํด๋์ค ๊ฒฝ๋ก์ ๋ฐ๋ผ ์๋ ๊ฐ์ง๋จ. | |
| spring.data.redis.connect-timeout | Connection ํ์์์. | |
| spring.data.redis.timeout | Read ํ์์์. | |
| spring.data.redis.database | connection factory ์์ ์ฌ์ฉํ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ธ๋ฑ์ค ์ค์ . Redis๋ ์ธ๋ฑ์ค๋ก ์๋ณ๋๋ ์ฌ๋ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ ๊ณตํ๋ฉฐ, ์ด ์์ฑ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ ํํ ์ ์์. | 0 |
| spring.data.redis.host | Redis ์๋ฒ ํธ์คํธ | localhost |
| spring.data.redis.port | Redis ์๋ฒ ํฌํธ | 6379 |
| spring.data.redis.username | Redis ์๋ฒ ๋ก๊ทธ์ธ username | |
| spring.data.redis.password | Redis ์๋ฒ ๋ก๊ทธ์ธ password | |
| spring.data.redis.url | Connection URL ์ค์ . ์ด ์ค์ ์ด ์ง์ ๋๋ฉด host, port, username, password ์ค์ ์ด ์ฌ์ ์ ๋จ. ex) redis://username:password@localhost:6379 | |
| spring.data.redis.ssl.enabled | SSL ์ง์ ํ์ฑํ ์ฌ๋ถ ์ค์ . ๋ณ๋๋ก ์ง์ ํ์ง ์๋ ๊ฒฝ์ฐ bundle์ ์ ๊ณตํ๋ฉด ์๋์ผ๋ก ํ์ฑํ ๋จ. | |
| spring.data.redis.ssl.bundle | SSL bundle ์ด๋ฆ | |
| spring.data.redis.cluster.max-redirects | ํด๋ฌ์คํฐ ์ ์ฒด์์ ๋ช ๋ น์ ์คํํ ๋ ๋ฐ๋ผ์ผ ํ ์ต๋ ๋ฆฌ๋ค์ด๋ ์ ์. | |
| spring.data.redis.cluster.nodes | ์ฐ๊ฒฐํ โhost:portโ ์์ ์ผํ(,)๋ก ๊ตฌ๋ถ๋ ๋ชฉ๋ก. ํด๋ฌ์คํฐ ๋ ธ๋์ ์ด๊ธฐ ๋ชฉ๋ก์ ๋ํ๋ด๋ฉฐ ์ ์ด๋ ํ๋์ ํญ๋ชฉ์ด ์์ด์ผ ํจ. | |
| spring.data.redis.sentinel.master | Redis ์๋ฒ์ ์ด๋ฆ. | |
| spring.data.redis.sentinel.nodes | ์ฐ๊ฒฐํ โhost:portโ ์์ ์ผํ(,)๋ก ๊ตฌ๋ถ๋ ๋ชฉ๋ก. | |
| spring.data.redis.sentinel.password | redis sentinel ์ธ์ฆ์ ์ํ ๋น๋ฐ๋ฒํธ | |
| spring.data.redis.sentinel.username | redis sentinel ์ธ์ฆ์ ์ํ ๊ณ์ | |
| spring.data.redis.repositories.enabled | Redis Repository๋ฅผ ํ์ฑํ ํ ์ง ์ฌ๋ถ. | true |
Connection Pool ๊ด๋ จ ์ค์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
| ํ๋กํผํฐ | ์ค๋ช | ๋ํดํธ |
|---|---|---|
| spring.data.redis.{jedis|lettuce}.pool.enabled | connection pool ํ์ฑํ ์ฌ๋ถ ์ค์ . commons-pool2 ๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ ์๋์ผ๋ก ํ์ฑํ ๋จ. Jedis๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ผํฐ๋ ๋ชจ๋์์ ํ๋ง์ด ์์์ ์ผ๋ก ํ์ฑํ๋๋ฉฐ ์ด ์ค์ ์ ๋จ์ผ ๋ ธ๋์์๋ง ์ ์ฉ๋จ. connection pool์ ํ์ฑํ ํ๊ธฐ ์ํด์๋ commons-pool2 ๋ํ๋์๋ฅผ ์ถ๊ฐํด์ผ ํ๋ค. (๋ํ๋์๊ฐ ์์ผ๋ฉด ConnectionFactory ์์ฑ์ ์คํจํจ) | |
| spring.data.redis.{jedis|lettuce}.pool.max-active | ์ฐ๊ฒฐ ํ์ ์ต๋ connection ์ ์ค์ . ์ ํ์ด ์๋ ๊ฒฝ์ฐ ์์ ๊ฐ์ ์ฌ์ฉ | 8 |
| spring.data.redis.{jedis|lettuce}.pool.max-idle | ์ฐ๊ฒฐ ํ์ ์ต๋ idle connection ์ ์ค์ . ์ ํ์ด ์๋ ๊ฒฝ์ฐ ์์ ๊ฐ์ ์ฌ์ฉ | 8 |
| spring.data.redis.{jedis|lettuce}.pool.min-idle | ์ฐ๊ฒฐ ํ์ด ํญ์ ์ ์งํด์ผ ํ๋ ์ต์ idle connection ์ ์ค์ . ์ด ์ค์ ์ time-between-eviction-runs ์ค์ ์๊ฐ์ด ์์์ธ ๊ฒฝ์ฐ์ ์ ์ฉ๋จ. | 0 |
| spring.data.redis.{jedis|lettuce}.pool.max-wait | ์ฐ๊ฒฐ ํ์ด ๋ชจ๋ ์ฌ์ฉ ์ค์ธ ๊ฒฝ์ฐ ์ฐ๊ฒฐ์ด ๊ฐ๋ฅํด์ง ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋ ์๊ฐ ์ค์ . ์ง์ ๋ ์๊ฐ ๋์์๋ ์ฐ๊ฒฐ ๊ฐ๋ฅํ ํ์ด ์๋ ๊ฒฝ์ฐ ์์ธ ๋ฐ์. ์์ ๊ฐ์ ์ค์ ํ ๊ฒฝ์ฐ ์ฐ๊ฒฐ ๊ฐ๋ฅํ ํ์ด ์๊ธฐ๊ธฐ๊น์ง ๊ณ์ ๋๊ธฐํจ. | -1ms |
| spring.data.redis.{jedis|lettuce}.pool.time-between-eviction-runs | ์ ๊ฑฐ ์ค๋ ๋์ ์คํ ์๊ฐ ์ฃผ๊ธฐ ์ค์ . ์์ ๊ฐ์ด ์ง์ ๋ ๊ฒฝ์ฐ ์ ๊ฑฐ ์ค๋ ๋๋ฅผ ์คํ๋์ง ์์. ์ ๊ฑฐ ์ค๋ ๋๋ฅผ ํตํด์ min-idle, max-idle ์ ์ค์ ๋ ์์น๋ฅผ ๋์ด์ ๊ฒฝ์ฐ ๋ถํ์ํ idle connection์ ์ ๊ฑฐํ์ฌ ๋ฆฌ์์ค๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํจ. |
- {jedis | lettuce} ํ์๋ ๋ถ๋ถ์ jedis ํน์ lettuce ๊ฐ ์ง์ ๋จ์ ์๋ฏธํ๋ค.
spring data properties์ ๋ํ ์ ๋ฐ์ ์ธ ๋ด์ฉ์
https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.data
๋งํฌ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋์์ด ๋ ๊ฒ์ด๋ค.
๊ธฐ๋ณธ ์ค์ ์์
spring:
data:
redis:
# redis ์ ์ ํธ์คํธ (default: localhost)
# redis ์ ์ ํธ์คํธ (default: localhost)
# - redis๋ฅผ spring boot docker compose๋ก ์์ฑํ ๊ฒฝ์ฐ spring์์ ์์ฑํ docker ์ปจํ
์ด๋๋ก๋ถํฐ ์ ๋ณด๋ฅผ ์ป์ด ์ ์ ์ ๋ณด๋ฅผ ์๋ ์
ํ
ํ๋ค.
# - redis๋ฅผ spring boot docker compose๋ก ์์ฑํ์ง ์์ ๊ฒฝ์ฐ redis ์ ์ ํธ์คํธ ์ ๋ณด๋ฅผ ์
ํ
ํ๋ค.
host: localhost
# redis ์ ์ ํฌํธ (default: 6379)
# - redis๋ฅผ spring boot docker compose๋ก ์์ฑํ ๊ฒฝ์ฐ spring์์ ์์ฑํ docker ์ปจํ
์ด๋๋ก๋ถํฐ ์ ๋ณด๋ฅผ ์ป์ด ์ ์ ์ ๋ณด๋ฅผ ์๋ ์
ํ
ํ๋ค.
# - redis๋ฅผ spring boot docker compose๋ก ์์ฑํ์ง ์์ ๊ฒฝ์ฐ redis ์ ์ ํฌํธ ์ ๋ณด๋ฅผ ์
ํ
ํ๋ค.
port: 6379
# connection timeout
connect-timeout: 30s
# read timeout
timeout: 10sYAMLConnectionDetails ์ถ์ํ
Spring Boot 3.1๋ถํฐ ๋์
๋ ConnectionDetails ์ถ์ํ๋ ์ฌ๋ฌ ์๊ฒฉ ์๋น์ค์ ๋ํ ์ฐ๊ฒฐ ์ค์ ์ ์๋ ๊ตฌ์ฑ์ ํตํด ๋ชจ๋ธ๋งํ ์ ์๊ฒ ๋์๋ค.
Redis ์๋น์ค์ ๊ฒฝ์ฐ ConnectionDetails ์ธํฐํ์ด์ค๋ฅผ ํ์ฅํ RedisConnectionDetails ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๋ฉฐ ์ด์ ๋ํ ๊ตฌํ์ฒด๋ ๋ค์๊ณผ ๊ฐ๋ค.
- RedisDockerComposeConnectionDetails
- Spring Boot Docker Compose์ ์ํด์ ๊ตฌ์ฑ๋ redis ์๋น์ค์ ๋ํด์ ์ฌ์ฉ๋๋ค.
- Spring Boot Docker Compose๋ ๊ฐ๋ฐ ๋ฐ ํ ์คํธ์ฉ์ด๋ผ์ ๊ทธ๋ฐ์ง standalone๋ง ์ง์ํ๋ค.
- Spring Boot Docker Compose๋ก ์๋ ์์ฑ๋ redis ์๋ฒ docker container์ ๋ํ ํธ์คํธ ํฌํธ๋ฅผ ์๋์ผ๋ก ๊ฐ์งํ๋ค.
- PropertiesRedisConnectionDetails
- application.yml (. properties)์ spring.data.redis ์ค์ ์ ์ํด์ ์ฐ๊ฒฐ ์ ๋ณด์ ๋ํ ConnectionDetails ์ถ์ํ๊ฐ ๊ตฌํ๋๋ค.
- RedisDockerComposeConnectionDetails์ ๋ฌ๋ฆฌ standalone, sentinel, cluster ๊ตฌ์ฑ ์ค์ ์ ๋ชจ๋ ์ฒ๋ฆฌํ๋ค.
ConnectionDetails๋ฅผ ํตํด์ standalone, sentinel, cluster ์ฐ๊ฒฐ ์ค์ ์ ๋ํ ํด๋์ค๊ฐ ๊ตฌ์ฑ์ด ๋๋๋ฐ ๊ฐ๊ฐ ๋ค์๊ณผ ๊ฐ๋ค.
- standalone – RedisStandaloneConfiguration – ๋จ์ผ ์๋ฒ ํ๊ฒฝ์ ๋ํ ์ ์ ์ ๋ณด ์ค์
- sentinel – RedisSentinelConfiguration – Sentinel ๊ตฌ์ฑ ํ๊ฒฝ์ ๋ํ ์ ์ ์ ๋ณด ์ค์
- cluster – RedisClusterConfiguration – Cluster ๊ตฌ์ฑ ํ๊ฒฝ์ ๋ํ ์ ์ ์ ๋ณด ์ค์
์ ํด๋์ค ๋ชจ๋ ConnectionDetails ๊ตฌํ์ฒด ํด๋์ค๋ฅผ ํตํด์ ์๋ง๊ฒ ์ค์ ๋๋ค.
Spring Boot Docker Compose๋ฅผ ํตํด์ Redis ์๋น์ค๋ฅผ ๊ตฌ์ฑํ ๊ฒฝ์ฐ Auto Configuration ๊ณผ์ ์์ ๋ค์์ ์ค๋ช
ํ LettuceConnectionConfiguration ํด๋์ค์ ์ฃผ์
๋๋ ConnectionDetails ํด๋์ค์ ๋ํ ์ ๋ณด๋ฅผ ๋๋ฒ๊ฑฐ๋ฅผ ํตํด์ ๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
์ฐธ๊ณ ๋ก ์์ ์ธ๊ธํ๋ฏ์ด RedisDockerComposeConnectionDetails ์๋ ๋์ปค ์ปจํ
์ด๋์ Redis ์๋ฒ ํฌํธ์ ์ฐ๊ฒฐ๋ ์ฐ๊ฒฐ๋ ํธ์คํธ ํฌํธ๋ฅผ ์๋์ผ๋ก ๊ฐ์งํจ์ ์ ์ ์๋ค.

LettuceConnectionConfiguration, JedisConnectionConfiguration ํด๋์ค
RedisAutoConfiguration ํด๋์ค์์ Import ํ๊ณ ์๋ ๋ RedisConnectionConfiguration ํด๋์ค๋ค.
๊ฐ๊ฐ์ ํด๋์ค์ ์ง์ ๋ ์ด๋
ธํ
์ด์
์ ์ดํด๋ณด์.
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({RedisClient.class})
@ConditionalOnProperty(
name = {"spring.data.redis.client-type"},
havingValue = "lettuce",
matchIfMissing = true
)
class LettuceConnectionConfiguration extends RedisConnectionConfiguration {...}JavaLettuceConnectionConfiguration ํด๋์ค๋ Lettuce ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํตํด์ ์ฐ๊ฒฐ์ ์ํ ์๋ ๊ตฌ์ฑ ์ค์ ์ ํ๋ค.
spring.data.redis.client-type ์ค์ ์ด ‘lettuce’๋ก ์ค์ ๋๊ฑฐ๋ ์ค์ ๋์ง ์์ ๊ฒฝ์ฐ(matchIfMissing = true) ์๋ ๊ตฌ์ฑ์ด ๋์ํ๋ฉฐ ํด๋น ํด๋์ค์์ LettuceConnectionFactory ๋น์ ์์ฑ(ํ์ฌ RedisConnectionFactory ๋น์ด ์์ฑ๋์ง ์์ ๊ฒฝ์ฐ) ํ๋ค.
JedisConnectionConfiguration ํด๋์ค์ ์ง์ ๋ ์ด๋
ธํ
์ด์
์ ๋ค์๊ณผ ๊ฐ๋ค.
@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({GenericObjectPool.class, JedisConnection.class, Jedis.class})
@ConditionalOnMissingBean({RedisConnectionFactory.class})
@ConditionalOnProperty(
name = {"spring.data.redis.client-type"},
havingValue = "jedis",
matchIfMissing = true
)
class JedisConnectionConfiguration extends RedisConnectionConfiguration {...}JavaLettuceConnectionConfiguration์ ์ง์ ๋ ์ด๋
ธํ
์ด์
๊ณผ ์ ์ฌํ์ง๋ง
@ConditionalOnClass({GenericObjectPool.class, JedisConnection.class, Jedis.class})
@ConditionalOnMissingBean({RedisConnectionFactory.class})
์ฐจ์ด๊ฐ ์๋ค.
Jedis๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ์ถ๊ฐ์ ์ผ๋ก Jedis ๋ํ๋์๊ฐ ์ถ๊ฐ๋์ด์ผ ํ๋ค.
implementation 'redis.clients:jedis:<version>'Groovy๋ํ LettuceConnectionConfiguration์์ LettuceConnectionFactory(RedisConnectionFactory)๋ฅผ ์์ฑํ์๋ค๋ฉด JedisConnectionConfiguration ํด๋์ค์ ์๋ ๊ตฌ์ฑ์ ์ํ๋์ง ์๋๋ค.
์ฆ ๋ช
์์ ์ผ๋ก spring.data.redis.client-type=jedis๋ก ์ค์ ๋ ๊ฒฝ์ฐ์๋ง JedisConnectionConfiguration ์๋ ๊ตฌ์ฑ์ด ์ํ๋๋ค.
CientResources Custom (Lettuce ๋ง ํด๋น)
ClientResource๋ Lettuce์์ Redis ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ์ ํ์ํ ์ด๋ฒคํธ ๋ฃจํ ๊ทธ๋ฃน, ์ค๋ ๋ ํ ๋ฐ ๊ธฐํ ์ธํ๋ผ ๊ตฌ์ฑ ์์์ ๊ฐ์ ๊ณต์ ๋ฆฌ์์ค๋ฅผ ๊ด๋ฆฌํ๋ Lettuce์ ์ธํฐํ์ด์ค๋ค.
์ด๋ฌํ ๋ฆฌ์์ค๋ ์์ฑํ๋๋ฐ ๋น์ฉ์ด ๋ง์ด ๋ค๊ธฐ ๋๋ฌธ์ RedisConnectionFactory ๋น์ ์์ฑํ ๋ ์๋์ผ๋ก DefaultClientResources ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ LettuceConnectionFactory์ ๋ฑ๋กํ์ฌ ์ฌ์ฉํ๋ค.
LettuceConnectionConfiguration ํด๋์ค๋ ๋ค์๊ณผ ๊ฐ์ด ClientResources ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค.
@Bean(
destroyMethod = "shutdown"
)
@ConditionalOnMissingBean({ClientResources.class})
DefaultClientResources lettuceClientResources(ObjectProvider<ClientResourcesBuilderCustomizer> customizers) {
DefaultClientResources.Builder builder = DefaultClientResources.builder();
customizers.orderedStream().forEach((customizer) -> {
customizer.customize(builder);
});
return builder.build();
}Java- ClientResources ์ธ์คํด์ค๋ฅผ ์ง์ ์์ฑํ์ง ์์๋ค๋ฉด Auto Configuration์ ์ํด์ ์๋์ผ๋ก ์ธ์คํด์ค๊ฐ ์์ฑ๋๋๋ฐ ์ด๋ ClientResourcesBuilderCustomizer๋ฅผ ๋น์ผ๋ก ๋ฑ๋กํ์ฌ ๋ณ๊ฒฝํ๊ณ ์ ํ๋ Resource ํญ๋ชฉ๋ง์ ๋ณ๊ฒฝํ ์ ์๋ค.
- ClientResources์ ์ผ๋ถ ํญ๋ชฉ์ ๋ณ๊ฒฝํ๋๋ก ClientResourcesBuilderCustomizer ๋น์ ์์ฑํ๋ ์ํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
@Configuration
public class RedisConfiguration {
@Bean
public ClientResourcesBuilderCustomizer clientResourceCustomize() {
return builder -> {
builder.reconnectDelay( Delay.constant( Duration.ofSeconds(50) ) );
builder.ioThreadPoolSize( 4 );
builder.commandLatencyRecorder( CommandLatencyRecorder.disabled() );
builder.commandLatencyPublisherOptions( DefaultEventPublisherOptions.disabled() );
};
}
}Java์ clientResourceCustomize ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ์ค์ ์ ๋ณ๊ฒฝํ๋ค.
- ClientResources์์ reconnect delay ์ ์ฑ
์ ConstantDelay ์ธ์คํด์ค๋ก ์ค์ ํ๋ค. (๋ํดํธ๋ ExponentialDelay ๋ค)
- ์ฌ์ ์์ ์ํด์ ๋๊ธฐํ๋ ์๊ฐ์ ๊ณ ์ ์๊ฐ์ผ๋ก ๋ณ๊ฒฝํ๋ค.
- ExponentialDelay๋ ์ฌ์ ์ ์๋ ํ์๊ฐ ์ฆ๊ฐํ ์๋ก Delay ์๊ฐ์ด ๊ทธ์ ๋น๋กํ์ฌ ์ฆ๊ฐํ๋ ๋ฐฉ์์ด๋ค.
- io thread pool size๋ฅผ 4๊ฐ๋ก ๊ณ ์ ํ๋ค. (๋ํดํธ๋ processors ์๋ค)
- ์ง์ฐ ์๊ฐ์ ๋ํ ๋ฉํธ๋ฆญ ์์ง์ disable ํ๋ค.
- ์ง์ฐ ์๊ฐ์ ๋ํด์ ์์ง๋ ๋ฉํธ๋ฆญ ์ด๋ฒคํธ ๋ฐํ์ disable ํ๋ค.
ClinetResources ํด๋์ค์ ์ ์๋ ๊ฐ ๋ฉค๋ฒ ๋ณ์๋ค์ ๋ํ ์ค๋ช ์ ๋ค์๊ณผ ๊ฐ๋ค.
| ํญ๋ชฉ | ์ค๋ช | ๋ํดํธ |
|---|---|---|
| ioThreadPoolSize | I/O ์ค๋ ๋ ํ์ ์ค๋ ๋ ๊ฐ์๋ฅผ ์ค์ ํ๋ค. ๊ธฐ๋ณธ๊ฐ์ ๋ฐํ์์ ์ฌ์ฉ๊ฐ๋ฅํ processor ์๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ํ๋ค. ๋ชจ๋ ์ค๋ ๋๋ ๋ชจ๋ I/O ์์ ์ด ์คํ๋๋ ๋ด๋ถ ์ด๋ฒคํธ ๋ฃจํ๋ฅผ ๋ํ๋ธ๋ค. ํด๋ผ์ด์ธํธ๋ ๋คํธ์ํฌ(NIO) ๋ฐ ์ ๋์ค ๋๋ฉ์ธ ์์ผ(EPoll) ์ฐ๊ฒฐ์ ์ํด ์๋ก ๋ค๋ฅธ ์ค๋ ๋ ํ์ ํ์๋ก ํ๊ธฐ ๋๋ฌธ์ ์ด ์ซ์๋ ์ค์ I/O ์ค๋ ๋ ์๋ฅผ ๋ฐ์ํ์ง ์๋๋ค. ์ต์ I/O ์ค๋ ๋๋ 2๊ฐ๋ค. | processors ์ |
| computationThreadPoolSize | compute ์ค๋ ๋ ํ์ ์ค๋ ๋ ๊ฐ์๋ฅผ ์ค์ ํ๋ค. ๊ธฐ๋ณธ๊ฐ์ ๋ฐํ์์ ์ฌ์ฉ๊ฐ๋ํ processor ์๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ํ๋ค. ๋ชจ๋ ์ค๋ ๋๋ ๋ชจ๋ ๊ณ์ฐ ์์ ์ด ์คํ๋๋ ๋ด๋ถ ์ด๋ฒคํธ ๋ฃจํ๋ฅผ ๋ํ๋ธ๋ค. ์ต์ compute ์ค๋ ๋๋ 2๊ฐ๋ค. | processors ์ |
| eventLoopGroupProvider | ๊ธฐ์กด ๋คํฐ ์ธํ๋ผ๋ฅผ ์ฌ์ฌ์ฉํ๊ฑฐ๋ ์ค๋ ๋ ํ์ ๋ํ ์ ์ฒด ์ ์ด๋ฅผ ์ํ๋ ๊ฒฝ์ฐ EventLoopGroupProvider API๊ฐ ์ด๋ฅผ ์ํ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค. EventLoopGroup์ EventLoopGroupProvider์ ์ํด์ ์ป์ด์ง๊ณ ๊ด๋ฆฌ๋๋ค. ์ ๊ณต๋ EventLoopGroupProvider๋ ํด๋ผ์ด์ธํธ์ ์ํด ๊ด๋ฆฌ๋์ง ์์ผ๋ฉฐ ๋ฆฌ์์ค๊ฐ ๋ ์ด์ ํ์ํ์ง ์์ผ๋ฉด ์ข ๋ฃํด์ผ ํ๋ค. | DefaultEventLoopGroupProvider (ioThreadPoolSize ์ ๊ด๋ จ์ด ์๋ค.) |
| eventExecutorGroup | ๊ธฐ์กด ๋คํฐ ์ธํ๋ผ๋ฅผ ์ฌ์ฌ์ฉํ๊ฑฐ๋ ์ค๋ ๋ ํ์ ๋ํ ์ ์ฒด ์ ์ด๋ฅผ ์ํ๋ ๊ฒฝ์ฐ Client Resource์ ๊ธฐ์กด EventExecutorGroup์ ์ ๊ณตํ ์ ์๋ค. ์ ๊ณต๋ EventExecutorGroup์ ํด๋ผ์ด์ธํธ์์ ๊ด๋ฆฌํ์ง ์์ผ๋ฉฐ ๋ฆฌ์์ค๊ฐ ๋ ์ด์ ํ์ํ์ง ์์ผ๋ฉด ์ข ๋ฃํด์ผ ํ๋ค. | DefaultEventExecutorGroup (computationThreadPoolSize ์ ๊ด๋ จ์ด ์๋ค.) |
| eventBus | ์ด๋ฒคํธ ๋ฒ์ค ์์คํ ์ ํด๋ผ์ด์ธํธ์์ subscriber์๊ฒ ์ด๋ฒคํธ๋ฅผ ์ ์กํ๋๋ฐ ์ฌ์ฉ๋๋ค. ์ด๋ฒคํธ๋ ์ฐ๊ฒฐ ์ํ ๋ณ๊ฒฝ, ๋ฉํธ๋ฆญ ๋ฑ์ ๊ดํ ๊ฒ์ด๋ค. ์ด๋ฒคํธ๋ Rxjava ์ฃผ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์๋๋ฉฐ ๊ธฐ๋ณธ ๊ตฌํ์ ๋ฐฑํ๋ ์ ์์ ์ด๋ฒคํธ๋ฅผ ์ญ์ ํ๋ค. | DefaultEventBus |
| commandLatencyCollectorOptions | ํด๋ผ์ด์ธํธ๋ ๋ช ๋ น์ dispatch ํ๋ ๋์ ์ง์ฐ ์๊ฐ ๋ฉํธ๋ฆญ์ ์์งํ ์ ์๋ค. ์ด ์ต์ ์ ์ฌ์ฉํ๋ฉด ๋ฐฑ๋ถ์์, ๋ฉํธ๋ฆญ ์์ค(์ฐ๊ฒฐ ๋๋ ์๋ฒ๋ณ), ๋ฉํธ๋ฆญ์ ๋์ ํ ์ง ๋๋ ์์ง ํ ์ด๊ธฐํ ํ ์ง ์ฌ๋ถ๋ฅผ ๊ตฌ์ฑํ ์ ์๋ค. commandLatencyCollectorOptions ์ค์ ์ ๊ธฐ๋ณธ์ ์ผ๋ก enabled ๋์ด ์์ผ๋ฉฐ commandLatencyCollectorOptions(โฆ)๋ฅผ DefaultCommandLatencyCollectorOptions.disabled()๋ก ์ค์ ํ์ฌ ๋นํ์ฑํ ํ ์ ์๋ค. – 6.0 ๋ฒ์ ๋ถํฐ ๋ฉ์๋๋ deprecated ๋์๋ค. spring ์ง์ฐ ์๊ฐ ์์ง๊ธฐ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ํด๋์ค ๊ฒฝ๋ก์ LatencyUtils๊ฐ ์์ด์ผ ํ๋ค. | DefaultCommandLatencyCollectorOptions |
| commandLatencyCollector | ํด๋ผ์ด์ธํธ๋ ๋ช ๋ น์ dispatch ํ๋ ๋์ ์ง์ฐ ์๊ฐ ๋ฉํธ๋ฆญ์ ์์งํ ์ ์๋ค. ๋ช ๋ น ๋๊ธฐ ์๊ฐ ๋ฉํธ๋ฆญ์ ์ฐ๊ฒฐ ๋๋ ์๋ฒ ์์ค์์ ์์ง๋๋ค. ๋ช ๋ น ์ง์ฐ ์๊ฐ ์์ง์ ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ฑํ ๋์ด ์์ผ๋ฉฐ commandLatencyCollectorOptions(โฆ)๋ฅผ DefaultCommandLatencyCollectorOptions.disabled()๋ก ์ค์ ํ์ฌ ๋นํ์ฑํ ํ ์ ์๋ค. – 6.0 ๋ฒ์ ๋ถํฐ ๋ฉ์๋๋ deprecated ๋์๋ค. | DefaultCommandLatencyCollector spring 6.0 ๋ถํฐ deprecated ๋จ. |
| commandLatencyRecorder | commandLatencyCollector๋ฅผ ๋์ฒดํ์ฌ ๋์จ ํ๋๋ก ๋ณด์ธ๋ค. CommandLatencyRecorder๋ ์ธํฐํ์ด์ค๋ฉฐ ๊ตฌํ์ฒด๋ CommandLatencyCollector ์ด๋ค. 6.0๋ฒ์ ์ดํ ๋ถํฐ ์ฌ์ฉ๋๋ ์ง์ฐ ์๊ฐ ๋ฉํธ๋ฆญ์ ์์งํ๊ธฐ ์ํ ์ธ์คํด์ค๋ก ๋ณด์ธ๋ค. ๋ด๋ถ์ ์ผ๋ก CommandLatencyCollector ํ์ ์ ์ธ์คํด์ค๋ฅผ ๊ฐ์ง๋ฉฐ CommandLatencyCollectorOptions๊ฐ null ์ธ ๊ฒฝ์ฐ์๋ DefaultCommandLatencyCollectorOptions๋ฅผ ์ฌ์ฉํ๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ฑํ ๋์ด ์์ผ๋ฉฐ commandLatencyRecorder(CommandLatencyRecorder.disabled()) ๋ก ๋นํ์ฑํ ํ๋๋ฏ ํ๋ค. | DefaultCommandLatencyCollector |
| commandLatencyPublisherOptions | eventBus๋ฅผ ์ฌ์ฉํ์ฌ ๋ช ๋ น ๋๊ธฐ ์๊ฐ ๋ฉํธ๋ฆญ์ ๊ฒ์ํ ์ ์๋ค. ์ง์ฐ ์๊ฐ ์ด๋ฒคํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก 10๋ถ๋ง๋ค ๋ฐํ๋๋ค. ์ด๋ฒคํธ ๊ฒ์๋ฅผ ๋นํ์ฑํ ํ๋ ค๋ฉด commandLatencyPublisherOptions(โฆ) ๋ฅผ DefaultEventPublisherOptions.disabled() ๋ก ์ค์ ํ์ฌ ๋นํ์ฑํ ํ ์ ์๋ค. | DefaultEventPublisherOptions |
| dnsResolver | 3.5, 4.2 ๋ฒ์ ํธ์คํธ ์ด๋ฆ์ java.net.InetAddress๋ก ํ์ธํ๋๋ก DNS resolver๋ฅผ ๊ตฌ์ฑํ๋ค. ๊ธฐ๋ณธ๊ฐ์ ํธ์คํธ ์ด๋ฆ ํ์ธ ๋ฐ ์กฐํ ๊ฒฐ๊ณผ ์บ์ฑ์ ์ฌ์ฉํ๋ JVM DNS resolver๋ฅผ ์ฌ์ฉํ๋ค. DNS ๊ธฐ๋ฐ Redis-HA ์ค์ (ex. AWS ElasticCache)๋ฅผ ์ฌ์ฉํ๋ ์ฌ์ฉ์๋ ๋ค๋ฅธ DNS resolver๋ฅผ ๊ตฌ์ฑํ ์ ์๋ค. Lettuce์๋ java์ DnsContextFactory๋ฅผ ์ฌ์ฉํ์ฌ ํธ์คํธ ์ด๋ฆ์ ํ์ธํ๋ DirContextDnsResolver๊ฐ ํจ๊ป ์ ๊ณต๋๋ค. DirContextDnsResolver๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์บ์ฑํ์ง ์๊ณ ์์คํ DNS ๋๋ ์ฌ์ฉ์ ์ ์ DNS ์๋ฒ๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก ๊ฐ ํธ์คํธ ์ด๋ฆ ์กฐํ์์ DNS ์กฐํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค. 4.4 ๋ฒ์ ๊ธฐ๋ณธ๊ฐ์ DnsResolvers.UNRESOLVED๋ก ์ค์ ๋์ด Bootstrap.connect()์์ DNS ์ด๋ฆ์ ํ์ธํ๋ netty์ AddressResolver๋ฅผ ์ฌ์ฉํ๋ค. | DnsResolvers.UNRESOLVED |
| reconnectDelay | ์ฌ์ฐ๊ฒฐ ์๋๋ฅผ ์ง์ฐ์ํค๋๋ฐ ์ฌ์ฉ๋๋ ์ฌ์ฐ๊ฒฐ ์ง์ฐ์ ๊ตฌ์ฑํ๋ค. ๊ธฐ๋ณธ๊ฐ์ ์ํ์ด 30์ด์ธ exponential delay๋ค. | Delay.exponential() |
Configuration ํด๋์ค์์ ConnectionFactory์ Client Configuration ๋ณ๊ฒฝํ๊ธฐ
ClientResources์ ํญ๋ชฉ์ Configuration ํด๋์ค์์ ClientResourcesBuilderCustomizer๋ฅผ ์ด์ฉํ์ฌ ๋ณ๊ฒฝํ ์ ์๋ ๊ฒ์ฒ๋ผ LettuceClientConfigurationBuilderCustomizer๋ฅผ ์ด์ฉํ์ฌ ConnectionFactory์ Client ์ค์ ์ ๋ณด๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค.
Lettuce๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ค์ ์ ๋ณ๊ฒฝํ๋ ์ํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
@Bean
public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigCustomize() {
return builder -> {
if ( builder instanceof
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder poolingBuilder ) {
// Pool configuration
GenericObjectPoolConfig<?> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxIdle(10);
poolConfig.setMinIdle(5);
poolConfig.setMaxTotal(20);
poolConfig.setMaxWait(Duration.ofSeconds( 30 ));
poolingBuilder.poolConfig( poolConfig );
}
builder.shutdownTimeout( Duration.ofMillis( 1000L ) );
};
}Java์ ์ฝ๋๋
- pool ์ค์ ์ด ์๋ ๊ฒฝ์ฐ์๋ LettucePoolingClientConfigurationBuilder ์ธ์คํด์ค๊ฐ ์์ฑ๋๋๋ฐ max-idle, min-idle, max-total, max-wait ์ค์ ์ ๊ฐ๊ฐ 10๊ฐ, 5๊ฐ, 20๊ฐ, 30์ด๋ก ๋ค์ ์ฌ์ค์ ์ ํ๋ค.
- lettuce shutdown timeout ์ค์ ์ 1000ms๋ก ์ค์ ํ๋ค. (default: 100ms)
- spring.data.redis.lettuce.shutdown-timeout ์ค์ ์ด ์ง์ ๋์ด ์๋ค๋ฉด override ํ๋ค.
lettuceClientConfigCustomize ๋น์ด ์ด๋ป๊ฒ ํธ์ถ๋๋์ง LettuceConnectionConfiguration ํด๋์ค๋ฅผ ํตํด ์ดํด๋ณด์.
LettuceConnectionFactory ๋น์ ์์ฑํ๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
@Bean
@ConditionalOnMissingBean(RedisConnectionFactory.class)
@ConditionalOnThreading(Threading.PLATFORM)
LettuceConnectionFactory redisConnectionFactory(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ClientResources clientResources) {
return createConnectionFactory(builderCustomizers, clientResources);
}Java์ ์ฝ๋์์ ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers ํ๋ผ๋ฏธํฐ์ ์ง์ ์์ฑํ lettuceClientConfigCustomize ๋น์ด ์ฃผ์
๋๊ฒ ๋๋ค.
createConnectionFactory(builderCustomizers, clientResources) ๋ฉ์๋ ๊ตฌํ์ ๋ค์๊ณผ ๊ฐ๋ค.
private LettuceConnectionFactory createConnectionFactory(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ClientResources clientResources) {
LettuceClientConfiguration clientConfig = getLettuceClientConfiguration(builderCustomizers, clientResources,
getProperties().getLettuce().getPool());
return createLettuceConnectionFactory(clientConfig);
}JavagetLettuceClientConfiguration(builderCustomizers, clientResources, getProperties().getLettuce().getPool()); ๋ฉ์๋๊ฐ Lettuce Client ์ค์ ์ ์์ฑํ๋ ๋ถ๋ถ์ด๋ค.
๋ฐ๋ผ๊ฐ ๋ณด์.
private LettuceClientConfiguration getLettuceClientConfiguration(
ObjectProvider<LettuceClientConfigurationBuilderCustomizer> builderCustomizers,
ClientResources clientResources, Pool pool) {
LettuceClientConfigurationBuilder builder = createBuilder(pool);
applyProperties(builder);
if (StringUtils.hasText(getProperties().getUrl())) {
customizeConfigurationFromUrl(builder);
}
builder.clientOptions(createClientOptions());
builder.clientResources(clientResources);
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
return builder.build();
}Java์ง์ ์์ฑํ lettuceClientConfigCustomize ๋น์ด ์ฌ๊ธฐ๊น์ง ํ๋ฌ ๋ค์ด์๋ค.
๊ฐ ๋ฉ์๋ ํธ์ถ์ ๋์์ ์ดํด๋ณด๋ฉด
- createBuilder(pool) : pool ์ค์ ์ด enabled์ธ ๊ฒฝ์ฐ LettucePoolingClientConfigurationBuilder๋ฅผ ์๋ ๊ฒฝ์ฐ LettuceClientConfigurationBuilder๋ฅผ ๋ฆฌํดํ๋ค.
- applyProperties(builder) : builder์ application ํ๋กํผํฐ ์ค์ ๋ด์ฉ์ ์ ์ฉํ๋ค. (spring.data.redis.~~~)
- ssl enabled ์ฌ๋ถ ์ค์ ์ ์ฉ (spring.data.redis.ssl.enabled)
- timeout ์ค์ ์ ์ฉ (spring.data.redis.timeout)
- lettuce์ shutdown timeout ์ค์ ์ ์ฉ (spring.data.redis.lettuce.shutdown-timeout)
- client name ์ค์ ์ ์ฉ (spring.data.redis.client-name)
- customizeConfigurationFromUrl(builder) : spring.data.redis.url ์ค์ ์ด ์ ์ฉ๋ ๊ฒฝ์ฐ url ๋ด์ฉ์ ํ์ฑ ํ์ฌ scheme๊ฐ rediss์ธ ๊ฒฝ์ฐ use ssl์ true๋ก ์ค์ ํ๋ค.
- builder.clientOptions(createClientOptions()) : ClientOptions ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ์ ์ฉํ๋ค. ClientOptions์ ์ ์ฉ๋๋ ์ค์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- application.yml (.properties) ์ค์ ์ cluster refresh ๊ด๋ จ ์ค์ ์ ์ ์ฉํ๋ค. (spring.data.redis.lettuce.cluster.refresh)
- connection timeout ์ค์ ์ ์ ์ฉํ๋ค. (spring.data.redis.connect-timeout)
- SSL bundle์ด ์ง์ ๋ ๊ฒฝ์ฐ bundle์ ์ง์ ๋ SSL ์ค์ ์ ์ ์ฉํ๋ค. (spring.data.redis.ssl.bundle)
- builder.clientResources : ClientResources ์ธ์คํด์ค๋ฅผ ์ ์ฉํ๋ค.
- builderCustomizers.orderedStream().forEach( ( customizer ) โ customizer.customize( builder ) )
- ์ง์ ์์ฑํ lettuceClientConfigCustomize ์ธ์คํด์ค๊ฐ ์ ์ฉ๋๋ค.
- customizer.customize(builder) ํธ์ถ์ ๊ฒฐ๊ตญ ์๋ ์ฝ๋์ ๊ฐ๊ฒ ๋๋ค.
void customize(LettuceClientConfigurationBuilder builder) {
if ( builder instanceof
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder poolingBuilder ) {
// Pool configuration
GenericObjectPoolConfig<?> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxIdle(10);
poolConfig.setMinIdle(5);
poolConfig.setMaxTotal(20);
poolConfig.setMaxWait(Duration.ofSeconds( 30 ));
poolingBuilder.poolConfig( poolConfig );
}
builder.shutdownTimeout( Duration.ofMillis( 1000L ) );
}JavaClientOptions (Lettuce๋ง ํด๋น)
LettuceClientConfiguration๋ฅผ ์์ฑ์์ ClientOptions ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ LettuceClientConfiguration ์ธ์คํด์ค์ ์ ์ฉํ๋ค.
ClientOptions ํด๋์ค๋ Spring ์ ํ๋ฆฌ์ผ์ด์
๋ด์์ ์ฌ์ฉ๋๋ Redis ํด๋ผ์ด์ธํธ์ ๋์์ ๊ตฌ์ฑํ๋ ํต์ฌ ๋ถ๋ถ์ด๋ค.
ํด๋ผ์ด์ธํธ๊ฐ Redis ์๋ฒ์ ์ํธ ์์ฉํ๋ ๋ฐฉ์์์ ์ค์ํ ์ญํ ์ ํ๋ฉฐ ์ฐ๊ฒฐ ์ฒ๋ฆฌ, ๋ช
๋ น ์คํ, ๋ณด์ ๋ฐ ์ฑ๋ฅ ์ต์ ํ์ ์ํฅ์ ๋ฏธ์น๋ค.
๋ค์์ ClientOptions ํด๋์ค์ ๊ฐ ํ๋์ ๋ํ ์ค๋ช
์ด๋ค.
| ํญ๋ชฉ | ์ค๋ช | ๋ํดํธ |
|---|---|---|
| autoReconnect | ํด๋ผ์ด์ธํธ๊ฐ ์ฐ๊ฒฐ์ด ๋๊ธด ํ ์๋์ผ๋ก ๋ค์ ์ฐ๊ฒฐํ ์ง ์ฌ๋ถ๋ฅผ ์ค์ ํ๋ค. ํ์ฑํ ๋ ๊ฒฝ์ฐ ๋คํธ์ํฌ ๋ฌธ์ ๋ก ์ธํ ์ฐ๊ฒฐ ์ค๋จ ํ ์๋ ์ฌ์ฐ๊ฒฐ ์๋๊ฐ ์ด๋ฃจ์ด์ง๋ค. | true |
| cancelCommandsOnReconnectFailure | ์ฌ์ฐ๊ฒฐ ์๋๊ฐ ์คํจํ์ ๋ ๋ณด๋ฅ ์ค์ธ ๋ชจ๋ ๋ช ๋ น์ ์ทจ์ํ ์ง ์ฌ๋ถ๋ฅผ ์ค์ ํ๋ค. ์ด๋ ์ฌ์ฐ๊ฒฐ ๊ณผ์ ์์ ๋ช ๋ น์ด ๋ฌดํํ ๋๊ธฐํ๋ ์ํฉ์ ๋ฐฉ์งํ๋ค. | false |
| decodeBufferPolicy | ์๋ต ๋ฒํผ๋ฅผ ์ด๋ป๊ฒ ๋์ฝ๋ฉํ ์ง ๊ฒฐ์ ํ๋ ์ ์ฑ ์ด๋ค. ๋๋์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ๋ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋๊ณผ ์ฑ๋ฅ์ ์ํฅ์ ์ค ์ ์๋ค. | ratio(3) |
| disconnectBehavior | ์ฐ๊ฒฐ์ด ๋์ด์ก์ ๋ ํด๋ผ์ด์ธํธ ๋์์ ์ ์ํ๋ค. ๋ช ๋ น์ด ์คํจํ๊ฑฐ๋ ์ผ์ ์๊ฐ ๋์ ์ฌ์๋ํ๊ฒ ํ๋ ๋ฑ์ ํ๋์ ์ง์ ํ ์ ์๋ค. | DEFAULT |
| pingBeforeActivateConnection | ์ ์ฐ๊ฒฐ์ด ํ์ฑํ๋๊ธฐ ์ ์ PING ๋ช ๋ น์ ๋ณด๋ด ์ฐ๊ฒฐ ์ํ๋ฅผ ํ์ธํ ์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ค. ์ด๋ฅผ ํตํด ์๋ฒ๊ฐ ์๋ตํ์ง ์๋ ์ฐ๊ฒฐ์ ๋ฏธ๋ฆฌ ํ์ธํ ์ ์๋ค. | true |
| protocolVersion | ์ฌ์ฉํ Redis ํ๋กํ ์ฝ ๋ฒ์ ์ ์ง์ ํ๋ค. | |
| publishOnScheduler | ๋ช ๋ น์ ๋น๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌํ ๋ ์ฌ์ฉํ ์ค์ผ์ค๋ฌ๋ฅผ ์ ์ํ๋ค. ์ด๋ ๋ช ๋ น ์ฒ๋ฆฌ์ ์ฑ๋ฅ๊ณผ ํจ์จ์ฑ์ ์ํฅ์ ์ค ์ ์๋ค. ์ด ์ต์ ์ ํ์ฑํํ๋ฉด ๋จ์ผ ํน์ ์์์ Redis ์ฐ๊ฒฐ๋ก ์๋นํ ์์ ์ฒ๋ฆฌ๊ฐ ํ์ํ ๋ฆฌ์กํฐ๋ธ ์ํ์ค์์ ๋จ์ผ ์ค๋ ๋์ ๊ฐ์ ๋์์ผ๋ก ์ธํด ์ฑ๋ฅ์ด ์ ํ๋๋ ๊ฒฝ์ฐ์ ์ ์ฉํ ์ ์๋ค. ์ด ์ต์ ์ ํ์ฑํํ๋ฉด ๋ฐ์ดํฐ/์๋ฃ ์ ํธ์ ๋ํด ClientResources๋ฅผ ํตํด ๊ตฌ์ฑ๋ EventExecutorGroup์ ์ฌ์ฉํ๋ค. | false |
| readOnlyCommands | ์ฝ๊ธฐ ์ ์ฉ ๋ช ๋ น์ ํ์ฉํ ์ง ์ฌ๋ถ๋ฅผ ์ค์ ํ๋ค. ์ด๋ ์ฃผ๋ก ํด๋ฌ์คํฐ ํ๊ฒฝ์์ ์ฌ๋ ์ด๋ธ ๋ ธ๋์ ์ฝ๊ธฐ ๋ช ๋ น์ ๋ณด๋ผ ์ ์๊ฒ ํ๊ธฐ ์ํด์ ์ฌ์ฉ๋๋ค. | |
| requestQueueSize | ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ด์ง ์์ ๋ช ๋ น์ ๋๊ธฐ์ํฌ ์ ์๋ ํ์ ํฌ๊ธฐ๋ฅผ ์ค์ ํ๋ค. ์ด ํฌ๊ธฐ๋ฅผ ์กฐ์ ํ์ฌ ๋คํธ์ํฌ ์ง์ฐ์ด๋ ์๋ฒ ์ฒ๋ฆฌ ๋ฅ๋ ฅ์ ๊ณ ๋ คํ ์ ์ฐํ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ๋ค. | Integer.MAX_VALUE |
| scriptCharset | ์คํฌ๋ฆฝํธ ๋ช ๋ น์ ์ธ์ฝ๋ฉ/๋์ฝ๋ฉํ ๋ ์ฌ์ฉํ ๋ฌธ์ ์ธ์ฝ๋ฉ์ ์ง์ ํ๋ค. | UTF-8 |
| socketOptions | ์์ผ ์ฐ๊ฒฐ์ ์ ์ฉํ ์ต์ ์ ์ค์ ํ๋ค. ํ์์์, ๋ฒํผ ํฌ๊ธฐ, KeepAlive๋ฑ์ ์ต์ ์ ํฌํจํ ์ ์๋ค. | AutoConfiguration์์ ์ค์ ํด์ค๋ค. |
| sslOptions | SSL/TLS๋ฅผ ํตํ ๋ณด์ ์ฐ๊ฒฐ ์ค์ ์ ์ ์ํ๋ค. ํค ์ ์ฅ์, ํธ๋ฌ์คํธ ์ ์ฅ์, ์ฌ์ฉํ ์ํธํ ์๊ณ ๋ฆฌ์ฆ ๋ฑ์ ํฌํจํ๋ค. | AutoConfiguration์์ ์ค์ ํด์ค๋ค. |
| suspendReconnectOnProtocolFailure | ํ๋กํ ์ฝ ์ค๋ฅ๋ก ์ธํด ์ฐ๊ฒฐ์ด ์คํจํ์ ๋ ์ฌ์ฐ๊ฒฐ์ ์ผ์ ์ค๋จํ ์ง ์ฌ๋ถ๋ฅผ ์ค์ ํ๋ค. | false |
| timeoutOptions | ๋ช ๋ น ์คํ ๋ฐ ์ ์ฒด ์ฐ๊ฒฐ ๊ด๋ฆฌ๋ฅผ ์ํด timeout์ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์ค์ ํ๋ค. |
decodeBufferPolicy
1. ratio ์ ์ฑ
– ์ง๊ณ ๋ฒํผ์ ์ฉ๋๊ณผ ์ฌ์ฉ๋์ ๊ณ ๋ คํ ๋น์จ ๊ธฐ๋ฐ์ ์ ์ฑ ์ด๋ค. ์ฌ์ฉ ๋น์จ์ ๊ณ ๋ คํ์ฌ CPU ์ฌ์ฉ๋๊ณผ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ต์ ํ ํ๋ค. ๊ฐ์ด ๋์ ์๋ก ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ๋์ด๋๋ค. parameter๋ก ์ ๋ฌ๋๋ bufferUsageRatio๋ ๋ฒํผ ์ฌ์ฉ ๋น์จ์ด๋ค.
๊ฐ์ด 1์ด๋ฉด 50%, ๊ฐ์ด 2์ด๋ฉด 66%, ๊ฐ์ด 3์ด๋ฉด 75% ์ ๊ฐ์ด ๊ณ์ฐ๋๋ค. 0์์ 2^31 – 1 ์ฌ์ด์ ๊ฐ์ด์ด์ผ ํ๋ฉฐ ์ผ๋ฐ์ ์ผ๋ก 50%์์ 90%๋ฅผ ๋ํ๋ด๋ 1์์ 10์ฌ์ด์ ๊ฐ์ ์ค์ ํ๋ค.2. always ์ ์ฑ
– ๊ฐ ๋์ฝ๋ฉ ๋จ๊ณ ํ์ ์ฝ์ ๋ฐ์ดํธ๋ฅผ ์ญ์ ํ๋ ์ ์ฑ ์ด๋ค. ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ์ด ๊ฐ์ฅ ๋์ ์ ๋ต์ด์ง๋ง CPU ๋ถ๋ด๋ ํจ๊ป ์ปค์ง๋ค.3. alwaysSome ์ ์ฑ
– ๊ฐ ๋์ฝ๋ฉ ๋จ๊ณ ํ์ ์ผ๋ถ ์ฝ๊ธฐ ๋ฐ์ดํธ๋ฅผ ์ญ์ ํ๋ ์ ์ฑ ์ด๋ค. ๋ด๋ถ ๊ตฌํ์ ๋ฐ๋ผ ์ฝ๊ธฐ ๋ฐ์ดํธ์ ์ผ๋ถ ๋๋ ์ ๋ถ๋ฅผ ์ญ์ ํ๊ฑฐ๋ ์ ํ ์ญ์ ํ์ง ์๊ณ ์ ์ฌ์ ์ผ๋ก ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์๋น๋ฅผ ๊ฐ์ํ๋ฉด์ ์ ์ฒด ๋ฉ๋ชจ๋ฆฌ ๋์ญํญ ์๋น๋ฅผ ์ค์ผ ์ ์๋ค.
disconnectBehavior
1. DEFAULT
– autoReconnect ์ต์ ์ด true์ธ ๊ฒฝ์ฐ ๋ช ๋ น์ ์๋ฝํ๊ณ , autoReconnect ์ต์ ์ด false์ธ ๊ฒฝ์ฐ ๋ช ๋ น์ ๊ฑฐ๋ถํ๋ค.2. ACCEPT_COMMANDS
– ์ฐ๊ฒฐ์ด ๋๊ธด ์ํ์์๋ ๋ช ๋ น์ ์๋ฝํ๋ค.3. REJECT_COMMANDS
– ์ฐ๊ฒฐ์ด ๋๊ธฐ ์ํ์์๋ ๋ช ๋ น์ ๊ฑฐ๋ถํ๋ค.
RedisConnectionFactory ์์ฑ (LettuceConnectionFactory)
์ง๊ธ๊น์ง ์์์ ์ดํด๋ณธ ClientResources, LettuceClientConfiguration, ConnectionDetails ์ธ์คํด์ค๋ฅผ ํตํด์ RedisConnectionFactory๊ฐ ์์ฑ๋๋ค.
์ต์ข
์ ์ผ๋ก LettuceConnectionFactory์๋ LettuceClientConfiguration๊ณผ RedisConfiguration ์ธ์คํด์ค๊ฐ ์ ์ฉ์ด ๋๋ค.
RedisConfiguration ์ธ์คํด์ค๋ application.yml (.properties) ์ค์ ์ ๋ค์ ํญ๋ชฉ์ ๋ฐ๋ผ์ ์ธ์คํด์ค๊ฐ ๊ฒฐ์ ๋๋ค.
| ํ๋กํผํฐ | RedisConfiguration ์ธ์คํด์ค | ์ค๋ช |
|---|---|---|
| spring.data.redis.sentinel | RedisSentinelConfiguration | Sentinel ๊ตฌ์ฑ ํ๊ฒฝ์ ๋ํ ์ ์ ์ค์ ์ ๋ณด |
| spring.data.redis.cluster | RedisClusterConfiguration | Cluster ๊ตฌ์ฑ ํ๊ฒฝ์ ๋ํ ์ ์ ์ค์ ์ ๋ณด |
| spring.data.redis.host spring.data.redis.port | RedisStandaloneConfiguration | ๋จ์ผ ๊ตฌ์ฑ์ ๋ํ ์ ์ ์ค์ ์ ๋ณด |
spring.data.redis.sentinel, spring.data.redis.cluster๊ฐ ๋ชจ๋ ์ค์ ๋ ๊ฒฝ์ฐ์๋ spring.data.redis.sentinel ์ค์ ์ด ์ ์ฉ๋๋ค.
์ ๋ฆฌํ์๋ฉด RedisConnectionFactory(LettuceConnectionFactory) ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
RedisTemplate Serializers
Spring Data Redis์์ ์ฌ์ฉ์ ์ ์ ํ์ ๊ณผ raw ๋ฐ์ดํฐ ๊ฐ์ ๋ณํ์ org.springframework.data.redis.serializer ํจํค์ง์์ ์ฒ๋ฆฌํ๋ค. ์ด ํจํค์ง์๋ ์ง๋ ฌํ ํ๋ก์ธ์ค๋ฅผ ์ฒ๋ฆฌํ๋ ๋ ๊ฐ์ง ์ ํ์ ์ง๋ ฌํ๊ธฐ๊ฐ ํฌํจ๋์ด ์๋ค.
- RedisSerializer๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ ์๋ฐฉํฅ ์ง๋ ฌํ๊ธฐ
- Element๋ฅผ ์ฝ๊ณ ์ฐ๊ธฐ ์ํด์ ์ฌ์ฉํ๋ RedisElementReader ๋ฐ RedisElementWriter
๋ ์ง๋ ฌํ๊ธฐ์ ์ฐจ์ด์ ์ RedisSerializer๋ byte[]๋ก ์ง๋ ฌํํ๋ ๋ฐ๋ฉด, Reader์ Writer๋ ByteBuffer๋ฅผ ์ฌ์ฉํ๋ค.
์ ๋ ๊ฐ์ง ์ ํ ์ด์ธ์๋ ์ฌ๋ฌ ์ง๋ ฌํ๊ธฐ๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- RedisCache์ RedisTemplate์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฌ์ฉ๋๋ JdkSerializationRedisSerializer
- StringRedisSerializer
- Spring OXM ์ง์์ ํตํด ๊ฐ์ฒด/XML ๋งคํ์ ์ํ OxmSerializer
- JSON ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ ์ํด Jacson2JsonRedisSerializer ๋๋ GenericJackson2JsonRedisSerializer
์ ์ฅ ํ์์ ๊ฐ์ผ๋ก๋ง ์ ํ๋์ง ์๊ณ ํค, ๊ฐ ๋๋ ํด์์ ์ ํ ์์ด ์ฌ์ฉํ ์ ์๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก RedisCache ๋ฐ RedisTemplate์ Java ๋ค์ดํฐ๋ธ ์ง๋ ฌํ๋ฅผ ์ฌ์ฉํ๋๋ก ๊ตฌ์ฑ๋๋ค. Java ๋ค์ดํฐ๋ธ ์ง๋ ฌํ๋ ํ์ธ๋์ง ์์ ๋ฐ์ดํธ์ฝ๋๋ฅผ ์ฝ์
ํ๋ ์ทจ์ฝํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ ํด๋์ค๋ฅผ ์
์ฉํ๋ ํ์ด๋ก๋๋ก ์ธํด ์๊ฒฉ ์ฝ๋๊ฐ ์คํ๋ ์ ์๋ ๊ฒ์ผ๋ก ์๋ ค์ ธ ์๋ค. ์กฐ์๋ ์
๋ ฅ์ผ๋ก ์ธํด ์ญ์ง๋ ฌํ ๋จ๊ณ์์ ์ ํ๋ฆฌ์ผ์ด์
์ ์ํ์ง ์๋ ์ฝ๋๊ฐ ์คํ๋ ์ ์๋ค. ๋ฐ๋ผ์ ์ ๋ขฐํ ์ ์๋ ํ๊ฒฝ์์๋ ์ง๋ ฌํ๋ฅผ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข๋ค. ์ผ๋ฐ์ ์ผ๋ก ๋ค๋ฅธ ๋ฉ์์ง ํ์(JSON๊ณผ ๊ฐ์)์ ๋์ ์ฌ์ฉํ ๊ฒ์ ๊ฐ๋ ฅํ ๊ถ์ฅํ๋ค๊ณ Spring ๋ฌธ์์์๋ ๋งํ๋ค.
๋ค์ ์ํ ์ฝ๋๋ Redis์ ํค์ ๊ฐ์ ๋ํ ์ง๋ ฌํ๊ธฐ๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋ค.
// lettuceConnectionFactory ์ธ์คํด์ค๋ spring data redis auto configuration์ ์ํด์ ์๋ ์์ฑ๋จ
@Bean
public RedisTemplate<String, Object> redisTemplate( LettuceConnectionFactory lettuceConnectionFactory ) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory( lettuceConnectionFactory );
// set key serializer
// template.setKeySerializer( new StringRedisSerializer(StandardCharsets.UTF_8) ); ์ ๋์ผํจ
template.setKeySerializer( RedisSerializer.string() );
template.setHashKeySerializer( RedisSerializer.string() );
// set value serializer
// template.setDefaultSerializer( new GenericJackson2JsonRedisSerializer() ); ์ ๋์ผํจ
template.setDefaultSerializer( RedisSerializer.json() );
template.setValueSerializer( RedisSerializer.json() );
template.setHashValueSerializer( RedisSerializer.json() );
return template;
}JavaSerializer๋ฅผ ๋ณ๊ฒฝํ๋ ค๋ฉด Auto Configuration์ผ๋ก ์์ฑ๋๋ RedisTemplate ๋์ ์ ์ง์ ๋น์ ์ ์ํด์ผ ํ๋ค.
์ ์ฝ๋๋
- ํค์ ํด์ํค์ ๋ํ Serializer๋ก StringRedisSerializer๋ฅผ ์ฌ์ฉํ๋๋ก ํ๋ค.
- ๊ฐ๊ณผ ํด์๊ฐ์ ๋ํ Serializer๋ก GenericJackson2JsonRedisSerializer๋ฅผ ์ฌ์ฉํ๋๋ก ํ๋ค.
RedisTemplate ์ฌ์ฉ ํ ์คํธ ์ฝ๋
์ง๊ธ๊น์ง Spring Redis Auto Configuration ๋ฐ RedisTemplate์ Serializer๋ฅผ ์ ์ฉํ ์ค์ ์ ํ ๋๋ก ๊ฐ๋จํ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํด ๋ณด์๋ค. UUID ํ์์ ํค์ Person ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๋ ์ฝ๋๋ค.
package com.example.spring.redis.model;
import lombok.Builder;
import org.springframework.data.annotation.Id;
import java.util.UUID;
@Builder
public record Person(@Id UUID id,
String personName,
Integer personAge,
String personNation,
String comment) {
public Person( UUID id, String personName, Integer personAge, String personNation, String comment ) {
this.id = (id == null) ? UUID.randomUUID() : id;
this.personName = personName;
this.personAge = personAge;
this.personNation = personNation;
this.comment = comment;
}
}JavaRedis ์ฐ๊ฒฐ์ ์ํ Configuration
@Configuration
@RequiredArgsConstructor
public class RedisConfiguration {
@Bean
public ClientResourcesBuilderCustomizer clientResourceCustomize() {
return builder -> {
builder.reconnectDelay( Delay.constant( Duration.ofSeconds(50) ) );
builder.ioThreadPoolSize( 4 );
builder.commandLatencyRecorder( CommandLatencyRecorder.disabled() );
builder.commandLatencyPublisherOptions( DefaultEventPublisherOptions.disabled() );
};
}
@Bean
public LettuceClientConfigurationBuilderCustomizer lettucyClientConfigCustomize() {
return builder -> {
if ( builder instanceof
LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder poolingBuilder ) {
// Pool configuration
GenericObjectPoolConfig<?> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxIdle(10);
poolConfig.setMinIdle(5);
poolConfig.setMaxTotal(20);
poolConfig.setMaxWait(Duration.ofSeconds( 30 ));
poolingBuilder.poolConfig( poolConfig );
}
builder.shutdownTimeout( Duration.ofMillis( 1000L ) );
};
}
// lettuceConnectionFactory ์ธ์คํด์ค๋ spring data redis auto configuration์ ์ํด์ ์๋ ์์ฑ๋จ
@Bean
public RedisTemplate<String, Object> redisTemplate( LettuceConnectionFactory lettuceConnectionFactory ) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory( lettuceConnectionFactory );
// set key serializer
// template.setKeySerializer( new StringRedisSerializer(StandardCharsets.UTF_8) ); ์ ๋์ผํจ
template.setKeySerializer( RedisSerializer.string() );
template.setHashKeySerializer( RedisSerializer.string() );
// set value serializer
// template.setDefaultSerializer( new GenericJackson2JsonRedisSerializer() ); ์ ๋์ผํจ
template.setDefaultSerializer( RedisSerializer.json() );
template.setValueSerializer( RedisSerializer.json() );
template.setHashValueSerializer( RedisSerializer.json() );
return template;
}
}Javaํ ์คํธ ์ฝ๋
@ExtendWith( SpringExtension.class )
@ContextConfiguration( classes = { RedisConfiguration.class } )
@DataRedisTest
@EnableAutoConfiguration
public class RedisTest {
@Autowired
RedisTemplate<String, Object> redisTemplate;
final UUID id = UUID.randomUUID();
@Test
@DisplayName( "redis insert test" )
void insert_person_redis_test() {
Person person = Person.builder()
.id( id )
.personName( "test-name" )
.personAge( 100 )
.personNation( "korea" )
.comment( "first commit data" )
.build();
redisTemplate.opsForValue().set( id.toString(), person );
// find data
Object object = redisTemplate.opsForValue().get( id.toString() );
Assertions.assertThat( object ).isInstanceOf( Person.class );
Person find = ( Person ) object;
Assertions.assertThat( find.id().toString() ).isEqualTo( id.toString() );
}
}Javaํ
์คํธ ์ฝ๋๋ฅผ ํตํด์ Redis์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ์ดํด๋ณด๋ฉด JSON ํ์์ผ๋ก ์ ์ฅ๋ ๊ฒ์ ํ์ธํด ๋ณผ ์ ์๋ค.

Spring Data Redis๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ์ฐ๊ฒฐ ์ค์ ์ ์ํด์ ์ํ๋๋ Redis Auto Configuration์ ๋ํด์ ์ดํด๋ณด์๋ค.
application.yml (.properties)์ spring.data.redis ์ค์ ๊ณผ ๋ณ๊ฒฝํ๊ณ ์ ํ๋ ClientResources, ClientConfiguration์ ๋ํ customize ๋น๊ณผ RedisTemplate์ ์ ์ฉํ Serializer๋ฅผ ์ค์ ํ๋ ๊ฒ๋ง์ผ๋ก ์์ฝ๊ฒ Redis๋ฅผ ํ์ฉํ ์ ์๋ค.
๋.
์ํ ์ฝ๋๋ GITLAB ๋งํฌ ์์ ํ์ธํ ์ ์๋ค.