0%

Book Description

Develop cloud native applications with microservices using Spring Boot, Spring Cloud, and Spring Cloud Data Flow

About This Book

  • Explore the new features and components in Spring
  • Evolve towards micro services and cloud native applications
  • Gain powerful insights into advanced concepts of Spring and Spring Boot to develop applications more effectively
  • Understand the basics of Kotlin and use it to develop a quick service with Spring Boot

Who This Book Is For

This book is for an experienced Java developer who knows the basics of Spring, and wants to learn how to use Spring Boot to build applications and deploy them to the cloud.

What You Will Learn

  • Explore the new features in Spring Framework 5.0
  • Build microservices with Spring Boot
  • Get to know the advanced features of Spring Boot in order to effectively develop and monitor applications
  • Use Spring Cloud to deploy and manage applications on the Cloud
  • Understand Spring Data and Spring Cloud Data Flow
  • Understand the basics of reactive programming
  • Get to know the best practices when developing applications with the Spring Framework
  • Create a new project using Kotlin and implement a couple of basic services with unit and integration testing

In Detail

Spring 5.0 is due to arrive with a myriad of new and exciting features that will change the way we’ve used the framework so far. This book will show you this evolution—from solving the problems of testable applications to building distributed applications on the cloud.

The book begins with an insight into the new features in Spring 5.0 and shows you how to build an application using Spring MVC. You will realize how application architectures have evolved from monoliths to those built around microservices. You will then get a thorough understanding of how to build and extend microservices using Spring Boot. You will also understand how to build and deploy Cloud-Native microservices with Spring Cloud. The advanced features of Spring Boot will be illustrated through powerful examples. We will be introduced to a JVM language that’s quickly gaining popularity - Kotlin. Also, we will discuss how to set up a Kotlin project in Eclipse.

By the end of the book, you will be equipped with the knowledge and best practices required to develop microservices with the Spring Framework.

Style and approach

