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

Declarative Clients in Spring

Declarative Clients in Spring

What is a declarative HTTP client vs an imperative one? Why are they useful and why should I care? We’ll talk about options that are available in the Spring Portfolio and what is coming on the horizon.

In this session, we will look at the history of declarative clients in Spring via Spring Cloud OpenFeign. We will also dive into upcoming options that are coming to Spring and the advantages that these new technologies bring to the developer experience.

Olga Maciaszek-Sharma

May 27, 2022
Tweet

More Decks by Olga Maciaszek-Sharma

Other Decks in Technology

Transcript

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

    View full-size slide

  2. 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

    View full-size slide

  3. About Rossen
    Spring Framework and Spring for GraphQL teams
    Spring MVC, WebFlux, WebSocket, RSocket

    View full-size slide

  4. What is a declarative client?

    View full-size slide

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

    View full-size slide

  6. Direct use of HTTP client

    View full-size slide

  7. Client interface

    View full-size slide

  8. Implementation

    View full-size slide

  9. Implementation with helper methods

    View full-size slide

  10. Client interface usage

    View full-size slide

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

    View full-size slide

  12. Declarative/ interface client
    Client interface
    method calls
    HTTP calls
    Proxy

    View full-size slide

  13. OpenFeign/Feign

    View full-size slide

  14. 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

    View full-size slide

  15. Spring Cloud OpenFeign

    View full-size slide

  16. 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

    View full-size slide

  17. A declarative/ interface client

    View full-size slide

  18. A declarative/ interface client use

    View full-size slide

  19. 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

    View full-size slide

  20. 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

    View full-size slide

  21. 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

    View full-size slide

  22. 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

    View full-size slide

  23. RetroSocket
    Interface client for RSocket
    ● Incubator project

    View full-size slide

  24. 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

    View full-size slide

  25. HTTP & RSocket Interface Clients
    in Spring Framework 6.0

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  28. Spring Framework 6.0 already had plans
    for RSocket interface client

    View full-size slide

  29. Old request for HTTP, deferred to…
    Spring Cloud OpenFeign

    View full-size slide

  30. Old request for HTTP, deferred to…
    Spring Cloud OpenFeign

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. Decided to explore
    for Spring Framework 6.0

    View full-size slide

  34. HTTP interface client programming model
    Relationship to server @RequestMapping?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  37. @RequestMapping method parameters
    ServletRequest
    HttpMethod
    @RequestHeader
    @RequestParam
    @RequestBody
    @RequestAttribute
    @ModelAttribute

    View full-size slide

  38. @RequestMapping return values
    ResponseEntity
    HttpHeaders
    Body
    void
    Model
    View
    SseEmitter

    View full-size slide

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

    View full-size slide

  40. @HttpExchange attributes
    url
    method
    contentType
    accept

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  44. status, headers, body

    View full-size slide

  45. execute only

    View full-size slide

  46. Spring Boot can declare this as a bean

    View full-size slide

  47. RSocket interface client planned for
    Spring Framework 6.0 M5

    View full-size slide

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

    View full-size slide

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

    View full-size slide