본문 바로가기
NoSQL/Redis

spring boot redis clustering config

by 아이티.파머 2020. 1. 13.
반응형

2020/01/10 - [NoSQL/Redis] - Redis 특징

2020/01/10 - [NoSQL/Redis] - Docker Redis

2020/01/10 - [NoSQL/Redis] - Spring Boot Redis (standard)

2020/01/10 - [NoSQL/Redis] - Redis install linux

2020/01/10 - [NoSQL/Redis] - Redis Replication (Master Slave) 구성

2020/01/10 - [NoSQL/Redis] - Redis Sentinel 구성

2020/01/10 - [NoSQL/Redis] - Redis Clustering 구성

2020/01/13 - [NoSQL/Redis] - Spring boot redis Sentinel Config

2020/01/13 - [NoSQL/Redis] - spring boot redis clustering config

 

spring boot redis clustering config

2020/01/10 - [NoSQL/Redis] - Redis Clustering 구성 2020/01/10 - [NoSQL/Redis] - Redis Sentinel 구성 2020/01/10 - [NoSQL/Redis] - Redis Replication (Master Slave) 구성 2020/01/10 - [NoSQL/Redis] - Re..

mycup.tistory.com

 

 

yml property

Redis Cluster 부분 참고

###########################################################
# Production - Server Configuration
###########################################################
server:
  port: 8080


###########################################################
# Spring Default  Configuration
###########################################################
spring:
  devtools:
    restart:
      enabled: false
  datasource:
    url: jdbc:mysql://{IP}:3306/audience?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Seoul
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: mezzo2018
    hikari:
      pool-name: hikari-cp
      minimum-idle: 5
      maximum-pool-size: 200
      idle-timeout: 10000
      connection-timeout: 10000
      validation-timeout: 10000
      max-lifetime: 580000
      data-source-properties:
        cachePrepStmts: true
        prepStmtCacheSize: 250
        prepStmtCacheSqlLimit: 2048
        useServerPrepStmts: true
  jpa:
    generate-ddl: true
    open-in-view: true
    show-sql: false
    hibernate:
      ddl-auto: validate
      naming:
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
        implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    database: mysql
    database-platform: org.hibernate.dialect.MySQL5Dialect
    properties:
      hibernate.jdbc.batch_size: 50
      hibernate.jdbc.batch_versioned_data: true
      hibernate.order_inserts: true
      hibernate.order_updates: true
      hibernate.enable_lazy_load_no_trans: true
      hibernate.format_sql: true
  data:
    jpa:
      repositories:
        enabled: true
  redis:
    cluster:
      nodes:
        - 127.0.0.1:6379
        - 127.0.0.1:6380
        - 127.0.0.1:6381
    port: 6379
    host: {IP}
    password: foobared


###########################################################
# Production - Logging policy
###########################################################
logging:
  file: logs/application-prod.log
  config: classpath:logback-spring.xml
  level:
    root: INFO
    cj.audience: debug  #WARN  # 초기 디버그 모드로 운영후 이후 INFO 로 변경
    org.springframework: debug
    org.hibernate: ERROR
    org.hibernate.type: INFO #TRACE
    org.hibernate.type.BasicTypeRegistry: INFO #WARN

###########################################################
# ETC properties
###########################################################

 

 

사용자 환경 설정 

클러스터 노드를 yml에서 읽어온뒤 인젝션 하여 해당 노드들을 Cluster Node로 등록 시켜준다.

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
class ClusterConfigurationProperties {

    /*
     * spring.redis.cluster.nodes[0] = 127.0.0.1:7379
     * spring.redis.cluster.nodes[1] = 127.0.0.1:7380
     * ...
     */
    List<String> nodes;

    /**
     * Get initial collection of known cluster nodes in format {@code host:port}.
     *
     * @return
     */
    public List<String> getNodes() {
        return nodes;
    }

    public void setNodes(List<String> nodes) {
        this.nodes = nodes;
    }
}


/**
 * <pre>
 * Description :
 *
 *
 * </pre>
 *
 * @author skan
 * @version Copyright (C) 2020 by CJENM|Mezzomedia. All right reserved.
 * @since 2020-01-06
 */
@Configuration
@EnableRedisRepositories
public class RedisConfiguration {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Value("${spring.redis.password}")
    private String password;

    @Autowired ClusterConfigurationProperties clusterConfigurationProperties;

    /**
     * Netty 기반 ConnectionFactory
     *
     * @return
     */
    @Bean
    public RedisConnectionFactory redisConnectionFactory() {

        // clustering  구성 config
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        clusterConfigurationProperties.getNodes().forEach(s -> {
            String[] url = s.split(":");
            redisClusterConfiguration.clusterNode(url[0],Integer.parseInt(url[1]));
        });

        return new LettuceConnectionFactory(redisClusterConfiguration);
    }

    @Bean
    public RedisTemplate<String, ?> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(this.redisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        //redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(UserEntity.class)); // <- 주고 받을 데이터(Entity)를 미리 설정 할때
        return redisTemplate;
    }
}
반응형

'NoSQL > Redis' 카테고리의 다른 글

Spring boot redis Sentinel Config  (0) 2020.01.13
Redis Clustering 구성  (0) 2020.01.10
Redis Sentinel 구성  (0) 2020.01.10
Redis Replication (Master Slave) 구성  (2) 2020.01.10
Redis install linux  (0) 2020.01.10