in the process of using feign, turn on the circuit breaker by configuring feign.hystrix.enabled=true
. However, the fallback will be executed directly after startup and will not determine whether the call was successful or not. The call is normal when the circuit breaker is not configured. Currently, you need to try to call it after enabling the circuit breaker, and call the fallback method of fallback only after the call fails.
< H2 > related codes < / H2 >
Feign client
@FeignClient(name = ServiceConstant.HNISTER_SECURITY,fallback = ResourceRestApiFallback.class,path = "api")
public interface ResourceRestApi {
@GetMapping(path = "resources")
List<ResourceDTO> findAll();
@GetMapping(path = "resources/status/{status}")
List<ResourceDTO> findByStatus(@PathVariable(name = "status") Integer status);
}
Fallback
@Component
public class ResourceRestApiFallback implements ResourceRestApi {
private Logger logger = LoggerFactory.getLogger(ResourceRestApiFallback.class);
@Override
public List<ResourceDTO> findAll() {
logger.error("-sharphnister-sharp feign client ResourceRestApiFallback.findAll() fail");
return Lists.newArrayList();
}
@Override
public List<ResourceDTO> findByStatus(Integer status) {
logger.error("-sharphnister-sharp feign client ResourceRestApiFallback.findByStatus() fail");
return Lists.newArrayList();
}
}