The previous article talked about demoting Hystrix when handling Feign calls, but the problem is that if we fail to call the provider service and return the base data, how do we view the logs of the failure? We could not get the error message that the consumer called the provider in the previous processing.

Feign Degrade record abnormal information

1. Create projects

Create a normal SpringCloud project

2. Basic Settings

The related dependencies are the same as in the previous project.

3. Add FallbackFactory

Add a utility class for Hystrix to return base data as follows:

@Component public class ProductServiceFallbackFactory implements FallbackFactory<ProductConsumerService> { private Logger logger = Logger.getLogger(ProductServiceFallbackFactory.class); @Override public ProductConsumerService create(Throwable throwable) { return new ProductConsumerService() { /** * * @return */ @override public List<Product> findAll() {// log exception information logger.warn("fegin fallback Exception:"+throwable); List<Product> list = new ArrayList<>(); List. add(new Product(-1, 1)); list.add(new Product(-1, 1)); return list; }}; }}Copy the code

Note: The interface implemented must be a FallbackFactory and the generic is a business interface. Re-create the method, which returns the anonymous inner class of the business interface, returns the bottom data in the inner class, and logs the exception information in the method.

4. Configure the service layer

Notice that the original fallback property is changed to the fallbackFactory property.

/** * FeignClient(name="shop-product-provider"); /** FeignClient(name="shop-product-provider"); ,fallbackFactory = ProductServiceFallbackFactory.class) public interface ProductConsumerService { @RequestMapping(value="/product/findAll",method= RequestMethod.GET) public List<Product> findAll(); }Copy the code

5. Test

Without starting the provider, start consumer access, return the base data, and then view the log information

The console can see the exception log information we recorded ~