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

Reactive Spring (JUG CH)

Mark Paluch
November 08, 2017

Reactive Spring (JUG CH)

These slides explain reactive programming concepts, using Project Reactor and Spring to build asynchronous, non-blocking applications.

Mark Paluch

November 08, 2017
Tweet

More Decks by Mark Paluch

Other Decks in Programming

Transcript

  1. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Spring Mark Paluch, Pivotal Software, Inc. @mp911de
  2. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is Reactive Programming?
  3. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Makes assumptions over resource usage ! Developers are in charge of resource usage efficiency 3 Imperative programming
  4. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Container calls code on a Thread ! Thread is occupied ! Thread is released at the end of work 4 Imperative: Web applications
  5. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use Case: Synchronous fetch model 5 Service App Data store App Service
  6. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Reactive is used to describe event-driven systems ! Reactive is used more for scalability and stability than for speed ! Reacts to resource availability 6 What is reactive?
  7. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use Case: Serve slow clients 7 Server
  8. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use Case: Serve slow clients 8 Server
  9. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Use Case: Push message to client 9 Server Message Broker
  10. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Live (continuous) database queries ! UI event handling (Android) ! Big Data ! Real time analytics ! HTTP/2 (flow control) 10 Other use cases
  11. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Collaborative initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. ! Co-designed by Twitter, Lightbend, Pivotal, Netflix and many others ! De-facto interop standard ! Java 9: Flow API 11 Reactive Streams
  12. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Publisher and Subscriber 12 Subscriber Publisher Subscribe Data
  13. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Publisher and Subscriber 13 Subscriber Publisher Publish as fast as possible
  14. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 14 Publisher<T> I/O thread push
  15. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Back pressure control 15 Subscriber Publisher Demand
  16. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Back pressure control 16 Subscriber Publisher Demand
  17. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring WebFlux
  18. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Spring’s reactive web framework ! End to end non-blocking and asynchronous execution 18 What is WebFlux?
  19. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Publisher<DataBuffer> to Publisher<T> ! JSON (Jackson) and XML (JAXB/Aalto) ! Server-Sent Events ! Zero-Copy transfer of resources 19 Reactive Object Mapping
  20. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive WebControllers 20 @GetMapping("/") Flux<Person> people() { return personRepository.findAll(); } @GetMapping("/people/{name}") Mono<Person> person(@PathVariable String name) { return personRepository.findByName(name); } @PostMapping("/people") Mono<Person> addUser(@RequestBody Mono<Person> person) { return personRepository.save(person); }
  21. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Non-blocking 21 @GetMapping("/people/{name}") Person person(@PathVariable String name) { return new Person(name); } @GetMapping("/people/{name}") Mono<Person> person(@PathVariable String name) { return personRepository.findByName(name); }
  22. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Web Client 22 WebClient client = WebClient.create(); client.get() // .uri("/api/people?count={count}", 10) .retrieve() .bodyToFlux(String.class) .doOnNext(item -> …) .blockLast();
  23. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Router Functions 23 RouterFunction<?> routes = route(GET("/"), request -> ServerResponse.ok().build()) .and(route(POST("/people/{name}") .and(accept(MediaType.APPLICATION_JSON)), request -> ServerResponse.accepted().build())) .and(resources("/static", new ClassPathResource(…))); HttpHandler handler = RouterFunctions.toHttpHandler(routes); HttpServer server = HttpServer.create(8080); server.startAndAwait(new ReactorHttpHandlerAdapter(handler));
  24. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 24
  25. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data
  26. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Accessing Data today 26 Request Data access Remote data service I/O Wait This one is waiting
  27. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Accessing Data today 27 Request Data access Remote data service Multiple calls Bulk fetch
  28. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Keep resources busy ! Connection contention ! Usually synchronous/blocking ! Multiple requests ! Asynchronous isn’t always a good answer 28 Todays’ data access
  29. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Project Reactor 3.0 ! Spring Framework 5.0 ! Spring Data 2.0 ! A reactive (asynchronous, ideally non-blocking) driver ! Optional: Spring Boot 2.0 29 Requirements for Reactive Spring Data
  30. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Non-blocking I/O ! Threading-infrastructure ! Back pressure ! Integration with reactive libraries 30 Why reactive drivers?
  31. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! MongoDB ! Apache Cassandra ! Redis ! Couchbase 31 Reactive Spring Data modules
  32. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! JDBC is a blocking API ! JPA is a blocking API ! Sorry, no reactive JPA support 32 What about JPA
  33. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ! Reactive Template API ! Reactive Repository support ! Reduced feature set 33 Reactive Spring Data
  34. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 34 Imperative Template API <T> T insert(T objectToSave) void insertAll(Collection<…> objects)
  35. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 35 Reactive Template API <T> Mono<T> insert(T objectToSave) <T> Mono<T> insert(Mono<T> objectToSave)
  36. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 36 Reactive Repository API public interface ReactivePersonRepository extends ReactiveCrudRepository<Person, String> {
 
 Flux<Person> findByLastname(String lastname);
 
 @Query("{ 'firstname': ?0 }")
 Mono<Person> customQuery(String firstname); Flux<Person> findByLastname(Mono<String> lastname); }
  37. public interface ReactivePersonRepository extends ReactiveCrudRepository<Person, String> {
 
 Flux<Person> findByLastname(String

    lastname);
 
 @Query("{ 'firstname': ?0 }")
 Mono<Person> customQuery(String firstname); Flux<Person> findByLastname(Mono<String> lastname); } Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 37 Reactive Repository API
  38. public interface ReactivePersonRepository extends ReactiveCrudRepository<Person, String> {
 
 Flux<Person> findByLastname(String

    lastname);
 
 @Query("{ 'firstname': ?0 }")
 Mono<Person> customQuery(String firstname); Flux<Person> findByLastname(Mono<String> lastname); } Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 38 Reactive Repository API
  39. public interface ReactivePersonRepository extends ReactiveCrudRepository<Person, String> {
 
 Flux<Person> findByLastname(String

    lastname);
 
 @Query("{ 'firstname': ?0 }")
 Mono<Person> customQuery(String firstname); Flux<Person> findByLastname(Mono<String> lastname); } Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 39 Reactive Repository API
  40. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Questions? Mark Paluch • @mp911de https://github.com/mp911de/reactive-spring https://mp911.de/reactive-spring
  41. Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Resources ! Code - Repository @ Github ! Reactor Rx Lite API – Repository @ Github ! Spring Data Examples – Repository @ Github ! Spring projects release calendar – Google Calendar 41 @mp911de