在上一篇ping通的基础上,再来通过jedis连接池连接redis
在resources下编写redis.properties配置文件,切记是redis.properties而不是application.properties
redis.properties
# 必配 # Redis服务器地址(域名或IP) redis.host=192.168.40.100 # Redis服务器连接端口(Redis默认端口号是6379) redis.port=6379 # Redis服务器连接密码(默认为空) redis.password= # 选配 # 最大连接数 redis.maxTotal=1000 # 最大空闲连接数 redis.maxIdle=30 # 最大的阻塞时长 redis.maxWait=60000 # 向资源池借用连接时是否做连接有效性检测(ping)。检测到的无效连接将会被移除 redis.testOnBorrow=true
创建JedisPoolUtil工具类,这些都是固定死的,可以直接cv
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.io.IOException; import java.io.InputStream; import java.time.Duration; import java.util.Properties; /** 单例模式优化Jedis连接池 */ public class JedisPoolUtil { // Redis服务器地址(域名或IP) private static String host; // Redis服务器连接端口(Redis默认端口号是6379) private static String port; // Redis服务器连接密码(默认为空) private static String password; // 最大连接数 private static String maxTotal; // 最大空闲连接数 private static String maxIdle; // 最大的阻塞时长 private static String maxWait; // 向资源池借用连接时是否做连接有效性检测(ping)。检测到的无效连接将会被移除 private static String testOnBorrow; private volatile static JedisPool jedisPool = null; private volatile static Jedis jedis = null; static { // 读取配置文件。加载redis.properties配置文件,通过反射的方式得到文件输入流 InputStream inputStream = JedisPoolUtil.class.getClassLoader().getResourceAsStream("redis.properties"); // 创建读取配置文件的properties对象,Properties继承了Hashtable类,Hashtable类实现了Map接口 Properties properties = new Properties(); try { /* * 1.方法作用:从字节输入流中读取键值对。该方法常用于读取配置文件。 * 2.参数含义:参数中使用了字节输入流,通过流对象可以关联到某文件上,这样就能够加载文本中的数据了。文本中的数据, * 必须是键值对形式,可以使用空格、等号、冒号等符号分隔。 */ properties.load(inputStream); // 获取Redis数据库连接信息 host = properties.getProperty("redis.host"); port = properties.getProperty("redis.port"); password = properties.getProperty("redis.password"); maxTotal = properties.getProperty("redis.maxTotal"); maxIdle = properties.getProperty("redis.maxIdle"); maxWait = properties.getProperty("redis.maxWait"); testOnBorrow= properties.getProperty("redis.testOnBorrow"); } catch (IOException e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } //私有化处理 private JedisPoolUtil() { } // 返回Jedis连接池对象的静态方法 private static JedisPool getInstance() { // 单例模式实现:双检锁/双重校验锁。这种方式采用双锁机制,安全且在多线程情况下能保持高性能 if(jedisPool == null) { synchronized (JedisPoolUtil.class) { if(jedisPool == null) { // 创建一个配置对象 JedisPoolConfig config = new JedisPoolConfig(); //可选项 config.setMaxTotal(Integer.parseInt(maxTotal)); // 资源池中的最大连接数 config.setMaxIdle(Integer.parseInt(maxIdle)); // 资源池允许的最大空闲连接数 // 当资源池连接用尽后,调用者的最大等待时间(单位为毫秒) config.setMaxWait(Duration.ofMillis(Integer.parseInt(maxWait))); // 向资源池借用连接时是否做连接有效性检测(业务量很大时候建议设置为false,减少一次ping的开销) config.setTestOnBorrow(Boolean.valueOf(testOnBorrow)); // 初始化JedisPool 必选项 jedisPool = new JedisPool(config, host, Integer.parseInt(port)); } } } return jedisPool; } /** 获取连接方法 */ public static Jedis getJedis() { if (jedis == null) { // 获取连接 jedis = getInstance().getResource(); } return jedis; } }
编写测试类进行测试: 通过连接池来连接redis
import com.leq.product.util.JedisPoolUtil; import redis.clients.jedis.Jedis; public class RedisTests { public static void main(String[] args) { Jedis jedis = JedisPoolUtil.getJedis(); System.out.println(jedis.ping()); } }