一,准备工作:
1.首先安装好redis集群,启动并配置集群。
2.SpringMVC环境,看项目或个人需要,可以使SpringMVC的web项目,也可以是只使用SpringBean管理器。
二,着手配置:
由于项目是由maven管理的所以需要的jar 包添加到maven 的pom文件即可
1.添加jar依赖,再maven pom.xml 文件中添加依赖如下:// 这里需要说明下,依赖的jar包 redis.client 和 spring-data-redis 的版本匹配问题 实验了好几个对应如下:
redis.client 2.9.0 ---- spring-data-redis 1.7.1.RELEASE
redis.client 2.9.0 -----spring-data-redis 1.7.2.RELEASE 这两个是可以使用的
由于版本不匹配,遇到的错误如下:
1. ClassNotFoundException : redis/client/util/geoUtils 说这个类找不到。
2. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [applicationContext.xml]
说创建 redisTemplate bean 对象时失败了。
<!-- jedis (一个redis client端的jar)-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- spring-data-redis 依赖-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
2.配置redis 配置文件 我把它单独提取出来放在一个配置文件(spring-data-redis-cluster.xml)中,然后import 到 spring ApplicationContext.xml 文件中:
spring-data-redis-cluster.xml 配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http:/