在linux下安装redis
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz $ tar xzf redis-2.8.17.tar.gz $ cd redis-2.8.17 $ make
make完后 redis-2.8.17目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli,两个程序位于安装目录 src 目录下:
下面启动redis服务,可以通过启动参数告诉redis使用指定配置文件使用下面命令启动。
$ cd src $ ./redis-server redis.conf
redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了。 比如:
$ cd src $ ./redis-cli redis> set foo bar OK redis> get foo "bar"
redis启动方式(3种)
1.直接启动
进入redis根目录,执行命令:
#加上‘&’号使redis以后台程序方式运行
1
|
./redis-server redis.conf &
|
2.通过指定配置文件启动
可以为redis服务启动指定配置文件,例如配置为/etc/redis/6379.conf
进入redis根目录,输入命令:
1
|
.
/redis-server
/etc/redis/6379
.conf
|
#如果更改了端口,使用`redis-cli`客户端连接时,也需要指定端口,例如:
1
|
redis-cli -p 6380
|
3.使用redis启动脚本设置开机自启动(https://www.cnblogs.com/pqchao/p/6549510.html)
redis主从配置和参数详解(https://www.cnblogs.com/chenmh/p/5121849.html):
主从配置,其实就是两个redis.conf文件,从conf文件一般命名为:redis_6380.conf,然后在conf中添加:slaveof 192.0.0.1 6379(本地主从)
redis切片和非切片及应用(java 对reids的操作 切片与非切片连接池的应用):
非切片池 一般项目基本都使用切片池。
切片池 主要用于分布式项目,设置主从Redis库。
如果需要指明Redis连接哪个库,需要在使用Redis进行数据操作之前使用如下语句:
Jedis resource = jedisPool.getResource();
resource.select(1);
select(1)表示指定Redis库的第二个库(第一个用0)
java连接redis服(使用的是切片连接池):
maven工程下引包:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
1:建一个redis_cfg.js文件,如:
importClass(com.util.RedisPool);
var config = new RedisPool();
config.setMaxActive(20);
config.setMaxIdle(5);
config.setMaxWait(10000);
config.setTestOnBorrow(false);
config.add("192.168.1.1", 6379, "master");
config.start();
var redisConnPool = config.getRedisConnPool();
2:RedisPool.java
public class RedisPool {
private int maxActive;
private int maxIdle;
private int maxWait;
private boolean testOnBorrow;
ArrayList<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
private ShardedJedisPool redisConnPool = null;
public RedisPool(){}
public void start(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWait(maxWait);
config.setTestOnBorrow(testOnBorrow);
redisConnPool = new ShardedJedisPool(config, shards);
}
public void add(String host, int port, String name){
shards.add(new JedisShardInfo(host, port, name));
}
public ShardedJedisPool getRedisConnPool(){
return redisConnPool;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public int getMaxWait() {
return maxWait;
}
public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}
3:获取ShardedJedisPool
获取文件的路径:redisCfg = Thread.class.getResource("/").getPath() + "redis_cfg.js";
通过ScriptEngineManager获取redisConnPool
ShardedJedisPool redisConnPool = (ShardedJedisPool) ScriptEngine.getValue(redisCfg, "redisConnPool");
public static Object getValue(String filepath, String key) throws FileNotFoundException, ScriptException{
ScriptEngineManager engineManager=new ScriptEngineManager();
javax.script.ScriptEngine jsEngine = engineManager.getEngineByExtension("js");
jsEngine.eval(new FileReader(new File(filepath)));
return jsEngine.get(key);
}
4:操作redis
public class RedisTest {
public static void main(String[] args) {
String redisCfg = Thread.class.getResource("/").getPath() + "redis_cfg.js";
ShardedJedisPool redisConnPool = null;
ShardedJedis jedis = null;
try {
redisConnPool = (ShardedJedisPool) getValue(redisCfg, "redisConnPool");
if (redisConnPool == null){
System.out.println("..................");
System.exit(1);
}
jedis = redisConnPool.getResource();
synchronized (jedis) {
jedis.get("");//redis接口操作
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (redisConnPool != null) {
redisConnPool.returnResource(jedis);
}
}
}
public static Object getValue(String filepath, String key) throws FileNotFoundException, ScriptException{
ScriptEngineManager engineManager=new ScriptEngineManager();
javax.script.ScriptEngine jsEngine = engineManager.getEngineByExtension("js");
jsEngine.eval(new FileReader(new File(filepath)));
return jsEngine.get(key);
}
}