gengby
1 year ago
9 changed files with 192 additions and 57 deletions
@ -0,0 +1,72 @@ |
|||
package org.nl.acs.ext.wms; |
|||
|
|||
import cn.hutool.http.HttpRequest; |
|||
import cn.hutool.http.HttpResponse; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.nl.acs.ext.wms.service.AcsToWmsService; |
|||
import org.nl.modules.common.exception.BadRequestException; |
|||
import org.nl.modules.common.utils.RedisUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.retry.annotation.Backoff; |
|||
import org.springframework.retry.annotation.Recover; |
|||
import org.springframework.retry.annotation.Retryable; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
/** |
|||
* @author: geng by |
|||
* @createDate: 2023/7/6 |
|||
*/ |
|||
@Component |
|||
@Slf4j |
|||
public class RetryableUtil { |
|||
private static ThreadLocal<Integer> retryTimes = new ThreadLocal<>(); |
|||
@Autowired |
|||
private RedisUtils redisUtils; |
|||
@Autowired |
|||
private AcsToWmsService acsToWmsService; |
|||
|
|||
/** |
|||
* 只针对对接系统连接拒绝、连接超时进行重试机制 |
|||
* 如果系统连接成功 但是对方返回失败不进行重试 |
|||
* |
|||
* @param url |
|||
* @param param |
|||
*/ |
|||
@Retryable(maxAttempts = 5, backoff = @Backoff(delay = 15000L, multiplier = 2)) |
|||
public void retryable(String url, String param) { |
|||
acsToWmsService.getTokenFromWms(); |
|||
HttpResponse httpResponse = null; |
|||
String respMessage = null; |
|||
try { |
|||
httpResponse = |
|||
HttpRequest |
|||
.post(url) |
|||
.header("Content-Type", "application/json;charset=UTF-8") |
|||
.header("Authorization", String.valueOf(redisUtils.get("wms_token"))) |
|||
.body(param) |
|||
.timeout(5000) |
|||
.execute(); |
|||
} catch (Exception e) { |
|||
respMessage = e.getMessage(); |
|||
} |
|||
if (retryTimes.get() == null) { |
|||
retryTimes.set(1); |
|||
} else { |
|||
retryTimes.set(retryTimes.get() + 1); |
|||
} |
|||
if (httpResponse == null) { |
|||
log.error("接口进行第{}次重试,请求路径:{},请求参数:{},响应参数:{}", retryTimes.get(), url, JSONObject.parse(param), respMessage); |
|||
throw new BadRequestException(url + "_" + param + "_" + retryTimes.get()); |
|||
} |
|||
retryTimes.remove(); |
|||
log.info("接口重试成功,请求路径:{},请求参数:{},重试次数:{}", url, JSONObject.parse(param), retryTimes.get()); |
|||
} |
|||
|
|||
@Recover |
|||
public void recover(RuntimeException e) { |
|||
retryTimes.remove(); |
|||
String[] excMessage = e.getMessage().split("_"); |
|||
log.error("请求路径:{},请求参数:{},已达到最大重试次数:{},停止重试!", excMessage[0], excMessage[1], excMessage[2]); |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<included> |
|||
<springProperty scope="context" name="logPath" source="logging.file.path" defaultValue="logs"/> |
|||
<property name="LOG_HOME" value="${logPath}"/> |
|||
<!-- 按照每天生成日志文件 --> |
|||
<appender name="FILE12" class="ch.qos.logback.core.rolling.RollingFileAppender"> |
|||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> |
|||
<!--日志文件输出的文件名--> |
|||
<FileNamePattern>${LOG_HOME}/接口重试/%d{yyyy-MM-dd}.%i.log</FileNamePattern> |
|||
<!--日志文件保留天数--> |
|||
<maxHistory>15</maxHistory> |
|||
<!--单个日志最大容量 至少10MB才能看得出来--> |
|||
<maxFileSize>200MB</maxFileSize> |
|||
<!--所有日志最多占多大容量--> |
|||
<totalSizeCap>2GB</totalSizeCap> |
|||
</rollingPolicy> |
|||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> |
|||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--> |
|||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern> |
|||
<charset>${log.charset}</charset> |
|||
</encoder> |
|||
|
|||
</appender> |
|||
|
|||
<!-- <logger name="org.nl.start.Init" level="info" additivity="false"> |
|||
<appender-ref ref="FILE3"/> |
|||
</logger>--> |
|||
|
|||
<!-- 打印sql --> |
|||
<logger name="org.nl.acs.ext.wms.RetryableUtil" level="error" additivity="false"> |
|||
<appender-ref ref="FILE12"/> |
|||
</logger> |
|||
</included> |
Loading…
Reference in new issue