redisjar quote
<-! Redis Tools -> <dependency> <the groupId> org.springframework.boot </ the groupId> <the artifactId> Starter-Spring-Boot-Data-Redis </ the artifactId>
<-! Removed following is because at high concurrent calls redis, if you use jedis way to connect a large number of connection error will not connect redis redis appears -> <Exclusions> <Exclusion> <groupId> io.lettuce </ groupId> <artifactId > Lettuce-Core </ the artifactId> </ Exclusion> </ Exclusions> </ dependency> <dependency> <the groupId> redis.clients </ the groupId> <artifactId>jedis</artifactId> </dependency>
springboot profile, this is my yml configuration file, (the following is redis cluster, if it is to add a single host and port, as well as their passwords)
the Spring: Redis: # Redis password, if the cluster, all passwords must match the password: jedis: the pool: # connection pool maximum number of connections (use a negative value means no limit) max -active: 8 # connection pool maximum block waiting time (use negative values indicate no limit) max -wait: -1 max idle connection pool # connection max -idle:. 8 minimum idle connection pool # connection min -idle: 0 # link timeout (ms) timeout: 30000 # Redis cluster configuration cluster: # Redis cluster all the servers : Nodes 10.5.9.140:7000,10.5.9.140:7001,10.5.9.140:7002 maximum number # redirect command execution time across the cluster to follow max -redirects: 3
redis initiates the connection tool
// private RedisTemplate redisTemplate; // 初始化redis连接工具 @Autowired(required = false) public void setRedisTemplate(RedisTemplate redisTemplate) { RedisSerializer stringSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringSerializer); redisTemplate.setValueSerializer(stringSerializer); redisTemplate.setHashKeySerializer(stringSerializer); redisTemplate.setHashValueSerializer(stringSerializer); this.redisTemplate = redisTemplate; }
Lock code
/ **
* is determined whether the lock has been locked flase true it indicates that there is no lock
* /
public Boolean isLock (String Key) {
the try {
Object Object = redisTemplate.opsForValue () GET (Key);.
! IF (Object = null) {
return to true;
} the else {
return to false;
}
} the catch (the Throwable E) {
log.error ( "[Redis distributed lock] acquire the lock abnormality, {}", Key);
}
return to false;
}
/ **
locking *
* @param Key
* @param value just store a value, may be stored without
* @return
* /
public Boolean Lock (String Key, String value) {
// SETNX command may be provided returns true, false returns may not
try {
IF (StringUtils.isBlank (value)) {
value = ". 1";
}
// This method indicates that if this value is not updated key value exists
IF {(redisTemplate.opsForValue () setIfAbsent (key, value).)
return to true ;
}
} the catch (the Throwable E) {
log.error ( "[Redis distributed lock] lock abnormality, {}", Key);
}
return to false;
}
/ **
* unlock
* /
public Boolean unLock (String Key) {
the try {
. = redisTemplate.opsForValue Object Object () GET (Key);
IF (Object = null!) {
return redisTemplate.opsForValue () getOperations () Delete (Key);..
}
} the catch (the Throwable E) {
log.error ( "[redis Distributed Lock] unlocking abnormal, {}", Key);
}
return to false;
}
Record personal experience, if the other point of view, welcomed the guidance together to discuss learning