spring:
redis:
database: 0
password: China1234
timeout: 10000
sentinel:
master: mymaster
nodes: 172.28.13.140:26379,10.195.128.19:26379,10.195.128.20:26379
jedis:
pool:
max-active: 500
min-idle: 5
max-idle: 50
max-wait: 60000
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* @description:
* @author: uwank171
* @date: 2022/8/26 14:25
*/
@Slf4j
@Configuration
public class JedisConfig {
@Value("${spring.redis.database}")
private String database;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.sentinel.master}")
private String master;
@Value("${spring.redis.sentinel.nodes}")
private String nodes;
@Value("${spring.redis.jedis.pool.max-active}")
private int maxTotal;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
private static JedisSentinelPool jedisSentinelPool = null;
public Jedis getJedis() {
if (jedisSentinelPool == null) {
jedisSentinelPool();
}
if (jedisSentinelPool != null) {
return jedisSentinelPool.getResource();
}
return null;
}
public void closeJedis(Jedis jedis) {
try {
if (jedis != null ) {
jedis.close();
}
} catch (Exception e) {
System.out.println("closeJedis Exception ");
e.printStackTrace();
}
}
@Bean
public JedisSentinelPool jedisSentinelPool() {
try {
Set nodeSet = new HashSet<>();
//分割出集群节点
String[] cNodes = nodes.split(",");
Arrays.stream(cNodes).filter(StringUtils::isNotBlank).forEach(node->{
nodeSet.add(node);
});
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxIdle(maxTotal);
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setBlockWhenExhausted(true);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
jedisSentinelPool = new JedisSentinelPool(master, nodeSet, jedisPoolConfig,
timeout, password, Integer.valueOf(database));
return jedisSentinelPool;
} catch (Exception e) {
e.printStackTrace();
}
return jedisSentinelPool;
}
}
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redmaple.common.config.JedisConfig;
/**
* @description:
* @author: uwank171
* @date: 2022/8/29 10:18
*/
@Slf4j
@Component
public class JedisUtil {
@Autowired
private JedisConfig jedisConfig;
/**
* 判断是否存在key
* @param key
* @param timeoutSeconds
* @return
*/
public boolean expire(String key, int timeoutSeconds) {
Jedis jedis = jedisConfig.getJedis();
try {
jedis.expire(key, timeoutSeconds);
return true;
} catch (Exception e) {
log.error(">>>>> JedisUtils error " + e);
return false;
} finally {
jedisConfig.closeJedis(jedis);
}
}
/**
* 存储数据
* @param key
* @param value
* @return
*/
public String set(String key, String value) {
Jedis jedis = jedisConfig.getJedis();
try {
return jedis.set(key, value);
} catch (Exception e) {
log.error(">>>>> JedisUtils error " + e);
return null;
} finally {
jedisConfig.closeJedis(jedis);
}
}
/**
* 获取数据
* @param key
* @return
*/
public String get(String key) {
Jedis jedis = jedisConfig.getJedis();
try {
return jedis.get(key);
} catch (Exception e) {
log.error(">>>>> JedisUtils error " + e);
return null;
} finally {
jedisConfig.closeJedis(jedis);
}
}
/**
* 删除数据
* @param key
* @return
*/
public boolean del(String key) {
Jedis jedis = jedisConfig.getJedis();
try {
jedis.del(key);
return true;
} catch (Exception e) {
log.error(">>>>> JedisUtils error " + e);
return false;
} finally {
jedisConfig.closeJedis(jedis);
}
}
/**
* 判断是否存在key
* @param key
* @return
*/
public boolean exists(String key) {
Jedis jedis = jedisConfig.getJedis();
try {
return jedis.exists(key);
} catch (Exception e) {
log.error(">>>>> JedisUtils error " + e);
return false;
} finally {
jedisConfig.closeJedis(jedis);
}
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redmaple.common.util.JedisUtil;
/**
* @description:
* @author: uwank171
* @date: 2022/9/1 14:01
*/
@RestController
public class RedisSentinelController {
@Autowired
private JedisUtil jedisUtil;
@GetMapping("/setKey")
public String setKey(String key, String value) {
jedisUtil.set(key, value);
return "===setKey完成===key:" + key + " value:" + value;
}
@GetMapping("/getKey")
public String getKey(String key) {
String value = jedisUtil.get(key);
return "===getKey完成===key:" + key + " value:" + value;
}
}
如果该文章能够帮助到你,希望麻烦点赞收藏下,谢谢。