redis4.0 集群,jedis客户端连接踩坑

Posted 蜗牛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis4.0 集群,jedis客户端连接踩坑相关的知识,希望对你有一定的参考价值。

使用jedis 2.9.0连接,异常信息:

redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set。

说我没有设置密码,当时我就震惊了。我明明设置了密码呀。我用redis-cli连接的时候 还让我输入密码了呢~

检查了好久才想起来,redis-cli默认连接6379,我只给6379设置密码了,实际上要给每个节点都设置密码:

config set masterauth abc  
config set requirepass abc  

 

spring配置,jedis连接集群,竟然没有RedisCluster一个构造函数的参数只由 Set<HostAndPort>和password组成,害得我还要配置这么多参数。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <context:property-placeholder location="classpath:conf/redis.properties"/>
   


    <!--cluster版本-->
    <bean class="redis.clients.jedis.JedisCluster" id="jedisClusterOrginal">
        <constructor-arg name="jedisClusterNode">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="${IP}"/>
                    <constructor-arg index="1" value="6379"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="${IP}"/>
                    <constructor-arg index="1" value="6380"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="${IP}"/>
                    <constructor-arg index="1" value="6381"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="${IP}"/>
                    <constructor-arg index="1" value="6382"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="${IP}"/>
                    <constructor-arg index="1" value="6383"/>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg index="0" value="${IP}"/>
                    <constructor-arg index="1" value="6384"/>
                </bean>
            </set>
        </constructor-arg>
        <constructor-arg name="connectionTimeout" value="10000"></constructor-arg>
        <constructor-arg name="soTimeout" value="10000"></constructor-arg>
        <constructor-arg name="maxAttempts" value="100"></constructor-arg>
        <constructor-arg name="password" value="${pwd}"></constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig">
        </constructor-arg>
    </bean>
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
    <bean class="com.s2s.common.jedis.JedisClientCluster" id="jedisCluster">
        <property name="jedisCluster" ref="jedisClusterOrginal"></property>
    </bean>

</beans>

 

以上是关于redis4.0 集群,jedis客户端连接踩坑的主要内容,如果未能解决你的问题,请参考以下文章

通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the poo

Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)

Redis Cluster集群搭建后,客户端的连接研究(Spring/Jedis)(待实践)

Redis连接池Lettuce踩坑记录

使用 jedis 客户端从春季开始的 aws Redis 集群

Jedis集成与踩坑经历