網站首頁 編程語言 正文
- 依賴
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.4version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>demoname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
<version>2.3.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.integrationgroupId>
<artifactId>spring-integration-redisartifactId>
<version>5.4.5version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
- RedisLockRegistryConfig
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;
@Configuration
public class RedisLockRegistryConfig {
/**
* 默認過期時間300s
*/
@Value("${distribute.lock.expireTime:300}")
private long expireTime;
@Value("${spring.application.name:'distributeLock'}")
private String registryKey;
@Bean
public RedisLockRegistry redisLockRegistry(RedisConnectionFactory factory){
return new RedisLockRegistry(factory, registryKey, expireTime * 1000);
}
}
- 自定義注解
package com.example.demo.aop.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DistributeLock {
String name() default "";
}
- 自定義切面
package com.example.demo.aop;
import com.example.demo.aop.annotation.DistributeLock;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.redis.util.RedisLockRegistry;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
@Aspect
@Order
@Component
public class DistributeLockAop {
private static final Logger LOGGER = LoggerFactory.getLogger(DistributeLockAop.class);
private static SpelExpressionParser parser = new SpelExpressionParser();
private static DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
private RedisLockRegistry redisLockRegistry;
public DistributeLockAop(RedisLockRegistry redisLockRegistry) {
this.redisLockRegistry = redisLockRegistry;
}
@Around("@annotation(com.example.demo.aop.annotation.DistributeLock)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Class<?> clazz = joinPoint.getTarget().getClass();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = clazz.getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes());
DistributeLock distributeLock = AnnotationUtils.findAnnotation(method, DistributeLock.class);
assert distributeLock != null;
String spel = distributeLock.name();
String lockName = generateKeyBySpEL(spel, method, joinPoint.getArgs());
Lock lock = redisLockRegistry.obtain(lockName);
if (lock.tryLock()) {
LOGGER.info("DistributeLock locked Success. key:{}", lockName);
return joinPoint.proceed();
} else {
LOGGER.error("DistributeLock locked Failure. key:{}", lockName);
throw new Exception("Lock failure");
}
}
public static String generateKeyBySpEL(String spELString, Method method, Object[] args) {
String[] paramNames = discoverer.getParameterNames(method);
Expression expression = parser.parseExpression(spELString);
EvaluationContext context = new StandardEvaluationContext();
for (int i = 0; i < args.length; i++) {
assert paramNames != null;
context.setVariable(paramNames[i], args[i]);
}
return Objects.requireNonNull(expression.getValue(context)).toString();
}
}
- 測試類
package com.example.demo.base;
import com.example.demo.aop.annotation.DistributeLock;
import org.springframework.stereotype.Service;
@Service
public class SomeService {
@DistributeLock(name = "'lock:' + #something.name")
public void doSomething(Something something) {
}
}
package com.example.demo.base;
public class Something {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.example.demo;
import com.example.demo.base.Something;
import com.example.demo.base.SomeService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
SomeService someService = context.getBean("someService", SomeService.class);
Something something = new Something();
something.setName("gogogo");
someService.doSomething(something);
}
}
- 執行結果
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.4)
2021-03-25 16:27:49.638 INFO 10492 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_141 on P80320948 with PID 10492 (D:\workspace\demo\target\classes started by 80320948 in D:\workspace\demo)
2021-03-25 16:27:49.641 INFO 10492 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2021-03-25 16:27:50.006 INFO 10492 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-03-25 16:27:50.008 INFO 10492 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2021-03-25 16:27:50.028 INFO 10492 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 6 ms. Found 0 Redis repository interfaces.
2021-03-25 16:27:50.144 INFO 10492 --- [ main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2021-03-25 16:27:50.153 INFO 10492 --- [ main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2021-03-25 16:27:50.156 INFO 10492 --- [ main] faultConfiguringBeanFactoryPostProcessor : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2021-03-25 16:27:50.253 INFO 10492 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.integration.config.IntegrationManagementConfiguration' of type [org.springframework.integration.config.IntegrationManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-03-25 16:27:50.329 INFO 10492 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationChannelResolver' of type [org.springframework.integration.support.channel.BeanFactoryChannelResolver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-03-25 16:27:50.330 INFO 10492 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationDisposableAutoCreatedBeans' of type [org.springframework.integration.config.annotation.Disposables] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-03-25 16:27:50.832 INFO 10492 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2021-03-25 16:27:50.872 INFO 10492 --- [ main] o.s.i.endpoint.EventDrivenConsumer : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2021-03-25 16:27:50.872 INFO 10492 --- [ main] o.s.i.channel.PublishSubscribeChannel : Channel 'demo.errorChannel' has 1 subscriber(s).
2021-03-25 16:27:50.872 INFO 10492 --- [ main] o.s.i.endpoint.EventDrivenConsumer : started bean '_org.springframework.integration.errorLogger'
2021-03-25 16:27:50.878 INFO 10492 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.756 seconds (JVM running for 2.569)
2021-03-25 16:27:51.545 INFO 10492 --- [ main] com.example.demo.aop.DistributeLockAop : DistributeLock locked Success. key:lock:gogogo
原文鏈接:https://blog.csdn.net/weixin_38422258/article/details/115211687
相關推薦
- 2022-09-30 Docker容器Consul部署概述_docker
- 2022-09-08 pytorch?tensor計算三通道均值方式_python
- 2022-10-11 spingboot默認日志系統LogBack、Log4j和slf4j使用詳解
- 2021-12-08 c語言單詞本的新增、刪除、查詢按順序顯示功能_C 語言
- 2023-01-28 Android?之Preference控件基本使用示例詳解_Android
- 2022-10-27 python圖像填充與裁剪/resize的實現代碼_python
- 2022-06-24 淺析C++?atomic?和?memory?ordering_C 語言
- 2022-06-06 Kafka在Zookeeper中存儲的信息有哪些?
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支