Display → Product 연동 구간에 Circuit Breaker를 적용! |
compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix') |
@EnableCircuitBreaker |
@HystrixCommand |
git checkout tags/step-2-hystrix-fallback -b step-2-hystrix-fallback |
@RestController @RequestMapping("/products") public class ProductController { @GetMapping(path = "{productId}") public String getProductInfo(@PathVariable String productId) { // return "[product id = " + productId + " at " + System.currentTimeMillis() + "]"; throw new RuntimeException("I/O Exception"); } } |
@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) { return "[ this product is sold out ]"; } } |
|
git checkout tags/step-2-hystrix-fallback2 -b step-2-hystrix-fallback2 |
@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 ]"; } } |
http://localhost:8081/displays/11111
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 |
|
@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"); } } |
hystrix: command: default: # command key. use 'default' for global setting. execution: isolation: thread: timeoutInMilliseconds: 1000 |
|
|
hystrix: command: default: # command key. use 'default' for global setting. execution: isolation: thread: timeoutInMilliseconds: 3000 circuitBreaker: requestVolumeThreshold: 1 # Minimum number of request to calculate circuit breaker's health. default 20 errorThresholdPercentage: 50 # Error percentage to open circuit. default 50 |
@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"); } } |
|
|