Springcloud +redis集群

springcloud  中 springboot版本是2.X.X以上的,版本不同,集群对应的客户端api方法,连接池不一样.
 

具体操作可以看git上源码.

redis集群的配置比较简单.

添加jar:

    org.springframework.boot
    spring-boot-starter-data-redis



    redis.clients
    jedis
    2.9.0

redis集群的配置文件:
redis:
    cache:
      cluster-nodes:
       192.168.200.X:X ,192.168.200.X:X 192.168.200.X:X ,192.168.200.X:X  ,192.168.200.X:X ,192.168.200.X:X
      # 执行命令超时时间
      command-timeout: 15000
      # 重试次数
      max-attempts: 5
      # 跨集群执行命令时要遵循的最大重定向数量
      max-redirects: 3
      # 连接池最大连接数(使用负值表示没有限制)
      max-active: 16
      # 连接池最大阻塞等待时间(使用负值表示没有限制)
      max-wait: -1
      # 连接池中的最大空闲连接
      max-idle: 8
      # 连接池中的最小空闲连接
      min-idle: 0
      # 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
      test-on-borrow: true
 

package com.ztgx.redis.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //这样该方法支持多种数据类型
    public void set(String key , Object object, Long time){
        //开启事务权限
        stringRedisTemplate.setEnableTransactionSupport(true);
        try {
            //开启事务
            stringRedisTemplate.multi();

            String argString =(String)object;  //强转下
            stringRedisTemplate.opsForValue().set(key, argString);

            //成功就提交
            stringRedisTemplate.exec();
        } catch (Exception e) {
            //失败了就回滚
            stringRedisTemplate.discard();

        }
        if (object instanceof String ) {  //判断下是String类型不
            String argString =(String)object;  //强转下
            //存放String类型的
            stringRedisTemplate.opsForValue().set(key, argString);
        }
        //如果存放Set类型
        if (object instanceof Set) {
            Set valueSet =(Set)object;
            for(String string:valueSet){
                stringRedisTemplate.opsForSet().add(key, string);  //此处点击下源码看下 第二个参数可以放好多
            }
        }
        //设置有效期
        if (time != null) {
            stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
        }

    }
    //做个封装
    public void setString(String key, Object object){
        String argString =(String)object;  //强转下
        //存放String类型的
        stringRedisTemplate.opsForValue().set(key, argString);
    }
    public void setSet(String key, Object object){
        Set valueSet =(Set)object;
        for(String string:valueSet){
            stringRedisTemplate.opsForSet().add(key, string);  //此处点击下源码看下 第二个参数可以放好多
        }
    }

    public String getString(String key){
        return    stringRedisTemplate.opsForValue().get(key);
    }

}
 

 

package com.ztgx.redis.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Web层 - Test
 * Created by xiepengcheng on 2017/9/12.
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/setString")
    public String setString(String key, String value){
        key="name";value="zhangsan";
        redisService.set(key, value, 500l); //超时时间500s   l表示long型
        return "成功";
    }

    @RequestMapping("get")
    public String get(String key){
        key="name";
        return redisService.getString(key);
    }
}
 

package com.ztgx.redis;



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
//@EnableSwagger2
@EnableCaching
public class RedisApplication {
    public static void main(String[] args) {
        SpringApplication.run( RedisApplication.class, args  );
    }

}

 

你可能感兴趣的:(Springcloud,+redis集群)