-
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 #65 from SWM-WeLike2Coding/conf/redis
�feat: 상품 서비스에 레디스 환경설정
- Loading branch information
Showing
5 changed files
with
157 additions
and
0 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
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 |
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
82 changes: 82 additions & 0 deletions
82
src/main/java/com/wl2c/elswhereproductservice/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.elswhereproductservice.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; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/wl2c/elswhereproductservice/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.wl2c.elswhereproductservice.global.config.redis; | ||
|
||
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.RedisPassword; | ||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@EnableCaching | ||
@Profile({"local", "test", "dev"}) | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.data.redis.port}") | ||
private int port; | ||
|
||
@Value("${spring.data.redis.password}") | ||
private String password; | ||
|
||
@Bean | ||
public LettuceConnectionFactory redisConnectionFactory() { | ||
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, port); | ||
redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); | ||
return new LettuceConnectionFactory(redisStandaloneConfiguration); | ||
} | ||
|
||
@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; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/wl2c/elswhereproductservice/global/config/redis/RedisKeys.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,24 @@ | ||
package com.wl2c.elswhereproductservice.global.config.redis; | ||
|
||
public class RedisKeys { | ||
public static final String KEY_DELIMITER = ":"; | ||
|
||
public static String combine(Object key1, Object key2) { | ||
return key1 + KEY_DELIMITER + key2; | ||
} | ||
|
||
public static String combine(Object key1, Object key2, Object key3) { | ||
return key1 + KEY_DELIMITER + key2 + KEY_DELIMITER + key3; | ||
} | ||
|
||
public static String combine(Object... keys) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < keys.length; i++) { | ||
sb.append(keys[i]); | ||
if (i < keys.length - 1) { | ||
sb.append(KEY_DELIMITER); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |