Jedis下的ShardedJedis

jedis客戶端操做redis主要三種模式:單臺模式、分片模式(ShardedJedis)、集羣模式(BinaryJedisCluster)java

ShardedJedis是經過一致性哈希來實現分佈式緩存的,經過必定的策略把不一樣的key分配到不一樣的redis server上,達到橫向擴展的目的。redis

1、簡介算法

一、jedisspring

redis是內存數據庫,常被用做緩存。jedis是java語言的redis客戶端。本文將基於原生的jedis,對spring與redis集成開發進行介紹。數據庫

二、ShardedJedisPoolapache

ShardedJedisPool是基於一致性哈希算法實現的分佈式jedis池。緩存

這裏的一致性哈希算法,簡單的說,就是將部署的redis-server(一個或多個)和存數據的key映射到同一地此空間,maven

這樣redisServer可動態添加或減小。分佈式

2、開發步驟函數

一、添加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>

 

 

二、配置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"
/>

 

 

三、配置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>

 

 

 

四、經過ShardedJedisPool獲取jedis實例

 

ShardedJedis shardedJedis = shardedJedisPool.getResource();

 

 

五、使用jedis進行數據存取

如:

 

shardedJedis.set(name,value);
shardedJedis.get(name);

 

六、使用完後,將jedis實例還給池

 

 
shardedJedisPool.returnResource(shardedJedis);  //正常使用完後,將jedis實例返回到池中
shardedJedisPool.returnBrokenResource(shardedJedis);  //沒法正常使用,將jedis實例返回到池中,標識該jedis實例不能使用

 

 

 

3、代碼實例

一、添加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>

 

二、添加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>

 

 

 

三、使用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實例返回到池中

            }
        }
    }
}
相關文章
相關標籤/搜索