Why does spring cloud feign execute the method in fallback directly after starting the circuit breaker?

< H2 > problem description < / H2 >
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();
    }
}

encountered the same problem, have you solved it?


I started out because I didn't use the Feign call and used restTemplate, so I added @ EnableHystrix, to the main program and integrated Feign. After removing the @ EnableHystrix from the main program, it was normal. I don't know if you have this problem.


the main reason should be that the default timeout of Hystrix is too short, which leads to direct access to fallback.

repair configuration in yml

The default timeout for < H1 > Hystrix is 1 second. If you do not respond after this time, you will enter the fallback code. The first request tends to be slow (because the Ribbon is lazily loaded, the relevant class will not be initialized until the first request), and the response time may be longer than 1 second. < / H1 > < H1 > method 1 Ribbon configuration Hunger loading (best recommended) self-organizing the format in yml < / H1 >

ribbon:
eager-load:

enabled: true -sharp RibbonRibbonRibbon
clients: microservice-provider-xxx -sharp ,  hystrixribbon

on the calling side, eureka.client.fetch-registy: true, is fine

Menu