所遇到的坑:必须使用如下的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 > <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 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 class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg ref="redisClusterConfiguration" /> <constructor-arg ref="redisPoolConfig" /> </bean> <!-- 存储序列化 --> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!-- redis 访问的模版 --> <bean > <property name="connectionFactory" ref="jedisConnectionFactory" /> <!-- 添加如下序列化配置解决key乱码问题以及令keys()方法生效 --> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="hashKeySerializer" ref="stringRedisSerializer"/> </bean>
**********单机版配置*******
<bean > <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 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 class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <!-- redis 访问的模版 --> <bean > <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 集成 - Python技术站