redis.clients.jedis.exceptions.JedisDataException: ERR Syntax error, try CLIENT (LIST | KILL ip:port

Table of contents

1. Error analysis

2. Check whether redis is connected

3. Use Lettuce


1. Error analysis

This error problem mainly depends on whether the redis configuration in your application.properties file is correct. Mine is like this.

# Redis服务器配置
spring.data.redis.host=127.0.0.1
spring.data.redis.port=6379
spring.data.redis.password=111111(没有设置密码就不要这段就行)

# Redis连接池配置
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.data.redis.timeout=30000

If your Redis connection pool configuration is in application.propertiesthe file it should be correct. However, since you are still experiencing Redis connection issues, there may be other reasons for this error.

2. Check whether redis is connected

First, make sure the Redis server is running and accessible from the host where your application is located. You can use the following command to test whether the Redis server is running properly.

Use command:

redis-cli ping

 If the Redis server is not running, make sure to start it.

Next, please check the application code, especially the Redis-related parts, to ensure that no incorrect Redis commands are sent. Make sure you use a version of the Redis client library that is compatible with your application and Redis server.

3. Use Lettuce

If the problem persists, you can try switching to using Lettuce as the Redis client, as it is the Redis client supported by Spring Boot 2.x by default. Here is pom.xmlan example of how to add a Lettuce dependency in:

        <dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </dependency>

You can modify the Redis connection factory configuration to work with Lettuce:

RedisConfig.java

import java.time.Duration;

/**
 * Redis 配置类
 */
@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
        lettuceConnectionFactory.setHostName("127.0.0.1");
        lettuceConnectionFactory.setPort(6379);
        lettuceConnectionFactory.setPassword("111111");

        System.out.println("Redis 连接工厂:" + lettuceConnectionFactory.toString());
        return lettuceConnectionFactory;
    }

Lettuce will be used as the Redis client and will no longer depend on Jedis. After switching to Lettuce, run your application again to see if you still encounter the same problem.

Complete RedisConfig configuration

package com.tscabinet.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.serializer.*;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

/**
 * Redis 配置类
 */
@Configuration
@EnableCaching
public class RedisConfig {

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
        lettuceConnectionFactory.setHostName("127.0.0.1");
        lettuceConnectionFactory.setPort(6379);
        lettuceConnectionFactory.setPassword("111111");

        System.out.println("Redis 连接工厂:" + lettuceConnectionFactory.toString());
        return lettuceConnectionFactory;
    }


    /**
     * 配置缓存管理器
     * @param factory Redis 线程安全连接工厂
     * @return 缓存管理器
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        // 生成两套默认配置,通过 Config 对象即可对缓存进行自定义配置
        RedisCacheConfiguration cacheConfig1 = RedisCacheConfiguration.defaultCacheConfig()
                // 设置过期时间 10 分钟
                .entryTtl(Duration.ofMinutes(10))
                // 设置缓存前缀
                .computePrefixWith(cacheName -> "cache:user:" + cacheName + ":")
                // 禁止缓存 null 值
                .disableCachingNullValues()
                // 设置 key 序列化
                .serializeKeysWith(keyPair())
                // 设置 value 序列化
                .serializeValuesWith(valuePair());

        RedisCacheConfiguration cacheConfig2 = RedisCacheConfiguration.defaultCacheConfig()
                // 设置过期时间 30 秒
                .entryTtl(Duration.ofSeconds(30))
                .computePrefixWith(cacheName -> "cache:admin:" + cacheName + ":")
                .disableCachingNullValues()
                .serializeKeysWith(keyPair())
                .serializeValuesWith(valuePair());

        RedisCacheConfiguration cacheConfig3 = RedisCacheConfiguration.defaultCacheConfig()
                // 设置过期时间 30 秒
                .entryTtl(Duration.ZERO)
                .computePrefixWith(cacheName -> "cache:NoTime:" + cacheName + ":")
                .disableCachingNullValues()
                .serializeKeysWith(keyPair())
                .serializeValuesWith(valuePair());

        // 返回 Redis 缓存管理器
        return RedisCacheManager.builder(factory)
                .withCacheConfiguration("user", cacheConfig1)
                .withCacheConfiguration("admin", cacheConfig2)
                .withCacheConfiguration("NoTime", cacheConfig3)
                .build();
    }

    /**
     * 配置键序列化
     * @return StringRedisSerializer
     */
    private RedisSerializationContext.SerializationPair<String> keyPair() {
        return RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer());
    }

    /**
     * 配置值序列化,使用 GenericJackson2JsonRedisSerializer 替换默认序列化
     * @return GenericJackson2JsonRedisSerializer
     */
    private RedisSerializationContext.SerializationPair<Object> valuePair() {
        return RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer());
    }

}

Guess you like

Origin blog.csdn.net/Lushengshi/article/details/132753921