java使用jedis连接redis需要的所有jar
地址 https://gitee.com/Kkk9527/Third-party-plug-ins/raw/master/software/jar/redis-all-jar.zip
或者用pom.xml配置jar
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
jedis配置类ConnectionManager 用于配置获取jedis
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class ConnectionManager {
private static final String redis_host = "127.0.0.1";
private static final int redis_port = 6379;
private static final int redis_timeout = 2000;
private static final String redis_password = "pwd";
private static JedisPool pool;
static {
JedisPoolConfig config = new JedisPoolConfig();
// 控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
// pool = new JedisPool(config, redis_host, 6379);
pool = new JedisPool(config, redis_host, redis_port, redis_timeout, redis_password);
}
public static void main(String[] args) throws Exception {
pool = new JedisPool(new JedisPoolConfig(), redis_host, redis_port, 3000, redis_password);
Jedis jedis = pool.getResource();
jedis.set("a", "abc");
System.out.println("redis中的值:" + jedis.get("a"));
closeConnection(jedis);
}
/**
* 获取连接
* @return
* @throws Exception
*/
public static synchronized Jedis getConnection() throws Exception {
try {
return pool.getResource();
} catch (Exception e) {
throw e;
}
}
/**
* 关闭连接
* @param connection
* @throws Exception
*/
public static synchronized void closeConnection(Jedis connection) throws Exception {
try {
if (connection != null && connection.isConnected()) {
connection.close();
}
} catch (Exception e) {
throw e;
}
}
}