Forráskód Böngészése

:sparkles: 添加新特性。feign fallback 注入异常堆栈

冷冷 7 éve
szülő
commit
5c8ecf4ea6

+ 2 - 2
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;
 package com.pig4cloud.pigx.admin.api.feign;
 
 
 import com.pig4cloud.pigx.admin.api.dto.UserInfo;
 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.constant.ServiceNameConstant;
 import com.pig4cloud.pigx.common.core.util.R;
 import com.pig4cloud.pigx.common.core.util.R;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.cloud.openfeign.FeignClient;
@@ -32,7 +32,7 @@ import org.springframework.web.bind.annotation.RequestHeader;
  * @author lengleng
  * @author lengleng
  * @date 2018/6/22
  * @date 2018/6/22
  */
  */
-@FeignClient(value = ServiceNameConstant.UMPS_SERVICE, fallback = RemoteUserServiceFallbackImpl.class)
+@FeignClient(value = ServiceNameConstant.UMPS_SERVICE, fallbackFactory = RemoteUserServiceFallbackFactory.class)
 public interface RemoteUserService {
 public interface RemoteUserService {
 	/**
 	/**
 	 * 通过用户名查询用户、角色信息
 	 * 通过用户名查询用户、角色信息

+ 38 - 0
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<RemoteUserService> {
+
+	@Override
+	public RemoteUserService create(Throwable throwable) {
+		RemoteUserServiceFallbackImpl remoteUserServiceFallback = new RemoteUserServiceFallbackImpl();
+		remoteUserServiceFallback.setCause(throwable);
+		return remoteUserServiceFallback;
+	}
+}

+ 6 - 2
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.dto.UserInfo;
 import com.pig4cloud.pigx.admin.api.feign.RemoteUserService;
 import com.pig4cloud.pigx.admin.api.feign.RemoteUserService;
 import com.pig4cloud.pigx.common.core.util.R;
 import com.pig4cloud.pigx.common.core.util.R;
+import lombok.Setter;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 
 
@@ -32,6 +33,9 @@ import org.springframework.stereotype.Component;
 @Slf4j
 @Slf4j
 @Component
 @Component
 public class RemoteUserServiceFallbackImpl implements RemoteUserService {
 public class RemoteUserServiceFallbackImpl implements RemoteUserService {
+	@Setter
+	private Throwable cause;
+
 	/**
 	/**
 	 * 通过用户名查询用户、角色信息
 	 * 通过用户名查询用户、角色信息
 	 *
 	 *
@@ -41,7 +45,7 @@ public class RemoteUserServiceFallbackImpl implements RemoteUserService {
 	 */
 	 */
 	@Override
 	@Override
 	public R<UserInfo> info(String username, String from) {
 	public R<UserInfo> info(String username, String from) {
-		log.error("feign 查询用户信息失败:{}", username);
+		log.error("feign 查询用户信息失败:{}", username, cause);
 		return null;
 		return null;
 	}
 	}
 
 
@@ -53,7 +57,7 @@ public class RemoteUserServiceFallbackImpl implements RemoteUserService {
 	 */
 	 */
 	@Override
 	@Override
 	public R<UserInfo> social(String inStr) {
 	public R<UserInfo> social(String inStr) {
-		log.error("feign 查询用户信息失败:{}", inStr);
+		log.error("feign 查询用户信息失败:{}", inStr, cause);
 		return null;
 		return null;
 	}
 	}
 }
 }

+ 2 - 1
pigx-upms/pigx-upms-api/src/main/resources/META-INF/spring.factories

@@ -1,3 +1,4 @@
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
   com.pig4cloud.pigx.admin.api.feign.fallback.RemoteUserServiceFallbackImpl,\
   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