您的位置:首页 > 数据库 > Redis

Linux下Redis环境搭建教程&Jedis简单使用教程

2014-10-23 12:28 846 查看

年龄大了,感觉不找个地方记下来过几天就忘了,同时也跟大家分享一下我的使用经验!有什么不对的欢迎大家指正。


环境和软件

redis-2.8.17

jedis-2.6.0

CentOS 6.4 服务器

Java1.7

1.安装Redis,参考 

1.1 下载redis
[root@i-htlxiwra ~]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz[/code] 
1.2 解压缩
[root@i-htlxiwra ~]# tar -zxvf redis-2.8.17.tar.gz


1.3 进入刚刚解压的redis的根目录make
[root@i-htlxiwra ~]# cd redis-2.8.17
[root@i-htlxiwra redis-2.8.17]# ls
00-RELEASENOTES  BUGS  CONTRIBUTING  COPYING  deps  INSTALL  Makefile  MANIFESTO  README  redis.conf  runtest  runtest-sentinel  sentinel.conf  src  tests  utils
[root@i-htlxiwra redis-2.8.17]# make


1.4 复制文件
[root@i-htlxiwra redis-2.8.17]# cp redis.conf /etc/
[root@i-htlxiwra redis-2.8.17]# cd src
[root@i-htlxiwra redis-2.8.17]# cp redis-benchmark redis-cli redis-server /usr/bin/

2.启动Redis

2.1 普通启动
[root@i-htlxiwra src]# redis-server /etc/redis.conf
[13529] 23 Oct 12:06:52.148 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-``    `.  `_.  ''-._           Redis 2.8.17 (00000000/0) 64 bit
.-`` .-```.  ```\/    _.,_ ''-._
(    '      ,       .-`  | `,    )     Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 13529
`-._    `-._  `-./  _.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |           http://redis.io `-._    `-._`-.__.-'_.-'    _.-'
|`-._`-._    `-.__.-'    _.-'_.-'|
|    `-._`-._        _.-'_.-'    |
`-._    `-._`-.__.-'_.-'    _.-'
`-._    `-.__.-'    _.-'
`-._        _.-'
`-.__.-'

[13529] 23 Oct 12:06:52.150 # Server started, Redis version 2.8.17
[13529] 23 Oct 12:06:52.150 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[13529] 23 Oct 12:06:52.150 * The server is now ready to accept connections on port 6379


2.2 发现以上方式很不方便,后台模式启动(daemon),需要修改配置文件
ctrl+c结束刚才启动的服务
[13529] 23 Oct 12:11:57.841 # User requested shutdown...
[13529] 23 Oct 12:11:57.841 * Saving the final RDB snapshot before exiting.
[13529] 23 Oct 12:11:57.858 * DB saved on disk
[13529] 23 Oct 12:11:57.858 # Redis is now ready to exit, bye bye...


修改配置文件
[root@i-htlxiwra src]# vim /etc/redis.conf
shift+:
/daemon 查找
把 daemonize no 改成 daemonize yes
顺便修改一下 增加一个访问密码

shift+:
/requirepass 查找

把 # requirepass foobared 的注释去掉

shift+:
wq

重新启动服务
[root@i-htlxiwra src]# redis-server /etc/redis.conf
[root@i-htlxiwra src]#
后台启动成功

3.Redis测试

3.1 客户端登陆
[root@i-htlxiwra src]# redis-cli
127.0.0.1:6379>
3.2 设置key-value
127.0.0.1:6379> set mykey myvalue
(error) NOAUTH Authentication required.
说明刚才设置的密码生效了

退出重新登陆
127.0.0.1:6379> quit
[root@i-htlxiwra src]# redis-cli -a foobared


重复
127.0.0.1:6379> set mykey myvalue
OK

Redis客户端常用命令,参考

4.Jedis客户端

4.1 刚刚吃饭去了,现在回来继续写吧
4.2 我的项目是maven项目所以在maven pom.xml 直接引用,大家也可以参考
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>


4.3 再JavaWeb项目下使用,可以再项目启动的时候初始化JedisPool,也可以在使用的初始化,因为JedisPool是线程安全的,所以你可以初始化以后保存下来
public class JedisPoolFactory {

private static Map<String, JedisPool> jedisPoolMap;

private JedisPoolFactory() {

}

/**
* 通过Ip获取redis实例
* @param ip
* @return
*/
public static synchronized JedisPool getInstance(String ip) {
if (jedisPoolMap == null) {
jedisPoolMap = new HashMap<>();
JedisPool pool = init(jedisPoolMap, ip);
return pool;
} else {
JedisPool pool = jedisPoolMap.get(ip);
if (pool == null) {
pool = init(jedisPoolMap, ip);
}
return pool;
}
}

private static JedisPool init(Map<String, JedisPool> jedisPoolMap, String ip) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
JedisPool pool = new JedisPool(poolConfig, ip, 6379, 2000, "xiaohuobantv1234");
jedisPoolMap.put(ip, pool);
return pool;
}

public static synchronized void destroy() {
if (jedisPoolMap != null) {
for (String ip : jedisPoolMap.keySet()) {
JedisPool pool = jedisPoolMap.get(ip);
if (pool != null) {
pool.destroy();
}
}
}
}

}


4.4 程序关闭的时候销毁JedisPool
@Override
public void contextDestroyed(ServletContextEvent sce) {
LOGGER.debug("destroyed..");
JedisPoolFactory.destroy();
}


4.5 使用完成以后一定要释放资源,不然资源耗尽,再从JedisPool中取连接的话,就会死锁(貌似啊,没细看,就卡在那了)
Jedis jedis = jedisPool.getResource();
try {
jedis.set("mykey", "myvalue");
} catch (Exception e) {
//TODO
} finally {
jedisPool.returnResource(jedis);
}


就是 它了 
jedisPool.returnResource(jedis);

4.6 如果需要set多次的话建议使用 事务,这样效率会高一些

Transaction transaction t = jedis.multi();
t.set("fool", "bar");
Response<String> result1 = t.get("fool");

t.zadd("foo", 1, "barowitch"); t.zadd("foo", 0, "barinsky"); t.zadd("foo", 0, "barikoviev");
Response<Set<String>> sose = t.zrange("foo", 0, -1);   // get the entire sortedset
t.exec();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Redis linux jedis java