Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] redis sentinel 설정 #86

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package pie.tomato.tomatomarket.infrastructure.config;

import java.util.stream.Collectors;

import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
Expand All @@ -14,19 +18,27 @@
@RequiredArgsConstructor
public class RedisConfig {

private final RedisProperties redisProperties;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());
}

@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
private final RedisProperties redisProperties;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
var nodes = redisProperties.getSentinel().getNodes().stream().map(node -> {
String[] hostAndPort = node.split(":");
return new RedisNode(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
}).collect(Collectors.toList());
var sentinelConfiguration = new RedisSentinelConfiguration()
.master(redisProperties.getSentinel().getMaster());
sentinelConfiguration.setSentinels(nodes);

return new LettuceConnectionFactory(sentinelConfiguration);
}

@Bean
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
}
5 changes: 3 additions & 2 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ spring:
username: ${db.dev.datasource.username}
password: ${db.dev.datasource.password}
redis:
host: localhost
port: 6379
sentinel:
master: mymaster
nodes: localhost:25379,localhost:25380,localhost:25381

cloud:
aws:
Expand Down
Loading