1.添加Redis依賴包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
2.RedisUtil實現代碼
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* description * created by A on 2021/3/26 */public class RedisUtil1 {
public static JedisPool jedisPool=null;
public static Jedis getJedis(){
if(jedisPool==null){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100); //最大可用連接數
jedisPoolConfig.setBlockWhenExhausted(true); //連接耗盡后是否等待
jedisPoolConfig.setMaxWaitMillis(2000); //等待時間
jedisPoolConfig.setMaxIdle(5); //最大閑置連接數
jedisPoolConfig.setMinIdle(5); //最小閑置連接數
jedisPoolConfig.setTestOnBorrow(true); //連接的時候測試一下ping pong
jedisPool=new JedisPool(jedisPoolConfig,"hadoop102",6379,1000) ;
System.out.println("開辟連接池");
return jedisPool.getResource();
}else{
return jedisPool.getResource();
}
}
}
