SpringMVC使用Redis集群

Redis是现在比较流行的非关系型数据库,同时又支持多种类型的存储结构,所以用来做缓存非常合适。SpringMVC也是现在常用的框架,两者结合使用更加方便。为了保证稳定性,redis最好使用集群方式。但是使用redis集群,也要舍弃一些东西,这个后面再说。

下面就简单说说如何配置使用redis集群:

1. 添加maven

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.7.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

注意,spring-data-redis必须是1.7.0以上版本才行!以前用的版本低,启动时报错Java.lang.ClassNotFoundException: org.springframework.data.redis.connection.RedisClusterConfiguration。同时要配置redis.clients,spring-data-redis低版本中是自带的,不需要另外引入,否则报错java.lang.ClassNotFoundException: redis.clients.jedis.JedisPoolConfig。

2. 配置redis-conf.properties

#redis的服务器地址
redis.host=127.0.0.1
#最大连接数
redis.maxTotal=800
#最小连接数
redis.minIdle=100
#最大空闲数
redis.maxIdle=200
#最大建立连接等待时间
redis.maxWait=10000
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true

redis.port0=7380
redis.port1=7381
redis.port2=7382
redis.port3=7383
redis.port4=7384
redis.port5=7385

3. 配置spring-redis.xml

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jeidsConnectionFactory"/>
    </bean>

    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jeidsConnectionFactory"/>
    </bean>

    <bean id="jeidsConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg ref="redisClusterConfiguration"/>
        <constructor-arg ref="jedisPoolConfig"/>
    </bean>

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="minIdle" value="${redis.minIdle}"/>

        <!--以前是maxActive-->
        <property name="maxTotal" value="${redis.maxTotal}"/>

        <!--以前是maxWait-->
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
    </bean>

    <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name="maxRedirects" value="${redis.maxRedirects}"></property>
        <property name="clusterNodes">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port0}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port1}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port2}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port3}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port4}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port5}"></constructor-arg>
                </bean>
            </set>
        </property>
    </bean>

把spring-redis.xml导入到主配置文件中

<import resource="spring-redis.xml"/>

4. 调用Redis

也许你主要到,在上面的配置文件中配置了两个bean:redisTemplate和stringRedisTemplate。这两个bean有的区别是什么呢?查看源码可以知道,StringRedisTemplate继承了RedisTemplate

public class StringRedisTemplate extends RedisTemplate<String, String> {
    public StringRedisTemplate() {
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        this.setKeySerializer(stringSerializer);
        this.setValueSerializer(stringSerializer);
        this.setHashKeySerializer(stringSerializer);
        this.setHashValueSerializer(stringSerializer);
    }

    public StringRedisTemplate(RedisConnectionFactory connectionFactory) {
        this();
        this.setConnectionFactory(connectionFactory);
        this.afterPropertiesSet();
    }

    protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
        return new DefaultStringRedisConnection(connection);
    }
}
  • redisTemplate操作方式
@Resource
 private RedisTemplate<String, String> redisTemplate;

 public Integer setStringValue(final String key, final String value) {
        Integer index = redisTemplate.execute(new RedisCallback<Integer>() {
            public Integer doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
                connection.set(serializer.serialize(key), serializer.serialize(value));
                return 1;
            }
        });
        return index;
    }
  • stringRedisTemplate操作方式
@Resource
 private StringRedisTemplate stringRedisTemplate;

 public void testStringRedisTemplate (String key,String value) {
        this.stringRedisTemplate.opsForValue().set(key,value);
 }

StringRedisTemplate还可以根据opsForHash、opsForList、opsForSet和opsForZSet等方式操作各种存取方法,简单易用。

Redis 3.0以上的才支持集群操作,而且集群之后,不能再分库了!集群设置密码的时候,也要保证所有的节点密码是一样的,不然会跳转失败而找不到数据!但是在程序中设置密码连接的时候却一直失败,所以最好还是使用内网地址作为连接IP,不设置密码了。以上就是整个流程了,虽然看似简单,但是对于一路踩着坑过来的我来说,也是不容易的。

下面关于Redis的文章您也可能喜欢,不妨参考下:

Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里

相关推荐