Hands on: Hystrix
-
Upload
christina-rasimus -
Category
Software
-
view
219 -
download
3
Transcript of Hands on: Hystrix
![Page 1: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/1.jpg)
Hands-On: HystrixBest practices & pitfalls
![Page 2: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/2.jpg)
Hystrix...What?
built, heavily tested & used in production by Net�ixJava library, implementation of resilience patternsGoals: fault tolerant/robust self-healing applications,zero downtime
![Page 3: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/3.jpg)
Which cases
![Page 4: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/4.jpg)
Distributed system
External dependencies
Limited resources
High Availability
Avoid incidents
![Page 5: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/5.jpg)
Availability99,9% (three nines) = 9 hour downtime per year5 dependencies with 99,9% availability = 99,9%^5 =99,5% = 9 days downtime per year
![Page 6: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/6.jpg)
How
Decoupling by asynchronous executionSafety �rst: bulkheads, fail fastCircuit BreakerFallback, graceful degradation
![Page 7: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/7.jpg)
WhereClose to the actionIsolate each dependency separatly2+ external calls per single client API call: wrap alldependencies additionally
![Page 8: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/8.jpg)
Example: Synchrounous Mail delivery@EnableHystrix
@SpringBootApplication
public class ShopApplication {}
[RegisterService]
@Transactional
public void register(UserDto userDto) {
User user = userRepo.save(map(userDto));
mailService.sendRegisterSuccessMail(map(user));
}
[MailService]
@HystrixCommand
public void sendRegisterSuccessMail(UserDto user) {
mailGateway.sendMail('register_success', user);
}
3rd Party Libs: Spring Boot, Spring Cloud Net�ix (autosetup- & shutdown-routines)
![Page 9: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/9.jpg)
Example: Delayed Order
@HystrixCommand(fallbackMethod = "placeOrderFallback") public OrderResult placeOrder(Order order) { long orderId = orderService.placeOrder(order); order.setOrderId(orderId); order.setStatus('RUNNING'); return OrderResult.ok(orderId); } public OrderResult placeOrderFallback(Order order) { // place order async (e.g. cron job) return OrderResult.OK_DELAYED; }
![Page 10: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/10.jpg)
Example: Plain Hystrix
public class OrderCommand extends HystrixCommand { public OrderCommand(OrderService orderService, Order order) { super(...config...); this.orderService = orderService; this.order = order; } @Override protected void run() { long orderId = orderService.placeOrder(order); return OrderResult.ok(orderId); } } new OrderCommand(orderService, order).execute();
![Page 11: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/11.jpg)
Hystrix by Annotations: Javanica
Smaller con�guration effort
Less boiler plate code
Automatic unwrapping of
HystrixBadRequestExceptions
Aspect != Aspect
Possibly skip other advices
buggy: v1.5.4 faulty unwrapping of
HystrixRuntimeExceptions
![Page 12: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/12.jpg)
Semaphores & thread-poolsSemaphore
concurrency & error control
remains in the caller thread
timeout != timeout
Thread-pool
Hystrix (& Net�ix internal) standard
real decoupling
async execution �ow
![Page 13: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/13.jpg)
![Page 14: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/14.jpg)
![Page 15: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/15.jpg)
TimeoutsHTTP connection/socket timeouts
connection pools
SQL driver
Hystrix timeouts & limits
timeout values: It depends!
![Page 16: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/16.jpg)
Protecting threadsThread interruption is a desire, not a constraintUse non-blocking API (NIO Channel, Netty, ...)Handle thread interruption appropriately
Check: Thread.currentThread().isInterrupted()Restore state after catching InterruptedException:Thread.currentThread().interrupt();
![Page 17: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/17.jpg)
Decoupling not for freeminimal overhead, butThreadLocal powered functionality missing
log context (MDC, NDC)DB transaction statussecurity contextthread bound DI scopes (request, session, thread)
![Page 18: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/18.jpg)
Solution: Custom Strategypublic class CustomStrategy extends HystrixConcurrencyStrategy { @Override public <T> Callable<T> wrapCallable(final Callable<T> callable) { final Map context = MDC.getContext(); final RequestAttributes attr = RequestContextHolder.get(); return super.wrapCallable(new Callable<T>() { @Override public T call() throws Exception { try { MDC.replaceAll(context); RequestContextHolder.set(attr); return callable.call(); } finally { RequestContextHolder.resetRequestAttributes(); MDC.clear(); } ... HystrixPlugins.getInstance() .registerConcurrencyStrategy(new CustomStrategy());
![Page 19: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/19.jpg)
Hystrix Dashboard
Standalone/selfcontained
version
(https://git.io/vPH8g)
Consumes Hystrix metrics
stream from each app
Aggregated metrics stream
with Hystrix Turbine
![Page 20: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/20.jpg)
Demo time!API Gateway (JHipster)
Angular JS / Bootstrap
Spring Boot
Hystrix
3 simple microservices
Monitoring
Hystrix dashboard
Kibana dashboard
(ELK)
![Page 21: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/21.jpg)
More Hystrix
HystrixObservableCommand: Wrapping non-blockingcode (semaphore only)Hystrix command collapsingHystrix request cache
![Page 22: Hands on: Hystrix](https://reader031.fdocuments.in/reader031/viewer/2022021416/587b3bd31a28ab9c0e8b4c2f/html5/thumbnails/22.jpg)
Vielen Dank!Gerrit Brehmer Software Architekt/SeniorDeveloper
inovex GmbH Of�ce Karlsruhe Ludwig-Erhard-Allee 6 76131 Karlsruhe
Mobil: 01733181007 Mail: [email protected]