当前位置: 首页 > news >正文

做网站的实训报告有没有网站建设的教程

做网站的实训报告,有没有网站建设的教程,如果用局域网做网站,张家界网站建设企业一、前言 有时在项目开发中某些接口逻辑比较复杂,响应时间长,那么可能导致重复提交问题。 二、如何解决 1.先定义一个防重复提交的注解。 import java.lang.annotation.*;Inherited Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) Do…

一、前言
有时在项目开发中某些接口逻辑比较复杂,响应时间长,那么可能导致重复提交问题。

二、如何解决
1.先定义一个防重复提交的注解。

import java.lang.annotation.*;@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatSubmit {/*** 防重复操作限时标记数值(存储redis限时标记数值)*/String value() default "value" ;/*** 防重复操作过期时间(借助redis实现限时控制)*/int expireSeconds() default 10;
}

2.编写防重复操作的AOP

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.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Objects;@Slf4j
@Component
@Aspect
@Order(0)
public class NoRepeatSubmitAspect  {private static final String TOKENAuthorization = "Authorization";private static final String TOKENUSERNAME = "api-userName";private static final String PREVENT_DUPLICATION_PREFIX = "PREVENT_DUPLICATION_PREFIX:";@Autowiredprivate RedisService redisService;@Pointcut("@annotation(com.dp.aop.annotation.RepeatSubmit)")public void preventDuplication() {}@Around("preventDuplication()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();if (Objects.isNull(request)) {return joinPoint.proceed();}//获取执行方法Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();//获取防重复提交注解RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);//获取token以及方法标记,生成redisKeyString header = request.getHeader(TOKENAuthorization);String token = header == null ? "" : header;String requestHeader = request.getHeader(TOKENUSERNAME);String headerToken = requestHeader == null ? "" : requestHeader;token = token + headerToken;String url = request.getRequestURI();// 通过前缀 + url + token + 函数参数签名 来生成redis上的 keyString redisKey = PREVENT_DUPLICATION_PREFIX.concat(url).concat(token).concat(getMethodSign(method, joinPoint.getArgs()));RedisLock redisLock = null;try {try {redisLock = redisService.tryLock(redisKey, annotation.expireSeconds());} catch (Exception e) {log.error("tryLock error  ", e);throw new BizException(CommonMsgConstants.NoRepeatSubmitMsg);}return joinPoint.proceed();} catch (Throwable throwable) {log.error("throwable trace is ", throwable);throw new RuntimeException(throwable);} finally {if (Objects.nonNull(redisLock)) {redisLock.unlock();}}}/*** 生成方法标记:采用数字签名算法SHA1对方法签名字符串加签** @param method* @param args* @return*/private String getMethodSign(Method method, Object... args) {StringBuilder sb = new StringBuilder(method.toString());for (Object arg : args) {sb.append(toString(arg));}return DigestUtil.sha1Hex(sb.toString());}private String toString(Object arg) {if (Objects.isNull(arg)) {return "null";}if (arg instanceof Number) {return arg.toString();}return JSONObject.toJSONString(arg);}}

3.接下来定义redisService类

@Component
public class RedisService {public RedisLock tryLock(String lockKey, int expireTime) {String lockValue = UUID.randomUUID().toString();Boolean hasLock = (Boolean)this.redisTemplate.execute((connection) -> {Object nativeConnection = connection.getNativeConnection();String status = null;if (nativeConnection instanceof Jedis) {Jedis jedis = (Jedis)nativeConnection;status = jedis.set(lockKey, lockValue, "nx", "ex", expireTime);} else {JedisCluster jedisx = (JedisCluster)nativeConnection;status = jedisx.set(lockKey, lockValue, "nx", "ex", (long)expireTime);}return "OK".equals(status);});if (hasLock) {return new RedisService.RedisLockInner(this.redisTemplate, lockKey, lockValue);} else {throw new RuntimeException("获取锁失败,lockKey:" + lockKey);}}private class RedisLockInner implements RedisLock {private RedisTemplate redisTemplate;private String key;private String expectedValue;protected RedisLockInner(RedisTemplate redisTemplate, String key, String expectedValue) {this.redisTemplate = redisTemplate;this.key = key;this.expectedValue = expectedValue;}public Object unlock() {final List<String> keys = new ArrayList();keys.add(this.key);final List<String> values = new ArrayList();values.add(this.expectedValue);Object result = this.redisTemplate.execute(new RedisCallback<Long>() {public Long doInRedis(RedisConnection connection) throws DataAccessException {Object nativeConnection = connection.getNativeConnection();return nativeConnection instanceof JedisCluster ? (Long)((JedisCluster)nativeConnection).eval("if redis.call('get',KEYS[1])==ARGV[1]\n then\n   return redis.call('del',KEYS[1])\n else\n   return 0\n end", keys, values) : (Long)((Jedis)nativeConnection).eval("if redis.call('get',KEYS[1])==ARGV[1]\n then\n   return redis.call('del',KEYS[1])\n else\n   return 0\n end", keys, values);}});return result;}public void close() throws Exception {this.unlock();}}
}

4.最后在Controller接口加上注解就行了。

http://www.yayakq.cn/news/175277/

相关文章:

  • 优化网站软文公司名称logo设计
  • 天津制作网站公司推荐微网站开发一般费用多少钱
  • 南宁建站免费模板网站制作成本多少钱
  • 凡科网站内容怎么做效果好提供信息门户网站制作
  • 昆明做网站优化哪家好网站建设费用主要包括那几项
  • 微信红包建设网站优化志愿网站
  • 做网站的公司经营范围乡村建设的网站
  • 网站开发报价单广元市城乡规划建设监察大队网站
  • 网站建设流程图解关于网站设计的会议
  • 怎样创建微网站微信官方网页版
  • 简单小网站成都高端网站建设公司
  • 带m开头的网站怎么做辽阳好的网站建设公司
  • 怎样把网站提交到百度山西太原发现4例阳性
  • 做网站多少钱PageAdmin怎样建立个人的网站
  • 做微信头图的网站安卓apk开发
  • 自己做免费手机网站正规的网店代运营
  • 网站建设先进工作者wordpress的介绍
  • 网站首页被k怎么办视频网站高管有做传统媒体出身的吗?优酷副总裁转型成功的概率有多少?
  • 网站做百度百科营销型网站建设页面
  • 郑州外贸网站建站关键词林俊杰在线听免费
  • 番禺开发网站费用在线平面设计接单
  • 网站开发的问题上海何鹏seo
  • 重庆工程建设造价信息网站wordpress主题租房
  • 上海网站设计案例大数据下的精准营销
  • 汕头市住房和城乡建设局网站企业如何申请网址
  • 技术支持:淄博网站建设百度公司全称
  • 小城镇建设官方网站设计咨询服务合同
  • 网站制作技术方案北京市建设工程交易服务中心网站
  • 1元网站建设精品网站制作哈尔滨最新信息
  • 天河区营销型网站建设东莞网络推广外包托管