Hands-On Microservices with Kotlin
eBook - ePub

Hands-On Microservices with Kotlin

  1. 414 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Microservices with Kotlin

About this book

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 WebFluxWho This Book Is ForIf 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 youWhat 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 OnlineIn DetailWith 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 approachThis 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.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Creating Reactive Microservices

Reactive microservices are the next step in the evolution of microservices. Based on the reactive paradigm, they target delivering more responsive, resilient, and elastic message-driven services that will outperform the more traditional non-reactive architectures.
In this chapter, we will learn how easily we can create them using Spring Framework 5.0, and how we can use reactive programming to create them.
We learned about the benefits of reactive programming in Chapter 1, Understanding Microservices. You can review the section covering reactive programming to understand this topic further.
The reactive microservices that we will create in this chapter will be based on our previously created RESTful API examples, showing how easily we can adapt to this new model.
In this chapter, you will learn about:
  • Spring WebFlux
  • Router functions
  • Mono
  • Flux
  • Introduction to functional programming
  • Reactive error handling

Understanding Spring WebFlux

Spring WebFlux is a new component introduced in Spring Framework 5.0 that allows the creation of reactive microservices using Netty as the new web/application server. WebFlux extensively uses the Reactor Framework to implement the reactive streams pattern. In this section, we will understand how to create Spring WebFlux applications, and how we can use them to migrate our non-reactive microservices into this new technological stack.

Creating a Spring WebFlux application

As before, we will use Spring Initializr to create a new Spring Boot Application, but in this case, a WebFlux application. First, navigate to the Spring Initializr site at https://start.spring.io/. We will choose to create a Maven Project with Kotlin and Spring Boot 2.0.0 M7. For the project Metadata, we will set the Group as com.microservices, Artifact as chapter4, and as Dependencies, we will include Reactive Web:
Creating a WebFlux application using Spring Initializr
Then, we will click on the Generate Project button to download a zip named chapter4.zip; we will uncompress it and open the resulting folder in IntelliJ IDEA. After some time, our project will be ready and we can open the Maven window to see the different life cycle phases, Maven plugins, and their goals.
We cover how to use Spring Initializr, Maven, and IntelliJ IDEA in Chapter 2, Getting Started with Spring Boot 2.0. You can visit this chapter to understand topics not covered in this section.
If we take a look at our newly created application and open our Chapter4Application.kt file, we can see it is a standard Spring Boot application:
package com.microservices.chapter4

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class Chapter4Application

fun main(args: Array<String>) {
runApplication<Chapter4Application>(*args)
}
Now, we could run our microservice to see some differences from our previously built microservices in other chapters. We can use the Maven window to double-click on the spring-boot plugin, run goal, or just do so from the command line:
mvnw spring-boot:run
After some seconds, we will see several log lines, including something like this:
INFO 12308 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
Our microservice is now ready to handle requests using Netty; this is the first change that we need to understand since, in our previous examples, we used Tomcat.
Interestingly enough, when we choose Reactive Web in the dependencies on Spring Initializr, our Maven pom will include spring-boot-starter-webflux as Dependency. If we choose Web as the Dependency in Spring Initializr, it will include spring-boot-starter-web. We will name this component WebFlux in this chapter for clarity, even though Spring Initializr named it Reactive Web.

Using Netty

Netty was originally developed by JBoss with the idea to create a Client-Server Framework that allows us to perform non-blocking IO operations. To archive this capability, we use a message-driven implementation of the reactor pattern. Nowadays, it is supported by an extensive open source community.
Netty includes support for major algorithm and protocols such as HTTP, SSL/TSL, or DNS, but adds support for modern protocols such as HTTP/2, WebSocket, or Google Protocol Buffers. This is just a summary of some of Netty's capabilities.
You can find more information on Netty on their website at https://netty.io/.
Spring Boot 1....

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. Packt Upsell
  5. Contributors
  6. Preface
  7. Understanding Microservices
  8. Getting Started with Spring Boot 2.0
  9. Creating RESTful Services
  10. Creating Reactive Microservices
  11. Reactive Spring Data
  12. Creating Cloud-Native Microservices
  13. Creating Dockers
  14. Scaling Microservices
  15. Testing Spring Microservices
  16. Monitoring Microservices
  17. Deploying Microservices
  18. Best Practices
  19. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Hands-On Microservices with Kotlin by Juan Antonio Medina Iglesias in PDF and/or ePUB format, as well as other popular books in Ciencia de la computación & Lenguajes de programación. We have over one million books available in our catalogue for you to explore.