...
- [display] ProductRemoteServiceImp에 Fallback Method 에 Throwable추가
Code Block @Service public class ProductRemoteServiceImpl implements ProductRemoteService { private static final String url = "http://localhost:8082/products/"; private final RestTemplate restTemplate; public ProductRemoteServiceImpl(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @Override @HystrixCommand(fallbackMethod = "getProductInfoFallback") public String getProductInfo(String productId) { return this.restTemplate.getForObject(url + productId, String.class); } public String getProductInfoFallback(String productId, Throwable t) { System.out.println("t = " + t); return "[ this product is sold out ]"; } }
- Display 호출 확인
http://localhost:8081/displays/11111
Code Block 2020-02-25 11:32:22.068 INFO 69092 --- [io-8081-exec-10] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2020-02-25 11:32:22.068 INFO 69092 --- [io-8081-exec-10] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2020-02-25 11:32:22.174 INFO 69092 --- [io-8081-exec-10] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 106 ms t = org.springframework.web.client.HttpServerErrorException: 500 null
- 정리
Info - Hystrix에서 Fallback의 실행 여부는 Exception이 발생 했는가 여부
- Fallback의 정의 여부는 Circuit Breaker Open과 무관.
- Throwable 파래매터의 추가로. Fallback 원인을 알 수 있다.
03. Hystrix로 Timeout 처리하기
...
- [product] ProductController의 throw Exception을 Thread.sleep(2000)로 수정
Code Block @RestController @RequestMapping("/products") public class ProductController { @GetMapping(path = "{productId}") public String getProductInfo(@PathVariable String productId) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "[product id = " + productId + " at " + System.currentTimeMillis() + "]"; //throw new RuntimeException("I/O Exception"); } }
- [display] application.yml 수정하여 Hystrix Timeout 시간 조정하기
Code Block hystrix: command: default: # command key. use 'default' for global setting. execution: isolation: thread: timeoutInMilliseconds: 30001000
- Display → Product 호출 하기
...