0%

Feign显示fallback异常原因

Feign显示fallback异常原因

我在最一开始使用Feign的时候,是使用FallBack类去实现的FeignClient接口,就像这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@FeignClient(name = "SPRINGCLOUD2-PROVIDER",contextId = "DeptClient",
fallback = DeptClient.DeptClientFallBack.class)
public interface DeptClient {

@GetMapping(value = "/dept/get/{id}")
CommonResult<Dept> get(@PathVariable("id") long id);

@GetMapping("/timeout")
String timeout();

@Component
class DeptClientFallBack implements DeptClient{

@Override
public CommonResult<Dept> get(long id) {
return null;
}

@Override
public String timeout() {
return null;
}
}
}

但是这样的话,我怎么查看是因为什么原因造成的fallback呢,这种方法肯定是不行的

然后发现@FeignClient注解中有一个属性是fallbackFactory,看着这个是有一个Throwable参数的,拿来试一试

1
T create(Throwable cause);

说干就干

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@FeignClient(name = "SPRINGCLOUD2-PROVIDER",contextId = "DeptClient",
fallbackFactory = DeptClientFallBackFactory.class)
public interface DeptClient {

@GetMapping(value = "/dept/get/{id}")
CommonResult<Dept> get(@PathVariable("id") long id);

@GetMapping("/timeout")
String timeout();
}

@Component
public class DeptClientFallBackFactory implements FallbackFactory<DeptClient> {

@Override
public DeptClient create(Throwable throwable) {
return new DeptClient() {
@Override
public CommonResult<Dept> get(long id) {
System.out.println("fallback 原因:"+throwable.getMessage());
CommonResult<Dept> result = new CommonResult<>();
result.isOk();
return result;
}

@Override
public String timeout() {
return null;
}
};
}
}

这不就有异常信息了嘛

欢迎关注我的其它发布渠道