SHARE
TWEET

My client and fallback

mdelong Sep 15th, 2016 (edited) 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.springframework.cloud.netflix.feign.FeignClient;
  2. import org.springframework.http.MediaType;
  3. import org.springframework.web.bind.annotation.RequestHeader;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7.  
  8. @FeignClient(
  9.         name = "my-service",
  10.         fallback = MyClientFallback.class
  11. )
  12. public interface MyClient {
  13.     @RequestMapping(value = "/do/stuff",
  14.             method = RequestMethod.GET,
  15.             produces = {MediaType.APPLICATION_JSON_VALUE})
  16.     @ResponseBody
  17.     boolean myServiceMethod();
  18. }
  19.  
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Component;
  22. import org.springframework.web.bind.annotation.RequestHeader;
  23.  
  24. @Component
  25. public class MyClientFallback implements MyClient {
  26.  
  27.     @Autowired
  28.     AlertBroadcaster broadcaster;
  29.  
  30.     @Override
  31.     public boolean myServiceMethod() {
  32.         broadcaster.alert("Failed to contact external service.");
  33.         return false;
  34.     }
  35. }
  36.  
  37.  
  38. @EnableSwagger2
  39. @SpringBootApplication
  40. @EnableMongoRepositories
  41. @EnableDiscoveryClient
  42. @EnableZuulProxy
  43. @EnableFeignClients
  44. @EnableHystrix
  45. public class Application {
  46.  
  47.     public static void main(String[] args) {
  48.         SpringApplication.run(Application.class, args);
  49.     }
  50.  
  51.     @Bean
  52.     public AlertBroadcaster alertBroadcaster() {
  53.         return new AlertBroadcaster();
  54.     }
  55.  
  56.     @Bean
  57.     public MyClientFallback myClientFallback() {
  58.         return new MyClientFallback();
  59.     }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top