redis安装:
1、下载源码,解压缩后编译源码。
$ wget http://download.redis.io/releases/redis-2.8.3.tar.gz $ tar xzf redis-2.8.3.tar.gz $ cd redis-2.8.3 $ make2、启动Redis服务。
$ suse11:/home/test/redis-3.2.8/src # ./redis-server ../redis.conf3、用客户端测试一下是否启动成功。
suse11:/home/test/redis-3.2.8/src # ./redis-cli redis> set foo bar OK redis> get foo "bar" 安装过程中可能出现的问题: 1)提示没有gcc环境 解决:安装gcc环境 2)zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory 解决:make MALLOC=libcjedis访问: public class JedisPoolUtil { private static JedisPool jedisPool = null; private static int TIMEOUT = 10000; private static JedisPoolUtil jedisPoolUtil = null; private JedisPoolUtil(){}; public static JedisPoolUtil getInstance(){ try { if(jedisPoolUtil == null){//懒汉式 synchronized (JedisPoolUtil.class) { if(jedisPoolUtil == null){//二次检查 jedisPoolUtil = new JedisPoolUtil(); setJedisPool(); } } } } catch (Exception e) { e.printStackTrace(); } return jedisPoolUtil; } /** * 初始化Redis连接池 */ private static void setJedisPool(){ String addr = "132.126.2.29"; int port = 6379; try { GenericObjectPoolConfig config = RedisUtil.getGenericConfig(); //获取ip、port List<Map<String, String>> list = RedisConfigUtil.getHostMap(); if(list != null && list.size() > 0){ Map<String, String> map = list.get(0); if(map != null){ addr = map.get(RedisConfigUtil.HOST); port = Integer.parseInt(map.get(RedisConfigUtil.PORT)); } if(StringUtils.isNotEmpty(map.get(RedisConfigUtil.PASSWORD))){ jedisPool = new JedisPool(config, addr, port, TIMEOUT, map.get(RedisConfigUtil.PASSWORD)); }else{ jedisPool = new JedisPool(config, addr, port, TIMEOUT); } } } catch (Exception e) { e.printStackTrace(); } } /** * 获取Jedis实例 * @return */ public Jedis getJedis() { try { if (jedisPool == null) { setJedisPool(); } Jedis resource = jedisPool.getResource(); return resource; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 释放jedis资源 * @param jedis */ public void returnResource(final Jedis jedis) { if (jedis != null) { jedisPool.returnResource(jedis); } } /** * 释放jedis资源 * @param jedis */ public void returnBrokenResource(final Jedis jedis) { if (jedis != null) { jedisPool.returnBrokenResource(jedis); } } public static void main(String[] args){ JedisPoolUtil.getInstance().getJedis(); } } public class RedisUtil { //可用连接实例的最大数目,默认值为8; //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 private static int MAX_ACTIVE = 1024; //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 private static int MAX_IDLE = 200; //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException; private static int MAX_WAIT = 10000; //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; private static boolean TEST_ON_BORROW = true; public static GenericObjectPoolConfig getGenericConfig(){ GenericObjectPoolConfig config = new GenericObjectPoolConfig(); if(RedisConfigUtil.get("max_active") != null && !"".equals(RedisConfigUtil.get("max_active"))){ MAX_ACTIVE = Integer.parseInt(RedisConfigUtil.get("max_active")); } if(RedisConfigUtil.get("max_idle") != null && !"".equals(RedisConfigUtil.get("max_idle"))){ MAX_IDLE = Integer.parseInt(RedisConfigUtil.get("max_idle")); } if(RedisConfigUtil.get("max_wait") != null && !"".equals(RedisConfigUtil.get("max_wait"))){ MAX_WAIT = Integer.parseInt(RedisConfigUtil.get("max_wait")); } config.setMaxTotal(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWaitMillis(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); return config; } } 访问过程中可能出现以下错误: 1)redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:50) at redis.clients.jedis.JedisPool.getResource(JedisPool.java:86) at com.web.session.redis.JedisPoolUtil.getJedis(JedisPoolUtil.java:75) at com.web.session.redis.JedisPoolUtil.main(JedisPoolUtil.java:104) Caused by: java.util.NoSuchElementException: Unable to validate object at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:506) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) at redis.clients.util.Pool.getResource(Pool.java:48) ... 3 more 2) redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. 3)Could not get a resource from the pool, Connection refused: connect 说明:上述3种错误的原因可能相同,如下: Redis protected-mode 是3.2之后加入的新特性,在Redis.conf的注释中,我们可以了解到,他的具体作用和启用条件链接redis时只能通过本地localhost (127.0.0.1) 这个来链接,而不能用网络ip(192.168..)这个链接。问题是如果用网络ip 链接会报错。 解决: 1)打开redis.conf配置文件把下面对应的注释掉# bind 127.0.0.1
2)Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程,设置为no
daemonize no3)保护模式
protected-mode no