1. 准备工做:java
将下载好的redis-3.2.2.tar.gz上传到linux服务器 /lamp,进行解压解压linux
tar -zxvf redis-3.2.2.tar.gzredis
网址:http://download.redis.io/releases/redis-3.2.2.tar.gzspring
2. 进行编译:服务器
cd redis-3.2.2session
makeapp
make这一步可能会报错,若是报错,能够尝试使用以下命令来编译:工具
make MALLOC=libc测试
( 注意:make指令是须要linux下安装gcc的 若是没有gcc能够尝试安装ui
yum -y install gcc )
编译好的二进制文件会放到src/目录下,能够看到有redis-server和redis-cli,这是redis的服务端和客户端,咱们到时候能够直接运行这两个文件便可启动服务端和客户端
3. 建立目录移动文件、便于管理:
mkdir –p /usr/local/redis/bin 放命令
mkdir –p /usr/local/redis/etc放配置文件
而后将上面src目录下的命令和配置文件分别移动到对应的目录
mv /lamp/redis-3.2.2/redis.conf /usr/local/redis/etc
cd /lamp/redis-3.2.2/src
mv mkreleasdhdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-server /usr/local/redis/bin
4. 启动redis服务:
进入到/usr/local/redis/bin目录下,执行
./redis-server /usr/local/redis/etc/redis.conf
这是根据配置文件redis.conf来启动redis服务,可是默认是前台启动的,会占用个人session,若是想要后台启动redis,还须要修改一下redis.conf的配置,打开该配置文件:
vi /usr/local/redis/etc/redis.conf
而后将daemonize no改成yes,而后再启动一下redis服务就能够后台启动了,而后咱们能够查看一下是否启动成功:
ps -ef | grep redis 查看是否启动成功
netstat -tunpl | grep 6379 查看该端口有没有占用
5. 启动redis客户端:(在redis.conf 中增长 requirepass = ylx (密码))
redis客户端命令也在bin目录下,是redis-cli文件,运行一下便可启动redis客户端:
./redis-cli -a ylx
随便往里面插入一个name为test测试一下,能够正常获取,说明客户端没有问题。
set name test
get name
退出客户端的话直接quit便可。
6. 关闭redis服务:
关闭redis服务的话直接使用以下命令便可:
pkill redis-server
配置文件 application.properties
#redis
redis.ip=192.168.203.13
redis.auth=ylx
redis.port=6379
redis.timeout=10000
redis.MaxTotal=200
redis.MaxIdle=200
redis.MinIdle=0
redis.MaxWaitMillis=-1
redis_list_len=100000
1. 加载redis配置文件
package com.eversec.pierce.redis;
import com.eversec.pierce.util.PropertiesLoader;
public class RedisConfig {
/**
* 加载redis配置文件
*/
private static PropertiesLoader propertiesLoader = null;
static{
propertiesLoader = new PropertiesLoader("application.properties");
String active=propertiesLoader.getProperty("spring.profiles.active");
propertiesLoader = new PropertiesLoader("application-"+active+".properties");
}
public static String getRedisConfig(String key){
return propertiesLoader.getProperty(key);
}
}
2. 维护redis链接池资源
package com.eversec.pierce.redis;
import java.io.Serializable;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* 维护redis链接池资源
* @author root
*
*/
public class RedisResources {
private static final Logger logger = LoggerFactory.getLogger(RedisResources.class);
private static String IP = RedisConfig.getRedisConfig("redis.ip");// 链接地址
private static String AUTH = RedisConfig.getRedisConfig("redis.auth");// 认证信息
private static int PORT = Integer.parseInt(RedisConfig.getRedisConfig("redis.port"));
private static int TIMEOUT = Integer.parseInt(RedisConfig.getRedisConfig("redis.timeout"));// 超时时间
private static int MaxTotal = Integer.parseInt(RedisConfig.getRedisConfig("redis.MaxTotal"));
private static int MaxIdle = Integer.parseInt(RedisConfig.getRedisConfig("redis.MaxIdle"));
private static int MinIdle = Integer.parseInt(RedisConfig.getRedisConfig("redis.MinIdle"));
private static int MaxWaitMillis = Integer.parseInt(RedisConfig.getRedisConfig("redis.MaxWaitMillis"));
private static JedisPool jedisPool = null;
/**
* 加载redis链接池
*/
static {
init();
}
private static void init(){
JedisPoolConfig config = new JedisPoolConfig();
//config.setMaxWaitMillis(MAX_WAIT);
//config.setMaxTotal(MAX_ACTIVE);
//config.setMinIdle(MIN_IDLE);
//config.setTestOnBorrow(TEST_ON_BORROW);
//-------------------------------------------------
config.setMaxTotal(MaxTotal);
config.setMaxIdle(MaxIdle);
config.setMinIdle(MinIdle);//设置最小空闲数
config.setMaxWaitMillis(MaxWaitMillis);
//config.setTestOnBorrow(TestOnBorrow);
//config.setTestOnReturn(TestOnReturn);
//Idle时进行链接扫描
//config.setTestWhileIdle(TestWhileIdle);
//表示idle object evitor两次扫描之间要sleep的毫秒数
//config.setTimeBetweenEvictionRunsMillis(TimeBetweenEvictionRunsMillis);
//表示idle object evitor每次扫描的最多的对象数
//config.setNumTestsPerEvictionRun(NumTestsPerEvictionRun);
//表示一个对象至少停留在idle状态的最短期,而后才能被idle object evitor扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
//config.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis);
jedisPool = new JedisPool(config,IP,PORT,TIMEOUT,AUTH);
}
/**
* 从链接池里获取redis链接
* @return
*/
public static Jedis getResources(){
int i=0;
Jedis jedis=null;
while(true){
try{
jedis=jedisPool.getResource();
}catch(Exception e){
init();
jedis=jedisPool.getResource();
logger.error("Redis链接失败,第"+i+"次链接");
}
if(jedis != null){
return jedis;
}
}
}
}
3. redis使用工具类
package com.eversec.pierce.redis;import java.io.Serializable;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.eversec.pierce.config.Global;import redis.clients.jedis.Jedis;/** * redis使用工具类 * @author root * */public class RedisTemplate<T extends Serializable> { private static final Logger logger = LoggerFactory.getLogger(RedisTemplate.class); /** * 保存字符串 * @param key * @param value */ public void setStr(String key,String value)throws Exception{ Jedis jedis = null; try{ jedis = RedisResources.getResources(); jedis.set(key, value); }catch(Exception e){ logger.error("Redis工具类异常",e); throw new Exception(e); }finally{ if (jedis != null) { jedis.close(); } } } /** * 获取字符串值 * @param key * @return */ public String getStr(String key) throws Exception{ Jedis jedis = null; String value=null; try{ jedis = RedisResources.getResources(); value = jedis.get(key); }catch(Exception e){ logger.error("Redis工具类异常",e); throw new Exception(e); }finally{ if (jedis != null) { jedis.close(); } } return value; } /** * 保存List * @param key * @param t */ public <T extends Serializable > long setList(String key,String value) throws Exception{ Jedis jedis = null; try { jedis = RedisResources.getResources(); long lag=jedis.rpush(key, value); return lag; } catch (Exception e) { logger.error("Redis工具类异常",e); throw new Exception(e); }finally{ if (jedis != null) { jedis.close(); } } } /** * 获取List */ public <T extends Serializable> List<T> getList(String key)throws Exception{ Jedis jedis = null; List<T> t = null; try{ jedis = RedisResources.getResources(); int len_end = Integer.parseInt(Global.getConfig("redis_list_len")); t=(List<T>) jedis.lrange(key,0,len_end);//返回List集合 }catch(Exception e){ logger.error("Redis工具类异常",e); throw new Exception(e); }finally{ if (jedis != null) { jedis.close(); } } return t; } /** * 清空List */ public <T extends Serializable> void delList(String key)throws Exception{ Jedis jedis = null; try{ jedis = RedisResources.getResources(); jedis.del(key); }catch(Exception e){ logger.error("Redis工具类异常",e); throw new Exception(e); }finally{ if (jedis != null) { jedis.close(); } } } /** * 移除List中的元素 */ public <T extends Serializable> void removeList(String key,Long index) throws Exception{ Jedis jedis = null; try{ jedis = RedisResources.getResources(); //ltrim: ltrim mylist 1 -1 //保留mylist中 1到末尾的值,即删除第一个值。 jedis.ltrim(key, index, -1); }catch(Exception e){ logger.error("Redis工具类异常",e); throw new Exception(e); }finally{ if (jedis != null) { jedis.close(); } } } }以上是本人本身总结,而且在项目总实际运用操做的。新手一枚,不喜勿喷!(接下来的文章中会有 redis集群版的安装部署以及java调用工具类)