在这里只说客户端分片。一般redis 分片都使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//设置连接池的相关配置 JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(2); poolConfig.setMaxIdle(1); poolConfig.setMaxWaitMillis(2000); poolConfig.setTestOnBorrow(false); poolConfig.setTestOnReturn(false); //设置Redis信息 String host = "127.0.0.1"; JedisShardInfo shardInfo1 = new JedisShardInfo(host, 6379, 500); shardInfo1.setPassword("test123"); JedisShardInfo shardInfo2 = new JedisShardInfo(host, 6380, 500); shardInfo2.setPassword("test123"); JedisShardInfo shardInfo3 = new JedisShardInfo(host, 6381, 500); shardInfo3.setPassword("test123"); //初始化ShardedJedisPool List<JedisShardInfo> infoList = Arrays.asList(shardInfo1, shardInfo2, shardInfo3); ShardedJedisPool jedisPool = new ShardedJedisPool(poolConfig, infoList); //进行查询等其他操作 ShardedJedis jedis = null; try { jedis = jedisPool.getResource(); jedis.set("test", "test"); jedis.set("test1", "test1"); String test = jedis.get("test"); System.out.println(test); ...... } finally { //使用后一定关闭,还给连接池 if(jedis!=null) { jedis.close(); } } |
ShardedRedisClient 代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
package com.xxx.xxx.xxx.util.redis; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.InitializingBean; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map;; /** * @author by ming on 2018/3/29. */ @Slf4j public class ShardedRedisClient<K,V> implements InitializingBean { private Map<Integer,RedisTemplate<K,V>> redisTemplateMap=new HashMap<>(); private RedisSerializer keySerializer = null; private RedisSerializer valueSerializer = null; private RedisSerializer<?> defaultSerializer=null; private List<JedisConnectionFactory> connectionFactoryList; public ShardedRedisClient(List<JedisConnectionFactory> list) { this.connectionFactoryList=list; } @Override public void afterPropertiesSet() throws Exception { if(defaultSerializer==null){ defaultSerializer=new JdkSerializationRedisSerializer(); } if(keySerializer==null){ keySerializer=defaultSerializer; } if(valueSerializer==null){ valueSerializer=defaultSerializer; } for(int i=0;i<connectionFactoryList.size();i++){ JedisConnectionFactory factory=connectionFactoryList.get(i); RedisTemplate<K,V> redisTemplate=new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); redisTemplate.setKeySerializer(keySerializer); redisTemplate.setValueSerializer(valueSerializer); redisTemplate.afterPropertiesSet(); redisTemplateMap.put(i,redisTemplate); } } /** * 根据key来获取不同的RedisTemplate 的不同 ValueOperations * @param key * @return */ public ValueOperations<K, V> opsForValue(K key){ RedisTemplate<K, V> redisTemplate = getRedisTemplate(key); return redisTemplate.opsForValue(); } /** * 删除值 * @param key * @return */ public void delete(K key){ RedisTemplate<K, V> redisTemplate = getRedisTemplate(key); redisTemplate.delete(key); } /** * 按照一定规则获取不同的RedisTemplate实例 * @param key * @return */ private RedisTemplate<K, V> getRedisTemplate(K key) { int hashcode= Arrays.hashCode(keySerializer.serialize(key)); Integer keyValue=Math.abs(hashcode) % connectionFactoryList.size(); if(log.isDebugEnabled()){ log.debug("hashcode:{},keyValue:{}",hashcode,keyValue); } return redisTemplateMap.get(keyValue); } public void setKeySerializer(RedisSerializer keySerializer) { this.keySerializer = keySerializer; } public void setValueSerializer(RedisSerializer valueSerializer) { this.valueSerializer = valueSerializer; } public void setDefaultSerializer(RedisSerializer<?> defaultSerializer) { this.defaultSerializer = defaultSerializer; } } |
使用方式 可以采用spring xml 配置方式
spring xml 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<!--redis pool--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="500"/> <property name="maxIdle" value="10"/> <property name="minIdle" value="5"/> <property name="maxWaitMillis" value="20000"/> <property name="testOnBorrow" value="true"/> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy"> <constructor-arg ref="jedisPoolConfig" name="poolConfig"/> <constructor-arg value="${redis.ip}" name="host" type="java.lang.String"/> <constructor-arg value="${redis.port}" name="port" type="int"/> <constructor-arg value="10000" name="timeout" type="int"/> <constructor-arg value="${redis.password}" name="password" type="java.lang.String"/> <constructor-arg value="1" name="database" type="int"/> </bean> <bean id="shardedRedisClient" class="com.xxx.xxx.xxx.util.redis.ShardedRedisClient"> <constructor-arg index="0"> <list> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig"/> <property name="port" value="${redis.sharded1.port}"/> <property name="hostName" value="${redis.sharded1.host}"/> <property name="password" value="${redis.sharded1.pwd}"/> <property name="database" value="${redis.sharded1.database}" /> <property name="timeout" value="20000"/> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig"/> <property name="port" value="${redis.sharded2.port}"/> <property name="hostName" value="${redis.sharded2.host}"/> <property name="password" value="${redis.sharded2.pwd}"/> <property name="database" value="${redis.sharded2.database}" /> <property name="timeout" value="20000"/> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig"/> <property name="port" value="${redis.sharded3.port}"/> <property name="hostName" value="${redis.sharded3.host}"/> <property name="password" value="${redis.sharded3.pwd}"/> <property name="database" value="${redis.sharded3.database}" /> <property name="timeout" value="20000"/> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig"/> <property name="port" value="${redis.sharded4.port}"/> <property name="hostName" value="${redis.sharded4.host}"/> <property name="password" value="${redis.sharded4.pwd}"/> <property name="database" value="${redis.sharded4.database}" /> <property name="timeout" value="20000"/> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig"/> <property name="port" value="${redis.sharded5.port}"/> <property name="hostName" value="${redis.sharded5.host}"/> <property name="password" value="${redis.sharded5.pwd}"/> <property name="database" value="${redis.sharded5.database}" /> <property name="timeout" value="20000"/> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="jedisPoolConfig"/> <property name="port" value="${redis.sharded6.port}"/> <property name="hostName" value="${redis.sharded6.host}"/> <property name="password" value="${redis.sharded6.pwd}"/> <property name="database" value="${redis.sharded6.database}" /> <property name="timeout" value="20000"/> </bean> </list> </constructor-arg> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> </bean> |