Redis的java客户端Jedis配置与使用

使用jedis java客户端 

一:一个简单的示例代码:

[java]   view plain copy
  1. public   static   void  main(String[] args) {  
  2.     Jedis jedis = new  Jedis( "147.151.240.234" , 6379 );  
  3.     jedis.set("foo" "bar" );  
  4.     String value = jedis.get("foo" );  
  5.     System.out.println(value);  

二:spring中配置


首先,在项目中引入jeids的jar包。   

Xml代码     收藏代码
  1. <!-- java readis客户端 -->     
  2. < dependency >     
  3.     < groupId > redis.clients </ groupId >     
  4.     < artifactId > jedis </ artifactId >     
  5.     < version > 2.0.0 </ version >     
  6. </ dependency >    



其次,在spring配置文件中添加配置(也可以直接new出这些对象,一样的) 

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
    <property name="maxActive"  value="50" />  
    <property name="maxIdle" value="10" />  
    <property name="maxWait" value="1000" />  
    <property name="testOnBorrow"  value="true"/>  
</bean>  
  
<bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">  
    <constructor-arg index="0" value="这里填你的reids服务器ip" />  
    <constructor-arg index="1" value="6379" />  
</bean>  
  
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">  
    <constructor-arg index="0" ref="jedisPoolConfig" />  
    <constructor-arg index="1">  
        <list>  
            <ref bean="jedis.shardInfo" />  
        </list>  
    </constructor-arg>  
</bean> 
 
最后,调用的时候
Java代码     收藏代码
  1. ShardedJedis jedis =  shardedJedisPool.getResource();    
  2.    jedis.get(key); //从redis服务器获取值     
  3.    jedis.set(key, value); //将值保存到redis服务器     

jedis pool的问题

在使用jedis pool时遇到了这个问题:It seems like server has closed the connection

原因分析:

1.redis server 关闭了此客户端的连接:server端设置了maxidletime(默认是5分钟),服务端会不断循环检测clinet的最后一次通信时间(lastinteraction),如果大于maxidletime,则关闭连接,并回收相关资源。client在向该连接中写数据后就会由于server端已经关闭而出现 broken pipe的问题。

2.pool的设置错误:

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxActive"  value="20" />
        <property name="maxIdle" value="10" />
        <property name="maxWait" value="1000" />
    </bean>
	<!-- jedis shard信息配置 -->
    <bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">
        <constructor-arg index="0" value="*.*.*.*" />
        <constructor-arg index="1" value="6379" />
    </bean>

    <!-- jedis shard pool配置 -->
    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <ref bean="jedis.shardInfo" />
            </list>
        </constructor-arg>
    </bean>

    <bean id="jedisCommands" factory-bean="shardedJedisPool"
        factory-method="getResource" />
 

上面的这种配法在spring初始化时获取一次实例化jedisCommands,而后每次的redis的调用时并未从pool中获取

解决方案:

设置

    <!-- POOL配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxActive"  value="20" />
        <property name="maxIdle" value="10" />
        <property name="maxWait" value="1000" />
        <property name="testOnBorrow"  value="true"/>
    </bean>

    <!-- jedis shard信息配置 -->
    <bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">
        <constructor-arg index="0" value="*.*.*.*" />
        <constructor-arg index="1" value="6379" />
    </bean>

    <!-- jedis shard pool配置 -->
    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <ref bean="jedis.shardInfo" />
            </list>
        </constructor-arg>
    </bean>

最近初次尝试使用redis,java客户端采用的jedis,写了几个简单的类满足基本的服务器配置,以及客户端对象的使用等。

客户端对象的创建
[java]   view plain copy
  1. package  jediscache.common;  
  2.   
  3. import  redis.clients.jedis.Jedis;  
  4. import  redis.clients.jedis.JedisPool;  
  5. import  redis.clients.jedis.JedisPoolConfig;  
  6. import  redis.clients.jedis.Protocol;  
  7.   
  8. public   class  JedisFactory {  
  9.       
  10.     private  JedisPoolConfig jedisPoolConfig;  
  11.       
  12.     private  JedisPool jedisPool;  
  13.   
  14.     public  JedisFactory(JedisPoolConfig jedisPoolConfig) {  
  15.         super ();  
  16.         this .jedisPoolConfig = jedisPoolConfig;  
  17.     }  
  18.       
  19.     public  Jedis getJedisInstance(String host) {  
  20.         return  getJedisPool(host, Protocol.DEFAULT_PORT).getResource();  
  21.     }  
  22.       
  23.     public  Jedis getJedisInstance(String host,  int  port) {  
  24.         return  getJedisPool(host, port).getResource();  
  25.     }  
  26.       
  27.     public  JedisPool getJedisPool(String host) {  
  28.         return  getJedisPool(host, Protocol.DEFAULT_PORT);  
  29.     }  
  30.   
  31.     public  JedisPool getJedisPool(String host,  int  port) {  
  32.         if  (jedisPool ==  null ) {  
  33.             jedisPool = new  JedisPool(jedisPoolConfig, host, port);  
  34.         }  
  35.         return  jedisPool;  
  36.     }  
  37.       
  38.     /**  
  39.      * 配合使用getJedisInstance方法后将jedis对象释放回连接池中  
  40.      *   
  41.      * @param jedis 使用完毕的Jedis对象  
  42.      * @return true 释放成功;否则返回false  
  43.      */   
  44.     public   boolean  release(Jedis jedis) {  
  45.         if  (jedisPool !=  null  && jedis !=  null ) {  
  46.             jedisPool.returnResource(jedis);  
  47.             return   true ;  
  48.         }  
  49.         return   false ;  
  50.     }  
  51.   
  52. }  

 

使用客户端对象
[java]   view plain copy
  1. public   void  testLpush() {  
  2.     JedisFactory factory = new  JedisFactory( new  JedisPoolConfig());  
  3.     Jedis jedis = factory.getJedisInstance("localhost" );  
  4.       
  5.     try  {  
  6.         String word = "word" ;  
  7.         jedis.lpush(word, "first" );  
  8.         jedis.lpush(word, "second" );  
  9.         jedis.lpush(word, "three" );  
  10.         System.out.println("word : "  + jedis.lrange(word,  0 , - 1 ));  
  11.     } finally  {  
  12.         factory.release(jedis);  
  13.     }  
  14. }  


猜你喜欢

转载自wb8206656.iteye.com/blog/1601956