POM file:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.5.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
The code snippet demonstration is based on junit.
Scenario 1: Single-machine Redis deployment, using Jedis
Configuration snippet:
<bean id="jedis" class="redis.clients.jedis.Jedis"> <constructor-arg name="host" value="192.168.0.200" /> <constructor-arg name="port" value="6379" /> </bean>
code segment:
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml"); Jedis jedis = (Jedis) context.getBean("jedis"); /** * Set the connection password */ //jedis.auth("123456"); jedis.set("jedis", "jedis test"); assertEquals("jedis test", jedis.get("jedis"));
Scenario 2: Cluster Redis deployment (master-slave + read-write separation), using Jedis connection pool (JedisPool)
Configuration snippet:
<!--Related configuration of Jedis connection pool--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--The new version is maxTotal, the old version is maxActive --> <property name="maxTotal"> <value>1000</value> </property> <property name="maxIdle"> <value>50</value> </property> <property name="testOnBorrow" value="true" /> <property name="testOnReturn" value="true" /> </bean> <!-- Jedis connection pool --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="poolConfig" ref="jedisPoolConfig" /> <constructor-arg name="host" value="192.168.0.200" /> <constructor-arg name="port" value="6379" type="int" /> <constructor-arg name="timeout" value="5000" type="int" /> <!--Password and database index configuration <constructor-arg name="password" value="123456" /> <constructor-arg name="database" value="0" type="int" /> --> </bean>
code segment:
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml"); JedisPool jedisPool=(JedisPool) context.getBean("jedisPool"); Jedis jedis=jedisPool.getResource(); jedis.set("jedis pool", "jedis pool test"); assertEquals("jedis pool test", jedis.get("jedis pool"));
Scenario 3: Cluster Redis deployment (master-slave + read-write separation) + sentinel cluster
Description: Use the client template ( RedisTemplate ) provided by spring for data operations, relying on spring's spring-data-redis-1.5.2.RELEASE.jar
Configuration snippet:
<!-- Relevant configuration of Jedis connection pool--> <bean id="jedisPoolConfig2" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1024" /> <property name="maxIdle" value="200" /> <property name="numTestsPerEvictionRun" value="1024" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="minEvictableIdleTimeMillis" value="30000" /> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <property name="maxWaitMillis" value="1000" /> <property name="testOnBorrow" value="true" /> </bean> <!-- Sentinel configuration --> <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <!--This value must be consistent with the value of the master specified in Sentinel, otherwise it will report an error if Sentinel is not found when starting --> <property name="name" value="m1"></property> </bean> </property> <!--Remember, here is the IP and port of Sentinel, not Master and Slave--> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.0.200"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.0.201"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.0.202"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> </set> </property> </bean> <!-- connection factory --> <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg> <constructor-arg name="poolConfig" ref="jedisPoolConfig2"></constructor-arg> </bean> <!-- redis template tool class --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisConnectionFactory"></property> <!-- Solve the problem of using RedisTemplate garbled--> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> </bean>
code segment:
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml"); RedisTemplate redisTemplate=(RedisTemplate) context.getBean("redisTemplate"); redisTemplate.opsForValue().set("xa", "西安"); assertEquals("西安",redisTemplate.opsForValue().get("xa"));
Scenario 4: Cluster Redis deployment (master-slave + read-write separation) + sentinel cluster
Description: Use the sentry pool (JedisSentinelPool) that comes with Jedis to manage Jedis resources (similar to JedisPool)
Configuration snippet:
<!-- Relevant configuration of Jedis connection pool--> <bean id="jedisPoolConfig3" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1024" /> <property name="maxIdle" value="200" /> <property name="numTestsPerEvictionRun" value="1024" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="minEvictableIdleTimeMillis" value="30000" /> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <property name="maxWaitMillis" value="1000" /> <property name="testOnBorrow" value="true" /> </bean> <!-- JedisSentinelPool池配置 --> <bean id="jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool"> <!--这个值要和Sentinel中指定的master的值一致,不然启动时找不到Sentinel会报错的 --> <constructor-arg index="0" value="m1" /> <constructor-arg index="1"> <set> <value>192.168.0.200:26379</value> <value>192.168.0.201:26379</value> <value>192.168.0.202:26379</value> </set> </constructor-arg> <constructor-arg index="2" ref="jedisPoolConfig3" /> </bean>
代码片段:
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml"); JedisSentinelPool jedisSentinelPool=(JedisSentinelPool) context.getBean("jedisSentinelPool"); Jedis jedis = null; try { jedis = jedisSentinelPool.getResource(); jedis.set("bj", "北京"); assertEquals("北京", jedis.get("bj")); } catch (Exception e) { e.printStackTrace(); } finally { jedisSentinelPool.returnBrokenResource(jedis); } jedisSentinelPool.close();
场景五:分片(多Master,独立)+一致性HASH
配置片段:
<!-- Jedis连接池的相关配置 --> <bean id="jedisPoolConfig4" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1024" /> <property name="maxIdle" value="200" /> <property name="numTestsPerEvictionRun" value="1024" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="minEvictableIdleTimeMillis" value="30000" /> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <property name="maxWaitMillis" value="1000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"> <constructor-arg index="0" ref="jedisPoolConfig4" /> <constructor-arg index="1"> <list> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg name="host" value="192.168.0.200" /> <constructor-arg name="name" value="master:200" /> <constructor-arg name="port" value="6379" /> <constructor-arg name="timeout" value="5000" /> <constructor-arg name="weight" value="1" /> <!-- <property name="password" value="123456" /> --> </bean> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg name="host" value="192.168.0.201" /> <constructor-arg name="name" value="master:201" /> <constructor-arg name="port" value="6379" /> <constructor-arg name="timeout" value="5000" /> <constructor-arg name="weight" value="1" /> <!-- <property name="password" value="123456" /> --> </bean> <bean class="redis.clients.jedis.JedisShardInfo"> <constructor-arg name="host" value="192.168.0.202" /> <constructor-arg name="name" value="master:202" /> <constructor-arg name="port" value="6379" /> <constructor-arg name="timeout" value="5000" /> <constructor-arg name="weight" value="1" /> <!-- <property name="password" value="123456" /> --> </bean> </list> </constructor-arg> </bean>
代码片段:
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml"); ShardedJedisPool shardedJedisPool=(ShardedJedisPool) context.getBean("shardedJedisPool"); ShardedJedis shardedJedis = shardedJedisPool.getResource();; try { shardedJedis.set("cw", "宠物"); assertEquals("宠物", shardedJedis.get("cw")); } catch (Exception e) { e.printStackTrace(); } finally { if(shardedJedis!=null) { shardedJedis.close(); } }
场景六:redis+cluster
配置片段:
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg index="0"> <!-- 集群中的任意节点,个数不限 --> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.0.200"/> <constructor-arg name="port" value="7000"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.0.200"/> <constructor-arg name="port" value="7001"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.0.201"/> <constructor-arg name="port" value="7000"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.0.201"/> <constructor-arg name="port" value="7001"/> </bean> </set> </constructor-arg> </bean>
代码片段:
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("classpath:spring/scm-cache-redis-config1.xml"); JedisCluster jedisCluster=(JedisCluster) context.getBean("jedisCluster"); try { jedisCluster.set("mary", "麦瑞"); assertEquals("麦瑞", jedisCluster.get("mary")); } catch (Exception e) { e.printStackTrace(); } finally { if(jedisCluster!=null) { try { jedisCluster.close(); } catch (IOException e) { e.printStackTrace(); } } }