9. redis的java客戶端
9.1 Jedis
9.1.1 maven配置
使用jedis需要引入jedis的jar包,下面提供了maven依賴
jedis.jar是封裝的包,commons-pool2.jar是管理連接的包
<code>
<dependency>
<groupid>redis.clients/<groupid>
<artifactid>jedis/<artifactid>
<version>2.9.3/<version>
/<dependency>
<dependency>
<groupid>org.apache.commons/<groupid>
<artifactid>commons-pool2/<artifactid>
<version>2.5.0/<version>
/<dependency>/<code>
9.1.2 配置文件
<code>abc.data.redis.connection.appRedis1.database = 0
abc.data.redis.connection.appRedis1.timeout = 20000
# master的ip地址
abc.data.redis.connection.appRedis1.host = 1.1.1.1
abc.data.redis.connection.appRedis1.port = 6410
abc.data.redis.connection.appRedis1.password = 8bH4Ft82P2JjwFiV
# 哨兵地址
abc.data.redis.connection.appRedis1.nodes = 1.1.1.1,1.1.1.2,1.1.1.3
abc.data.redis.connection.appRedis1.master = sentinel-1.1.1.1-6410
abc.data.redis.connection.appRedis1.minIdle = 10
abc.data.redis.connection.appRedis1.maxActive = 500
abc.data.redis.connection.appRedis1.maxWait = 20000
abc.data.redis.connection.appRedis1.maxIdle = 200/<code>
9.1.3 建立配置文件對應的類
<code>@Component
@ConfigurationProperties(prefix = "abc.data.redis.connection.appRedis1")
@Data
public class RedisConfig {
/** 節點名稱 */
private String nodes;
/** master名稱 */
private String master;
/** 密碼 */
private String password;
/** 超時時長 */
private Integer timeout;
/** 最小空閒數量 */
private Integer minIdle;
/** 連接池的最大數據庫連接數 */
private Integer maxActive;
/** 最大建立連接等待時間 */
private Integer maxWait;
/** 最大空閒數量 */
private Integer maxIdle;
}/<code>
9.1.4 編寫連接池工具類
<code>package com.test.jedis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisUtils {
@Resource
private RedisConfig redisConfig;
private JedisSentinelPool jedisSentinelPool;
// 1、定義一個連接池對象
private final static JedisPool POOL;
static {
try{
// 初始化
// 1、設置連接池的配置對象
JedisPoolConfig config = new JedisPoolConfig();
JedisPoolConfig config = new JedisPoolConfig();
//設置最大空閒數量
config.setMaxIdle(redisConfig.getMaxIdle());
//設置最小空閒數量
config.setMinIdle(redisConfig.getMinIdle());
//設置最長等待時間
config.setMaxWaitMillis(redisConfig.getMaxWait());
//設置連接池的最大數據庫連接數
config.setMaxTotal(redisConfig.getMaxActive());
//是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個
config.setTestOnBorrow(true);
//是否進行有效性檢查
config.setTestOnReturn(true);
//在空閒時檢查有效性, 默認false
config.setTestWhileIdle(true);
// 2、設置連接池對象(連接的是哨兵redis)
String[] split = redisConfig.getNodes().split(",");
Set<string> nodeSet = Sets.newHashSet(split);
jedisSentinelPool = new JedisSentinelPool(redisConfig.getMaster(), nodeSet, jedisPoolConfig, redisConfig.getTimeout(), redisConfig.getPassword());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 從連接池中獲取連接
*/
public synchronized static Jedis getJedis() {
try {
if (jedisSentinelPool != null) return jedisSentinelPool.getResource();
} catch (Exception e) {
e.printStackTrace();
}
return null;
return POOL.getResource();
}
}/<string>/<code>
9.1.4 在開發過程中遇到的坑
java.net.SocketException: Broken pipe
原因: 在開發過程中, 混用了pipeline和jedis,
推介博客解決地址:
https://blog.csdn.net/wabiaozia/article/details/64921520?fps=1&locationNum=6
9.2 redisTemplate
9.2.1 maven 配置
<code>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-data-redis/<artifactid>
/<dependency>/<code>
9.2.2 application.properties配置Redis
<code>spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123/<code>
9.2.3 使用示例
<code>public class StudentServiceImpl implements StudentService {
@Autowired
private RedisTemplate<string> redisTemplate;
public List<student> getAllStudent() {
//查詢緩存
List<student> studentList= (List<student>)redisTemplate.opsForValue().get("allStudents");
if(null == studentList) {
//緩存為空,查詢一遍數據庫
studentList = studentMapper.selectAllStudent();
//把數據庫查詢出來數據,放入Redis中
redisTemplate.opsForValue().set("allStudents",studentList);
}
return studentList;
}
}/<student>/<student>/<student>/<string>/<code>
9.2.4 redisTemplate方法
其實如果經常寫程序, 簡單看一下方法名字, 大概就知道做什麼的啦!
簡介:生活中的段子手,目前就職於一家地產公司做 Devops 相關工作,曾在大型互聯網公司做高級運維工程師,熟悉 Linux 運維,Python 運維開發,Java 開發,Devops 常用開發組件等,個人公眾號:stromling,歡迎來撩我哦!
閱讀更多 故事凌 的文章