Springboot2.x-統合Redis(Lettuce接続プール)
19727 ワード
1.Redisのインストール
https://blog.csdn.net/W_Meng_H/article/details/100112360
2、Lettuce
Lettuceの接続はNettyに基づいており、接続インスタンス(StatefulRedisConnection)は複数のスレッド間で同時アクセス可能である.StatefulRedisConnectionはスレッドが安全であるため、1つの接続インスタンス(StatefulRedisConnection)はマルチスレッド環境での同時アクセスを満たすことができる.もちろんこれも伸縮可能な設計である.1つの接続インスタンスが不足している場合は、必要に応じて接続インスタンスを追加することもできます.
3、pom依存
4、application.propertiesプロファイル
5、Redis配置クラス
カスタム設定:
6、Redisツール類
https://blog.csdn.net/W_Meng_H/article/details/100112360
2、Lettuce
Lettuceの接続はNettyに基づいており、接続インスタンス(StatefulRedisConnection)は複数のスレッド間で同時アクセス可能である.StatefulRedisConnectionはスレッドが安全であるため、1つの接続インスタンス(StatefulRedisConnection)はマルチスレッド環境での同時アクセスを満たすことができる.もちろんこれも伸縮可能な設計である.1つの接続インスタンスが不足している場合は、必要に応じて接続インスタンスを追加することもできます.
3、pom依存
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
com.alibaba
fastjson
1.2.35
4、application.propertiesプロファイル
#IP
spring.redis.host=
#
spring.redis.port=
#
spring.redis.password=
# ( )
spring.redis.timeout=10000
# Redis 16 , , 0
spring.redis.database=0
# ( ) 8
spring.redis.lettuce.pool.max-active=8
# ( ) -1
spring.redis.lettuce.pool.max-wait=-1
# 8
spring.redis.lettuce.pool.max-idle=8
# 0
spring.redis.lettuce.pool.min-idle=0
#
#spring.redis.cluster.nodes=IP:7001
5、Redis配置クラス
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(lettuceConnectionFactory);
//
MyRedisSerializer myRedisSerializer = new MyRedisSerializer();
//key
template.setKeySerializer(myRedisSerializer);
//value
template.setValueSerializer(myRedisSerializer);
//value hashmap
template.setHashValueSerializer(myRedisSerializer);
return template;
}
static class MyRedisSerializer implements RedisSerializer
カスタム設定:
@Value("${spring.redis.cache.nodes}")
private String nodes;
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.lettuce.pool.max-idle}")
private Integer maxIdle;
@Value("${spring.redis.lettuce.pool.min-idle}")
private Integer minIdle;
@Value("${spring.redis.lettuce.pool.max-active}")
private Integer maxTotal;
@Value("${spring.redis.lettuce.pool.max-wait}")
private Long maxWaitMillis;
@Bean
LettuceConnectionFactory lettuceConnectionFactory() {
//
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxIdle(maxIdle == null ? 8 : maxIdle);
poolConfig.setMinIdle(minIdle == null ? 1 : minIdle);
poolConfig.setMaxTotal(maxTotal == null ? 8 : maxTotal);
poolConfig.setMaxWaitMillis(maxWaitMillis == null ? 5000L : maxWaitMillis);
LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder()
.poolConfig(poolConfig)
.build();
// redis
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
redisConfig.setHostName(host==null||"".equals(host)?"localhost":host.split(":")[0]);
redisConfig.setPort(Integer.valueOf(host==null||"".equals(host)?"6379":host.split(":")[1]));
if (password != null && !"".equals(password)) {
redisConfig.setPassword(password);
}
// redis
// RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration();
// redis
RedisClusterConfiguration redisConfig = new RedisClusterConfiguration();
Set nodeses = new HashSet<>();
String[] hostses = nodes.split("-");
for (String h : hostses) {
h = h.replaceAll("\\s", "").replaceAll("
", "");
if (!"".equals(h)) {
String host = h.split(":")[0];
int port = Integer.valueOf(h.split(":")[1]);
nodeses.add(new RedisNode(host, port));
}
}
redisConfig.setClusterNodes(nodeses);
//
redisConfig.setMaxRedirects(3);
redisConfig.setPassword(password);
return new LettuceConnectionFactory(redisConfig, lettucePoolingClientConfiguration);
}
6、Redisツール類
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
public void setRedisTemplate(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
//=============================common============================
/**
*
* @param key
* @param time ( )
* @return
*/
public boolean expire(String key,long time){
try {
if(time>0){
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* key
* @param key null
* @return ( ) 0
*/
public long getExpire(String key){
return redisTemplate.getExpire(key,TimeUnit.SECONDS);
}
/**
* key
* @param key
* @return true false
*/
public boolean hasKey(String key){
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
*
* @param key
*/
@SuppressWarnings("unchecked")
public void del(String ... key){
if(key!=null&&key.length>0){
if(key.length==1){
redisTemplate.delete(key[0]);
}else{
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
//============================String=============================
/**
*
* @param key
* @return
*/
public Object get(String key){
return key==null?null:redisTemplate.opsForValue().get(key);
}
/**
*
* @param key
* @param value
* @return true false
*/
public boolean set(String key,Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
*
* @param key
* @param value
* @param time ( ) time 0 time 0
* @return true false
*/
public boolean set(String key,Object value,long time){
try {
if(time>0){
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
}else{
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
*
* @param key
* @param by ( 0)
* @return
*/
public long incr(String key, long delta){
if(delta<0){
throw new RuntimeException(" 0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
*
* @param key
* @param by ( 0)
* @return
*/
public long decr(String key, long delta){
if(delta<0){
throw new RuntimeException(" 0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
//================================Map=================================
/**
* HashGet
* @param key null
* @param item null
* @return
*/
public Object hget(String key,String item){
return redisTemplate.opsForHash().get(key, item);
}
/**
* hashKey
* @param key
* @return
*/
public Map