沃梦达 / 编程技术 / 数据库 / 正文

四、springboot配置redis

1、添加依赖 dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-redis/artifactId /dependencydependencygroupIdredis.clients/group...

1、添加依赖

  <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-data-redis</artifactId>

   </dependency>

<dependency>

  <groupId>redis.clients</groupId>

  <artifactId>jedis</artifactId>

</dependency>

  

2、添加配置文件 

redis.hostName=192.168.0.120

#\u7aef\u53e3\u53f7  

redis.port=6379

#\u5ba2\u6237\u7aef\u8d85\u65f6\u65f6\u95f4\u5355\u4f4d\u662f\u6beb\u79d2 \u9ed8\u8ba4\u662f2000 

redis.timeout=10000  

 

#\u6700\u5927\u7a7a\u95f2\u6570  

redis.maxIdle=300  

#\u8fde\u63a5\u6c60\u7684\u6700\u5927\u6570\u636e\u5e93\u8fde\u63a5\u6570\u3002\u8bbe\u4e3a0\u8868\u793a\u65e0\u9650\u5236,\u5982\u679c\u662fjedis 2.4\u4ee5\u540e\u7528redis.maxTotal  

#redis.maxActive=600  

#\u63a7\u5236\u4e00\u4e2apool\u53ef\u5206\u914d\u591a\u5c11\u4e2ajedis\u5b9e\u4f8b,\u7528\u6765\u66ff\u6362\u4e0a\u9762\u7684redis.maxActive,\u5982\u679c\u662fjedis 2.4\u4ee5\u540e\u7528\u8be5\u5c5e\u6027  

redis.maxTotal=1000  

#\u6700\u5927\u5efa\u7acb\u8fde\u63a5\u7b49\u5f85\u65f6\u95f4\u3002\u5982\u679c\u8d85\u8fc7\u6b64\u65f6\u95f4\u5c06\u63a5\u5230\u5f02\u5e38\u3002\u8bbe\u4e3a-1\u8868\u793a\u65e0\u9650\u5236\u3002  

redis.maxWaitMillis=1000  

#\u8fde\u63a5\u7684\u6700\u5c0f\u7a7a\u95f2\u65f6\u95f4 \u9ed8\u8ba41800000\u6beb\u79d2(30\u5206\u949f)  

redis.minEvictableIdleTimeMillis=300000  

#\u6bcf\u6b21\u91ca\u653e\u8fde\u63a5\u7684\u6700\u5927\u6570\u76ee,\u9ed8\u8ba43  

redis.numTestsPerEvictionRun=1024  

#\u9010\u51fa\u626b\u63cf\u7684\u65f6\u95f4\u95f4\u9694(\u6beb\u79d2) \u5982\u679c\u4e3a\u8d1f\u6570,\u5219\u4e0d\u8fd0\u884c\u9010\u51fa\u7ebf\u7a0b, \u9ed8\u8ba4-1  

redis.timeBetweenEvictionRunsMillis=30000  

#\u662f\u5426\u5728\u4ece\u6c60\u4e2d\u53d6\u51fa\u8fde\u63a5\u524d\u8fdb\u884c\u68c0\u9a8c,\u5982\u679c\u68c0\u9a8c\u5931\u8d25,\u5219\u4ece\u6c60\u4e2d\u53bb\u9664\u8fde\u63a5\u5e76\u5c1d\u8bd5\u53d6\u51fa\u53e6\u4e00\u4e2a  

redis.testOnBorrow=true  

#\u5728\u7a7a\u95f2\u65f6\u68c0\u67e5\u6709\u6548\u6027, \u9ed8\u8ba4false  

redis.testWhileIdle=true  

 

#redis\u96c6\u7fa4\u914d\u7f6e      

spring.redis.cluster.nodes=10.2.193.28:7000,10.2.193.28:7001,10.2.193.28:7002,10.2.193.29:7003,10.2.193.29:7004,10.2.193.29:7005

spring.redis.cluster.max-redirects=4

 

3、读取配置文件

@Configuration

@PropertySource("classpath:config/redis.properties")

public class RedisConfig {

 

@Value("${redis.hostName}")

private String hostName;

 

@Value("${redis.port}")

private Integer port;

 

@Value("${redis.maxIdle}")

