熔断第一篇可以正常实现熔断,但是有个缺点是无法获取异常信息,基于第一篇接着改为fallbackFactory实现
1、userService代码如下
package com.cloud.service;
import com.cloud.service.backup.UserServiceBack;
import com.cloud.service.backup.UserServiceBackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author: zhouwei
* @version: v1.0
* @description: com.cloud.service.hello
* @date:2021-01-25
*/
@FeignClient(value = "service-hi", fallbackFactory = UserServiceBackFactory.class) // 要调用的服务的名称
public interface UserService {
@GetMapping("/user/{id}") // mapping地址
String getUser(@RequestParam long id);
}
2、增加UserServiceBackFactory类
代码如下
package com.cloud.service.backup;
import com.cloud.service.UserService;
import feign.hystrix.FallbackFactory;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.stereotype.Component;
/**
* @author: zhouwei
* @version: v1.0
* @description: com.cloud.service.backup
* @date:2021-01-25
*/
@Component
public class UserServiceBackFactory implements FallbackFactory<UserService> {
private UserServiceBack userServiceBack;
public UserServiceBackFactory(UserServiceBack userServiceBack) {
this.userServiceBack = userServiceBack;
}
@Override
public UserService create(Throwable throwable) {
System.out.println("异常信息" + ExceptionUtils.getFullStackTrace(throwable));
return userServiceBack;
}
}
3、UserServiceBack不变,访问效果不变控制台打印异常
异常信息java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: service-hi
at org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:90)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89)
at feign