diff --git a/workflow-engine-server/src/main/java/cn/axzo/workflow/server/common/interceptor/RequestHeaderContextInterceptor.java b/workflow-engine-server/src/main/java/cn/axzo/workflow/server/common/interceptor/RequestHeaderContextInterceptor.java index d8c65b447..3ca7631bd 100644 --- a/workflow-engine-server/src/main/java/cn/axzo/workflow/server/common/interceptor/RequestHeaderContextInterceptor.java +++ b/workflow-engine-server/src/main/java/cn/axzo/workflow/server/common/interceptor/RequestHeaderContextInterceptor.java @@ -3,6 +3,7 @@ package cn.axzo.workflow.server.common.interceptor; import cn.axzo.workflow.core.common.exception.WorkflowEngineException; import cn.axzo.workflow.core.repository.entity.ExtAxProperty; import cn.axzo.workflow.core.service.ExtAxPropertyService; +import cn.axzo.workflow.server.common.util.RedisUtils; import lombok.extern.slf4j.Slf4j; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.springframework.beans.factory.annotation.Autowired; @@ -12,8 +13,10 @@ import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.time.Duration; import java.util.Enumeration; import java.util.Objects; +import java.util.Optional; import static cn.axzo.workflow.client.config.WorkflowRequestInterceptor.HEADER_API_VERSION; import static cn.axzo.workflow.client.config.WorkflowRequestInterceptor.HEADER_HTTP_CLIENT; @@ -36,6 +39,8 @@ public class RequestHeaderContextInterceptor implements HandlerInterceptor { private String serviceVersion; @Autowired private ExtAxPropertyService extAxPropertyService; + private static final ThreadLocal KEY_CACHE = new ThreadLocal<>(); + private static final String REPEAT_KEY = "global:api_application:"; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { @@ -78,27 +83,47 @@ public class RequestHeaderContextInterceptor implements HandlerInterceptor { if (!StringUtils.hasText(request.getHeader(HEADER_SERVER_NAME))) { return; } - ExtAxProperty property = extAxPropertyService.getByName(request.getHeader(HEADER_SERVER_NAME)).map(entity -> { - entity.setCreated(!Objects.equals(entity.getValue(), serviceVersion)); - entity.setValue(headerClientVersion); - return entity; - }).orElseGet(() -> { - ExtAxProperty extAxProperty = new ExtAxProperty(); - extAxProperty.setCreated(true); - extAxProperty.setName(request.getHeader(HEADER_SERVER_NAME)); - extAxProperty.setValue(clientVersion.toString()); - return extAxProperty; - }); - if (property.getCreated()) { - if (Objects.isNull(property.getId())) { - extAxPropertyService.add(property); - } else { - extAxPropertyService.update(property); - } + String requestApplicationName = request.getHeader(HEADER_SERVER_NAME); + Optional extAxProperty = extAxPropertyService.getByName(requestApplicationName); + String cacheRepeatKey = REPEAT_KEY + requestApplicationName; + log.info("repeatApi key: {}", cacheRepeatKey); + + String key = RedisUtils.getCacheObject(cacheRepeatKey); + if (Objects.isNull(key)) { + RedisUtils.setCacheObject(cacheRepeatKey, "", Duration.ofSeconds(5)); + KEY_CACHE.set(cacheRepeatKey); + insert(extAxProperty, headerClientVersion, clientVersion); + } else { + update(extAxProperty, headerClientVersion, clientVersion); } } + private void update(Optional extAxProperty, String requestApplicationName, DefaultArtifactVersion clientVersion) { + if (!extAxProperty.isPresent()) { + return; + } + ExtAxProperty property = extAxProperty.get(); + if (Objects.equals(property.getValue(), clientVersion.toString())) { + return; + } + property.setValue(clientVersion.toString()); + extAxPropertyService.update(property); + } + + private void insert(Optional extAxProperty, String requestApplicationName, DefaultArtifactVersion clientVersion) { + if (extAxProperty.isPresent()) { + return; + } + extAxPropertyService.add(extAxProperty.orElseGet(() -> { + ExtAxProperty property = new ExtAxProperty(); + property.setCreated(true); + property.setName(requestApplicationName); + property.setValue(clientVersion.toString()); + return property; + })); + } + private void printHeader(HttpServletRequest request) { Enumeration headerNames = request.getHeaderNames(); log.info("parse header start, current uri: {}", request.getRequestURI());