Jedis:
- Jedis是Redis官方推荐的Java连接服务工具。
- Java语言连接redis服务还有这些SpringData、Redis 、 Lettuce
- 下载地址:https://mvnrepository.com/artifact/redis.clients/jedis
- API文档:http://xetorthio.github.io/jedis/
连接练习:
jar包导入,下载地址:https://mvnrepository.com/artifact/redis.clients/jedis
idea链接redis
public class JedisTest {
public static void main(String[] args) {
// 获取连接
Jedis jedis = new Jedis("119.29.152.66", 6379);
// 测试能不能获取到设置的参数
jedis.set("name", "itzhuzhu");
// 获取设置的name
System.out.println(jedis.get("name"));
// 在客户端设置age 在idea获取
System.out.println(jedis.get("age"));
// 关闭连接
jedis.close();
}
}
基于maven
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
Jedis简易工具类开发
测试类:
public class JedisTest {
public static void main(String[] args) {
Jedis jedis = JedisUtils.getJedis();
System.out.println(jedis.get("name"));
//3.关闭连接
jedis.close();
}
}
工具类:
public class JedisUtils {
/**
JedisPool:Jedis提供的连接池技术
poolConfig:连接池配置对象
host:redis服务地址
port:redis服务端口号
*/
private static int maxTotal;
private static int maxIdel;
private static String host;
private static int port;
private static JedisPoolConfig jpc;
private static JedisPool jp;
static {
ResourceBundle bundle = ResourceBundle.getBundle("redis");
maxTotal = Integer.parseInt(bundle.getString("redis.maxTotal"));
maxIdel = Integer.parseInt(bundle.getString("redis.maxIdel"));
host = bundle.getString("redis.host");
port = Integer.parseInt(bundle.getString("redis.port"));
//Jedis连接池配置
jpc = new JedisPoolConfig();
jpc.setMaxTotal(maxTotal);
jpc.setMaxIdle(maxIdel);
jp = new JedisPool(jpc, host, port);
}
public static Jedis getJedis() {
return jp.getResource();
}
}
配置文件:
redis.maxTotal=50
redis.maxIdel=10
redis.host=你的ip地址
redis.port=6379