使用SpringData更加方便我们对关系型数据库和非关系型数据库更好的操作,封装了通用的代码,使得操作更加快捷简单。
一、使用Spring-data-redis Jar包准备
spring-data-redis 需要在1.7 版本以上,他会依赖一些包,比如说 spring-data-commons ,在spring-data-x 系列里,都要依赖这个包。如果你之前项目里使用到了spring-data-x 系列的包,可能需要升级,因为都共同依赖了spring-data-commons ,但是在当前集群要使用的spring-data-redis 中 spring-data-commons 必须要1.12.x 版本以上,这个问题要注意一下。
手动添加jar包 : 相关包下载地址
二、开始配置
1.在spring-application.xml中加入如下代码
<import resource="spring-redis.xml"/>
2.创建redis配置文件spring-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 加载配置属性文件 按需加载 -->
<!-- <context:property-placeholder
ignore-unresolvable="true" location="classpath:redis.cluster.properties" /> -->
<!-- 配置Cluster -->
<bean id="redisClusterConfiguration"
class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="3"></property>
<!-- 节点配置 -->
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisClusterNode">
<constructor-arg name="host" value="120.99.158.113 "></constructor-arg>
<constructor-arg name="port" value="7000"></constructor-arg>
</