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 的文章您也可能喜欢,不妨参考下:
Ubuntu 14.04下Redis安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm
Redis主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm
Redis集群明细文档 http://www.linuxidc.com/Linux/2013-09/90118.htm
Ubuntu 14.04安装Redis与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm
Ubuntu 16.04环境中安装php7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm
CentOS 7.0 安装Redis 3.2.1详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm
Ubuntu 16.04环境中安装PHP7.0 Redis扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm
Ubuntu 15.10下Redis集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm
Redis实战 中文PDF http://www.linuxidc.com/Linux/2016-04/129932.htm
Redis 的详细介绍 : 请点这里
Redis 的下载地址 : 请点这里
本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-01/140056.htm