Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Resilience mit Hystrix: Fehlertoleranz statt Ho...

Resilience mit Hystrix: Fehlertoleranz statt Hochverfügbarkeit

Die Hystrix-Bibliothek aus dem Netflix-Stack isoliert einen Service von den Aussetzern und Fehlern seiner Abhängigkeiten. Verbindungen werden im Fehlerfall zeitweilig gekappt: Statt Timeouts und Exceptions können wohl definierte Defaults zurückgegeben werden.Im Talk werden wir - nach einem kurzem Intro - die Erfahrungen und Lessons Learned aus zwei verschiedenen Hystrix-Projekten teilen. Unser Projekt-Setup, die Fallen, in die wir gelaufen sind und natürlich unsere Erfolge. Außerdem erfährst Du, wieviel Spaß wir in unseren Projekten mit dem Hystrix-Dashboard und einem µService-Integration-Day hatten.

Benjamin Wilms

October 13, 2016
Tweet

More Decks by Benjamin Wilms

Other Decks in Technology

Transcript

  1. 3 Agenda Hystrix Einführung Resilience Patterns Praxis 1: Hystrix und

    Legacy Backends Demo Praxis 2: Hystrix Erfahrungen Advanced Features
  2. 4 About us Benjamin Wilms Felix Braun Senior Agile IT-Consultant

    www.codecentric.de blog.codecentric.de Senior IT-Consultant
  3. 6

  4. 11

  5. 13 Hello World public class CommandHelloWorld extends HystrixCommand<String> { private

    final String name; public CommandHelloWorld(String name) { super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")); this.name = name; } @Override protected String run() { return "Hello " + name; } }
  6. 14 Hello World public class CommandHelloWorld extends HystrixCommand<String> { [...]

    @Override protected String run() { return getLocalizedHello(name); } @Override protected String getFallback(){ return "Hello " + name; } }
  7. 16 Pattern • Timeout (Hystrix) • Bulkheads (Hystrix) • Circuit

    Breaker (Hystrix) • Steady State • Fail Fast • Handshaking • Test harness • Decoupling middleware
  8. 26 Java 8 public class LambdaHystrixCommand<T> extends HystrixCommand<T> { private

    Supplier<T> runSupplier; private Supplier<T> fallbackSupplier; public LambdaHystrixCommand(String cmdkey, Supplier<T> runSup, Supplier<T> fallbackSup) { [...] } @Override protected T run() throws Exception { return runSupplier.get();} @Override protected T getFallback() { return fallbackSupplier.get();} }
  9. 45 Hystrix & Archaius • dynamic & typed • thread

    safe • polling framework • callback mechanism • dynamic configuration
  10. 49 Archaius & etcd // Config fallback (config.properties) and Etcd

    configuration compositeConfig = new ConcurrentCompositeConfiguration(); ClasspathPropertiesConfiguration.initialize(); // File based config // CoreOS Etcd service configuration etcdConfigSource = new EtcdConfigurationSource(etcd, "/hystrix/"); etcdConfig = new DynamicWatchedConfiguration(etcdConfigSource); compositeConfig.addConfiguration(etcdConfig,"etcd dynamic override config"); ConfigurationManager.install(compositeConfig);
  11. 57 Spring Cloud Netflix & Spring Boot @SpringBootApplication @EnableCircuitBreaker @EnableHystrixDashboard

    @Import(ArchaiusConfiguration.class) public class TransportServiceAppl { public static void main(String[] args) { SpringApplication.run(TransportServiceApp.class, args); } }
  12. 60 Zusammenfassung • Chaos Monkey aktiviert • Remote Services konfiguriert

    • Hystrix Timeouts gesetzt • Hystrix Circuit Breaker open/close • Hystrix Fallbacks deaktiviert
  13. 64 Request Caching • Caching auf Request-Ebene (HystrixRequestContext per Thread)

    • Aktiviert durch implementieren von HystrixCommand.getCacheKey()
  14. 65 Cache-Example @GET @Path("/cacheTest/{name}") public void cacheTest(@PathParam("name") String name) {

    try (HystrixRequestContext context = HystrixRequestContext.initializeContext()) { for (int i = 0; i < 3; i++) { LambdaHystrixCommand<String> pingCmd = new LambdaHystrixCommand<>( „CacheCmd", „CacheCmdCache",() -> getPing(name), () -> "pong"); String result = pingCmd.execute(); System.out.println(i + ": " + result + " is from cache: " + pingCmd.isResponseFromCache()); }} }
  15. 66 Cache-Example @GET @Path("/cacheTest/{name}") public void cacheTest(@PathParam("name") String name) {

    try (HystrixRequestContext context = HystrixRequestContext.initializeContext()) { for (int i = 0; i < 3; i++) { LambdaHystrixCommand<String> pingCmd = new LambdaHystrixCommand<>( „CacheCmd", „CacheCmdCache",() -> getPing(name), () -> "pong"); String result = pingCmd.execute(); System.out.println(i + ": " + result + " is from cache: " + pingCmd.isResponseFromCache()); }} }
  16. 67 Cache-Example @GET @Path("/cacheTest/{name}") public void cacheTest(@PathParam("name") String name) {

    try (HystrixRequestContext context = HystrixRequestContext.initializeContext()) { for (int i = 0; i < 3; i++) { LambdaHystrixCommand<String> pingCmd = new LambdaHystrixCommand<>( „CacheCmd", „CacheCmdCache",() -> getPing(name), () -> "pong"); String result = pingCmd.execute(); System.out.println(i + ": " + result + " is from cache: " + pingCmd.isResponseFromCache()); }} }
  17. 70 Request Cancelling with Futures Future<String> future = command.queue(); //something

    happened //sends an interrupt to the command thread future.cancel(true)
  18. 71 Request Cancelling with Observables final Observable<String> o = command.observe();

    // returns a hot-observable final Subscription s = o.subscribe((v) -> { System.out.println("Result: " + v); }); //some conditions or other calls... // unsubscribe=cancel s.unsubscribe();
  19. 74 median (and lower): no cost Q0.90 = cost of

    3ms Q0.99 = cost of 9ms (Source Netflix)
  20. 77