栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

解决springboot2.x报错:io.lettuce.core.RedisAsyncCommandsImpl cannot be cast to redis.clients.jedis.Jedis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决springboot2.x报错:io.lettuce.core.RedisAsyncCommandsImpl cannot be cast to redis.clients.jedis.Jedis

解决springboot2报错:io.lettuce.core.RedisAsyncCommandsImpl cannot be cast to redis.clients.jedis.Jedis的问题

源代码:

  redisTemplate.opsForValue().set("int_key", "1");
        stringRedisTemplate.opsForValue().set("int", "1");
        //+1运算
        stringRedisTemplate.opsForValue().increment("int");

        //-1运算,stringRedisTemplate耐不活
        //所以,通过StringRedisTemplate获取底层Jedis连接
        Jedis jedis = (Jedis) stringRedisTemplate.
                getConnectionFactory().
                getConnection().
                getNativeConnection();
        
        //-1运算
        jedis.decr("int");

        Map hash = new HashMap<>();

        hash.put("file1","value1");
        hash.put("file2","value2");

        stringRedisTemplate.opsForHash().putAll("hash",hash);

        BoundHashOperations hashOps =
                stringRedisTemplate.boundHashOps("hash");
        hashOps.delete("file1","file2");
        hashOps.put("file4","value5");

        Map map = new HashMap<>();
        map.put("success",true);
        return map;

重点就是出错在:

		//-1运算,stringRedisTemplate耐不活
        //所以,通过StringRedisTemplate获取底层Jedis连接
        Jedis jedis = (Jedis) stringRedisTemplate.
                getConnectionFactory().
                getConnection().
                getNativeConnection();

因为springboot2.x中默认使用了Lettuce集成了Redis服务,
所以spring-boot-starter-data-redis中默认只引入了Lettuce,而没有引入Jedis

所以需要我们在pom文件中手动引入**Jedis**,并且去除redis中的Lettuce

pom:


    org.springframework.boot
    spring-boot-starter-data-redis
    
        
            io.lettuce
            lettuce-core
        
    


    org.springframework.boot
    spring-boot-starter-test
    test


    redis.clients
    jedis

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号