♻️ Refactoring code. 重构retry 相关的逻辑

This commit is contained in:
lbw 2023-03-09 11:49:21 +08:00
parent 5d183c2297
commit 677f8e072a
5 changed files with 56 additions and 5 deletions

View File

@ -67,6 +67,7 @@
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -20,11 +20,11 @@ import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pig.common.feign.retry.FeignRetryAspect;
import com.pig4cloud.pig.common.feign.sentinel.ext.PigSentinelFeign;
import com.pig4cloud.pig.common.feign.sentinel.handle.PigUrlBlockHandler;
import com.pig4cloud.pig.common.feign.sentinel.parser.PigHeaderRequestOriginParser;
import feign.Feign;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -32,6 +32,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import java.util.concurrent.TimeUnit;
/**
* sentinel 配置
*
@ -62,9 +64,18 @@ public class PigFeignAutoConfiguration {
return new PigHeaderRequestOriginParser();
}
/**
* OkHttp 客户端配置
* @return OkHttp 客户端配
*/
@Bean
public FeignRetryAspect feignRetryAspect() {
return new FeignRetryAspect();
public OkHttpClient okHttpClient() {
return new OkHttpClient.Builder().retryOnConnectionFailure(false) // 是否开启缓存
.connectTimeout(30L, TimeUnit.SECONDS) // 连接超时时间
.readTimeout(30L, TimeUnit.SECONDS) // 读取超时时间
.writeTimeout(30L, TimeUnit.SECONDS)
.followRedirects(true) // 是否允许重定向
.build();
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.pig4cloud.pig.common.feign;
import com.pig4cloud.pig.common.feign.retry.FeignRetryAspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.support.RetryTemplate;
/**
* 重试配置
*
* @author lengleng
* @date 2023年03月09日
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RetryTemplate.class)
public class PigFeignRetryAutoConfiguration {
@Bean
public FeignRetryAspect feignRetryAspect() {
return new FeignRetryAspect();
}
}

View File

@ -13,7 +13,6 @@ import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.HashMap;
@ -28,7 +27,6 @@ import java.util.Map;
*/
@Slf4j
@Aspect
@Component
public class FeignRetryAspect {
@Around("@annotation(feignRetry)")

View File

@ -1,3 +1,4 @@
com.pig4cloud.pig.common.feign.PigFeignAutoConfiguration
com.pig4cloud.pig.common.feign.PigFeignRetryAutoConfiguration
com.pig4cloud.pig.common.feign.sentinel.SentinelAutoConfiguration
com.pig4cloud.pig.common.feign.sentinel.handle.GlobalBizExceptionHandler