From 5c8ecf4ea612784b3d03fd3a602230355cabed42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= Date: Sat, 1 Sep 2018 13:08:41 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=89=B9=E6=80=A7=E3=80=82feign=20fallback=20=E6=B3=A8?= =?UTF-8?q?=E5=85=A5=E5=BC=82=E5=B8=B8=E5=A0=86=E6=A0=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/api/feign/RemoteUserService.java | 4 +- .../RemoteUserServiceFallbackFactory.java | 38 +++++++++++++++++++ .../RemoteUserServiceFallbackImpl.java | 8 +++- .../main/resources/META-INF/spring.factories | 3 +- 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/factory/RemoteUserServiceFallbackFactory.java diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/RemoteUserService.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/RemoteUserService.java index bcf806353..6a95024bf 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/RemoteUserService.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/RemoteUserService.java @@ -20,7 +20,7 @@ package com.pig4cloud.pigx.admin.api.feign; import com.pig4cloud.pigx.admin.api.dto.UserInfo; -import com.pig4cloud.pigx.admin.api.feign.fallback.RemoteUserServiceFallbackImpl; +import com.pig4cloud.pigx.admin.api.feign.factory.RemoteUserServiceFallbackFactory; import com.pig4cloud.pigx.common.core.constant.ServiceNameConstant; import com.pig4cloud.pigx.common.core.util.R; import org.springframework.cloud.openfeign.FeignClient; @@ -32,7 +32,7 @@ import org.springframework.web.bind.annotation.RequestHeader; * @author lengleng * @date 2018/6/22 */ -@FeignClient(value = ServiceNameConstant.UMPS_SERVICE, fallback = RemoteUserServiceFallbackImpl.class) +@FeignClient(value = ServiceNameConstant.UMPS_SERVICE, fallbackFactory = RemoteUserServiceFallbackFactory.class) public interface RemoteUserService { /** * 通过用户名查询用户、角色信息 diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/factory/RemoteUserServiceFallbackFactory.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/factory/RemoteUserServiceFallbackFactory.java new file mode 100644 index 000000000..93dbb02b4 --- /dev/null +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/factory/RemoteUserServiceFallbackFactory.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.admin.api.feign.factory; + +import com.pig4cloud.pigx.admin.api.feign.RemoteUserService; +import com.pig4cloud.pigx.admin.api.feign.fallback.RemoteUserServiceFallbackImpl; +import feign.hystrix.FallbackFactory; +import org.springframework.stereotype.Component; + +/** + * @author lengleng + * @date 2018/9/1 + */ +@Component +public class RemoteUserServiceFallbackFactory implements FallbackFactory { + + @Override + public RemoteUserService create(Throwable throwable) { + RemoteUserServiceFallbackImpl remoteUserServiceFallback = new RemoteUserServiceFallbackImpl(); + remoteUserServiceFallback.setCause(throwable); + return remoteUserServiceFallback; + } +} diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/fallback/RemoteUserServiceFallbackImpl.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/fallback/RemoteUserServiceFallbackImpl.java index 7f501bca0..37ebaba44 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/fallback/RemoteUserServiceFallbackImpl.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/feign/fallback/RemoteUserServiceFallbackImpl.java @@ -22,6 +22,7 @@ package com.pig4cloud.pigx.admin.api.feign.fallback; import com.pig4cloud.pigx.admin.api.dto.UserInfo; import com.pig4cloud.pigx.admin.api.feign.RemoteUserService; import com.pig4cloud.pigx.common.core.util.R; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -32,6 +33,9 @@ import org.springframework.stereotype.Component; @Slf4j @Component public class RemoteUserServiceFallbackImpl implements RemoteUserService { + @Setter + private Throwable cause; + /** * 通过用户名查询用户、角色信息 * @@ -41,7 +45,7 @@ public class RemoteUserServiceFallbackImpl implements RemoteUserService { */ @Override public R info(String username, String from) { - log.error("feign 查询用户信息失败:{}", username); + log.error("feign 查询用户信息失败:{}", username, cause); return null; } @@ -53,7 +57,7 @@ public class RemoteUserServiceFallbackImpl implements RemoteUserService { */ @Override public R social(String inStr) { - log.error("feign 查询用户信息失败:{}", inStr); + log.error("feign 查询用户信息失败:{}", inStr, cause); return null; } } diff --git a/pigx-upms/pigx-upms-api/src/main/resources/META-INF/spring.factories b/pigx-upms/pigx-upms-api/src/main/resources/META-INF/spring.factories index d11d4b57c..cc68aecba 100644 --- a/pigx-upms/pigx-upms-api/src/main/resources/META-INF/spring.factories +++ b/pigx-upms/pigx-upms-api/src/main/resources/META-INF/spring.factories @@ -1,3 +1,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.pig4cloud.pigx.admin.api.feign.fallback.RemoteUserServiceFallbackImpl,\ - com.pig4cloud.pigx.admin.api.feign.fallback.RemoteLogServiceFallbackImpl\ + com.pig4cloud.pigx.admin.api.feign.fallback.RemoteLogServiceFallbackImpl,\ + com.pig4cloud.pigx.admin.api.feign.factory.RemoteUserServiceFallbackFactory