29 Nisan 2019 Pazartesi

SpringData Redis JedisConnectionFactory Sınıfı

constructor - XML İle JedisPoolConfig
Örnek
JedisPoolConfig tanımlamak için şöyle yaparız.
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  <!-- max connections -->
  <property name="maxTotal" value="30" />
  <!-- max idle connections -->
  <property name="maxIdle" value="10" />
  <!-- max released connections each time -->
  <property name="numTestsPerEvictionRun" value="1024" />
  <!-- time interval of releasing connection scan (ms) -->
  <property name="timeBetweenEvictionRunsMillis" value="30000" />
  <!-- min connection idle time -->
  <property name="minEvictableIdleTimeMillis" value="1800000" />
  <!-- time interval to release for idle connection, 
  when the number of  idle connection is bigger than 'maxIdle' and reaches this time
  it would be realsed inmediately-->
  <property name="softMinEvictableIdleTimeMillis" value="10000" />
  <!-- max waiting time of getting connection, less than zero means uncertain time,
 default -1 -->
  <property name="maxWaitMillis" value="1500" />
  <!-- test connection work when get connection, default false -->
  <property name="testOnBorrow" value="true" />
  <!-- test idle connection work, default false -->
  <property name="testWhileIdle" value="true" />
 <!-- if it is blocked when connection is exhausted, false throws exception, true blocked
 until timeout, default true-->
  <property name="blockWhenExhausted" value="false" />
</bean>
Örnek
Şöyle yaparız.
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" p:max-total="400" 
:maxIdle="350" p:maxWaitMillis="1000"
  p:test-on-borrow="true" p:test-on-return="true" p:testOnCreate="true" />

<bean id="jedisConnectionFactory"
 class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
     p:host-name="127.0.0.1" p:port="6379" p:use-pool="true" p:password="11223344">
   <constructor-arg ref="jedisPoolConfig"/>
</bean>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
 p:connection-factory-ref="jedisConnectionFactory" p:enable-transaction-support="true"/>
constructor - RedisStandaloneConfiguration 
Şöyle yaparız
@Configuration
public class RedisConfiguration {

  @Bean
  public JedisConnectionFactory jedisConnectionFactory() {
    RedisStandaloneConfiguration redisConfiguration = 
      new RedisStandaloneConfiguration("192.168.0.80", 6379);
    return new JedisConnectionFactory(redisConfiguration);
  }
}
constructor - RedisSentinelConfiguration
Şöyle yaparız
@Configuration
public class RedisConfiguration {
  @Value("${spring.redis.sentinel.master}")
  private String SENTINEL_MASTER;

  @Value("${spring.redis.sentinel.nodes}")
  private String SENTINEL_NODES;

  @Value("${spring.redis.security.enabled:false}")
  private boolean REDIS_SECURITY_ENABLED;

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

  
  @Bean
  public RedisConnectionFactory jedisConnectionFactory() { 
    // create set of sentinel nodes
    Set<String> sentinelNodesSet = new HashSet<>(5);
    StringTokenizer st = new StringTokenizer(SENTINEL_NODES, ",");
    while (st.hasMoreTokens())
      sentinelNodesSet.add(st.nextToken());
    RedisSentinelConfiguration sentinelConfig = new
      RedisSentinelConfiguration(SENTINEL_MASTER, sentinelNodesSet);
    if (REDIS_SECURITY_ENABLED) {
      sentinelConfig.setPassword(REDIS_PASSWORD);
    }
    JedisConnectionFactory jedisConnectionFactory = new
       JedisConnectionFactory(sentinelConfig);
    return jedisConnectionFactory;
  }
}