Jedis 用java进行连接redis
首先 对redis进行配置
本次的redis放在服务器上面
进入redis的配置文件
**并将protected-mode修改为no
如果开启protected-mode 那么在没有设定bind ip且没有设置密码的情况下 Reids只允许接收本机
**注销掉对应的bind****
默认情况是 只接受本机的访问请求
不写的情况 则表名 无限制接收人任何ip地址访问
客户端连接redis
- 连接redis
- 操作redis
- 关闭redis连接
Jedis简易工具类开发
- 加载连接资源池静态资源
- 设置连接池参数
- 通过连接池获取对象
环境配置

redis.properties
redis.timeout=3000 #超时时间:单位ms
redis.host=IP地址 #对应的ip地址
redis.port=6379 #对应的端口号码
redis.maxTotal=50
redis.maxIdle=10
redis.pool.maxActive=200 #最大连接数:能够同时建立的“最大链接个数”
redis.pool.maxIdle=20 #最大空闲数:空闲链接数大于maxIdle时,将进行回收
redis.pool.minIdle=5 #最小空闲数:低于minIdle时,将创建新的链接
redis.pool.maxWait=3000 #最大等待时间:单位ms
redis.pool.testOnBorrow=true #使用连接时,检测连接是否成功
redis.pool.testOnReturn=true #返回连接时,检测连接是否成功
redisUtils工具类
public class JedisUtils {
//在外进行抽取 static
private static int maxTotal;
private static int maxIdle;
private static String host;
private static int port;
// poolConfig:连接池配置对象
private static JedisPoolConfig jpc;
// JedisPool:Jedis提供的连接池技术
private static JedisPool jp;
static{
ResourceBundle bundle = ResourceBundle.getBundle("redis");
maxTotal = Integer.parseInt(bundle.getString("redis.maxTotal"));
maxIdle = Integer.parseInt(bundle.getString("redis.maxIdle"));
host = bundle.getString("redis.host");
port = Integer.parseInt(bundle.getString("redis.port"));
// jedis 连接配置
jpc = new JedisPoolConfig();
jpc.setMaxTotal(maxTotal);
jpc.setMaxIdle(maxIdle);
jp = new JedisPool(jpc,host,port);
}
public static Jedis getJedis(){
// 获取Jedis的连接池对象
return jp.getResource();
}
}
本文介绍如何使用Java通过Jedis连接Redis服务器,包括Redis服务器的基本配置调整、连接池参数设置及其实现的工具类代码。文章详细说明了如何禁用保护模式、设置连接池的最大连接数等关键步骤。
602

被折叠的 条评论
为什么被折叠?



