redis集群spring配置(java形式配置)

  1. maven包

     
     
     	redis.clients
     	jedis
     	2.9.0
     
    
  2. 配置文件

address1=192.168.55.130:7001
address2=192.168.55.130:7002
address3=192.168.55.130:7003
address4=192.168.55.130:7004
address5=192.168.55.130:7005
address6=192.168.55.130:7006
  1. 代码结构

redis集群spring配置(java形式配置)_第1张图片

  1. redis配置类


 package com.config;

import com.config.Factory.JedisClusterFactory;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import redis.clients.jedis.JedisCluster;

/**
 * @author lyh on 2019/1/18
 */
@Configuration
public class RedisConfig {

    private Resource AddressConfig =new ClassPathResource("connect-redis.properties");

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Bean
    public GenericObjectPoolConfig genericObjectPoolConfig(){
        GenericObjectPoolConfig config =new GenericObjectPoolConfig();
        config.setMaxWaitMillis(-1);
        config.setMaxTotal(1000);
        config.setMinIdle(8);
        config.setMaxIdle(100);
        return config;
    }

    @Bean
    public JedisCluster jedisClusterFactory()throws Exception{
        JedisClusterFactory jedisClusterFactory = new JedisClusterFactory();
        jedisClusterFactory.setAddressConfig(AddressConfig);
        jedisClusterFactory.setAddressKeyPrefix("address");
        jedisClusterFactory.setTimeout(30000);
        jedisClusterFactory.setMaxRedirections(6);
        jedisClusterFactory.setGenericObjectPoolConfig(genericObjectPoolConfig());
        jedisClusterFactory.afterPropertiesSet();
        JedisCluster jedisCluster= jedisClusterFactory.getObject();
        logger.info("jedisCluster===="+jedisCluster);
        return jedisCluster;
    }

}
  1. 使用redis
     @Autowired
        JedisCluster JedisCluster;

结语:
整个配置的过程还是比较简单的,java配置的优势是调试简单,代码机构整洁,逻辑更加清晰,spring4.0以后推荐使用java方式的配置,现在idea的提示也快,java配置敲起来也快。
**

链接: 项目源码

**

你可能感兴趣的:(大数据)