Spring uses redis cluster configuration, springredis
The above two articles introduce some basic concepts of the Integration of redis cluster configuration, so the next step is to use it in the project. redis java support has been done very well, so we will try to use these APIs for redis operations. First, we need to operate the redis rack package:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency>
All we need to do is inject these basic classes into the spring configuration file and implement dao by ourselves. The following is the configuration file:
<Context: property-placeholder ignore-unresolvable = "true" location = "classpath: yonyou. properties "/> <bean id =" jedisPoolConfig "class =" redis. clients. jedis. jedisPoolConfig "> <property name =" maxTotal "value =" 1000 "/> <property name =" maxIdle "value =" 10 "/> <property name =" minIdle "value = "1"/> <property name = "maxWaitMillis" value = "30000"/> <property name = "testOnBorrow" value = "true"/> <property name = "testOn Return "value =" true "/> <property name =" testWhileIdle "value =" true "/> <! -- <Property name = "testWhileIdle" value = "true"/> --> </bean> <bean id = "shardedJedisPool" class = "redis. clients. jedis. shardedJedisPool "destroy-method =" destroy "> <constructor-arg ref =" jedisPoolConfig "/> <constructor-arg>
<! -- If you need to expand the cluster in the future, you only need to copy redis, modify the port, and then configure it here --> <list> <bean class = "redis. clients. jedis. jedisShardInfo "> <constructor-arg index =" 0 "value =" 127.0.0.1 "/> <constructor-arg index =" 1 "value =" 6379 "/> <constructor-arg index = "2" value = "instance: 01 "/> </bean> <bean class =" redis. clients. jedis. jedisShardInfo "> <constructor-arg index =" 0 "value =" 127.0.0.1 "/> <constructor-arg index =" 1 "value =" 6380 "/> <constructor-ar G index = "2" value = "instance: 02"/> </bean> <bean class = "redis. clients. jedis. jedisShardInfo "> <constructor-arg index =" 0 "value =" 127.0.0.1 "/> <constructor-arg index =" 1 "value =" 6381 "/> <constructor-arg index = "2" value = "instance: 03 "/> </bean> </list> </constructor-arg> </bean> <! -- Java helps us synchronize the sentinel information and synchronize the master and slave information to the client --> <bean class = "redis. clients. jedis. jedisSentinelPool "> <constructor-arg index =" 0 "value =" mymaster "/> <constructor-arg index =" 1 "> <set> <value> 127.0.0.1: 26379 </value> </set> </constructor-arg> <constructor-arg index = "2" ref = "jedisPoolConfig"/> </bean>
After the above configuration is complete, we will map the redis cluster to it. Next we only need to do some operations, involving CRUD of data.
Package com. yonyou. hotusm. module. nosql. redis; import redis. clients. jedis. required; // The operation interface RedisDataSource {public abstract ShardedJedis getRedisClient (); public void returnResource (inclushardedjedis); public void returnResource (ShardedJedis, boolean broken );}
Implementation:
package com.yonyou.hotusm.module.nosql.redis;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;@Repository("redisDataSource")public class RedisDataSourceImpl implements RedisDataSource { private static final Logger log = LoggerFactory.getLogger(RedisDataSourceImpl.class); @Autowired private ShardedJedisPool shardedJedisPool; public ShardedJedis getRedisClient() { try { ShardedJedis shardJedis = shardedJedisPool.getResource(); return shardJedis; } catch (Exception e) { log.error("getRedisClent error", e); } return null; } public void returnResource(ShardedJedis shardedJedis) { shardedJedisPool.close(); //shardedJedisPool.returnResource(shardedJedis); } public void returnResource(ShardedJedis shardedJedis, boolean broken) { if (broken) { shardedJedisPool.close(); //shardedJedisPool.returnBrokenResource(shardedJedis); } else { shardedJedisPool.close(); //shardedJedisPool.returnResource(shardedJedis); } }}
The following are the specific classes used to operate jedis:
@ Repository ("jedisTemplate") public class JedisTemplate {private static final Logger log = LoggerFactory. getLogger (JedisTemplate. class); @ Autowired private RedisDataSource redisDataSource; public void disconnect () {ShardedJedis shardedJedis = redisDataSource. getRedisClient (); shardedJedis. disconnect ();}/*** set a single value ** @ param key * @ param value * @ return */public String set (String key, String value) {String result = null; ShardedJedis shardedJedis = redisDataSource. getRedisClient (); if (shardedJedis = null) {return result;} boolean broken = false; try {result = shardedJedis. set (key, value);} catch (Exception e) {e. printStackTrace (); log. error (e. getMessage (), e); broken = true;} finally {redisDataSource. returnResource (shardedJedis, broken);} return result;}/*** get a single value ** @ param key * @ return */public String get (String key) {String result = null; ShardedJedis shardedJedis = redisDataSource. getRedisClient (); if (shardedJedis = null) {return result;} boolean broken = false; try {result = shardedJedis. get (key);} catch (Exception e) {log. error (e. getMessage (), e); broken = true;} finally {redisDataSource. returnResource (shardedJedis, broken);} return result ;}
There are still a lot of operations, such as list, set, and hash. I will not post the code here. I will know it when I look at the api.