Redis集群开发 java代码示例
下载jredis的java包
我这里直接引入jar包,大家可以用maven直接引入。
注意:直接下载jar包的时候要注意本身所依赖的包
单示例java代码示例
package com.osgc.oc.redis;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import com.osgc.oc.utils.ServerDirUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public final class RedisDBUtil {
//Redis服务器IP
private static String HOST = ""; //192.168.0.100
//Redis的端口号
private static int PORT = 6379;
//访问密码
private static String PASSWORD = ""; //admin
private static int DATABASE = -1;
//可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = 1024;
//链接池中最大空闲的连接数,默认为8.
private static int MAX_IDLE = 200;
//当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时.
private static int MAX_WAIT = 10000;
//连接超时时间。单位,毫秒数;
private static int TIMEOUT = 10000;
//向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值.
private static boolean TEST_ON_BORROW = false;
//向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值.
private static boolean TEST_ON_RETURN = false;
private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
static {
try {
//读取redis配置文件
BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(ServerDirUtils.getServerPoolDir()+File.separator+"redis.properties"));
ResourceBundle bundle = new PropertyResourceBundle(inputStream);
inputStream.close();
MAX_ACTIVE = Integer.valueOf(bundle.getString("redis.maxActive"));
MAX_WAIT = Integer.valueOf(bundle.getString("redis.maxWait"));
TEST_ON_BORROW = Boolean.valueOf(bundle.getString("redis.testOnBorrow"));
TEST_ON_RETURN = Boolean.valueOf(bundle.getString("redis.testOnReturn"));
HOST = bundle.getString("redis.host");
PORT = Integer.valueOf(bundle.getString("redis.port"));
TIMEOUT = Integer.valueOf(bundle.getString("redis.timeout"));
PASSWORD = bundle.getString("redis.password");
String database = bundle.getString("redis.database");
if(database != null && database.trim().length() > 0){
DATABASE = Integer.valueOf(database);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
config.setTestOnReturn(TEST_ON_RETURN);
if(DATABASE != -1 && (PASSWORD != null && PASSWORD.trim().length() > 0)){ //设置密码并指定数据块
jedisPool = new JedisPool(config, HOST, PORT, TIMEOUT, PASSWORD, DATABASE);
}else if(PASSWORD != null && PASSWORD.trim().length() > 0){ //仅设置密码
jedisPool = new JedisPool(config, HOST, PORT, TIMEOUT, PASSWORD);
}else{//无密码
jedisPool = new JedisPool(config, HOST, PORT, TIMEOUT);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Jedis实例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void closeResource(Jedis jedis, boolean isOK) {
if (null != jedis) {
if(!isOK){
jedisPool.returnBrokenResource(jedis);
}else{
jedisPool.returnResource(jedis);
}
}
}
/**
* 销毁jedis对象
* @param jedis
*/
public static void returnBrokenResource(final Jedis jedis){
if (jedis != null) {
jedisPool.returnBrokenResource(jedis);
}
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
package com.osgc.sqlapi;
import com.osgc.oc.logger.Logger;
import com.osgc.oc.logger.LoggerFactory;
import com.osgc.oc.redis.RedisDBUtil;
import redis.clients.jedis.Jedis;
public class RedisSqlUtils {
private static Logger logger = LoggerFactory.getLogger(RedisSqlUtils.class);
/*
* 封装redis API
*/
/**
* 设置值
* @param key
* @param value
* @return
*/
public static String set(String key,String value){
Jedis jedis = null;
String result = null;
try {
jedis = RedisDBUtil.getJedis();
result = jedis.set(key, value);
} catch (Exception e) {
logger.error("set key:{} value:{} ", e , key, value);
RedisDBUtil.returnBrokenResource(jedis);
return result;
}
RedisDBUtil.returnResource(jedis);
return result;
}
/**
* 设置可以过期的值
* @param key
* @param value
* @param exTime(单位为秒)
* @return
*/
public static String setEx(String key,String value, int exTime){
Jedis jedis = null;
String result = null;
try {
jedis = RedisDBUtil.getJedis();
result = jedis.setex(key, exTime, value);
} catch (Exception e) {
logger.error("setex key:{} value:{} error", e , key, value);
RedisDBUtil.returnBrokenResource(jedis);
return result;
}
RedisDBUtil.returnResource(jedis);
return result;
}
/**
* 设置有效期
* @param key
* @param exTime
* @return
*/
public static Long expire(String key, int exTime){
Jedis jedis = null;
Long result = null;
try {
jedis = RedisDBUtil.getJedis();
result = jedis.expire(key, exTime);
} catch (Exception e) {
logger.error("expire key:{} error", e , key);
RedisDBUtil.returnBrokenResource(jedis);
return result;
}
RedisDBUtil.returnResource(jedis);
return result;
}
/**
* 获取值
* @param key
* @return
*/
public static String get(String key){
Jedis jedis = null;
String result = null;
try {
jedis = RedisDBUtil.getJedis();
result = jedis.get(key);
} catch (Exception e) {
logger.error("get key:{}", e , key);
RedisDBUtil.returnBrokenResource(jedis);
return result;
}
RedisDBUtil.returnResource(jedis);
return result;
}
/**
* 删除值
* @param key
* @return
*/
public static Long del(String key){
Jedis jedis = null;
Long result = null;
try {
jedis = RedisDBUtil.getJedis();
result = jedis.del(key);
} catch (Exception e) {
logger.error("get key:{}", e , key);
RedisDBUtil.returnBrokenResource(jedis);
return result;
}
RedisDBUtil.returnResource(jedis);
return result;
}
public static void main(String[] args) {
RedisSqlUtils.set("kkkkk", "454545454");
RedisSqlUtils.setEx("zhk", "123456", 10);
}
}