This book follows an end-to-end tutorial approach with lots of examples and sample applications, covering the major building blocks of the Spring framework.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Errata
      3. Piracy
      4. Questions
  2. Evolution to Spring Framework 5.0
    1. Spring Framework
      1. Problems with EJB
    2. Why is Spring Framework popular?
      1. Simplified unit testing
      2. Reduction in plumbing code
        1. How does Spring Framework do this magic?
      3. Architectural flexibility
      4. Keep up with changing times
    3. Spring modules
      1. Spring Core Container
      2. Cross-cutting concerns
      3. Web
      4. Business
      5. Data
    4. Spring Projects
      1. Spring Boot
      2. Spring Cloud
      3. Spring Data
      4. Spring Batch
      5. Spring Security
      6. Spring HATEOAS
    5. New features in Spring Framework 5.0
      1. Baseline upgrades
      2. JDK 9 runtime compatibility
      3. Usage of JDK 8 features in Spring Framework code
      4. Reactive programming support
      5. Functional web framework
      6. Java modularity with Jigsaw
      7. Kotlin support
      8. Dropped features
    6. Spring Boot 2.0 new features
    7. Summary
  3. Dependency Injection
    1. Understanding dependency injection
      1. Understanding dependencies
      2. The Spring IoC container
        1. Defining beans and wiring
        2. Creating a Spring IoC container
        3. Java configuration for the application context
          1. A quick review
          2. Launching the application context with Java configuration
          3. The console log
        4. The XML configuration for the application context
          1. Defining the XML Spring configuration
          2. Launching an application context with the XML configuration
        5. Writing JUnit using the Spring context
      3. Unit testing with mocks
      4. Container managed beans
      5. Dependency injection types
        1. The setter injection
        2. The constructor injection
        3. Constructor versus setter injection
      6. Spring bean scopes
      7. Java versus XML configuration
      8. The @Autowired annotation in depth
        1. The @Primary annotation
        2. The @Qualifier annotation
      9. Other important Spring annotations
      10. Exploring Contexts and dependency injection
        1. An example of CDI
    2. Summary
  4. Building a Web Application with Spring MVC
    1. Java web application architecture
      1. Model 1 architecture
      2. Model 2 architecture
      3. Model 2 Front Controller architecture
    2. Basic flows
      1. Basic setup
        1. Adding dependency for Spring MVC
        2. Adding DispatcherServlet to web.xml
        3. Creating Spring context
      2. Flow 1 - Simple controller flow without View
        1. Creating a Spring MVC controller
        2. Running the web application
        3. Unit testing
          1. Setting up the Controller to test
          2. Writing the Test method
      3. Flow 2 - Simple controller flow with a View
        1. Spring MVC controller
        2. Creating a View - a JSP
          1. View resolver
        3. Unit testing
          1. Setting up the Controller to test
          2. Writing the Test method
      4. Flow 3 - Controller redirecting to a View with Model
        1. Spring MVC controller
        2. Creating a View
        3. Unit testing
          1. Setting up the Controller to test
          2. Writing the Test method
      5. Flow 4 - Controller redirecting to a View with ModelAndView
        1. Spring MVC controller
        2. Creating a View
        3. Unit testing
      6. Flow 5 - Controller redirecting to a View with a form
        1. Creating a command or form backing object
        2. The Controller method to show the form
        3. Creating the View with a form
        4. Controller get method to handle form submit
        5. Unit testing
      7. Flow 6 - Adding validation to the previous flow
        1. Hibernate Validator dependency
        2. Simple validations on the bean
        3. Custom validations
        4. Unit testing
          1. Controller setup
          2. The Test method
    3. An overview of Spring MVC
      1. Important features
      2. How it works
    4. Important concepts behind Spring MVC
      1. RequestMapping
        1. Examples of request mapping
          1. Example 1
          2. Example 2
          3. Example 3
        2. Request Mapping methods - supported method arguments
        3. RequestMapping methods - supported return types
      2. View resolution
        1. Configuring JSP view resolver
        2. Configuring Freemarker
      3. Handler mappings and Interceptors
        1. Defining a HandlerInterceptor
        2. Mapping HandlerInterceptor to handlers
      4. Model attributes
      5. Session attributes
        1. Putting an attribute in the session
        2. Reading an attribute from the session
        3. Removing an attribute from the session
      6. InitBinders
      7. The @ControllerAdvice annotation
    5. Spring MVC - advanced features
      1. Exception handling
        1. Common exception handling across controllers
          1. The error view
        2. Specific exception handling in a Controller
      2. Internationalization
        1. Message bundle setup
        2. Configuring a SessionLocaleResolver
        3. Configuring a CookieLocaleResolver
      3. Integration testing Spring controllers
      4. Serving static resources
        1. Exposing static content
        2. Caching static content
        3. Enabling GZip compression of static content
      5. Integrating Spring MVC with Bootstrap
        1. Bootstrap WebJar as Maven dependency
        2. Configure Spring MVC resource handler to deliver WebJar static content
        3. Using Bootstrap resources in JSP
    6. Spring Security
      1. Adding Spring Security dependency
      2. Configuring a filter to intercept all requests
      3. Logout
    7. Summary
  5. Evolution toward Microservices and Cloud-Native Applications
    1. Typical web application architecture with Spring
      1. Web layer
        1. Web application - rendering an HTML View
        2. RESTful services
      2. Business layer
      3. Data layer
      4. Integration layer
      5. Cross-cutting concerns
    2. Problems solved by Spring
      1. Loose coupling and testability
      2. Plumbing code
      3. Lightweight architecture
      4. Architecture flexibility
      5. Simplified implementation of cross-cutting concerns
      6. Design patterns for free
    3. Application development goals
      1. Speed
      2. Safety
        1. Reliability
        2. Availability
        3. Security
        4. Performance
        5. High resilience
      3. Scalability
    4. Challenges with monolithic applications
      1. Long release cycles
      2. Difficult to scale
      3. Adapting new technologies
      4. Adapting new methodologies
      5. Adapting modern development practices
    5. Understanding microservices
      1. What is a microservice?
      2. The microservice architecture
      3. Microservice characteristics
        1. Small and lightweight microservices
        2. Interoperability with message-based communication
        3. Capability-aligned microservices
        4. Independently deployable units
        5. Stateless
        6. Automated build and release process
        7. Event-driven architecture
          1. Approach 1 - sequential approach
          2. Approach 2 - event-driven approach
        8. Independent teams
      4. Microservice advantages
        1. Faster time to market
        2. Technology evolution
        3. Availability and scaling
        4. Team dynamics
      5. Microservice challenges
        1. Increased need for automation
        2. Defining the boundaries of subsystems
        3. Visibility and monitoring
        4. Fault tolerance
        5. Eventual consistency
          1. Shared capabilities (enterprise level)
        6. Increased need for operations teams
    6. Cloud-Native applications
      1. Twelve-Factor App
        1. Maintain one code base
        2. Dependencies
        3. Config
        4. Backing services
        5. Build, release, run
        6. Stateless
        7. Port binding
        8. Concurrency
        9. Disposability
        10. Environment parity
        11. Logs as event streams
        12. No distinction of admin processes
    7. Spring projects
      1. Spring Boot
      2. Spring Cloud
    8. Summary
    9. Spring Framework
      1. Problems with EJB
    10. Why is Spring Framework popular?
      1. Simplified unit testing
      2. Reduction in plumbing code
        1. How does Spring Framework do this magic?
      3. Architectural flexibility
      4. Keep up with changing times
    11. Spring modules
      1. Spring Core Container
      2. Cross-cutting concerns
      3. Web
      4. Business
      5. Data
    12. Spring Projects
      1. Spring Boot
      2. Spring Cloud
      3. Spring Data
      4. Spring Batch
      5. Spring Security
      6. Spring HATEOAS
    13. New features in Spring Framework 5.0
      1. Baseline upgrades
      2. JDK 9 runtime compatibility
      3. Usage of JDK 8 features in Spring Framework code
      4. Reactive programming support
      5. Functional web framework
      6. Java modularity with Jigsaw
      7. Kotlin support
      8. Dropped features
    14. Spring Boot 2.0 new features
    15. Summary
  6. Building Microservices with Spring Boot
    1. What is Spring Boot?
      1. Building a quick prototype for a microservice
      2. Primary goals
      3. Nonfunctional features
    2. Spring Boot Hello World
      1. Configure spring-boot-starter-parent
        1. spring-boot-starter-parent
      2. Configure pom.xml with the required starter projects
        1. Understanding starter projects
      3. Configuring spring-boot-maven-plugin
      4. Creating your first Spring Boot launch class
        1. SpringApplication class
        2. The @SpringBootApplication annotation
      5. Running our Hello World application
      6. Auto-configuration
      7. Starter projects
    3. What is REST?
    4. First REST service
      1. Simple method returning string
        1. Unit testing
        2. Integration testing
      2. Simple REST method returning an object
        1. Executing a request
        2. Unit testing
        3. Integration testing
      3. Get method with path variables
        1. Executing a request
        2. Unit testing
        3. Integration testing
    5. Creating a todo resource
      1. Request methods, operations, and URIs
      2. Beans and services
      3. Retrieving a Todo list
        1. Executing the service
        2. Unit testing
        3. Integration testing
      4. Retrieving details for a specific Todo
        1. Executing the service
        2. Unit testing
        3. Integration testing
      5. Adding a Todo
        1. Postman
        2. Executing the POST service
        3. Unit testing
        4. Integration testing
    6. Spring Initializr
      1. Creating your first Spring Initializr project
        1. pom.xml
        2. FirstSpringInitializrApplication.java class
        3. FirstSpringInitializrApplicationTests class
    7. A quick peek into auto-configuration
    8. Summary
  7. Extending Microservices
    1. Exception handling
      1. Spring Boot default exception handling
        1. Nonexistent resource
        2. Resource throwing an exception
        3. Throwing a custom exception
        4. Customizing the exception message
        5. Response status
    2. HATEOAS
      1. Sending HATEOAS links in response
        1. Spring Boot starter HATEOAS
    3. Validation
      1. Enabling validation on ;the controller method
      2. Defining validations on the bean
      3. Unit testing validations
    4. Documenting REST services
      1. Generating ;a Swagger specification
        1. Swagger UI
        2. Customizing Swagger documentation using annotations
    5. Securing REST services with Spring Security
      1. Adding Spring Security starter
      2. Basic authentication
        1. Integration testing
        2. Unit testing
      3. OAuth 2 authentication
        1. High-level ;flow
        2. Implementing OAuth 2 authentication for our service
          1. Setting up authorization and resource servers
          2. Executing OAuth requests
          3. Obtaining an access token
          4. Executing the request using the access token
          5. Integration test
    6. Internationalization
    7. Caching
      1. Spring-boot-starter-cache
      2. Enabling caching
      3. Caching data
      4. JSR-107 caching annotations
        1. Auto-detection order
    8. Summary
  8. Advanced Spring Boot Features
    1. Externalised configuration
      1. Customizing frameworks through application.properties
        1. Logging
        2. Embedded server configuration
        3. Spring MVC
        4. Spring starter security
        5. Data Sources, JDBC and JPA
        6. Other configuration options
      2. Custom properties in application.properties
        1. Configuration properties - type-safe Configuration Management
      3. Profiles
        1. Profiles-based Bean configuration
      4. Other options ;for application configuration values
      5. YAML configuration
    2. Embedded servers
      1. Switching to Jetty and Undertow
      2. Building a WAR file
    3. Developer tools
      1. Live reload
    4. Spring Boot Actuator
      1. HAL Browser
      2. Configuration properties
      3. Environment details
      4. Health
      5. Mappings
      6. Beans
      7. Metrics
      8. Auto-configuration
      9. Debugging
    5. Deploying ;an application to Cloud
      1. Cloud Foundry
    6. Summary
  9. Spring Data
    1. Background - data stores
    2. Spring Data
      1. Spring Data Commons
        1. Repository
        2. The CrudRepository interface
        3. The PagingAndSortingRepository interface
      2. Spring Data JPA
        1. Spring Data JPA example
          1. New project with Starter Data JPA
          2. Entities
          3. The SpringBootApplication class
          4. Populating some data
        2. A simple repository
          1. Unit test
        3. The CrudRepository interface
          1. Unit test
        4. The PagingAndSortingRepository interface
          1. Unit tests
        5. Query methods
        6. Queries
          1. Named Parameters
          2. Named Query
          3. Native query
    3. Spring Data Rest
      1. The GET method
      2. The POST method
      3. The search resource
    4. Big Data
      1. MongoDB
        1. Unit test
    5. Summary
  10. Spring Cloud
    1. Introducing Spring Cloud
      1. Spring Cloud Netflix
    2. Demo microservices setup
      1. Microservice A
      2. Service consumer
        1. Ports
    3. Centralized microservice configuration
      1. Problem statement
      2. Solution
      3. Options
      4. Spring Cloud Config
        1. Implementing Spring Cloud Config Server
          1. Setting up Spring Cloud Config Server
          2. Connecting Spring Cloud Config Server to a local Git repository
          3. Creating an environment-specific configuration
          4. Spring Cloud Config Client
    4. Spring Cloud Bus
      1. The need for Spring Cloud Bus
      2. Propogating configuration changes using Spring Cloud Bus
      3. Implementation
    5. Declarative REST Client - Feign
    6. Load balancing
      1. Ribbon
        1. Implementation
    7. The Name server
      1. Limitations of hard coding microservice URLs
    8. Workings of Name server
      1. Options
      2. Implementation
        1. Setting up a Eureka Server
        2. Registering microservices with Eureka
        3. Connecting the service consumer microservice with Eureka
    9. API Gateways
      1. Implementing client-side load balancing with Zuul
        1. Setting up a new Zuul API Gateway Server
          1. Zuul custom filters
          2. Invoking microservices through Zuul
        2. Configuring service consumer to use Zuul API gateway
    10. Distributed tracing
      1. Distributed tracing options
      2. Implementing Spring Cloud Sleuth and Zipkin
        1. Integrating microservice components with Spring Cloud Sleuth
        2. Setting up Zipkin Distributed Tracing Server
        3. Integrating microservice components with Zipkin
    11. Hystrix - fault tolerance
      1. Implementation
    12. Summary
  11. Spring Cloud Data Flow
    1. Message-based asynchronous communication
      1. Complexities of asynchronous communication
    2. Spring projects for asynchronous messages
      1. Spring Integration
      2. Spring Cloud Stream
      3. Spring Cloud Data Flow
    3. Spring Cloud Stream
      1. Spring Cloud Stream architecture
      2. Event processing - stock trading example
        1. Model for stock trading example
        2. The source application
        3. Processor
        4. Sink
    4. Spring Cloud Data Flow
      1. High-level architecture
      2. Implementing Spring Cloud Data Flow
        1. Setting up Spring Cloud Data Flow server
        2. Setting up Data Flow Shell project
        3. Configuring the apps
        4. Configuring the stream
        5. Deploying the stream
        6. Log messages - setting up connections to the message factory
        7. Log messages - the flow of events
      3. Spring Cloud Data Flow REST APIs
    5. Spring Cloud Task
    6. Summary
  12. Reactive Programming
    1. The Reactive Manifesto
      1. Characteristics of Reactive Systems
    2. Reactive use case - a stock price page
      1. The traditional approach
      2. The reactive approach
      3. Comparison between the traditional and reactive approaches
    3. Reactive programming in Java
      1. Reactive streams
      2. Reactor
        1. Mono
        2. Flux
      3. Spring Web Reactive
        1. Creating a project using Spring Initializr
        2. Creating a Reactive Controller
        3. Creating an HTML view
        4. Launching SpringReactiveExampleApplication
      4. Reactive databases
        1. Integrating Spring Boot Reactive MongoDB Starter
        2. Creating a model object - a stock document
        3. Creating a ReactiveCrudRepository
        4. Initialising stock data using the Command Line Runner
        5. Creating Reactive methods in Rest Controller
        6. Updating the view to subscribe to the event stream
        7. Launching SpringReactiveExampleApplication
    4. Summary
  13. Spring Best Practices
    1. Maven standard directory layout
    2. Layered architecture
      1. Recommended practices
        1. Separate API and impl for important layers
    3. Exception handling
      1. Spring's approach to exception handling
      2. The recommended approach
    4. Keeping your Spring configuration light
      1. Using the basePackageClasses attribute in ComponentScan
      2. Not using version numbers in schema references
      3. Preferring constructor injection over setter injection for mandatory dependencies
    5. Managing dependency versions for Spring Projects
    6. Unit testing
      1. The business layer
      2. Web layer
      3. The data layer
      4. Other best practices
    7. Integration testing
      1. Spring Session
      2. Example
        1. Adding dependencies for Spring Session
        2. Configuring Filter to replacing HttpSession with Spring Session
        3. Enabling filtering for Tomcat by extending AbstractHttpSessionApplicationInitializer
    8. Caching
      1. Adding the Spring Boot Starter Cache dependency
      2. Adding caching annotations
    9. Logging
      1. Logback
      2. Log4j2
      3. Framework independent configuration
    10. Summary
  14. Working with Kotlin in Spring
    1. Kotlin
    2. Kotlin versus Java
      1. Variables and type inference
      2. Variables and immutability
      3. Type system
      4. Functions
      5. Arrays
      6. Collections
      7. No c
      8. Data class
    3. Creating a Kotlin project in Eclipse
      1. Kotlin plugin
      2. Creating a Kotlin project
      3. Creating a Kotlin class
      4. Running a Kotlin class
    4. Creating a Spring Boot project using Kotlin
      1. Dependencies and plugins
      2. Spring Boot application class
      3. Spring Boot application test class
    5. Implementing a REST service using Kotlin
      1. Simple method returning a string
        1. Unit testing
        2. Integration testing
      2. Simple REST method returning an object
        1. Executing a request
        2. Unit testing
        3. Integration testing
      3. Get method with path variables
        1. Executing a request
        2. Unit testing
        3. Integration testing
    6. Summary