@FeignClient(name = "microservice-provider-user", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {
@GetMapping("/users/{id}")
User findById(@PathVariable("id") Long id);
}
@Component
class UserFeignClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
return new User(id, "默认用户", "默认用户", 0, new BigDecimal(1));
}
}
获得造成fallback的原因
@FeignClient(name = "microservice-provider-user", fallbackFactory = UserFeignClientFallbackFactory.class)
public interface UserFeignClient {
@GetMapping("/users/{id}")
User findById(@PathVariable("id") Long id);
}
@Component
@Slf4j
class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
@Override
public UserFeignClient create(Throwable throwable) {
return new UserFeignClient() {
@Override
public User findById(Long id) {
log.error("进入回退逻辑", throwable);
return new User(id, "默认用户", "默认用户", 0, new BigDecimal(1));
}
};
}
}
public class FeignDisableHystrixConfiguration {
@Bean
@Scope("prototype")
public HystrixFeign.Builder feignBuilder() {
return HystrixFeign.builder();
}
}
局部禁用
public class FeignDisableHystrixConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}