Slide 1

Slide 1 text

Olga Maciaszek-Sharma Declarative Clients in Spring Copyright © 2020 VMware, Inc. or its affiliates.

Slide 2

Slide 2 text

About Olga Software Engineer in Spring Cloud team Spring Cloud LoadBalancer, Spring Cloud OpenFeign, Spring Cloud Netflix, Spring Cloud Contract, Spring Cloud Square, RSocket HTTP Bridge

Slide 3

Slide 3 text

What is a declarative client?

Slide 4

Slide 4 text

Sample applications Customer Service Verification Service HTTP POST /verify Customer Verification Result

Slide 5

Slide 5 text

Direct use of HTTP client

Slide 6

Slide 6 text

Client interface

Slide 7

Slide 7 text

Implementation

Slide 8

Slide 8 text

Implementation with helper methods

Slide 9

Slide 9 text

Client interface usage

Slide 10

Slide 10 text

What if we could just create an annotated interface and have a library/ framework translate it into actual HTTP client requests?

Slide 11

Slide 11 text

Declarative/ interface client Client interface method calls HTTP calls Proxy

Slide 12

Slide 12 text

Feign

Slide 13

Slide 13 text

OpenFeign/Feign

Slide 14

Slide 14 text

Feign Java to HTTP client binder ● Core part of a former Spring Cloud Netflix module, passed over to the community ● Supports various HTTP clients, including Apache HTTP, Apache HC5, Ok HTTP ● Contracts - support for custom annotations and argument resolution ● Support for a broad range of encoders/ decoders, including Jackson and GSON ● Metrics, including Micrometer support

Slide 15

Slide 15 text

Spring Cloud OpenFeign

Slide 16

Slide 16 text

Spring Cloud OpenFeign Spring MVC and Spring Cloud support for OpenFeign ● Spring MVC annotations support ● Auto-configurations for Feign-specific beans ● Spring Cloud LoadBalancer support ● Spring Cloud CircuitBreaker support ● Tracing support

Slide 17

Slide 17 text

A declarative/ interface client

Slide 18

Slide 18 text

A declarative/ interface client use

Slide 19

Slide 19 text

Spring Cloud OpenFeign Demo Applications communicating over HTTP Customer Service SC OpenFeign Client SC LoadBalancer SC Sleuth Verification Service POST /verify Customer Verification Result Eureka Service Registry

Slide 20

Slide 20 text

Spring Cloud OpenFeign - popularity Highly-readable interface clients with robust integrations ● 797 GitHub stars ● 8670549 Maven downloads in 2021 ● Nearly flat learning curve for Spring MVC users

Slide 21

Slide 21 text

Spring Cloud OpenFeign - issues ● Issues with reusing client interfaces as controllers ○ @RequestMapping at class level user issues (GH-547, GH-678) ○ CVE-2021-22044 ● Maintenance issues caused by dependence on third party upstream project ● Lack of non-blocking support

Slide 22

Slide 22 text

Spring Cloud Square Spring Boot and Spring Cloud support for Retrofit and OkHttp ● WebClient support for Retrofit interface clients ● Auto-configurations ● Load-balancing ● Tracing ● Only Retrofit annotations ● Incubator project

Slide 23

Slide 23 text

RetroSocket Interface client for RSocket ● Incubator project

Slide 24

Slide 24 text

Considerations More third-party integrations or an in-house solution? ● HTTP/reactive, RSocket/RetroSocket - converging solutions ● Opportunity to create a first-class solution in the Spring Framework ○ No maintenance dependence on third party projects ● Previous Spring Cloud support built around external projects ○ Possibility of providing integration on top of the Spring Framework interface clients

Slide 25

Slide 25 text

HTTP & RSocket Interface Clients in Spring Framework 6.0

Slide 26

Slide 26 text

Interface clients are not new in the Spring Framework Same mechanism, different protocols

Slide 27

Slide 27 text

Why HTTP and RSocket now? Discussions with Spring Cloud around OpenFeign, Retrofit, and RSocket integrations

Slide 28

Slide 28 text

Spring Framework 6.0 already had plans for RSocket interface client

Slide 29

Slide 29 text

Old request for HTTP, deferred to… Spring Cloud OpenFeign

Slide 30

Slide 30 text

Old request for HTTP, deferred to… Spring Cloud OpenFeign

Slide 31

Slide 31 text

Opportunity to revisit Underlying mechanism is not specific to Spring Cloud Cloud features are extensions on top

Slide 32

Slide 32 text

Some issues can only be addressed in Spring Framework programming model vs reusing @RequestMapping

Slide 33

Slide 33 text

Decided to explore for Spring Framework 6.0

Slide 34

Slide 34 text

HTTP interface client programming model Relationship to server @RequestMapping?

Slide 35

Slide 35 text

Two goals Define a single request (no wildcards of any kind!) Avoid outright server-specific

Slide 36

Slide 36 text

@RequestMapping attributes For mapping of 1..N requests Any HTTP method Any URL path

Slide 37

Slide 37 text

@RequestMapping method parameters ServletRequest HttpMethod @RequestHeader @RequestParam @RequestBody @RequestAttribute @ModelAttribute …

Slide 38

Slide 38 text

@RequestMapping return values ResponseEntity HttpHeaders Body void Model View SseEmitter …

Slide 39

Slide 39 text

New method annotation to declare rather than map requests @RequestMapping @HttpExchange

Slide 40

Slide 40 text

@HttpExchange attributes url method contentType accept

Slide 41

Slide 41 text

@HttpExchange method parameters Subset of those for @RequestMapping i.e. reusing method parameter annotations

Slide 42

Slide 42 text

@HttpExchange method parameters URI HttpMethod @RequestHeader @PathVariable @RequestBody @RequestParam @CookieValue

Slide 43

Slide 43 text

@HttpExchange return values Subset of those for @RequestMapping i.e. also using return value types

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Blocking

Slide 46

Slide 46 text

Reactive

Slide 47

Slide 47 text

status, headers, body

Slide 48

Slide 48 text

headers

Slide 49

Slide 49 text

execute only

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Spring Boot can declare this as a bean

Slide 52

Slide 52 text

RSocket interface client planned for Spring Framework 6.0 M5

Slide 53

Slide 53 text

Resources ● Demo code ● Spring Cloud OpenFeign ● Spring Framework documentation for HTTP interface client ● Spring Cloud Square ● RetroSocket

Slide 54

Slide 54 text

Thank you Twitter: @olga_maciaszek © 2020 Spring. A VMware-backed project.