Share
Embed
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
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
41 slides
-
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 -
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? -
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 -
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 -
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 -
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? -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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? -
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 -
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); } -
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); } -
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(); -
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)); -
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 -
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 -
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 -
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 -
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 -
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 -
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? -
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 -
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 -
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 -
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) -
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) -
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); } -
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 -
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 -
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 -
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 -
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