Java Redis实战之Redis + Jedis 博客分类: 技术总结 JavaRedis实战Jedis
程序员文章站
2024-02-12 14:47:58
...
目前Redis大概有3中基于Java语言的Client:
- Jredis
- Jedis
- Redis4J
一、简单使用Jedis
需要Jedis就从Maven获取吧!
Maven Pom.xml
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.1.0</version>
- <type>jar</type>
- <scope>compile</scope>
- </dependency>
如果只是简单使用Jedis,以下这么几行代码足够:
- Jedis jedis = new Jedis("10.11.20.140");
- String keys = "name";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
- System.out.println(value);
- Jedis jedis = new Jedis("10.11.20.140");
- String keys = "name";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
- System.out.println(value);
二、池化使用Jedis
Jedis使用commons-pool完成池化实现。
先做个配置文件:
- #最大分配的对象数
- redis.pool.maxActive=1024
- #最大能够保持idel状态的对象数
- redis.pool.maxIdle=200
- #当池内没有返回对象时,最大等待时间
- redis.pool.maxWait=1000
- #当调用borrow Object方法时,是否进行有效性检查
- redis.pool.testOnBorrow=true
- #当调用return Object方法时,是否进行有效性检查
- redis.pool.testOnReturn=true
- #IP
- redis.ip=10.11.20.140
- #Port
- redis.port=6379
在静态代码段中完成初始化:
- private static JedisPool pool;
- static {
- ResourceBundle bundle = ResourceBundle.getBundle("redis");
- if (bundle == null) {
- throw new IllegalArgumentException(
- "[redis.properties] is not found!");
- }
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxActive(Integer.valueOf(bundle
- .getString("redis.pool.maxActive")));
- config.setMaxIdle(Integer.valueOf(bundle
- .getString("redis.pool.maxIdle")));
- config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
- config.setTestOnBorrow(Boolean.valueOf(bundle
- .getString("redis.pool.testOnBorrow")));
- config.setTestOnReturn(Boolean.valueOf(bundle
- .getString("redis.pool.testOnReturn")));
- pool = new JedisPool(config, bundle.getString("redis.ip"),
- Integer.valueOf(bundle.getString("redis.port")));
- }
- private static JedisPool pool;
- static {
- ResourceBundle bundle = ResourceBundle.getBundle("redis");
- if (bundle == null) {
- throw new IllegalArgumentException(
- "[redis.properties] is not found!");
- }
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxActive(Integer.valueOf(bundle
- .getString("redis.pool.maxActive")));
- config.setMaxIdle(Integer.valueOf(bundle
- .getString("redis.pool.maxIdle")));
- config.setMaxWait(Long.valueOf(bundle.getString("redis.pool.maxWait")));
- config.setTestOnBorrow(Boolean.valueOf(bundle
- .getString("redis.pool.testOnBorrow")));
- config.setTestOnReturn(Boolean.valueOf(bundle
- .getString("redis.pool.testOnReturn")));
- pool = new JedisPool(config, bundle.getString("redis.ip"),
- Integer.valueOf(bundle.getString("redis.port")));
- }
然后,修改前面那段jedis操作Redis
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
- String keys = "name";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
- System.out.println(value);
- // 释放对象池
- pool.returnResource(jedis);
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
- String keys = "name";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, "snowolf");
- // 取数据
- String value = jedis.get(keys);
- System.out.println(value);
- // 释放对象池
- pool.returnResource(jedis);
改为从对象池中,获取Jedis实例:
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
- // 从池中获取一个Jedis对象
- Jedis jedis = pool.getResource();
切记,最后使用后,释放Jedis对象:
- // 释放对象池
- pool.returnResource(jedis);
- // 释放对象池
- pool.returnResource(jedis);
三、一致性哈希
Memcached完全基于分布式集群,而Redis是Master-Slave,如果想把Reids,做成集群模式,无外乎多做几套Master-Slave,每套Master-Slave完成各自的容灾处理,通过Client工具,完成一致性哈希。
PS:Memcached是在Server端完成Sharding,Redis只能依靠各个Client做Sharding。可能会在Redis 3.0系列支持Server端Sharding。
保留前面的JedisPoolConfig,新增两个Redis的IP(redis1.ip,redis2.ip),完成两个JedisShardInfo实例,并将其丢进List中:
- JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
- bundle.getString("redis1.ip"), Integer.valueOf(bundle .getString("redis.port")));
- JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
- bundle.getString("redis2.ip"), Integer.valueOf(bundle .getString("redis.port")));
- List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
- list.add(jedisShardInfo1);
- list.add(jedisShardInfo2);
- JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
- bundle.getString("redis1.ip"), Integer.valueOf(bundle .getString("redis.port")));
- JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
- bundle.getString("redis2.ip"), Integer.valueOf(bundle .getString("redis.port")));
- List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
- list.add(jedisShardInfo1);
- list.add(jedisShardInfo2);
初始化ShardedJedisPool代替JedisPool:
- ShardedJedisPool pool = new ShardedJedisPool(config, list);
- ShardedJedisPool pool = new ShardedJedisPool(config, list);
改由ShardedJedis,获取Jedis对象:
- // 从池中获取一个Jedis对象
- ShardedJedis jedis = pool.getResource();
- String keys = "name";
- String value = "snowolf";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, value);
- // 取数据
- String v = jedis.get(keys);
- System.out.println(v);
- // 释放对象池
- pool.returnResource(jedis);
- // 从池中获取一个Jedis对象
- ShardedJedis jedis = pool.getResource();
- String keys = "name";
- String value = "snowolf";
- // 删数据
- jedis.del(keys);
- // 存数据
- jedis.set(keys, value);
- // 取数据
- String v = jedis.get(keys);
- System.out.println(v);
- // 释放对象池
- pool.returnResource(jedis);
使用commons-pool连接池