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=

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

redis.timeout=  

#\u6700\u5927\u7a7a\u95f2\u6570  

redis.maxIdle=  

#\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=  

#\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=  

#\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-\u8868\u793a\u65e0\u9650\u5236\u3002  

redis.maxWaitMillis=  

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

redis.minEvictableIdleTimeMillis=  

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

redis.numTestsPerEvictionRun=  

#\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-  

redis.timeBetweenEvictionRunsMillis=  

#\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:,10.2.193.28:,10.2.193.28:,10.2.193.29:,10.2.193.29:,10.2.193.29:

spring.redis.cluster.max-redirects=

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() > ) {

      for (String node : nodesList) {

        if (node != null) {

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

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

           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, , -);

     }

    @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的更多相关文章

  1. springboot(四).配置FastJson自定义消息转化器

    配置FastJson自定义消息转化器 一.fastJson简介 fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件.它是目前json解析最快 ...

  2. SpringBoot中整合Redis、Ehcache使用配置切换 并且整合到Shiro中

    在SpringBoot中Shiro缓存使用Redis.Ehcache实现的两种方式实例 SpringBoot 中配置redis作为session 缓存器. 让shiro引用 本文是建立在你是使用这sh ...

  3. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  4. (一)由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  5. springboot配置redis+jedis,支持基础redis,并实现jedis GEO地图功能

    Springboot配置redis+jedis,已在项目中测试并成功运行,支持基础redis操作,并通过jedis做了redis GEO地图的java实现,GEO支持存储地理位置信息来实现诸如附近的人 ...

  6. springboot配置redis

    https://www.cnblogs.com/xiaoping1993/p/7761123.html https://www.cnblogs.com/gdpuzxs/p/7222309.html s ...

  7. springboot中配置主从redis

    测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...

  8. SpringBoot配置redis和分布式session-redis

    springboot项目 和传统项目 配置redis的区别,更加简单方便,在分布式系统中,解决sesssion共享问题,可以用spring session redis. 1.pom.xml <d ...

  9. Redis(四)-配置

    Redis 配置 Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过 CONFIG 命令查看或设置配置项. 语法 Redis CONFIG 命令格式如下: ...

  10. springboot(七).springboot整合jedis实现redis缓存

    我们在使用springboot搭建微服务的时候,在很多时候还是需要redis的高速缓存来缓存一些数据,存储一些高频率访问的数据,如果直接使用redis的话又比较麻烦,在这里,我们使用jedis来实现r ...

随机推荐

  1. L265 - 5 questions to ask yourself before you ask for a raise or promotion

    You’ve been in your role for a while now, giving 110% to every assignment your manager hands out. Yo ...

  2. 七、在U-boot中让LCD显示图片

    1. 增加Nandflash读取代码 因为要显示图片,而图片明显是放在Nandflash中比较合适,因此需要有能够操作Nandflash的函数.在U-boot中已经有能操作Nandflash的函数了, ...

  3. office web apps server安装部署

    操作系统:windows 2012 软件下载地址: 链接:https://pan.baidu.com/s/1c3WWFs8 密码:4dcy NDP452-KB2901954-Web.exe(.Net ...

  4. POJ 2369 Permutations(置换群概念题)

    Description We remind that the permutation of some final set is a one-to-one mapping of the set onto ...

  5. Java中的13个原子操作类

    java.util.concurrent.atomic包一共提供了13个类.属于4种类型的原子更新方式,分别是原子更新基本类型,原子更新数组,原子更新引用和原子更新属性.Atomic包里的类基本都是使 ...

  6. github上DQN代码的环境搭建,及运行(Human-Level Control through Deep Reinforcement Learning)conda配置

    最近师弟在做DQN的实验,由于是强化学习方面的东西,正好和我现在的研究方向一样于是我便帮忙跑了跑实验,于是就有了今天的这个内容. 首先在github上进行搜寻,如下图: 发现第一个星数最多,而且远高于 ...

  7. pytorch实现autoencoder

    关于autoencoder的内容简介可以参考这一篇博客,可以说写的是十分详细了https://sherlockliao.github.io/2017/06/24/vae/ 盗图一张,自动编码器讲述的是 ...

  8. shell 脚本中将输出内容赋值给一个变量时的换行问题

    for ((i=0; i<4; i++) do ....... done 如果使用 echo $a 输出变量,则变量中的 换行都会被忽略掉,所有内容输出到一行 而使用 echo "$a ...

  9. JS中的变量与常量

    变量 1.创建变量 1.先声明,后赋值 使用var关键字进行变量的声明 使用=进行变量的赋值 自定义变量名 2.声明的同时赋值 var age = 20: 2.命名规范 1.由数字,字母,下划线和$组 ...

  10. numpy unable to find vcvarsall.bat

    出现这种情况,是需要引用vc的编译器,可以安装vs来解决,并且安装numpy前, 设置如下宏 SET VS100COMNTOOLS=%VS110COMNTOOLS% SET VS90COMNTOOLS ...