昨晚刚刚好搭好redis集群,所以今天就是快速上手试试 本次采用spring-data 集成redis的集群 废话少说 代码来了
一 maven项目 只需要引入两个jar
pom.xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
接下来spring的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes" >
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.31.121"/>
<constructor-arg name="port" value="7000"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.31.121"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.31.122"/>
<constructor-arg name="port" value="7002"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.31.122"/>
<constructor-arg name="port" value="7003"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.31.123"/>
<constructor-arg name="port" value="7004"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.31.123"/>
<constructor-arg name="port" value="7005"/>
</bean>
</set>
</property>
</bean>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="1000"/>
<property name="maxTotal" value="1000"/>
<property name="maxWaitMillis" value="1000"/>
<property name="testOnBorrow" value="true" />
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig"/>
<property name="password" value="123456"/>
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
</bean>
<!-- 配置RedisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<!--<!–开启事务–>-->
<!--<property name="enableTransactionSupport" value="true"/>-->
</bean>
</beans>
最后是运行 main
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring.xml");
RedisTemplate redisTemplate =(RedisTemplate)context.getBean("redisTemplate");
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("test","第一个集群redis");
System.out.println("从集群中获取 -->>"+valueOperations.get("test"));
}
运行结果
总结 遇到的报错
Jedis does not support password protected Redis Cluster configurations的异常
解决
spring-data-redis版本太低 更新
Caused by: java.lang.NoSuchMethodError: redis.clients.jedis.JedisShardInfo 的异常
jedis 版本太低 更新