redis配置

 

在单机Redis环境:Jedis

Jedis jedis = new Jedis("url", 8001);

在多台Redis环境(没有集群):ShardedJedis+ShardedJedisPool

@Configuration
@ConditionalOnExpression("${gcloud.redis.jedisClient.enable:false} == true")
public class JedisConfig {

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

    @Value("${spring.redis.port:}")
    private Integer port;

    @Value("${spring.redis.timeout:}")
    private Integer timeout;

    @Value("${spring.redis.pool.max-idle:}")
    private Integer maxIdle;

    @Value("${spring.redis.pool.max-wait:}")
    private Long maxWaitMillis;

    @Value("${spring.redis.pool.max-active:}")
    private Integer maxActive;

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


    @Bean
    public ShardedJedisPool shardedJedisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        if(maxActive != null){
            jedisPoolConfig.setMaxTotal(maxActive);
        }
        if(maxIdle != null){
            jedisPoolConfig.setMaxIdle(maxIdle);
        }
        if(maxWaitMillis != null){
            jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        }
        List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>();
        jedisShardInfoList.add(new JedisShardInfo(host, port));
        return new ShardedJedisPool(jedisPoolConfig, jedisShardInfoList);
    }
 

在Redis集群环境:JedisCluster

 

  1. Set<HostAndPort> nodes = new HashSet<>();  
  2.         nodes.add(new HostAndPort("192.168.25.153", 7001));  
  3.         nodes.add(new HostAndPort("192.168.25.153", 7002));  
  4.         nodes.add(new HostAndPort("192.168.25.153", 7003));  
  5.         nodes.add(new HostAndPort("192.168.25.153", 7004));  
  6.         nodes.add(new HostAndPort("192.168.25.153", 7005));  
  7.         nodes.add(new HostAndPort("192.168.25.153", 7006));  
  8.         JedisCluster jedisCluster = new JedisCluster(nodes);  
  9.         // 第二步:直接使用JedisCluster对象操作redis。在系统中单例存在。  
  10.         jedisCluster.set("hello", "100");  
  11.         String result = jedisCluster.get("hello");  
  12.         // 第三步:打印结果  
  13.         System.out.println(result);  
  14.         // 第四步:系统关闭前,关闭JedisCluster对象。  
  15.         jedisCluster.close();  

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值