6 Ekim 2019 Pazar

SpringData Redis LettuceConnectionFactory Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
Bu sınıf 
1. Eğer RedisTemplate kullanacaksak redisTemplate.setConnectionFactory() metoduna geçilir
2. Eğer RedisCacheManager kullanacaksak RedisCacheManager.builder() metoduna geçilir.

Örnek
Şu satırı dahil ederiz.
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
  <version>2.0.5.RELEASE</version>
</dependency>
Örnek
Şu satırı dahil ederiz.. Burada spring ile gelenden daha farkı bir Lettuce sürümü kullanılıyor
<dependency>
  <groupId>org.springframework.session</groupId>
  <artifactId>spring-session-data-redis</artifactId>
  <version>1.3.2.RELEASE</version>
  <type>pom</type>
</dependency>
<dependency>
  <groupId>io.lettuce</groupId>
  <artifactId>lettuce-core</artifactId>
  <version>5.0.4.RELEASE</version>
</dependency>
Açıklaması şöyle.
Developers use Redis for several purposes including for a database, a cache, and a message broker.
Redis'e bağlanmak için Jedis, Luttuce, Redisson kullanılabilir. 

Lettuce kütüphanesi Jedis'ten farklı olarak asenkron çalışabilir

constructor
Örnek - standalone
Şöyle yaparız.
@EnableRedisHttpSession
public class HttpSessionConfig {
  @Bean
  public LettuceConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory();
  }
}
Örnek - standalone
Şöyle yaparız
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
  RedisStandaloneConfiguration redisStandaloneConfiguration =
    new RedisStandaloneConfiguration();
  redisStandaloneConfiguration.setHostName(...);
  redisStandaloneConfiguration.setPort(...);
  
  return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
constructor - RedisSentinelConfiguration
Sentinel ayarlarını okumak için Spring org.springframework.boot.autoconfigure.data.redis.RedisProperties  diye bir sınıf sağlıyor. Bu sınıf application.properties'deki şu ayarları okuyabiliyor
spring.data.redis.port=26379
spring.data.redis.password=
spring.data.redis.sentinel.maste
Açıklaması şöyle
The RedisProperties we defined is a @ConfigrationProperties class. It maps the fields in its own class with the spring.data.redis prefix which we defined in application.properties and presents it to us.
Örnek
Şöyle yaparız.
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = ...
  return new LettuceConnectionFactory(sentinelConfig);
}
Örnek - RedisSentinelConfiguration  + LettuceClientConfiguration 
Şöyle yaparız
@Bean
protected LettuceConnectionFactory redisConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
    .master(redisProperties.getSentinel().getMaster());

  redisProperties.getSentinel().getNodes()
    .forEach(s -> sentinelConfig.sentinel(s, redisProperties.getPort()));

  sentinelConfig.setPassword(RedisPassword.of(redisProperties.getPassword()));

  return new LettuceConnectionFactory(sentinelConfig,
LettuceClientConfiguration.defaultConfiguration());
}
Örnek - RedisSentinelConfiguration  + LettuceClientConfiguration 
Şöyle yaparız
@Configuration
@RequiredArgsConstructor
@EnableCaching
public class RedisConfig {
  private final RedisProperties redisProperties;

  @Bean
  public LettuceConnectionFactory redisConnectionFactory() {
    LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
      .readFrom(ReadFrom.REPLICA_PREFERRED).build();
      
    RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
      .master(redisProperties.getSentinel().getMaster());
    redisProperties.getSentinel().getNodes().forEach(s -> 
      sentinelConfig.sentinel(redisProperties.getUrl(), Integer.valueOf(s)));
    sentinelConfig.setPassword(RedisPassword.of(redisProperties.getPassword()));
    return new LettuceConnectionFactory(sentinelConfig, clientConfig);
  }
}
Örnek - RedisSentinelConfiguration  + LettuceClientConfiguration 
Şöyle yaparız
private final RedisProperties redisProperties = ...;

@Bean
protected LettuceConnectionFactory redisConnectionFactory() {
  RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
    .master(redisProperties.getSentinel().getMaster());

  redisProperties.getSentinel().getNodes()
    .forEach(s -> sentinelConfig.sentinel(s, redisProperties.getPort()));
  sentinelConfig.setPassword(RedisPassword.of(redisProperties.getPassword()));

  LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
    .commandTimeout(redisCommandTimeout)
    .readFrom(ReadFrom.REPLICA_PREFERRED)
    .build();
  return new LettuceConnectionFactory(sentinelConfig, clientConfig);
}




Hiç yorum yok:

Yorum Gönder