0.0.2.0
修复本地重试未开启 修复响应头未传递异常
This commit is contained in:
parent
27c927c845
commit
39abb25373
@ -38,4 +38,4 @@ logging:
|
||||
|
||||
x-retry:
|
||||
server:
|
||||
host: 127.0.0.1
|
||||
host: 192.168.100.3
|
||||
|
@ -6,7 +6,9 @@ import com.x.retry.common.core.model.XRetryHeaders;
|
||||
import com.x.retry.common.core.util.JsonUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -17,17 +19,17 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 请求头和响应头传递
|
||||
*
|
||||
* @author: shuguang.zhang
|
||||
* @date : 2022-04-18 09:19
|
||||
*/
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
@Slf4j
|
||||
public class HeaderAspect {
|
||||
|
||||
@Before(value = "@within(org.springframework.web.bind.annotation.RestController)")
|
||||
public void before(JoinPoint joinPoint){
|
||||
public void before(){
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
String xRetry = attributes.getRequest().getHeader(SystemConstants.X_RETRY_HEAD_KEY);
|
||||
if (Objects.nonNull(xRetry)) {
|
||||
@ -39,8 +41,30 @@ public class HeaderAspect {
|
||||
}
|
||||
}
|
||||
|
||||
@AfterReturning(pointcut = "@within(org.springframework.web.bind.annotation.RestController)", returning = "o")
|
||||
public void afterReturning(Object o) {
|
||||
@Around(value = "@within(org.springframework.web.bind.annotation.RestController)")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
|
||||
before();
|
||||
|
||||
Throwable throwable = null;
|
||||
Object result = null;
|
||||
try {
|
||||
result = point.proceed();
|
||||
} catch (Throwable t) {
|
||||
throwable = t;
|
||||
} finally {
|
||||
afterReturning();
|
||||
}
|
||||
|
||||
if (throwable != null) {
|
||||
throw throwable;
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void afterReturning() {
|
||||
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletResponse response = attributes.getResponse();
|
||||
response.addHeader(SystemConstants.X_RETRY_STATUS_CODE_KEY, RetrySiteSnapshot.getRetryStatusCode());
|
||||
|
@ -1,12 +1,14 @@
|
||||
package com.x.retry.client.core.intercepter;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSON;
|
||||
import com.x.retry.client.core.annotation.Retryable;
|
||||
import com.x.retry.client.core.exception.XRetryClientException;
|
||||
import com.x.retry.client.core.retryer.RetryerResultContext;
|
||||
import com.x.retry.client.core.strategy.RetryStrategy;
|
||||
import com.x.retry.common.core.enums.RetryResultStatusEnum;
|
||||
import com.x.retry.common.core.log.LogUtils;
|
||||
import com.x.retry.common.core.util.JsonUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
@ -79,6 +81,13 @@ public class RetryAspect {
|
||||
// 下游响应不重试码,不开启重试
|
||||
|| RetrySiteSnapshot.isRetryForStatusCode()
|
||||
) {
|
||||
LogUtils.info("校验不通过不开启重试 methodEntrance:[{}] isRunning:[{}] throwable:[{}] isRetryFlow:[{}] isRetryForStatusCode:[{}]" ,
|
||||
!RetrySiteSnapshot.isMethodEntrance(methodEntrance),
|
||||
RetrySiteSnapshot.isRunning(),
|
||||
Objects.isNull(throwable),
|
||||
RetrySiteSnapshot.isRetryFlow(),
|
||||
RetrySiteSnapshot.isRetryForStatusCode()
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -103,6 +112,8 @@ public class RetryAspect {
|
||||
try {
|
||||
|
||||
RetryerResultContext context = retryStrategy.openRetry(retryable.scene(), executorClassName, point.getArgs());
|
||||
LogUtils.info("本地重试结果 message:[{}]", context);
|
||||
|
||||
if (RetryResultStatusEnum.SUCCESS.getStatus().equals(context.getRetryResultStatusEnum().getStatus())) {
|
||||
LogUtils.debug("aop 结果成功 traceId:[{}] result:[{}]", traceId, context.getResult());
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ public class RetrySiteSnapshot {
|
||||
}
|
||||
|
||||
public static boolean isRetryForStatusCode() {
|
||||
return getRetryStatusCode().equals(SystemConstants.X_RETRY_STATUS_CODE);
|
||||
return Objects.nonNull(getRetryStatusCode()) && getRetryStatusCode().equals(SystemConstants.X_RETRY_STATUS_CODE);
|
||||
}
|
||||
|
||||
public static Long getEntryMethodTime() {
|
||||
@ -134,8 +134,6 @@ public class RetrySiteSnapshot {
|
||||
RETRY_STATUS.remove();
|
||||
RETRY_CLASS_METHOD_ENTRANCE.remove();
|
||||
RETRY_STAGE.remove();
|
||||
RETRY_HEADER.remove();
|
||||
RETRY_STATUS_CODE.remove();
|
||||
|
||||
}
|
||||
|
||||
|
@ -71,7 +71,7 @@ public class LocalRetryStrategies extends AbstractRetryStrategies {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!RetrySiteSnapshot.isRetryForStatusCode()) {
|
||||
if (RetrySiteSnapshot.isRetryForStatusCode()) {
|
||||
resultContext.setRetryResultStatusEnum(RetryResultStatusEnum.FAILURE);
|
||||
resultContext.setMessage("执行重试检验不通过 原因: 下游标志禁止重试");
|
||||
return false;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.x.retry.server.support.dispatch.actor.exec;
|
||||
|
||||
import akka.actor.AbstractActor;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.x.retry.client.model.DispatchRetryDTO;
|
||||
import com.x.retry.client.model.DispatchRetryResultDTO;
|
||||
import com.x.retry.common.core.constant.SystemConstants;
|
||||
@ -112,6 +113,7 @@ public class ExecUnitActor extends AbstractActor {
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
XRetryHeaders xRetryHeaders = new XRetryHeaders();
|
||||
xRetryHeaders.setXRetry(Boolean.TRUE);
|
||||
xRetryHeaders.setXRetryId(IdUtil.simpleUUID());
|
||||
requestHeaders.add(SystemConstants.X_RETRY_HEAD_KEY, JsonUtil.toJsonString(xRetryHeaders));
|
||||
|
||||
HttpEntity<DispatchRetryDTO> requestEntity = new HttpEntity<>(dispatchRetryDTO, requestHeaders);
|
||||
|
Loading…
Reference in New Issue
Block a user