jedis客户端操作redis主要三种模式:单台模式、分片模式(ShardedJedis)、集群模式(BinaryJedisCluster)
ShardedJedis是通过一致性哈希来实现分布式缓存的,通过一定的策略把不同的key分配到不同的redis server上,达到横向扩展的目的。
一、简介
1、jedis
redis是内存数据库,常被用作缓存。jedis是java语言的redis客户端。本文将基于原生的jedis,对spring与redis集成开发进行介绍。
2、ShardedJedisPool
ShardedJedisPool是基于一致性哈希算法实现的分布式jedis池。
这里的一致性哈希算法,简单的说,就是将部署的redis-server(一个或多个)和存数据的key映射到同一地此空间,
这样redisServer可动态添加或减少。
二、开发步骤
1、添加java的redis驱动器jedis,以及连接池,maven依赖如下:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
2、配置jedis控制参数,redis.clients.jedis.JedisPoolConfig,其中的参数有:
a)maxTotal,池中最多可以有多少个jedis实例,默认值是8
b)maxIdle,池中最多有多少个空闲的jedis实例,默认值是8
c)minIdle,池中最小有多少个空闲的jedis实例,默认值是0
d)maxWaitMillis,获取jedis实例的最大等待毫秒数,默认值是-1
配置可如:
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"
p:maxTotal="6000"
p:maxIdle="300"
p:minIdle="100"
p:maxWaitMillis="1000"
/>
3、配置jedis池,redis.clients.jedis.ShardedJedisPool,其有多个构造函数,最主要的是:
public ShardedJedisPool(GenericObjectPoolConfig poolConfig, List<JedisShardInfo> shards) {
this(poolConfig, shards, Hashing.MURMUR_HASH);
}
即是:
a)添加控制参数,也就是上一步配置的JedisPoolConfig。
b)添加redis分片,每一个分片在redis.clients.jedis.JedisShardInfo中配置,
内部参数包括部署地址、端口、超时时间、权重等信息,原码如下:
public class JedisShardInfo extends ShardInfo<Jedis> {
private int timeout;
private String host;
private int port;
private String password;
private String name;
public abstract class ShardInfo<T> {
private int weight;
}
完整配置如:
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="127.0.0.1" />
<constructor-arg name="port" value="6379" />
</bean>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="127.0.0.1" />
<constructor-arg name="port" value="6380" />
</bean>
</list>
</constructor-arg>
</bean>
4、通过ShardedJedisPool获取jedis实例
ShardedJedis shardedJedis = shardedJedisPool.getResource();
5、使用jedis进行数据存取
如:
shardedJedis.set(name,value);
shardedJedis.get(name);
6、使用完后,将jedis实例还给池
shardedJedisPool.returnResource(shardedJedis); //正常使用完后,将jedis实例返回到池中
shardedJedisPool.returnBrokenResource(shardedJedis); //无法正常使用,将jedis实例返回到池中,标识该jedis实例不能使用
三、代码实例
1、添加maven依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
2、添加spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"
p:maxTotal="6000"
p:maxIdle="300"
p:minIdle="100"
p:maxWaitMillis="1000"
/> <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg name="host" value="127.0.0.1" />
<constructor-arg name="port" value="6379" />
</bean>
<!--<bean class="redis.clients.jedis.JedisShardInfo">-->
<!--<constructor-arg name="host" value="127.0.0.1" />-->
<!--<constructor-arg name="port" value="6380" />-->
<!--</bean>-->
</list>
</constructor-arg>
</bean>
</beans>
3、使用jedis进行数据存取
public class ShardedJedisPoolMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/spring-redis.xml");
ShardedJedisPool shardedJedisPool = (ShardedJedisPool) ac.getBean("shardedJedisPool");
boolean broken = false; //jedis实例是否已不能使用
ShardedJedis shardedJedis = null;
try{
shardedJedis = shardedJedisPool.getResource(); //从池中获取jedis实例
shardedJedis.set("key1","value1");
String val = shardedJedis.get("key1");
System.out.println(val);
}catch (Exception e){
broken=true;
}finally {
if(broken){
shardedJedisPool.returnBrokenResource(shardedJedis); //无法正常使用,将jedis实例返回到池中,标识该jedis实例不能使用
}else{
shardedJedisPool.returnResource(shardedJedis); //正常使用完后,将jedis实例返回到池中 }
}
}
}