-
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 #43 from SWM-WeLike2Coding/feat/redisCluster
feat: 레디스 클러스터 모드에 대한 환경 설정 코드 추가 및 github actions 파일 수정
- Loading branch information
Showing
5 changed files
with
97 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,6 @@ out/ | |
|
||
### application.yml ### | ||
application.yml | ||
application-local.yml | ||
application-dev.yml | ||
application-prod.yml |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/wl2c/elswhereuserservice/global/config/redis/RedisClusterConfig.java
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,82 @@ | ||
package com.wl2c.elswhereuserservice.global.config.redis; | ||
|
||
import io.lettuce.core.ClientOptions; | ||
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.cache.annotation.EnableCaching; | ||
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.RedisNode; | ||
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.serializer.StringRedisSerializer; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
@Configuration | ||
@EnableCaching | ||
@Profile({"prod"}) | ||
public class RedisClusterConfig { | ||
|
||
@Value("${spring.data.redis.cluster.nodes}") | ||
private List<String> clusterNodes; | ||
|
||
@Value("${spring.data.redis.password}") | ||
private String password; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
List<RedisNode> redisNodes = clusterNodes.stream() | ||
.map(clusterNode -> new RedisNode(clusterNode.split(":")[0], Integer.parseInt(clusterNode.split(":")[1]))) | ||
.toList(); | ||
RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(); | ||
clusterConfiguration.setClusterNodes(redisNodes); | ||
clusterConfiguration.setPassword(password); | ||
|
||
// Socket 옵션 | ||
SocketOptions socketOptions = SocketOptions.builder() | ||
.connectTimeout(Duration.ofMillis(100L)) | ||
.keepAlive(true) | ||
.build(); | ||
|
||
// Cluster topology refresh 옵션 | ||
ClusterTopologyRefreshOptions clusterTopologyRefreshOptions = ClusterTopologyRefreshOptions.builder() | ||
.dynamicRefreshSources(true) | ||
.enableAllAdaptiveRefreshTriggers() | ||
.enablePeriodicRefresh(Duration.ofMinutes(30L)) | ||
.build(); | ||
|
||
// Cluster Client 옵션 | ||
ClientOptions clientOptions = ClusterClientOptions.builder() | ||
.topologyRefreshOptions(clusterTopologyRefreshOptions) | ||
.socketOptions(socketOptions) | ||
.build(); | ||
|
||
// Lettuce Client 옵션 | ||
LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder() | ||
.clientOptions(clientOptions) | ||
.commandTimeout(Duration.ofMillis(3000L)) | ||
.build(); | ||
|
||
return new LettuceConnectionFactory(clusterConfiguration, clientConfiguration); | ||
} | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate() { | ||
RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setHashValueSerializer(new StringRedisSerializer()); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory()); | ||
return redisTemplate; | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/wl2c/elswhereuserservice/global/config/redis/RedisConfig.java
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