m0_ sixty-four million eight hundred and sixty-seven thousand e 2022-01-27 01:40:50 阅读数:609
database: 0
dnsMonitoringInterval: 5000 #DNS Monitoring intervals ( millisecond ), Default 5000
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.JsonJacksonCodec> {}
#“transportMode”: “NIO”
Cluster pattern :
clusterServersConfig:
idleConnectionTimeout: 10000
pingTimeout: 1000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
reconnectionTimeout: 3000
failedAttempts: 3
password: null
subscriptionsPerConnection: 5
clientName: null
loadBalancer: !<org.redisson.connection.balancer.RoundRobinLoadBalancer> {}
slaveSubscriptionConnectionMinimumIdleSize: 1
slaveSubscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 32
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 32
masterConnectionPoolSize: 64
readMode: “SLAVE”
nodeAddresses:
“redis://127.0.0.1:7004”
“redis://127.0.0.1:7001”
“redis://127.0.0.1:7000”
scanInterval: 1000
threads: 0
nettyThreads: 0
codec: !<org.redisson.codec.JsonJacksonCodec> {}
#“transportMode”:“NIO”
Injection profile
Here, the configuration file is read according to the environment
RedissionConfig
package com.dzhjj.dzhjjapi.config;
《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 Full content open source sharing
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.redisson.config.Config;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
/**
@Auther: heng
@Date: 2020/12/3 13:56
@Description: redission Distributed lock
@Version 1.0.0
*/
@Order(2)
@Slf4j
@Configuration
public class RedissionConfig {
//@Autowired
// private RedisProperties redisProperties;
@Autowired
private Environment env;
/**
standalone mode
@return
//
@Bean
public RedissonClient redissonClient() {
RedissonClient redissonClient;
Config config = new Config();
String url = “redis://” + redisProperties.getHost() + “:” + redisProperties.getPort();
SingleServerConfig serverConfig = config.useSingleServer()
.setAddress(url)
.setTimeout(3000)
.setDatabase(redisProperties.getDatabase())
.setConnectionPoolSize(64)
.setConnectionMinimumIdleSize(50);
if (StringUtils.isNotBlank(redisProperties.getPassword())) {
serverConfig.setPassword(redisProperties.getPassword());
}
try {
redissonClient = Redisson.create(config);
return redissonClient;
} catch (Exception e) {
log.error(“RedissonClient init redis url:[{}], Exception:”, url, e);
return null;
}
}*/
@Bean(destroyMethod = “shutdown”)
public RedissonClient redisson() throws IOException {
Config config = Config.fromYAML(new ClassPathResource(“application-single-”+env.getActiveProfiles()[0]+".yml").getInputStream());
RedissonClient redisson = Redisson.create(config);
return redisson;
}
}
Test the distributed lock code
@Autowired
private RedissonClient redissonClient;
public Object testRedisLoak(){
Boolean result=false;
final String lockKey= “RedissonLock”;
RLock lock=redissonClient.getLock(lockKey);
try {
//TODO: The first parameter 30s= Indicates an attempt to acquire a distributed lock , And the maximum waiting time for obtaining the lock is 30s
//TODO: The second parameter 10s= Indicates that after locking ,10s The lock will be released automatically after the internal operation
Boolean cacheRes=lock.tryLock(10,10, TimeUnit.SECONDS);
return cacheRes;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//TODO: Release the lock
lock.unlock();
}
return result;
}
Start integrating annotation mode
Definition notes : RsionLock
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
/**
Description:
Annotations for distributed locks
Use in pairs in the same annotation , For example, in the sample code ,value and path They are aliases for each other .
But be careful ,@AliasFor There are some restrictions on the use of labels , But this should come to mind , For example, attribute value types that require mutual aliases , The default value is , It's all the same , Mutually alias annotations must appear in pairs , such as value Attribute added @AliasFor(“path”),
that path Property must be added @AliasFor(“value”), One other thing , Properties that are aliases to each other must have default values defined .
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface RsionLock {
/**
Support spring El expression
Locked resources ,key.
*/
//@AliasFor(“value”)
String key() default “”;
/**
Support spring El expression
Locked resources ,value.
If it is not empty, this time splicing key
*/
//@AliasFor(“key”)
String value() default “”;
/**
Holding time , Company : second
TODO:10s= Indicates that after locking ,10s The lock will be released automatically after the internal operation
*/
long lockTime() default 10;
/**
*/
// LockFailAction action() default LockFailAction.CONTINUE;
/*public enum LockFailAction{
//* give up //
GIVEUP,
//* continue //
CONTINUE;
}*/
/**
Waiting time , Company : second
TODO:10s= Indicates an attempt to acquire a distributed lock , And the maximum waiting time for obtaining the lock is 10s
@return
*/
int waitTime() default 10;
/**
Whether the automatic extension mechanism
Automatic extension is not allowed by default
@return
*/
boolean isWatchDog() default false;
/**
Extension of time Company : second
@return
*/
int watchDogTime() default 2;
}
Define the annotation facet LockAspectConfiguration
import com.dzhjj.dzhjjapi.annotations.RsionLock;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
/**
@Auther: heng
@Date: 2020/12/3 17:50
@Description: LockAspectConfiguration
@Version 1.0.0
*/
@Slf4j
@Aspect
@Configuration
public class LockAspectConfiguration {
private ExpressionParser parser = new SpelExpressionParser();
private LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
@Autowired
private RedissonClient redissonClient;
/**
*/
@Pointcut("@annotation(com.dzhjj.dzhjjapi.annotations.RsionLock)")
private void lockPoint() {
}
/**
Surrounding the notification
@param pjp pjp
@return Method returns the result
@throws Throwable throwable
*/
@Around(“lockPoint()”)
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
RsionLock lockAction = method.getAnnotation(RsionLock.class);
String logKey = getLogKey(lockAction, pjp, method);
copyright:author[m0_ sixty-four million eight hundred and sixty-seven thousand e],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201270140473677.html