Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Reactive Microservice And Spring5

326 views

Published on

Reactive Microservice And Spring5

Published in: Technology
  • Be the first to comment

Reactive Microservice And Spring5

  1. 1. Reactive Microservices And Spring 5 Jay Lee(jaylee@pivotal.io) Senior Platform Architect
  2. 2. Jay Lee(이창재) Senior Platform Architect
  3. 3. Reactive Streams
  4. 4. Reactive Programming in One Picture
  5. 5. Reactive Programming is paradigm oriented around data flows and propagation of change
  6. 6. Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. This encompasses efforts aimed at runtime environments (JVM and JavaScript) as well as network protocols. - http://www.reactive-streams.org/
  7. 7. Asynchronous? http://www.ibm.com/developerworks/library/l-async/figure4.gif
  8. 8. Asynchronous? http://www.ibm.com/developerworks/library/l-async/figure5.gif
  9. 9. Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. This encompasses efforts aimed at runtime environments (JVM and JavaScript) as well as network protocols. - http://www.reactive-streams.org/
  10. 10. Publisher Subscriber Stream Processing
  11. 11. Publisher Subscriber Stream Processing 3msg/sec 1msg/sec
  12. 12. Publisher Subscriber Stream Processing 3msg/sec 1msg/sec
  13. 13. Publisher Subscriber Stream Processing 3msg/sec 1msg/sec
  14. 14. Publisher Subscriber Stream Processing 3msg/sec 1msg/sec
  15. 15. Publisher Subscriber Stream Processing With Back Pressure 3msg/sec 1msg/sec Hey, I can only take 1msg/sec
  16. 16. Publisher Subscriber Stream Processing With Back Pressure 1msg/sec 1msg/sec Hey, I can only take 1msg/sec Gotcha!!
  17. 17. Back Pressure in real world
  18. 18. Back Pressure in real world
  19. 19. Reactive Stream Implementation Ÿ RxJavaReactiveStreams with RxJava 1.x 2.x Ÿ Project Reactor Ÿ Vert.x Ÿ Akka Streams Ÿ Slick Ÿ …
  20. 20. Project Reactor
  21. 21. Journey of Project Reactor
  22. 22. 2
  23. 23. 2
  24. 24. 2
  25. 25. Project Reactor
  26. 26. Project Reactor Ÿ Reactive Streams Library for JVM Ÿ Declarative operations similar to Java 8 Streams Ÿ Flux and Mono reactive composable API types Ÿ Compatible with RxJava,
  27. 27. Reactive Streams http://javasampleapproach.com/java/java-9/java-9-flow-api-reactive-streams
  28. 28. Reactive Streams: Mono – sequence of 0..1Mono
  29. 29. Mono<String> emptyMono() { return Mono.empty(); } Mono<String> fooMono() { return Mono.just("foo"); } Mono<String> toUpperCase(Mono<String> mono) { return mono.map(String::toUpperCase); } Mono BasicMono Basic
  30. 30. Reactive Streams: Flux – sequence of 0..NFlux
  31. 31. Flux Basic Flux<String> emptyFlux() { return Flux.empty();} Flux<String> fooBarFluxFromValues() { return Flux.just("foo", "bar");} Flux<String> fooBarFluxFromList() { return Flux.fromIterable(Arrays.asList("foo", "bar")); } Flux<String> toUpperCase(Flux<String> flux) { return flux.flatMap(s -> Mono.just(s.toUpperCase())); } Flux Basic
  32. 32. Spring 5
  33. 33. Spring 5 - Reactive Repository public interface ReactivePersonRepository extends ReactiveCrudRepository<Person, String> { Flux<Person> findByLastname(Mono<String> lastname); Mono<Person> findByFirstnameAndLastname(String firstname, String lastname); } @EnableReactiveMongoRepositories public class AppConfig extends AbstractReactiveMongoConfiguration { } (Repository)
  34. 34. Ÿ MongoDB Ÿ Cassandra Ÿ Redis Ÿ Couchbase Spring 5 – Spring Data Compatible
  35. 35. Ÿ Oracle, MySQL Ÿ A database access API for Java that does not block user threads Ÿ Target high throughput apps From: JDBC Next by Douglas Surber (JavaOne 2016) NonBlocking JDBC Access to Oracle,Mysql
  36. 36. public void trivialSelect(DataSource ds, List<Integer> result) { String sql = "select <<id>>, <<name>>, <<answer>> " + "from tab where answer = <<i target>>"; try (Connection conn = ds.getConnection()) { conn.<List<Integer>>rowOperation(sql) .set("target", 42, JdbcType.NUMERIC) .rowAggregator( (ignore, row) -> { result.add(row.get("id", Integer.class)); return null; } ) .submit(); }} NonBlocking JDBC Example
  37. 37. Spring 5 - Reactive Web Controller @Controller Public class UserController { private final UserRepository userRepository; public UserController(UserRepository userRepository) { this.userRepository = userRepository; } @GetMapping(“/users/{id}”) public Mono<User> getUser(@PathVariable Long id) { return this.userRepository.findOne(id);} @PostMapping(“/users”) public Mono<Void> addUser(@RequestBody User user) { return this.userRepository.save(user);} } (Controller) - (Repository)
  38. 38. Spring 5 - Reactive Web Framework public interface ReactiveHttpInputMessage extends HttpMessage { Flux<DataBuffer> getBody(); } public interface ReactiveHttpOutputMessage extends HttpMessage { Mono<Void> writeWith(Publisher<DataBuffer> body); } (Framework) - (Controller) - (Repository)
  39. 39. Spring 5 – Reactive HTTP Server Ÿ Servlet 3.1 Bridge Ÿ Undertow in spring-web Ÿ Reactor Netty Ÿ RxNetty Ÿ Reactive Stream Bridge to Tomcat and Jetty (HTTP Server) - (Framework) - (Controller) - (Repository)
  40. 40. Observable<User> fromFluxToObservable(Flux<User> flux) { return Observable.fromPublisher(flux); } Flux<User> fromObservableToFlux(Observable<User> observable) { return Flux.from(observable.toFlowable(BackpressureStrategy.BUFFER)); } RxJava Observable and Reactor
  41. 41. CompletableFuture<User> fromMonoToCompletableFuture(Mono<User> mono) { return mono.toFuture(); } Mono<User> fromCompletableFutureToMono(CompletableFuture<User> future) { return Mono.fromFuture(future); } Mono and CompletableFuture
  42. 42. Summary • Spring 5.0 GA 곧 출시!!
  43. 43. Ÿ Reactive Streams Ÿ Microservices and Reactive Streams Ÿ Spring 5.0 GA – Jul 27, 2017 Ÿ Being adopted for Boot, Security, Data, Cloud, Integration, etc Summary

×