Merge remote-tracking branch 'origin/leng_dev' into leng_dev

This commit is contained in:
冷冷 committed 2019-01-31 14:11:57 +08:00
commit db0a3e0de6
7 files changed
+16 -16

No files matched your search

@@ -42,7 +42,7 @@ import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResour
*/ */
@Configuration @Configuration
@ConditionalOnClass(Feign.class) @ConditionalOnClass(Feign.class)
public class PigxFeginConfiguration { public class PigxFeignConfiguration {
@Bean @Bean
@ConditionalOnProperty("security.oauth2.client.client-id") @ConditionalOnProperty("security.oauth2.client.client-id")
@@ -61,7 +61,7 @@ public class PigxFeginConfiguration {
public Feign.Builder feignHystrixBuilder(FeignContext feignContext) { public Feign.Builder feignHystrixBuilder(FeignContext feignContext) {
return PigxHystrixFeign.builder(feignContext) return PigxHystrixFeign.builder(feignContext)
.decode404() .decode404()
.errorDecoder(new PigxFeginErrorDecoder()); .errorDecoder(new PigxFeignErrorDecoder());
} }
} }
@@ -48,30 +48,30 @@ import static java.util.concurrent.TimeUnit.SECONDS;
* *
* @author L.cm * @author L.cm
*/ */
public class PigxFeginErrorDecoder extends ErrorDecoder.Default { public class PigxFeignErrorDecoder extends ErrorDecoder.Default {
private final RetryAfterDecoder retryAfterDecoder = new RetryAfterDecoder(); private final RetryAfterDecoder retryAfterDecoder = new RetryAfterDecoder();
private static final String REGEX = "^[0-9]+$"; private static final String REGEX = "^[0-9]+$";
@Override @Override
public Exception decode(String methodKey, Response response) { public Exception decode(String methodKey, Response response) {
PigxFeginException exception = errorStatus(methodKey, response); PigxFeignException exception = errorStatus(methodKey, response);
Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers())); Date retryAfter = retryAfterDecoder.apply(firstOrNull(response.headers()));
return new RetryableException(exception.getMessage(), response.request().httpMethod(), exception, retryAfter); return new RetryableException(exception.getMessage(), response.request().httpMethod(), exception, retryAfter);
} }
private static PigxFeginException errorStatus(String methodKey, Response response) { private static PigxFeignException errorStatus(String methodKey, Response response) {
try { try {
if (response.body() != null) { if (response.body() != null) {
Reader reader = response.body().asReader(); Reader reader = response.body().asReader();
return new PigxFeginException(R.builder() return new PigxFeignException(R.builder()
.msg(IoUtil.read(reader)) .msg(IoUtil.read(reader))
.code(CommonConstants.FAIL).build()); .code(CommonConstants.FAIL).build());
} }
} catch (IOException ignored) { // NOPMD } catch (IOException ignored) { // NOPMD
} }
String message = format("status %s reading %s", response.status(), methodKey); String message = format("status %s reading %s", response.status(), methodKey);
return new PigxFeginException(message); return new PigxFeignException(message);
} }
@Nullable @Nullable
@@ -29,16 +29,16 @@ import lombok.Getter;
* *
* @author L.cm * @author L.cm
*/ */
public class PigxFeginException extends RuntimeException { public class PigxFeignException extends RuntimeException {
@Getter @Getter
private final R result; private final R result;
public PigxFeginException(R result) { public PigxFeignException(R result) {
super(result.getMsg()); super(result.getMsg());
this.result = result; this.result = result;
} }
public PigxFeginException(String message) { public PigxFeignException(String message) {
super(message); super(message);
this.result = R.builder() this.result = R.builder()
.code(CommonConstants.FAIL) .code(CommonConstants.FAIL)
@@ -36,8 +36,8 @@ import java.util.concurrent.ConcurrentMap;
*/ */
@Slf4j @Slf4j
@NoArgsConstructor @NoArgsConstructor
public final class PigxFeginFallbackFactory<T> implements FallbackFactory<T> { public final class PigxFeignFallbackFactory<T> implements FallbackFactory<T> {
public static final PigxFeginFallbackFactory INSTANCE = new PigxFeginFallbackFactory(); public static final PigxFeignFallbackFactory INSTANCE = new PigxFeignFallbackFactory();
private static final ConcurrentMap<Class<?>, Object> FALLBACK_MAP = new ConcurrentHashMap<>(); private static final ConcurrentMap<Class<?>, Object> FALLBACK_MAP = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -27,8 +27,8 @@ public class PigxFeignFallbackMethod implements MethodInterceptor {
type.getName(), method.getName(), cause.getMessage()); type.getName(), method.getName(), cause.getMessage());
if (R.class == method.getReturnType()) { if (R.class == method.getReturnType()) {
final R result = cause instanceof PigxFeginException ? final R result = cause instanceof PigxFeignException ?
((PigxFeginException) cause).getResult() : R.builder() ((PigxFeignException) cause).getResult() : R.builder()
.code(CommonConstants.FAIL) .code(CommonConstants.FAIL)
.msg(cause.getMessage()).build(); .msg(cause.getMessage()).build();
return result; return result;
@@ -137,7 +137,7 @@ public final class PigxHystrixInvocationHandler implements InvocationHandler {
Object fallback; Object fallback;
try { try {
if (fallbackFactory == null) { if (fallbackFactory == null) {
fallback = PigxFeginFallbackFactory.INSTANCE.create(target.type(), getExecutionException()); fallback = PigxFeignFallbackFactory.INSTANCE.create(target.type(), getExecutionException());
} else { } else {
fallback = fallbackFactory.create(getExecutionException()); fallback = fallbackFactory.create(getExecutionException());
} }
@@ -1,3 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.pig4cloud.pigx.common.security.feign.PigxFeginConfiguration,\ com.pig4cloud.pigx.common.security.feign.PigxFeignConfiguration,\
com.pig4cloud.pigx.common.security.service.PigxUserDetailsServiceImpl com.pig4cloud.pigx.common.security.service.PigxUserDetailsServiceImpl