java web redis使用(二)
上篇中已经安装好redis,然后下面就说怎么在java web中用客户端来使用regis
使用jedis java客户端
一:一个简单的示例代码:
- public static void main(String[] args) {
- Jedis jedis = new Jedis( "147.151.240.234" , 6379 );
- jedis.set("foo" , "bar" );
- String value = jedis.get("foo" );
- System.out.println(value);
- }
二:spring中配置 :
首先,在项目中引入jeids的jar包。
- <!-- java readis客户端 -->
- < dependency >
- < groupId > redis.clients </ groupId >
- < artifactId > jedis </ artifactId >
- < version > 2.0.0 </ version >
- </ dependency >
其次,在spring配置文件中添加配置(也可以直接new出这些对象,一样的)
最后,调用的时候
- ShardedJedis jedis = shardedJedisPool.getResource();
- jedis.get(key); //从redis服务器获取值
- 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,写了几个简单的类满足基本的服务器配置,以及客户端对象的使用等。
客户端对象的创建
- package jediscache.common;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- import redis.clients.jedis.Protocol;
- public class JedisFactory {
- private JedisPoolConfig jedisPoolConfig;
- private JedisPool jedisPool;
- public JedisFactory(JedisPoolConfig jedisPoolConfig) {
- super ();
- this .jedisPoolConfig = jedisPoolConfig;
- }
- public Jedis getJedisInstance(String host) {
- return getJedisPool(host, Protocol.DEFAULT_PORT).getResource();
- }
- public Jedis getJedisInstance(String host, int port) {
- return getJedisPool(host, port).getResource();
- }
- public JedisPool getJedisPool(String host) {
- return getJedisPool(host, Protocol.DEFAULT_PORT);
- }
- public JedisPool getJedisPool(String host, int port) {
- if (jedisPool == null ) {
- jedisPool = new JedisPool(jedisPoolConfig, host, port);
- }
- return jedisPool;
- }
- /**
- * 配合使用getJedisInstance方法后将jedis对象释放回连接池中
- *
- * @param jedis 使用完毕的Jedis对象
- * @return true 释放成功;否则返回false
- */
- public boolean release(Jedis jedis) {
- if (jedisPool != null && jedis != null ) {
- jedisPool.returnResource(jedis);
- return true ;
- }
- return false ;
- }
- }
使用客户端对象
- public void testLpush() {
- JedisFactory factory = new JedisFactory( new JedisPoolConfig());
- Jedis jedis = factory.getJedisInstance("localhost" );
- try {
- String word = "word" ;
- jedis.lpush(word, "first" );
- jedis.lpush(word, "second" );
- jedis.lpush(word, "three" );
- System.out.println("word : " + jedis.lrange(word, 0 , - 1 ));
- } finally {
- factory.release(jedis);
- }
- }