Skip to content

Commit

Permalink
Merge pull request #65 from SWM-WeLike2Coding/conf/redis
Browse files Browse the repository at this point in the history
�feat: 상품 서비스에 레디스 환경설정
  • Loading branch information
kjungw1025 authored Sep 8, 2024
2 parents 922d033 + 16f46dd commit 0628c1c
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ out/

### application.yml ###
application.yml
application-local.yml
application-dev.yml
application-prod.yml
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// redis
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

// Querydsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
Expand Down
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;
}

}
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;
}
}
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();
}
}

0 comments on commit 0628c1c

Please sign in to comment.