- 浏览: 421784 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (269)
- 原创 (7)
- Java (51)
- Java Concurrency (2)
- IDE (16)
- Linux (46)
- Database (23)
- NoSQL (35)
- Web服务器 (23)
- Log日志 (11)
- HTTP (11)
- HTML (2)
- XML (1)
- Test (7)
- Mina (0)
- Amoeba (4)
- Cobar (1)
- 序列化 (2)
- Python (5)
- PHP (1)
- Socket通信 (1)
- Network (3)
- Struts (2)
- Web前端 (10)
- Maven (6)
- SVN (15)
- Json (1)
- XMPP (2)
- Go (1)
- Other (4)
- 未整理 (5)
最新评论
-
u012374672:
[color=darkred][/color][flash=2 ...
Mongo的ORM框架的学习Morphia(annotations) -
b_l_east:
很有问题啊
利用redis的transaction功能,实现分布式下加锁
使用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出这些对象,一样的)
<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>
最后,调用的时候
- 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);
-
}
-
}
- 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);
- }
- }
发表评论
-
mongodb 地理位置处理
2016-05-16 13:39 1390我只记录我用到的部分,没有完整分析mongodb对地理位置 ... -
Redis配置文件redis.conf
2014-11-14 14:10 1844# Redis configuration file ex ... -
Redis高可用部署及监控
2014-11-12 13:25 1078一、 Re ... -
JCS官方文档的简单笔记,仅供自己参考
2014-09-26 20:08 7531. 基本配置 jcs.default=DCjcs.de ... -
JCS基本配置
2014-09-26 19:39 9191、默认的内存缓存 ... -
NoSQL解决方案比较(MongoDB vs Redis, Tokyo Cabinet, and Berkeley DB)
2013-09-30 14:20 1315NoSQL解决方案比较 NoSQL Solution: E ... -
morphia与spring的整合
2012-12-07 15:06 1462转自: http://www.blogjava.net/wat ... -
Mongo的ORM框架的学习Morphia(十五)Morphia+spring整合
2012-12-07 15:06 1634转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(十二) morphia的Query和Update
2012-12-07 15:06 1843转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(十) morphia应用
2012-12-05 14:47 1437转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(九) morphia简单使用
2012-12-05 14:44 1350转自 http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(八) morphia数据库访问接口
2012-12-05 14:35 1994转自:http://topmanopensource.itey ... -
Mongo的ORM框架的学习Morphia(annotations)
2012-12-05 14:33 2507一:@Entity的使用 @Entity ... -
Instagram的Redis实践(内存占用优化)
2012-11-30 10:43 1173转自:http://blog.nosqlfan.com/htm ... -
SQL 和Mongo 对比图表
2012-11-28 14:54 2182参看官方说明: http://www.mongodb ... -
MongoDB 入门指南、示例
2012-11-23 10:38 830转自:http://www.cnblogs.com/hoojo ... -
mongodb中使用MapReduce
2012-11-23 10:12 1190MapReduce函数的用法如下: db.users.ma ... -
python的redis用法
2012-11-22 15:48 1144#! /usr/bin/env python #coding ... -
Python连接redis
2012-11-22 15:46 5587一、Redis是流行的NOSQL内存数据库,以Key-Valu ... -
【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】
2012-08-29 10:42 1350转自 http://www.bwkeji.com/a/wang ...
相关推荐
一个简单java客户端Jedis,大家可以到https://github.com/xetorthio/jedis这网址下载相关源码 这里对其进行一个简单封转 包括key valye set list hashset的封装
Redis Java客户端Jedis 2.9.0 jar+commons-pool2-2.4.2
Redis的官方推荐Java客户端——Jedis 此Jar包为Jedis 2.9.0版(2016年7月22日发布) 支持Redis 3.0 (亲测有效) 使用方法: import redis.clients.jedis.Jedis; Jedis jedis = new Jedis("localhost"); jedis.set...
Jedis 是 Redis 官方首选的 Java 客户端开发包。此包包含最新的版本截止2018-06-10,版本2.9.0,并且包含依赖的JAR包,仅需导入即可使用。
redis的Java客户端jedis使用示例.rar
jedis:Redis的Java客户端
redis的Java客户端jedis池的介绍及使用.rar
spring_redis集成,通过jedis作为redis的客户端。 只提供集成方式,具体api,参照jedis API
Redis支持很多编程语言的客户端,有C、C#、C++、Clojure、Common Lisp、Erlang、Go、Lua、...Redis官方列出的Java客户端也有少,有Jedis、JRedis、JDBC-Redis、RJC等,当然,Jedis是Redis官方首选的Java客户端开发包。
* redis主从架构的jedis客户端管理类,大概1000多行代码左右 * 1 支持主从复制key/value,pop/push,pub/sub,读/写分离等功能的灾难失败自动选择和恢复 * 2 可以选择读写分离功能,主写从读,默认不启用,都使用主服务...
redis3.0 java下客户端jedis
* redis主从架构的jedis客户端管理类,大概1000行代码左右 * 1 支持主从复制key/value,pop/push,pub/sub,读/写分离等功能的灾难失败自动选择和恢复 * 2 可以选择读写分离功能,主写从读,默认不启用,都使用主服务进行...
redis的Java客户端开发包Jedis,有两个jar包 commons-pool2-2.6.0.jar jedis-2.9.0.jar
redis java 客户端,里面包含两个jar文件、jedis-2.1.0.jar和commons-pool-1.5.6.jar
分享一下Redis的JAVA客户端,Jedis工具包
RedisClient是Redis客户端的GUI工具,使用Java swt和jedis编写,可以方便开发者浏览Redis数据库。该软件支持简体中文,非常适合国内用户使用,不需要汉化就可以直接使用。RedisClient将redis数据以资源管理器的界面...
NULL 博文链接:https://yuhuiblog695685688425687986842568269.iteye.com/blog/2391347
RedisClient是Redis客户端的GUI工具,使用Java swt和jedis编写,可以方便开发者浏览Redis数据库。该软件支持简体中文,非常适合国内用户使用,不需要汉化就可以直接使用。
jedis客户端两个包:jedis2.7.2.jar+commons-pool2-2.0.jar,已亲测可用
Redis高可用集群Java(jedis客户端)操作源码