mirror of
https://gitee.com/52itstyle/spring-boot-seckill.git
synced 2025-12-30 10:22:26 +00:00
guava限流
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -105,6 +105,12 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
<!-- guava 限流 -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>25.1-jre</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>spring-boot-seckill</finalName><plugins>
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.itstyle.seckill.common.aop;
|
||||
|
||||
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.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
/**
|
||||
* 限流 AOP
|
||||
* 创建者 张志朋
|
||||
* 创建时间 2015年6月3日
|
||||
*/
|
||||
@Component
|
||||
@Scope
|
||||
@Aspect
|
||||
public class LimitAspect {
|
||||
////每秒只发出5个令牌,此处是单进程服务的限流
|
||||
private static RateLimiter rateLimiter = RateLimiter.create(5.0);
|
||||
|
||||
//Service层切点 限流
|
||||
@Pointcut("@annotation(com.itstyle.seckill.common.aop.ServiceLimit)")
|
||||
public void ServiceAspect() {
|
||||
|
||||
}
|
||||
|
||||
@Around("ServiceAspect()")
|
||||
public Object around(ProceedingJoinPoint joinPoint) {
|
||||
Boolean flag = rateLimiter.tryAcquire();
|
||||
Object obj = null;
|
||||
try {
|
||||
if(flag){
|
||||
obj = joinPoint.proceed();
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.itstyle.seckill.common.aop;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
/**
|
||||
* 自定义注解 限流
|
||||
* 创建者 张志朋
|
||||
* 创建时间 2015年6月3日
|
||||
*/
|
||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ServiceLimit {
|
||||
String description() default "";
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.itstyle.seckill.common.aop.ServiceLimit;
|
||||
import com.itstyle.seckill.common.aop.Servicelock;
|
||||
import com.itstyle.seckill.common.dynamicquery.DynamicQuery;
|
||||
import com.itstyle.seckill.common.entity.Result;
|
||||
@@ -56,6 +57,7 @@ public class SeckillServiceImpl implements ISeckillService {
|
||||
dynamicQuery.nativeExecuteUpdate(nativeSql, new Object[]{seckillId});
|
||||
}
|
||||
@Override
|
||||
@ServiceLimit
|
||||
@Transactional
|
||||
public Result startSeckil(long seckillId,long userId) {
|
||||
//校验库存
|
||||
|
||||
@@ -46,7 +46,11 @@ public class SeckillController {
|
||||
@Override
|
||||
public void run() {
|
||||
Result result = seckillService.startSeckil(killId, userId);
|
||||
LOGGER.info("用户:{}{}",userId,result.get("msg"));
|
||||
if(result!=null){
|
||||
LOGGER.info("用户:{}{}",userId,result.get("msg"));
|
||||
}else{
|
||||
LOGGER.info("用户:{}{}",userId,"哎呦喂,人也太多了,请稍后!");
|
||||
}
|
||||
}
|
||||
};
|
||||
executor.execute(task);
|
||||
|
||||
Reference in New Issue
Block a user