Spring集成redis集群

Posted zzrs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring集成redis集群相关的知识,希望对你有一定的参考价值。

Spring集成redis集群 有密码

Maven

<jedis.version>2.9.0</jedis.version>
<spring-data-redis.version>1.7.1.RELEASE</spring-data-redis.version>
<spring.version>3.2.17.RELEASE</spring.version>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>$jedis.version</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>$spring-data-redis.version</version>
</dependency>

 

Spring配置文件 

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!-- <property name="fairness" value="true" /> -->
    <property name="maxWaitMillis" value="$redis.maxWaitMillis"/>
    <property name="maxTotal" value="$redis.maxTotal"/>
    <property name="maxIdle" value="$redis.maxIdle"/>
    <property name="minIdle" value="$redis.minIdle"></property>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"></property>
</bean>
<bean id="redisClusterConfig"
      class="org.springframework.data.redis.connection.RedisClusterConfiguration">
    <property name="maxRedirects" value="3"></property>
    <property name="clusterNodes">
        <set>
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <constructor-arg name="host" value="$redis.host1"></constructor-arg>
                <constructor-arg name="port" value="$redis.port1"></constructor-arg>
            </bean>
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <constructor-arg name="host" value="$redis.host2"></constructor-arg>
                <constructor-arg name="port" value="$redis.port2"></constructor-arg>
            </bean>
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <constructor-arg name="host" value="$redis.host3"></constructor-arg>
                <constructor-arg name="port" value="$redis.port3"></constructor-arg>
            </bean>
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <constructor-arg name="host" value="$redis.host4"></constructor-arg>
                <constructor-arg name="port" value="$redis.port4"></constructor-arg>
            </bean>
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <constructor-arg name="host" value="$redis.host5"></constructor-arg>
                <constructor-arg name="port" value="$redis.port5"></constructor-arg>
            </bean>
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <constructor-arg name="host" value="$redis.host6"></constructor-arg>
                <constructor-arg name="port" value="$redis.port6"></constructor-arg>
            </bean>
        </set>
    </property>
</bean>
<bean id="jedisConnectionFactory"
      class="com.web.common.redis.JedisConnectionFactory">
    <constructor-arg name="clusterConfig" ref="redisClusterConfig" />
    <property name="timeout" value="$redis.timeout" />
    <property name="password" value="$cluster.redis.pass"></property>
    <property name="usePool" value="true"/>
    <property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<bean id="sessionManager" class="com.web.security.SessionManager">
    <property name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>

 

reids.properties

#redis cluster
cluster.redis.pass=test@123
redis.host1=127.0.0.1
redis.port1=7001
redis.host2=127.0.0.1
redis.port2=7002
redis.host3=127.0.0.1
redis.port3=7003
redis.host4=127.0.0.1
redis.port4=7004
redis.host5=127.0.0.1
redis.port5=7005
redis.host6=127.0.0.1
redis.port6=7006

java相关

public class SessionManager extends StringRedisTemplate 

  public void setTest(String key) opsForValue().set(key, value); private String getTest(String key) String value = opsForValue().get(key); return value;

 

有可能会遇到的问题。

1、在redis集群没有设置密码之前 以上配置是可以用的。加上密码之后则出现

Jedis does not support password protected Redis Cluster configurations

查资料发现, 他这里判断密码不为空就直接抛出异常。
if (StringUtils.hasText(this.getPassword())) 
    throw new IllegalArgumentException("Jedis does not support password protected Redis Cluster configurations!");
 else 
    return poolConfig != null ? new JedisCluster(hostAndPort, this.timeout, redirects, poolConfig) : new JedisCluster(hostAndPort, this.timeout, redirects, poolConfig);

接着查看 JedisCluster构造方法里面竟然有密码的方法。

public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, GenericObjectPoolConfig poolConfig) 
    super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, password, poolConfig);

可能是Spring-data-redis版本过低,将Spring-data-redis版本升级到1.8.4就可以解决。

注意:再不想升级版本的情况可以修改Spring-data-redis的源码。

org.springframework.data.redis.connection.jedis.JedisConnectionFactory

如下:

int redirects = clusterConfig.getMaxRedirects() != null ? clusterConfig.getMaxRedirects() : 5;
if (StringUtils.hasText(this.getPassword())) 
    /*throw new IllegalArgumentException("Jedis does not support password protected Redis Cluster configurations!");*/
    return new JedisCluster(hostAndPort,this.timeout, this.timeout, redirects, this.getPassword(), poolConfig);
 else 
    return poolConfig != null ? new JedisCluster(hostAndPort, this.timeout, redirects, poolConfig) : new JedisCluster(hostAndPort, this.timeout, redirects, poolConfig);

这样就可以完美运行了。

 
2、升级之后出现Spring版本和Spring-data-redis版本兼容问题
NoSuchMethodError: org.springframework.core.serializer.support.DeserializingConverter.<init>(Ljava/lang/ClassLoader;)V。

将Spring版本升级到4.3.9.RELEASE就可以解决。

 

 

如有不对,请帮忙指出。

以上是关于Spring集成redis集群的主要内容,如果未能解决你的问题,请参考以下文章

Redis 集群与 Spring Boot 的集成

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

spring集成 JedisCluster 连接 redis3.0 集群

SpringBoot集成Redis集群

基于哨兵sentinel模式的redis服务集群并与spring集成

Java集成redis集群