redis集群 与spring-data-redis 集成
Posted 你所能做的,就是不断的学习
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis集群 与spring-data-redis 集成相关的知识,希望对你有一定的参考价值。
所遇到的坑:必须使用如下的jedis 版本与spring-data-redis 版本,才能够达到集群效果 。1.7版本以前是不支持集群的
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.1.RELEASE</version> </dependency>
***********集群配置**********
<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- 配置redis 节点 --> <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="192.168.241.129 "></constructor-arg> <constructor-arg name="port" value="9006"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="192.168.241.129 "></constructor-arg> <constructor-arg name="port" value="9001"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="192.168.241.129 "></constructor-arg> <constructor-arg name="port" value="9002"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="192.168.241.129"></constructor-arg> <constructor-arg name="port" value="9003"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="192.168.241.129"></constructor-arg> <constructor-arg name="port" value="9004"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisClusterNode"> <constructor-arg name="host" value="192.168.241.129"></constructor-arg> <constructor-arg name="port" value="9005"></constructor-arg> </bean> </set> </property> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="redisClusterConfiguration" /> <constructor-arg ref="redisPoolConfig" /> </bean> <!-- 存储序列化 --> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!-- redis 访问的模版 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <!-- 添加如下序列化配置解决key乱码问题以及令keys()方法生效 --> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="hashKeySerializer" ref="stringRedisSerializer"/> </bean>
**********单机版配置*******
<bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- 单机版配置 --> <!-- 配置JedisConnectionFactory --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="127.0.0.1" /> <property name="port" value="6379" /> <!-- <property name="password" value="${redis.pass}" /> --> <property name="database" value="1" /> <property name="poolConfig" ref="redisPoolConfig" /> </bean> <!-- 存储序列化 --> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!-- redis 访问的模版 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <!-- 添加如下序列化配置解决key乱码问题以及令keys()方法生效 --> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="hashKeySerializer" ref="stringRedisSerializer"/> </bean>
在java 代码中引入即可
@Autowired private RedisTemplate template;
以上是关于redis集群 与spring-data-redis 集成的主要内容,如果未能解决你的问题,请参考以下文章
Spring整合Redis(spring-data-redis)