본문 바로가기
NoSQL/Redis

Spring boot redis Sentinel 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

 

 

yml property 설정  

spring.redis 부분 참고 

###########################################################
# 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:
    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
###########################################################

 

Sprimg Sentinel Custom config 

.master 에 redis sentinel 구성시 사용 하였던 master 이름을 적어준다. 

이후 센티넬로 구성된 호스트와 아이피를 설정. 보안상  패스워드를 설정한경우엔 패스워드까지 적어준다.

커넥터로 LettuceConnection 을 사용한다.  (Netty 기반이며 성능이 좋음)

 

@Configuration
@EnableRedisRepositories
@Deprecated
public class RedisSentinelConfiguration {
    @Value("${spring.redis.host}")
    private String redisHost;

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

    @Autowired
    ClusterConfigurationProperties clusterConfigurationProperties;

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

        // Standalone 구성 config
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort);
        redisStandaloneConfiguration.setPassword("foobared");

        // Sentinel 구성 config
        org.springframework.data.redis.connection.RedisSentinelConfiguration redisSentinelConfiguration = new org.springframework.data.redis.connection.RedisSentinelConfiguration()
                .master("mymaster")
                .sentinel(redisHost, 11001)
                .sentinel(redisHost, 11002)
                .sentinel(redisHost, 11003)
                ;
        redisSentinelConfiguration.setPassword("foobared");

        return new LettuceConnectionFactory(redisSentinelConfiguration);
    }

    @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 clustering 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