Spring Interface Clients Lecture

Jul 3, 2024

Spring Interface Clients Lecture Notes

Introduction

  • Presenter: Olga Maash Sharma, Spring Cloud Team
  • Topics covered: Spring interface clients, Spring Cloud projects, AOT, Spring GRVM native images, Spring Framework team collaboration.

Declarative Clients

  • Scenario: Two services - Customer Service (register customers) and Verification Service (verify customers).
  • Problem: Manual HTTP client integration in service classes (error-prone, redundant).
  • Solution: Create interfaces for known parameters, return types, and readability.

Libraries & Frameworks

  • Fain Project: Popular GitHub project for declarative clients, integrated with Spring Cloud OpenFain.
  • Spring Cloud OpenFain: Allows use of Fain interfaces with Spring MVC style annotations.
  • **Criticism: Overloaded server-side annotations used on the client-side, leading to limitations.

Challenges with Fain

  • Third Party Dependency: Dependent on Fain core team for changes and updates.
  • Reactive Stack Support: Difficulty integrating reactive requests (Project Reactor, WebClient, etc.) with Fain.
  • Proposed Solutions: Spring Cloud Square and Retrofit for reactive requests, Retrosocket for RSocket.

Spring Interface Clients

  • Introduction (Spring 6.0): Supports HTTP and RSocket clients with specific annotations.
  • HTTP Exchange Annotation: Custom annotation (HTTPExchange) instead of reusing server-side annotations (PostMapping, etc.).
  • Attributes: HTTP method, URL, content type, accept headers.
  • Shortcut Annotations: @GetExchange, @PostExchange, etc.

Parameters Handling

  • Types: HTTP method, URI, Base URI, Path variables, Request parameters, Cookies, Request headers, etc.
  • Request Attributes: Useful for transforming or modifying requests on the client-side (WebClient example).

Return Values

  • Types: Concrete values, Mono, Flux, ResponseEntity, HTTP headers, etc.
  • Goal: Avoid blocking in reactive threads where possible.

Creating and Configuring Clients

  • Steps: Create service proxy factory, create service instance beans.
  • WebClient and RestClient: Use adapters (WebClientAdapter, RestClientAdapter) for underlying clients.
  • Additional Configurations: Custom argument resolvers and value converters.
  • Error Handling: Default status handlers for WebClient, RestClient, and RestTemplate.

Load Balancing and Tracing

  • Service Registries: Eureka, Consul, Zookeeper, Kubernetes, SimpleDiscoveryClient for hardcoded instances.
  • Load Balancer Setup: Annotate with @LoadBalanced, service ID in base URL.
  • Tracing: Handled via underlying clients (WebClient, RestClient, RestTemplate).

Server-Side Integration

  • Use Scenario: Possible to use client annotations (@HTTPExchange) on server-side for shared contracts.
  • Consideration: Use Spring Cloud Contract for better contract management despite support for interface-based contracts.

RSocket Interface Clients

  • Introduction: Async, message-driven protocol supporting multiplex streams (TCP, WebSockets, Aeron).
  • Annotations: @RSocketExchange for clients, @MessageMapping for responders.
  • Features: Metadata, Payload annotation, Destination variables.
  • Creating and Configuring RSocket Clients: Similar steps to HTTP clients, with RSocketRequester and RSocketServiceProxyFactory.

Auto-configurations

  • Spring Cloud OpenFain and Square: Partial feature parity achieved, working towards auto-configurations.
  • Challenges: Deciding mechanisms for enabling, finding interfaces, creating underlying clients, and registering beans.

Conclusion

  • Sample Application: Provided for experimentation (load balancing, HTTP calls, RSocket interactions).
  • Future Work: Enhancements, community feedback, and collaborations.
  • Contact: Available for further questions through the app, Twitter, or in-person post-session.

Additional Resources

  • Sample link and slides to be shared for detailed code examples and configurations.