一、问题描述
原来使用的是redis,现在更换成pika使用,使用springboot2.1.8版本,里边配置了clientName,启动的时候并没有什么问题,但是在使用的时候会报出来下边的异常信息:
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool] with root cause
redis.clients.jedis.exceptions.JedisDataException: ERR Syntax error, try CLIENT (LIST [order by [addr|idle]| KILL ip:port)
@Bean
public RedisConnectionFactory connectionFactory(RedisProperties redisProperties) {
//添加额外属性
JedisClientConfiguration clientConfig = JedisClientConfiguration.builder()
//springboot2.1.x没有此字段需要在这里写死,springboot2.2.x可以通过RedisProperties获取
.clientName("zcx")
.usePooling().poolConfig(getGenericObjectJedisPoolConfig(redisProperties)).and().readTimeout(redisProperties.getTimeout())
.build();
//单实例连接配置
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(redisProperties.getHost());
configuration.setPort(redisProperties.getPort());
configuration.setPassword(RedisPassword.of(redisProperties.getPassword()));
return new JedisConnectionFactory(configuration, clientConfig);
}
二、解决方案
经过查找就是clientName字段引起的,将clientName的配置去掉就可以了,问题查找如下:
GenericObjectPool类中调用创建连接
// 创建连接
private PooledObject<T> create() throws Exception {
...
final PooledObject<T> p;
try {
//调用jedisFactory的makeObject()方法
p = factory.makeObject();
} catch (final Throwable e) {
createCount.decrementAndGet();
throw e;
} finally {
synchronized (makeObjectCountLock) {
makeObjectCount--;
makeObjectCountLock.notifyAll();
}
}
}
JedisFactory类中调用了client setname "zcx"命令,抛出了异常
@Override
public PooledObject<Jedis> makeObject() throws Exception {
final HostAndPort hostAndPort = this.hostAndPort.get();
final Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout,
soTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier);
try {
jedis.connect();
if (password != null) {
jedis.auth(password);
}
if (database != 0) {
jedis.select(database);
}
// 因为上边的配置里边有clientName,所以这里会调用client setname "zcx"命令
if (clientName != null) {
jedis.clientSetname(clientName);
}
} catch (JedisException je) {
//此处捕获到了ERR Syntax error, try CLIENT (LIST [order by [addr|idle]| KILL ip:port)
jedis.close();
throw je;
}
归根结底就是pika不支持client setname命令,所以在创建连接的时候获取不到连接同时报出命令不支持