Hands-On Reactive Programming in Spring 5

Book description

Explore the reactive system and create efficient microservices with Spring Boot 2.1 and Spring Cloud

Key Features

  • Understand the kind of system modern businesses require with Spring
  • Gain deeper insights into reactive programming with Reactor and Spring Cloud
  • Get in-depth knowledge on asynchronous and nonblocking communication with Spring 5 WebFlux

Book Description

These days, businesses need a new type of system that can remain responsive at all times. This is achievable with reactive programming; however, the development of these kinds of systems is a complex task, requiring a deep understanding of the domain. In order to develop highly responsive systems, the developers of the Spring Framework came up with Project Reactor.

Hands-On Reactive Programming in Spring 5 begins with the fundamentals of Spring Reactive programming. You'll explore the endless possibilities of building efficient reactive systems with the Spring 5 Framework along with other tools such as WebFlux and Spring Boot. Further on, you'll study reactive programming techniques and apply them to databases and cross-server communication. You will advance your skills in scaling up Spring Cloud Streams and run independent, high-performant reactive microservices.

By the end of the book, you will be able to put your skills to use and get on board with the reactive revolution in Spring 5.1!

What you will learn

  • Discover the difference between a reactive system and reactive programming
  • Explore the benefits of a reactive system and understand its applications
  • Get to grips with using reactive programming in Spring 5
  • Gain an understanding of Project Reactor
  • Build a reactive system using Spring 5 and Project Reactor
  • Create a highly efficient reactive microservice with Spring Cloud
  • Test, monitor, and release reactive applications

Who this book is for

