Hands-On Microservices with Kotlin

Book description

Build smart, efficient, and fast enterprise-grade web implementation of the microservices architecture that can be easily scaled.

About This Book

  • Write easy-to-maintain lean and clean code with Kotlin for developing better microservices
  • Scale your Microserivces in your own cloud with Docker and Docker Swarm
  • Explore Spring 5 functional reactive web programming with Spring WebFlux

Who This Book Is For

If you are a Kotlin developer with a basic knowledge of microservice architectures and now want to effectively implement these services on enterprise-level web applications, then this book is for you

What You Will Learn

  • Understand microservice architectures and principles
  • Build microservices in Kotlin using Spring Boot 2.0 and Spring Framework 5.0
  • Create reactive microservices that perform non-blocking operations with Spring WebFlux
  • Use Spring Data to get data reactively from MongoDB
  • Test effectively with JUnit and Kotlin
  • Create cloud-native microservices with Spring Cloud
  • Build and publish Docker images of your microservices
  • Scaling microservices with Docker Swarm
  • Monitor microservices with JMX
  • Deploy microservices in OpenShift Online

In Detail

With Google's inclusion of first-class support for Kotlin in their Android ecosystem, Kotlin's future as a mainstream language is assured. Microservices help design scalable, easy-to-maintain web applications; Kotlin allows us to take advantage of modern idioms to simplify our development and create high-quality services. With 100% interoperability with the JVM, Kotlin makes working with existing Java code easier. Well-known Java systems such as Spring, Jackson, and Reactor have included Kotlin modules to exploit its language features.

This book guides the reader in designing and implementing services, and producing production-ready, testable, lean code that's shorter and simpler than a traditional Java implementation. Reap the benefits of using the reactive paradigm and take advantage of non-blocking techniques to take your services to the next level in terms of industry standards. You will consume NoSQL databases reactively to allow you to create high-throughput microservices. Create cloud-native microservices that can run on a wide range of cloud providers, and monitor them. You will create Docker containers for your microservices and scale them. Finally, you will deploy your microservices in OpenShift Online.

Style and approach

This book guides the reader in designing and implementing services, achieving production- ready, testable, easy-to-maintain, lean code that's shorter and simpler than a traditional Java implementation.

