-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from soma-baekgu/feature/BG-376-redis-cluster
[BG-376]: 레디스 클러스터링 (4h / 3h)
- Loading branch information
Showing
15 changed files
with
315 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
api/src/main/kotlin/com/backgu/amaker/api/config/ClusterConfigProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.backgu.amaker.api.config | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
@ConfigurationProperties(prefix = "spring.data.redis.cluster") | ||
class ClusterConfigProperties { | ||
lateinit var nodes: List<String> | ||
var maxRedirects: Int = 3 | ||
} |
75 changes: 75 additions & 0 deletions
75
api/src/main/kotlin/com/backgu/amaker/api/config/ProdRedisConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.backgu.amaker.api.config | ||
|
||
import io.lettuce.core.SocketOptions | ||
import io.lettuce.core.cluster.ClusterClientOptions | ||
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.data.redis.connection.RedisClusterConfiguration | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.connection.RedisPassword | ||
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories | ||
import org.springframework.data.redis.serializer.StringRedisSerializer | ||
import java.time.Duration | ||
|
||
@Configuration | ||
@Profile("prod") | ||
@EnableRedisRepositories | ||
class ProdRedisConfig( | ||
private val clusterConfigProperties: ClusterConfigProperties, | ||
) { | ||
@Value("\${spring.data.redis.password}") | ||
lateinit var password: String | ||
|
||
@Bean | ||
fun redisConnectionFactory(): RedisConnectionFactory { | ||
val clusterConfiguration = RedisClusterConfiguration(clusterConfigProperties.nodes) | ||
clusterConfiguration.maxRedirects = clusterConfigProperties.maxRedirects | ||
clusterConfiguration.password = RedisPassword.of(password) | ||
|
||
val socketOptions = | ||
SocketOptions | ||
.builder() | ||
.connectTimeout(Duration.ofMillis(100L)) | ||
.keepAlive(true) | ||
.build() | ||
|
||
val clusterTopologyRefreshOptions = | ||
ClusterTopologyRefreshOptions | ||
.builder() | ||
.dynamicRefreshSources(true) | ||
.enableAllAdaptiveRefreshTriggers() | ||
.enablePeriodicRefresh(Duration.ofMinutes(30L)) | ||
.build() | ||
|
||
val clientOptions = | ||
ClusterClientOptions | ||
.builder() | ||
.topologyRefreshOptions(clusterTopologyRefreshOptions) | ||
.socketOptions(socketOptions) | ||
.build() | ||
|
||
val clientConfiguration = | ||
LettuceClientConfiguration | ||
.builder() | ||
.clientOptions(clientOptions) | ||
.commandTimeout(Duration.ofMillis(3000L)) | ||
.build() | ||
|
||
return LettuceConnectionFactory(clusterConfiguration, clientConfiguration) | ||
} | ||
|
||
@Bean | ||
fun redisTemplate(): RedisTemplate<String, Any> { | ||
val template = RedisTemplate<String, Any>() | ||
template.connectionFactory = redisConnectionFactory() | ||
template.keySerializer = StringRedisSerializer() | ||
template.valueSerializer = StringRedisSerializer() | ||
return template | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
api/src/main/kotlin/com/backgu/amaker/api/config/ProdRedissonConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.backgu.amaker.api.config | ||
|
||
import org.redisson.Redisson | ||
import org.redisson.api.RedissonClient | ||
import org.redisson.config.Config | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
|
||
@Configuration | ||
@Profile("prod") | ||
class ProdRedissonConfig( | ||
private val clusterConfigProperties: ClusterConfigProperties, | ||
) { | ||
@Value("\${spring.data.redis.password}") | ||
lateinit var password: String | ||
|
||
@Bean | ||
fun redissonClient(): RedissonClient { | ||
val config = Config() | ||
val clusterServersConfig = | ||
config | ||
.useClusterServers() | ||
.setScanInterval(2000) | ||
.setConnectTimeout(100) | ||
.setTimeout(3000) | ||
.setRetryAttempts(3) | ||
.setRetryInterval(1500) | ||
|
||
clusterConfigProperties.nodes.forEach { node -> | ||
clusterServersConfig.addNodeAddress("redis://$node") | ||
} | ||
|
||
if (password.isNotEmpty()) { | ||
clusterServersConfig.setPassword(password) | ||
} | ||
|
||
return Redisson.create(config) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...fication/src/main/kotlin/com/backgu/amaker/notification/config/ClusterConfigProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.backgu.amaker.notification.config | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
@ConfigurationProperties(prefix = "spring.data.redis.cluster") | ||
class ClusterConfigProperties { | ||
lateinit var nodes: List<String> | ||
var maxRedirects: Int = 3 | ||
} |
77 changes: 77 additions & 0 deletions
77
notification/src/main/kotlin/com/backgu/amaker/notification/config/ProdRedisConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.backgu.amaker.notification.config | ||
|
||
import com.backgu.amaker.infra.redis.session.SessionRedisData | ||
import io.lettuce.core.SocketOptions | ||
import io.lettuce.core.cluster.ClusterClientOptions | ||
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.data.redis.connection.RedisClusterConfiguration | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.connection.RedisPassword | ||
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer | ||
import org.springframework.data.redis.serializer.StringRedisSerializer | ||
import java.time.Duration | ||
|
||
@Configuration | ||
@Profile("prod") | ||
@EnableRedisRepositories | ||
class ProdRedisConfig( | ||
private val clusterConfigProperties: ClusterConfigProperties, | ||
) { | ||
@Value("\${spring.data.redis.password}") | ||
lateinit var password: String | ||
|
||
@Bean | ||
fun redisConnectionFactory(): RedisConnectionFactory { | ||
val clusterConfiguration = RedisClusterConfiguration(clusterConfigProperties.nodes) | ||
clusterConfiguration.maxRedirects = clusterConfigProperties.maxRedirects | ||
clusterConfiguration.password = RedisPassword.of(password) | ||
|
||
val socketOptions = | ||
SocketOptions | ||
.builder() | ||
.connectTimeout(Duration.ofMillis(100L)) | ||
.keepAlive(true) | ||
.build() | ||
|
||
val clusterTopologyRefreshOptions = | ||
ClusterTopologyRefreshOptions | ||
.builder() | ||
.dynamicRefreshSources(true) | ||
.enableAllAdaptiveRefreshTriggers() | ||
.enablePeriodicRefresh(Duration.ofMinutes(30L)) | ||
.build() | ||
|
||
val clientOptions = | ||
ClusterClientOptions | ||
.builder() | ||
.topologyRefreshOptions(clusterTopologyRefreshOptions) | ||
.socketOptions(socketOptions) | ||
.build() | ||
|
||
val clientConfiguration = | ||
LettuceClientConfiguration | ||
.builder() | ||
.clientOptions(clientOptions) | ||
.commandTimeout(Duration.ofMillis(3000L)) | ||
.build() | ||
|
||
return LettuceConnectionFactory(clusterConfiguration, clientConfiguration) | ||
} | ||
|
||
@Bean | ||
fun redisTemplate(): RedisTemplate<String, SessionRedisData> { | ||
val template = RedisTemplate<String, SessionRedisData>() | ||
template.connectionFactory = redisConnectionFactory() | ||
template.keySerializer = StringRedisSerializer() | ||
template.valueSerializer = GenericJackson2JsonRedisSerializer() | ||
return template | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
realtime/src/main/kotlin/com/backgu/amaker/realtime/config/ClusterConfigProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.backgu.amaker.realtime.config | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
@ConfigurationProperties(prefix = "spring.data.redis.cluster") | ||
class ClusterConfigProperties { | ||
lateinit var nodes: List<String> | ||
var maxRedirects: Int = 3 | ||
} |
77 changes: 77 additions & 0 deletions
77
realtime/src/main/kotlin/com/backgu/amaker/realtime/config/ProdRedisConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.backgu.amaker.realtime.config | ||
|
||
import com.backgu.amaker.infra.redis.session.SessionRedisData | ||
import io.lettuce.core.SocketOptions | ||
import io.lettuce.core.cluster.ClusterClientOptions | ||
import io.lettuce.core.cluster.ClusterTopologyRefreshOptions | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.data.redis.connection.RedisClusterConfiguration | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.connection.RedisPassword | ||
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer | ||
import org.springframework.data.redis.serializer.StringRedisSerializer | ||
import java.time.Duration | ||
|
||
@Configuration | ||
@Profile("prod") | ||
@EnableRedisRepositories | ||
class ProdRedisConfig( | ||
private val clusterConfigProperties: ClusterConfigProperties, | ||
) { | ||
@Value("\${spring.data.redis.password}") | ||
lateinit var password: String | ||
|
||
@Bean | ||
fun redisConnectionFactory(): RedisConnectionFactory { | ||
val clusterConfiguration = RedisClusterConfiguration(clusterConfigProperties.nodes) | ||
clusterConfiguration.maxRedirects = clusterConfigProperties.maxRedirects | ||
clusterConfiguration.password = RedisPassword.of(password) | ||
|
||
val socketOptions = | ||
SocketOptions | ||
.builder() | ||
.connectTimeout(Duration.ofMillis(100L)) | ||
.keepAlive(true) | ||
.build() | ||
|
||
val clusterTopologyRefreshOptions = | ||
ClusterTopologyRefreshOptions | ||
.builder() | ||
.dynamicRefreshSources(true) | ||
.enableAllAdaptiveRefreshTriggers() | ||
.enablePeriodicRefresh(Duration.ofMinutes(30L)) | ||
.build() | ||
|
||
val clientOptions = | ||
ClusterClientOptions | ||
.builder() | ||
.topologyRefreshOptions(clusterTopologyRefreshOptions) | ||
.socketOptions(socketOptions) | ||
.build() | ||
|
||
val clientConfiguration = | ||
LettuceClientConfiguration | ||
.builder() | ||
.clientOptions(clientOptions) | ||
.commandTimeout(Duration.ofMillis(3000L)) | ||
.build() | ||
|
||
return LettuceConnectionFactory(clusterConfiguration, clientConfiguration) | ||
} | ||
|
||
@Bean | ||
fun redisTemplate(): RedisTemplate<String, SessionRedisData> { | ||
val template = RedisTemplate<String, SessionRedisData>() | ||
template.connectionFactory = redisConnectionFactory() | ||
template.keySerializer = StringRedisSerializer() | ||
template.valueSerializer = GenericJackson2JsonRedisSerializer() | ||
return template | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters