Feign support for Hystrix

   Achieve the desired effect:

  1. call the external rest interface, not by the service name;
  2. Realize the Hystrix effect;

 

   Taking calling the cloud chip interface as an example, the complete sample code:

 

@FeignClient(name = "yunpian", url = "https://sms.yunpian.com/v2",
        fallback = YunpianClient.YunpianFallback.class,
        fallbackFactory = YunpianClient.YunpianFallbackFactory.class)
public interface YunpianClient {

    @RequestMapping(value = "/sms/single_send.json", method = RequestMethod.POST,
            headers = {"Accept=application/json;charset=utf-8;", "Content-Type=application/x-www-form-urlencoded;charset=utf-8;"})
    Object singleSend(@RequestParam("apikey") String apikey,
                             @RequestParam("text") String text,
                             @RequestParam("mobile") String mobile);

    @Component
    class YunpianFallback extends BaseClient implements YunpianClient{

        @Override
        public Object singleSend(String apikey,
                                 String text,
                                 String mobile) {
            logger.error("Call exception, enter fallback..., parameters: {}, {}, {}", apikey, text, mobile);
            return "YunpianFallback ....";
        }
    }

    @Component
    class YunpianFallbackFactory extends BaseClient implements FallbackFactory<YunpianClient> {

        @Override
        public YunpianClient create(Throwable throwable) {
            return new YunpianClient() {
                @Override
                public Object singleSend(@RequestParam("apikey") String apikey, @RequestParam("text") String text, @RequestParam("mobile") String mobile) {
                    logger.error("Call exception, FallbackFactory..., parameters: {}, {}, {}", apikey, text, mobile);
                    return "FallbackFactory ....";
                }
            };
        }
    }
}

 

Using @FeignClient can easily call the external rest interface and realize the Hystrix effect.

 

But in the above code, only fallback takes effect, and fallbackFactory will not be executed. To see the effect, fallback and fallbackFactory only need to configure one.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563095&siteId=291194637