    private Integer maxIdle;

 

    @Value("${redis.maxTotal}")

    private Integer maxTotal;

 

    @Value("${redis.maxWaitMillis}")

    private Integer maxWaitMillis;

 

    @Value("${redis.minEvictableIdleTimeMillis}")

    private Integer minEvictableIdleTimeMillis;

 

    @Value("${redis.numTestsPerEvictionRun}")

    private Integer numTestsPerEvictionRun;

 

    @Value("${redis.timeBetweenEvictionRunsMillis}")

    private long timeBetweenEvictionRunsMillis;

 

    @Value("${redis.testOnBorrow}")

    private boolean testOnBorrow;

 

    @Value("${redis.testWhileIdle}")

    private boolean testWhileIdle;

 

 

    @Value("${spring.redis.cluster.nodes}")

    private String clusterNodes; 

 

    @Value("${spring.redis.cluster.max-redirects}")

    private Integer mmaxRedirectsac;

 

    @Bean("redisPool")

  public JedisPool getJedisPool() {

          JedisPool jedisPool = new JedisPool(hostName, port);

          return jedisPool;

    }

 

    @Bean("redisCluster")

    public JedisCluster getJedisCluster() {

           List<String> nodesList = Arrays.asList(clusterNodes.split(","));

           HashSet<HostAndPort> nodesSet = new HashSet<>();

           if (nodesList != null && nodesList.size() > 0) {

      for (String node : nodesList) {

        if (node != null) {

           String[] hostAndPort = node.split(":");

           HostAndPort hostAndPort2 = new HostAndPort(hostAndPort[0], Integer.parseInt(hostAndPort[1]));

           nodesSet.add(hostAndPort2);

        }

      }

    }

    return new JedisCluster(nodesSet);

  }

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4、获取redis对象 

@Service("redisService")

public class RedisServiceImpl implements IRedisService{

 

    private JedisCommands redis;

    

    @Autowired

    private JedisPool jedisPool;

    

    @Autowired

    private JedisCluster jedisCluster;

    

    @Override

    public void afterPropertiesSet() throws Exception {

        if("test".equals("test")){

            this.redis = jedisPool.getResource();

        }else{

            this.redis = jedisCluster;

        }

    }

 

    public JedisCommands getRedis(){

        return this.redis;

    }

    

    public String getValue(String key){

        return this.redis.get(key);

    }

    

    public String setValue(String key, String value){

        return this.redis.set(key, value);

    }

 

    public Long incr(String key){

        return redis.incr(key);

    }

    

    public String setValue(String key, String value, int seconds){

        return this.redis.setex(key, seconds, value);

    }

    

    public Map<String, String> hgetAll(String key){

        return redis.hgetAll(key);

    }

    public String hmset(String key, Map<String, String> map){

        return redis.hmset(key, map);

    }

    

    public Long expire(String key, int seconds){

        return redis.expire(key, seconds);

    }

    

    public void del(String key) {

    redis.del(key);

    }

 

  @Override

     public List<String> lrange(String key) {

        return redis.lrange(key, 0, -1);

     }

 

    @Override

    public Long lpush(String key, String... string) {

        return redis.lpush(key, string);

    }

}

 

 

public interface IRedisService extends InitializingBean{

 

public String getValue(String key);

 

public String setValue(String key, String value);

 

public Long incr(String key);

 

public Long expire(String key, int seconds);

 

public Map<String, String> hgetAll(String key);

 

public String hmset(String key, Map<String, String> map);

 

public void del(String key);

 

public String setValue(String key, String value, int seconds);

 

public List<String> lrange(String key);

 

public Long lpush(String key, String... string);

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5、测试接口

@RestController

@RequestMapping("/redis")

public class RedisController {

 

@Autowired

private IRedisService redisService;

 

@RequestMapping("/set")

public String set(String key,String value) {

  redisService.setValue(key, value);

  return "success";

}

 

@RequestMapping("/get")

public String get(String key) {

  String value = redisService.getValue(key);

  return value;

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6、启动运行

  

本文标题为:四、springboot配置redis

基础教程推荐