42
Spring 5.0 Meets Reactive Programming Cláudio E. de Oliveira [email protected] +55 19 3705-5775

Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Spring 5.0 Meets Reactive Programming

Cláudio E. de Oliveira

[email protected]+55 19 3705-5775

Page 2: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Hello!Cláudio E. de OliveiraBook AuthorArchitect and Software Developerat @sensedia

2

Page 3: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

“ Launched at February

3

Page 4: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Agenda

⊷ Why Spring Boot⊷ Reactive Programming⊷ Project Reactor⊷ Spring Boot 2⊷ New Features⊷ Spring Cloud Projects

4

Page 5: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Spring BootWhy Spring Boot became a huge success???

5

Page 6: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

⊷ Stand-alone applications ⊷ Starter POMs - Production Friendly⊷ Automatically configurations (a.k.a JPA,

Brokers)⊷ No code generation and NO XML anymore⊷ Production ready features metrics and

monitoring

Spring Boot Main Features

6

Page 7: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

A lot of sub-projects

Spring Data

7

Spring AMQP

Spring Framework

Spring Cloud GCPSpring Cloud

Spring Cloud Data Flow

Spring SecuritySpring Integration

Spring Messaging

Page 8: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Reactive ProgrammingThe JVM Scenario

8

Page 9: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

9

JVM Players

Page 10: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Project ReactorReactive library implementation for Reactive Streams Specification

10

Page 11: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Motivation

11

“Reactor is a fourth-generation Reactive library for building non-blocking applications onthe JVM based on the Reactive Streams Specification”

https://projectreactor.io/

Page 12: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Why not RXjava??

12

Page 13: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Let’s review some conceptsReactive CoreReactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API, CompletableFuture, Stream and Duration.

Non Blocking IOSuited for Microservices Architecture, Reactor offers backpressure-ready network engines for HTTP (including Websockets), TCP and UDP. Reactive Encoding/Decoding is fully supported.

13

Page 14: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

14

Reactor Types

MONO[0|1]

FLUX[N]

Page 15: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

15

public Mono<Flight> flight(String id) { return this.flightRepository.findById(id); }

public Flux<Flight> flights() { return this.flightRepository.findAll(); }

Samples

Page 16: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Spring Boot 2What is coming???

16

Page 17: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

17

Page 18: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

18Reactive

Page 19: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

19

⊷ Simpler Code, more readable⊷ Focus on Business logic⊷ Stream processing implies

memory efficient⊷ Flow API Java 9

Page 20: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

20

Page 21: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

21

Event-Loop

Page 22: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

22

Page 23: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

23

Page 24: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Simple Reactive Controller

// omitted imports

@Bean public RouterFunction<ServerResponse> routes() { return route(GET("/api/goodbye"), serverRequest -> ok().body(fromPublisher(goodbye(),String.class))) .andRoute(GET("/api/nap"), request -> ok().body(fromPublisher(nap(),String.class)));}

24

Page 25: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Reactive WebClient

// omitted imports

public Mono<Flight> flight(String id) { return discoveryService.serviceAddressFor(this.flightService).next().flatMap( address -> this.webClient.mutate() .baseUrl(address + "/" + this.flightServiceApiPath + "/" + id).build().get().exchange() .flatMap(clientResponse -> clientResponse.bodyToMono(Flight.class)));}

25

Page 26: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

26

Spring Data Reactive

Page 27: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

27Reactive

Page 28: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Kotlin Support

28

Page 29: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Simple REST Controller

// omitted imports

@RestController@RequestMapping("/api/temperature")class TemperatureResource(val temperatureService: TemperatureService) {

@GetMapping fun all() = this.temperatureService.all()

@PostMapping fun register(request: TemperatureRequest) =

this.temperatureService.register(request)

}29

Page 30: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Repository

// omitted imports

interface TemperatureRepository:ReactiveCrudRepository<Temperature,String>{

@Tailable fun findByDeviceId(deviceId:String):Flux<Temperature>

}

30

Page 31: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Continuous Queries

Makes queries on DB and get notification when data changes.

@Tailable for MongoDB

31

Page 32: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

SSEServer-Sent Events

32

Page 33: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

SSE REST Controller

// omitted imports

@RestController@RequestMapping("/api/device")class DeviceResource(val temperatureService: TemperatureService) {

@GetMapping(path = ["/{id}/real-time"],produces = [MediaType.APPLICATION_STREAM_JSON_VALUE]) fun byDeviceRealTime(@PathVariable("id")deviceId:String) = this.temperatureService.byDevice(deviceId)

}

33

Page 34: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

34Demo SSE

Page 35: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Java 9

35

Page 36: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Full Support for Java 9 Module

System

36

Page 37: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Others changes

Actuator EndpointsReactive ThymeleafjUnit 5 Support

37

Page 38: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Spring Boot Actuator

38

Page 39: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

39

How About Spring Cloud Projects???

Page 40: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

https://github.com/claudioed/kotlin-mongodb-demo

40

https://github.com/claudioed/goodbye

https://github.com/PacktPublishing/Spring-5.0-By-Example

GitHub

Page 41: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

@claudioed

41

https://www.linkedin.com/in/claudioedoliveira/

[email protected]

Contacts

Page 42: Programming Spring 5.0 Meets Reactive · Reactive Core Reactor is a fully non-blocking foundation with efficient demand management. It directly interacts with Java 8 functional API,

Cláudio E. de Oliveira

[email protected]+55 19 3705-5775

Obrigado!