Table of contents

  1. 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
  2. Understanding Microservices
    1. What is a microservice?
      1. Understanding SoA
      2. Differentiating microservices from SoA
      3. From the monolith to microservices
    2. Microservices principles
      1. Defining design principles
      2. Modelled around business capabilities
      3. Loosely couple
      4. Single responsibility
      5. Hiding implementation
      6. Isolation
      7. Independently deployable
      8. Build for failure
        1. Upstream
        2. Downstream
        3. Logging
        4. Monitoring
        5. Alerting
        6. Recovery
        7. Fallbacks
      9. Scalability
      10. Automation
    3. Domain-Driven Design
      1. What is Domain-Driven Design
      2. Ubiquitous language
      3. Bounded context
      4. Context mapping
      5. Using DDD in microservices
    4. Reactive microservices
      1. Reactive programming
      2. Reactive Manifesto
        1. Responsive
        2. Resilient
        3. Elastic
        4. Message-driven
        5. Back pressure
      3. Reactive frameworks
        1. Reactive Extensions
        2. Project Reactor
        3. Java reactive streams
        4. Akka
      4. Reactive microservices
    5. Cloud Native microservices
      1. Cloud computing
      2. Containers
      3. Deployment models
        1. Private cloud
        2. Public cloud
        3. Hybrid cloud
      4. Service models
        1. Infrastructure as a Service
        2. Platform as a Service
        3. Software as a Service
      5. Cloud Native microservices
    6. Summary
  3. Getting Started with Spring Boot 2.0
    1. Creating a Spring Boot application
      1. Setting up
        1. Installing JDK 8
          1. UNIX/Mac OS X
          2. Windows
          3. Testing the installation
        2. Installing Maven 3.5
          1. Unix /MacOS X
          2. Windows
          3. Testing the installation
        3. Installing IntelliJ IDEA CE 2017.2
      2. Using Spring Initializr
        1. Understanding basic usage
        2. Reviewing the generated project files
          1. Maven Files
          2. Source files
          3. Resource files
          4. Gitignore
        3. Understanding advanced usage
      3. Using Maven
        1. Life cycle phases
        2. Introduction to Maven goals
        3. Understanding the POM file
          1. Project definition
          2. Maven parent
          3. Project properties
          4. Dependencies
          5. Build
          6. Repositories
      4. Using IntelliJ IDEA
        1. Opening our Maven project
        2. Executing life cycle phases and goals
        3. Modifying our microservice
        4. Debugging
        5. Tips and Tricks
    2. Spring Boot application structure
      1. Creating an application object
      2. Defining Spring application context
      3. Understanding the component scan
      4. Using components
      5. Autowiring
    3. Packaging and running a Spring Boot application
      1. Packaging
      2. Making JARs not WARs
      3. Running Spring Boot applications
      4. Creating executable JARs
    4. Configuring our application
      1. Setting configuration values
        1. Using properties
        2. Using Yaml
        3. Using command-line arguments
      2. Understanding Spring Expression Language
      3. Using profiles
        1. Defining profile values
        2. Executing with profiles
      4. Creating conditional beans
        1. Creating beans explicitly
        2. Defining an additional bean
        3. Defining beans with conditionals
        4. Running different configurations
    5. Summary
  4. Creating RESTful Services
    1. Understanding RestController
      1. What is a controller
      2. Creating a RestController
      3. Understanding our controller
    2. Path and request parameters
      1. Creating a map of customers
      2. Getting path variables
      3. Understanding path variables
      4. Defining request parameters
      5. Understanding request parameters
    3. HTTP verbs and statuses
      1. Standard HTTP verbs and statuses
        1. Single resources
        2. Collections
        3. Generic errors
      2. Handling HTTP verbs
        1. Handling HTTP POST
        2. Handling HTTP DELETE
        3. Handling HTTP PUT
        4. Using verb and mapping annotations
      3. Implementing a service layer
        1. Creating a service interface
        2. Creating an implementation
        3. Binding the right bean
        4. Using the service
      4. Handling HTTP statuses
        1. Using ResponseEntity
        2. Answering a GET request
        3. Answering a POST request
        4. Answering a DELETE request
        5. Answering an UPDATE request
        6. Empty responses
    4. Working with JSON
      1. Understanding serialization
        1. Using simple objects
        2. Working with complex objects
        3. Adding telephones to our customers
        4. Handling null values
      2. Understanding deserialization
        1. From a request into an object
        2. Deserializing complex objects
        3. Validating objects
    5. Handling errors
      1. Using controller advice
      2. Creating business exceptions
      3. Avoiding the controller advice
    6. Summary
  5. Creating Reactive Microservices
    1. Understanding Spring WebFlux
      1. Creating a Spring WebFlux application
      2. Using Netty
      3. Serving static context
      4. Adding a RestController
      5. Creating a customer service
      6. Blocking is not reactive
    2. Creating reactive services
      1. Understanding subscribers and publishers
      2. Publishing a single object
      3. Using Mono in a service
      4. Publishing multiple objects
      5. Using Flux in a service
      6. Receiving objects reactively
    3. Using functional web programming
      1. Using RouterFunction
      2. Creating handlers
      3. Using reactive services
      4. Handling multiple routes
      5. Using query parameters
      6. Processing a JSON body
    4. Handling reactive errors
      1. Capturing errors on Handlers
      2. Publishing errors
    5. Summary
  6. Reactive Spring Data
    1. NoSQL databases
      1. What is a SQL database?
      2. What is a NoSQL database?
      3. Installing MongoDB
        1. Starting our database server
        2. Connecting with the Mongo client
      4. Creating a database
      5. Using commands
    2. Using Spring Data
      1. Setting up the project
      2. Configuring our connection
      3. Sending commands to MongoDB
    3. Reactive repositories
      1. Creating a repository
      2. Using the repository
      3. Completing our initialization
      4. Using ReactiveMongoTemplate
    4. From CRUD to REST
      1. Bringing back our service
      2. Mapping GET to READ
      3. Mapping POST to CREATE
      4. Mapping DELETE
      5. Finding Customers
    5. Summary
  7. Creating Cloud-Native Microservices
    1. Understanding Spring Cloud
      1. What is a Cloud-Native application?
      2. Spring Cloud architecture
        1. Configuration server
        2. Service discovery
        3. Load Balancer
        4. Gateway
        5. Circuit breaker
      3. Spring Cloud Netflix
      4. Creating a Spring Cloud microservice
    2. Configuration Server
      1. Creating a Configuration Server
      2. Getting configurations
      3. Ciphering data
      4. Using application profiles
    3. Service discovery
      1. Creating a Service Discovery Server
      2. Connecting to the Discovery Server
      3. Using Spring Boot actuator
    4. Gateway
      1. Creating a Gateway
      2. Defining routes
    5. Summary
  8. Creating Dockers
    1. Starting with Docker
      1. Understanding containers
        1. Virtual machines
        2. Containers
        3. Docker
      2. Installing Docker
      3. Starting with Docker
      4. Pulling images
      5. Managing images
    2. Building Dockers with microservices
      1. Creating an example microservice
      2. Creating a Dockerfile
      3. Dockerize our microservice
      4. Running the microservice
    3. Publishing Dockers
      1. Understanding repositories
      2. Creating an account on Docker Hub
      3. Publishing a Docker
      4. Integrating Docker with Maven
    4. Summary
  9. Scaling Microservices
    1. A scalable architecture
      1. Scaling directions
        1. Vertical scaling
        2. Horizontal scaling
      2. Design for scaling
      3. Independently scaling
      4. Scaling down
    2. Creating a Cloud
      1. Creating a swarm
      2. Adding a service
      3. Getting logs from a service
      4. Deleting a service
    3. Publishing a microservice as a service
      1. Creating a registry
      2. Creating a microservice
      3. Creating our Docker
      4. Creating the service
    4. Controlling services
      1. Scaling instances
      2. Getting logs from a service
      3. Controlling the service
    5. Summary
  10. Testing Spring Microservices
    1. Understanding SpringBootTest
      1. Creating a simple microservice
      2. Understanding SpringBootTest
      3. Testing beans
      4. Using MockMvc
    2. Mocking beans
      1. Why we mock
      2. Setting up a mock
      3. Returning mocked values
      4. Verifying Mock invocation
    3. Fluent tests
      1. Adding Kluent
      2. Testing our services expressively
      3. Testing our controller expressively
    4. Summary
  11. Monitoring Microservices
    1. Understanding monitoring
      1. What is monitoring?
      2. Alerting
      3. Recovering
      4. Building a failing microservice
    2. SpringBoot Actuator
      1. Enabling SpringBoot Actuator
      2. Understanding Actuator's endpoints
        1. Health endpoint
        2. Metrics endpoint
        3. Trace endpoint
      3. Defining health indicators
      4. Protecting Actuator endpoints
    3. Java Managing Extensions
      1. Understanding JMX
      2. Using JMX
      3. Create management beans
    4. Summary
  12. Deploying Microservices
    1. Setting things up
      1. Creating a GitHub account
      2. Installing Git
      3. Create an OpenShift account
      4. Downloading the OpenShift client
    2. Building our application
      1. Creating an example microservice
      2. Adding tests
      3. Uploading to GitHub
    3. Publishing to OpenShift
      1. Creating an application
      2. Managing our application
      3. Updating the application
    4. Summary
  13. Best Practices
    1. Using Kotlin idioms
      1. Inferred types
      2. Expressions
      3. Default parameters
      4. Lambda
    2. Managing the Spring context
      1. Constructor injection
      2. Explicit context configuration
      3. Decouple services
    3. Layering the application
      1. Understanding our classes
      2. Domains
      3. Splitting
    4. Testing effectively
      1. Understanding type of tests
        1. Unit tests
        2. Integration tests
        3. E2E tests
        4. Other tests
      2. Testing pyramid
      3. Test Driven Development
      4. Behavior-Driven Development
    5. Handling CI and CD
      1. Continuous Integration
      2. Continuous Delivery
      3. Pipeline
      4. Dashboard
    6. Summary
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Microservices with Kotlin
  • Author(s): Juan Antonio Medina Iglesias
  • Release date: January 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788471459