This book is for Java developers who use Spring to develop their applications and want to build robust and reactive applications that can scale in the cloud. Basic knowledge of distributed systems and asynchronous programming will help you understand the concepts covered in this book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Reactive Programming in Spring 5
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  5. Foreword
  6. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  8. Why Reactive Spring?
    1. Why reactive?
      1. Message-driven communication
    2. Reactivity use cases
    3. Why Reactive Spring? 
      1. Reactivity on the service level
    4. Summary
  9. Reactive Programming in Spring - Basic Concepts
    1. Early reactive solutions in Spring
      1. Observer pattern
      2. Observer pattern usage example
      3. Publish-Subscribe pattern with @EventListener
      4. Building applications with @EventListener
        1. Bootstrapping a Spring application
        2. Implementing business logic
        3. Asynchronous HTTP with Spring Web MVC
        4. Exposing the SSE endpoint
        5. Configuring asynchronous support  
        6. Building a UI with SSE support
        7. Verifying application functionality
        8. Criticism of the solution
    2. RxJava as a reactive framework
      1. Observer plus iterator equals Reactive Stream
      2. Producing and consuming streams
      3. Generating an asynchronous sequence
      4. Stream transformation and marble diagrams
        1. Map operator
        2. Filter operator
        3. Count operator
        4. Zip operator
      5. Prerequisites and benefits of RxJava
      6. Rebuilding our application with RxJava
        1. Implementing business logic
        2. Custom SseEmitter
        3. Exposing the SSE endpoint
        4. Application configuration
    3. Brief history of reactive libraries
    4. Reactive landscape
    5. Summary
  10. Reactive Streams - the New Streams' Standard
    1. Reactivity for everyone
      1. The API's inconsistency problem
      2. Pull versus push
      3. The flow control problem
        1. Slow producer and fast consumer
        2. Fast producer and slow consumer
          1. Unbounded queue
          2. Bounded drop queue
          3. Bounded blocking queue
      4. The solution
    2. The basics of the Reactive Streams spec
      1. Reactive Streams spec in action
        1. The introduction of the Processor notion 
      2. Reactive Streams technology compatibility kit
        1. The Publisher verification
        2. The Subscriber verification
      3. JDK 9
    3. Advanced - async and parallel in Reactive Streams
    4. Transfiguration of the Reactive Landscape
      1. RxJava transfiguration
      2. Vert.x adjustments
      3. Ratpack improvements
      4. MongoDB Reactive Streams driver 
      5. A composition of reactive technologies in action
    5. Summary
  11. Project Reactor - the Foundation for Reactive Apps
    1. A brief history of Project Reactor
      1. Project Reactor version 1.x
      2. Project Reactor version 2.x
    2. Project Reactor essentials
      1. Adding Reactor to the project
      2. Reactive types – Flux and Mono
        1. Flux
        2. Mono
        3. Reactive types of RxJava 2
          1. Observable
          2. Flowable
          3. Single
          4. Maybe
          5. Completable
      3. Creating Flux and Mono sequences
      4. Subscribing to Reactive Streams
        1. Implementing custom subscribers
      5. Transforming reactive sequences with operators
        1. Mapping elements of reactive sequences 
        2. Filtering reactive sequences
        3. Collecting reactive sequences
        4. Reducing stream elements
        5. Combining Reactive Streams
        6. Batching stream elements
        7. The flatMap, concatMap, and flatMapSequential operators
        8. Sampling elements 
        9. Transforming reactive sequences into blocking structures
        10. Peeking elements while sequence processing
        11. Materializing and dematerializing signals
        12. Finding an appropriate operator
      6. Creating streams programmatically
        1. Factory methods – push and create
        2. Factory method – generate
        3. Wrapping disposable resources into Reactive Streams
        4. Wrapping reactive transactions with the usingWhen factory
      7. Handling errors
      8. Backpressure handling
      9. Hot and cold streams
        1. Multicasting elements of a stream
        2. Caching elements of a stream
        3. Sharing elements of a stream
      10. Dealing with time
      11. Composing and transforming Reactive Streams
      12. Processors
      13. Testing and debugging Project Reactor
      14. Reactor Addons
    3. Advanced Project Reactor 
      1. Reactive Streams life cycle
        1. Assembly-time
        2. Subscription-time
        3. Runtime
      2. The thread scheduling model in Reactor
        1. The publishOn operator
          1. Parallelization with the publishOn operator
        2. The subscribeOn operator
        3. The parallel operator
        4. Scheduler
        5. Rector Context
      3. Internals of Project Reactor
        1. Macro-fusion
        2. Micro-fusion
    4. Summary
  12. Going Reactive with Spring Boot 2
    1. A fast start as the key to success
      1. Using Spring Roo to try to develop applications faster
      2. Spring Boot as a key to fast-growing applications
    2. Reactive in Spring Boot 2.0
      1. Reactive in Spring Core
        1. Support for reactive types conversion
        2. Reactive I/O
      2. Reactive in web
      3. Reactive in Spring Data
      4. Reactive in Spring Session
      5. Reactive in Spring Security
      6. Reactive in Spring Cloud
      7. Reactive in Spring Test
      8. Reactive in monitoring
    3. Summary
  13. WebFlux Async Non-Blocking Communication
    1. WebFlux as a central reactive server foundation
      1. The reactive web core
      2. The reactive web and MVC frameworks
      3. Purely functional web with WebFlux
      4. Non-blocking cross-service communication with WebClient
      5. Reactive WebSocket API
        1. Server-side WebSocket API 
        2. Client-side WebSocket API
        3. WebFlux WebSocket versus the Spring WebSocket module 
      6. Reactive SSE as a lightweight replacement for WebSockets
      7. Reactive template engines
      8. Reactive web security
        1. Reactive access to SecurityContext
        2. Enabling reactive security
      9. Interaction with other reactive libraries
    2. WebFlux versus Web MVC
      1. Laws matter when comparing frameworks 
        1. Little's Law
        2. Amdahl's Law
        3. The Universal Scalability Law
      2. Thorough analysis and comparison
        1. Understanding the processing models in WebFlux and Web MVC
        2. Impact of processing models on throughput and latency
        3. Challenges with the WebFlux processing model
        4. Impact of different processing models on memory consumption
        5. Impact of processing models on usability
    3. Application of WebFlux
      1. Microservice-based systems
      2. Systems that handle clients with slow connections
      3. Streaming or real-time systems
      4. WebFlux in action
    4. Summary
  14. Reactive Database Access
    1. Data handling patterns in the modern world
      1. Domain-driven design
      2. Data stores in the era of microservices
      3. Polyglot persistence
      4. Database as a Service
      5. Sharing data across microservices
        1. Distributed transactions
        2. Event-driven architecture
        3. Eventual consistency
        4. The SAGA pattern
        5. Event sourcing
        6. Command Query Responsibility Segregation
        7. Conflict-free replicated data types
        8. Messaging system as a data store
    2. Synchronous model for data retrieval
      1. Wire protocol for database access
      2. Database driver
      3. JDBC
        1. Connection management
        2. Making relational database access reactive
      4. Spring JDBC
      5. Spring Data JDBC
        1. Making Spring Data JDBC reactive
      6. JPA
        1. Making JPA reactive
      7. Spring Data JPA
        1. Making Spring Data JPA reactive
      8. Spring Data NoSQL
      9. Limitations of the synchronous model
      10. Advantages of the synchronous model
    3. Reactive data access with Spring Data
      1. Using MongoDB reactive repository
      2. Combining repository operations
      3. How reactive repositories work
        1. Pagination support
        2. ReactiveMongoRepository implementation details
        3. Using ReactiveMongoTemplate
        4. Using reactive drivers (MongoDB)
        5. Using asynchronous drivers (Cassandra)
      4. Reactive transactions
        1. Reactive transactions with MongoDB 4
        2. Distributed transactions with the SAGA pattern
      5. Spring Data reactive connectors
        1. Reactive MongoDB connector
        2. Reactive Cassandra connector
        3. Reactive Couchbase connector
        4. Reactive Redis connector
      6. Limitations and anticipated improvements
      7. Asynchronous Database Access
      8. Reactive Relational Database Connectivity
        1. Using R2DBC with Spring Data R2DBC
    4. Transforming a synchronous repository into reactive
      1. Using the rxjava2-jdbc library
      2. Wrapping a synchronous CrudRepository
    5. Reactive Spring Data in action
    6. Summary
  15. Scaling Up with Cloud Streams
    1. Message brokers as the key to message-driven systems
      1. Server-side load balancing
      2. Client-side load balancing with Spring Cloud and Ribbon
      3. Message brokers as an elastic, reliable layer for message transferring
      4. The market of message brokers
    2. Spring Cloud Streams as a bridge to Spring Ecosystem
    3. Reactive programming in the cloud
      1. Spring Cloud Data Flow
      2. The finest-grained application with Spring Cloud Function
      3. Spring Cloud – function as a part of a data flow
    4. RSocket for low-latency, reactive message passing
      1. RSocket versus Reactor-Netty
      2. RSocket in Java
      3. RSocket versus gRPC
      4. RSocket in Spring Framework
      5. RSocket in other frameworks 
        1. The ScaleCube Project
        2. The Proteus Project
      6. Summarizing RSocket
    5. Summary
  16. Testing the Reactive Application
    1. Why are reactive streams hard to test?
    2. Testing reactive streams with StepVerifier
      1. Essentials of StepVerifier
      2. Advanced testing with StepVerifier
      3. Dealing with virtual time
      4. Verifying reactive context
    3. Testing WebFlux
      1. Testing Controllers with WebTestClient 
      2. Testing WebSocket
    4. Summary
  17. And, Finally, Release It!
    1. The importance of DevOps-friendly apps
    2. Monitoring the Reactive Spring application
      1. Spring Boot Actuator
        1. Adding an actuator to the project
        2. Service info endpoint
        3. Health information endpoint
        4. Metrics endpoint
        5. Loggers management endpoint
        6. Other valuable endpoints
        7. Writing custom actuator endpoints
        8. Securing actuator endpoints
      2. Micrometer
        1. Default Spring Boot metrics
      3. Monitoring Reactive Streams
        1. Monitoring reactor flows
        2. Monitoring reactor schedulers
        3. Adding custom Micrometer meters
      4. Distributed tracing with Spring Boot Sleuth
      5. Pretty UI with Spring Boot Admin 2.x
    3. Deploying to the cloud
      1. Deploying to Amazon Web Services
      2. Deploying to the Google Kubernetes Engine
      3. Deploying to Pivotal Cloud Foundry
        1. Discovering RabbitMQ in PCF
        2. Discovering MongoDB in PCF
        3. Configurationless deployment with Spring Cloud Data Flow for PCF
      4. Knative for FaaS over Kubernetes and Istio
      5. Bits of advice for successful application deployment
    4. Summary
  18. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Reactive Programming in Spring 5
  • Author(s): Oleh Dokuka, Igor Lozynskyi
  • Release date: October 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781787284951