스프링 - redis cluster

아침7시개발·2022년 6월 8일
0

스프링

목록 보기
3/6

gradle 추가

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

application.yml 설정

spring:
  #Redis Cluster Config(마스터노드의 리스트)
  redis:
    cluster:
      nodes: 127.0.0.1:7000, 127.0.0.1:7001, 127.0.0.1:7002, 127.0.0.1:7003
      #클러스터 노드간의 리다이렉션 숫자를 제한
      max-redirects: 3

컴포넌트 추가

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/** * Redis Cluster Config  * */
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterConfigurationProperties {
    /**     
     * * spring.redis.cluster.nodes[0]=127.0.0.1:7000
     * * spring.redis.cluster.nodes[1]=127.0.0.1:7001
     * * spring.redis.cluster.nodes[2]=127.0.0.1:7002
     * * spring.redis.cluster.nodes[3]=127.0.0.1:7003
     * */
    List<String> nodes;
    public List<String> getNodes() {
        return nodes;
    }
    public void setNodes(List<String> nodes) {
        this.nodes = nodes;
    }
}

RedisConfig 주입


import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import java.util.Objects;
import static org.springframework.session.FlushMode.IMMEDIATE;

@Configuration
@EnableRedisRepositories
@RequiredArgsConstructor
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 18000, redisNamespace="user", flushMode = IMMEDIATE)
public class RedisConfig {
    @Autowired
    RedisClusterConfigurationProperties clusterProperties;
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        String profile = System.getProperty("spring.profiles.active");
        if (Objects.equals(profile,"local")) {
            return new LettuceConnectionFactory();
        } else {
            return new LettuceConnectionFactory(new RedisClusterConfiguration(clusterProperties.getNodes()));
        }
    }
    @Primary
    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(SessionUser.class));
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(SessionUser.class));

        return redisTemplate;
    }


}
profile
쉬엄쉬엄하는 개발자

0개의 댓글