1. Jedis是redis的java版本的客户端实现。
1.1 以下是jedis代码测试,使用了两个redis服务:
public class RedisShardPoolTest { static ShardedJedisPool pool;//切片连接池 static{ JedisPoolConfig config =new JedisPoolConfig();//Jedis池配置 config.setMaxActive(300);//最大活动的对象个数 config.setMaxIdle(1000 * 60);//对象最大空闲时间 config.setMaxWait(1000 * 10);//获取对象时最大等待时间 config.setTestOnBorrow(true);//;如果为true,则得到的jedis实例均是可用的; String hostA = "127.0.0.1";//服务器地址 int portA = 6379;//redis端口号 String hostB = "127.0.0.1"; int portB = 6378; List<JedisShardInfo> jdsInfoList =new ArrayList<JedisShardInfo>(2); JedisShardInfo infoA = new JedisShardInfo(hostA, portA); JedisShardInfo infoB = new JedisShardInfo(hostB, portB); jdsInfoList.add(infoA); jdsInfoList.add(infoB); pool =new ShardedJedisPool(config, jdsInfoList); } public static void main(String[] args) { long s1=System.currentTimeMillis(); Map<String, String> map=new HashMap<String, String>(); for(int i=0; i<1000; i++){ ShardedJedis jds = null;//切片客户端连接 jds = pool.getResource(); try { map.put("s"+i, "s"+i); jds.hmset("s"+i, map); } catch (Exception e) { e.printStackTrace(); } finally { pool.returnResource(jds); } } long s2=System.currentTimeMillis(); System.out.println(testTime(s2-s1)); } public static String testTime(long ss){ String aa=null; long zongmiaoshu = ss / 1000; long dangqianmiaoshu = zongmiaoshu % 60; long zongfenshu = zongmiaoshu /60; long dangqianfenshu = zongfenshu % 60; long zongshishu = zongfenshu / 60; long dangqianshishu = zongshishu % 24; aa="当前时间:" + dangqianshishu + ":" + dangqianfenshu + ":" + dangqianmiaoshu; return aa; } }
Codis 是一个分布式 Redis 解决方案,对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别 ,上层应用可以像使用单机的 Redis 一样使用, Codis 底层会处理请求的转发, 不停机的数据迁移等工作, 所有后边的一切事情, 对于前面的客户端来说是透明的, 可以简单的认为后边连接的是一个内存无限大的 Redis 服务.
下图可以看到redis客户端连接是是codis的代理。代理连接很多的分组,一个组包括一个master和0到多个slave。
2.1 codis代码测试:先配置codis,codis的安装配置就不讲了,我测试在集成在spring中了。
spring中的配置:
<bean id="ehcacheService" class="net.okdi.core.common.redis.RedisServiceImpl"/> <context:property-placeholder location="classpath:/redis.properties" ignore-unresolvable="true" /> <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxActive" value="${redis_max_active}"></property> <property name="maxIdle" value="${redis_max_idle}"></property> <property name="maxWait" value="${redis_max_wait}"></property> <property name="testOnBorrow" value="${redis_test_on_borrow}"></property> </bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis_addr}"></property> <property name="port" value="${redis_port}"></property> <property name="password" value="${redis_auth}"></property> <property name="poolConfig" ref="jedisConfig"></property> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast to String!!! <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean>
redis配置文件:
redis_addr=192.168.31.204 redis_port=19000 (codis代理服务器端口) redis_auth=okdi redis_max_active=1024 redis_max_idle=200 redis_max_wait=10000 redis_timeout=10000 redis_test_on_borrow=true
封装的redis方法,用的是redisTemplate:
public class RedisServiceImpl implements EhcacheService { private static Logger logger = Logger.getLogger(EhcacheServiceImpl.class); @Autowired private StringRedisTemplate redisTemplate; @Autowired private RedisConstant redisConstant; @Override public void put(String cacheName, String key, String value) { // boolean bool = redisTemplate.hasKey(cacheName); if (cacheName==null || "".equals(cacheName) || key==null || "".equals(key)) { return; } //放入redis redisTemplate.opsForHash().put(cacheName, key, value); long expireTime = redisConstant.getExpireTime(cacheName); //如果不等于-1,则该cacheName配置有过期时间 if(expireTime != -1){ redisTemplate.expire(cacheName, expireTime, TimeUnit.SECONDS); } } @Override public void put(String cacheName, String key, Object value) { if (cacheName==null || "".equals(cacheName) || key==null || "".equals(key)) { return; } //放入redis redisTemplate.opsForHash().put(cacheName, key, JSON.toJSONString(value)); long expireTime = redisConstant.getExpireTime(cacheName); //如果不等于-1,则该cacheName配置有过期时间 if(expireTime != -1){ redisTemplate.expire(cacheName, expireTime, TimeUnit.SECONDS); } } }
用junit测试:
public class test11 extends BaseTest{ @Autowired private EhcacheService redisService; @SuppressWarnings("rawtypes") @Test public void test1(){ long s1=System.currentTimeMillis(); for(int i=0;i<10000;i++){ redisService.put("a"+i, "a"+i,"a"+i); redisService.remove("a"+i, "a"+i); } long s2=System.currentTimeMillis(); System.out.println(testTime(s2-s1)); System.out.println(redisService.getValueByKey("eeee9", "e9")); }
二. 性能对比
两者的性能比较,如下图:
三. 动态扩容
1. codis动态扩容
下图是整个codis的界面
转自:http://blog.csdn.net/dayibagou/article/details/43937039