Skip to content

Commit

Permalink
Merge pull request #43 from SWM-WeLike2Coding/feat/redisCluster
Browse files Browse the repository at this point in the history
feat: 레디스 클러스터 모드에 대한 환경 설정 코드 추가 및 github actions 파일 수정
  • Loading branch information
kjungw1025 authored Aug 11, 2024
2 parents ca34033 + 7ca2a87 commit e090872
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ jobs:

- name: Make test properties
env:
PROPERTIES: ${{ secrets.PROPERTIES_TEST }}
PROPERTIES: ${{ secrets.PROPERTIES }}
PROPERTIES_TEST: ${{ secrets.PROPERTIES_TEST }}
run: |
mkdir -p ./src/test/resources && cd "$_"
touch ./application.yml
touch ./application-test.yml
echo $PROPERTIES | base64 --decode > application.yml
echo $PROPERTIES_TEST | base64 --decode > application-test.yml
shell: bash

- name: Setup Gradle
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/dev_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ jobs:

- name: Make test properties
env:
PROPERTIES: ${{ secrets.PROPERTIES_TEST }}
PROPERTIES: ${{ secrets.PROPERTIES }}
PROPERTIES_TEST: ${{ secrets.PROPERTIES_TEST }}
run: |
mkdir -p ./src/test/resources && cd "$_"
touch ./application.yml
touch ./application-test.yml
echo $PROPERTIES | base64 --decode > application.yml
echo $PROPERTIES_TEST | base64 --decode > application-test.yml
shell: bash

- name: Setup Gradle
Expand Down
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
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;
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.wl2c.elswhereuserservice.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}")
Expand Down

0 comments on commit e090872

Please sign in to comment.