Building Microservices with Micronaut®

Book description

Explore different aspects of building modular microservices such as development, testing, maintenance, and deployment using the Micronaut framework

Key Features

  • Learn how to build scalable, fast, and resilient microservices with this concise guide
  • Explore the many advantages of using reflection-free, compile-time dependency injections and aspect-oriented programming
  • Build cloud-native applications easily with the Micronaut framework

Book Description

The open source Micronaut® framework is a JVM-based toolkit designed to create microservices quickly and easily. This book will help full-stack and Java developers build modular, high-performing, and reactive microservice-based apps using the Micronaut framework.

You'll start by building microservices and learning about the core components, such as ahead-of-time compilation, reflection-less dependency injection, and reactive baked-in HTTP clients and servers. Next, you will work on a real-time microservice application and learn how to integrate Micronaut projects with different kinds of relational and non-relational databases. You'll also learn how to employ different security mechanisms to safeguard your microservices and integrate microservices using event-driven architecture in the Apache Kafka ecosystem. As you advance, you'll get to grips with automated testing and popular testing tools. The book will help you understand how you can easily handle microservice concerns in Micronaut projects, such as service discovery, API documentation, distributed configuration management, fallbacks, and circuit breakers. Finally, you'll explore the deployment and maintenance aspects of microservices and get up to speed with the Internet of Things (IoT) using the Framework.

By the end of this book, you'll be able to build, test, deploy, and maintain your own microservice apps using the framework.

What you will learn

  • Understand why the Micronaut framework is best suited for building microservices
  • Build web endpoints and services in the Micronaut framework
  • Safeguard microservices using Session, JWT, and OAuth in Micronaut projects
  • Get to grips with event-driven architecture in Micronaut applications
  • Discover how to automate testing at various levels using built-in tools and testing frameworks
  • Deploy your microservices to containers and cloud platforms
  • Become well-versed with distributed logging, tracing, and monitoring in Micronaut projects
  • Get hands-on with the IoT using Alexa and the Micronaut framework

Who this book is for

This book is for developers who have been building microservices on traditional frameworks such as Spring Boot and are looking for a faster alternative. Intermediate-level knowledge of Java programming and implementing web services development in Java is required.

Table of contents

  1. Building Microservices with Micronaut®
  2. Foreword
  3. Contributors
  4. About the authors
  5. About the reviewer
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  7. Section 1: Core Concepts and Basics
  8. Chapter 1: Getting Started with Microservices Using the Micronaut Framework
    1. Technical requirements
    2. Introducing microservices and their evolution
    3. Understanding microservices design patterns
      1. Decomposition design patterns
      2. Integration design patterns
      3. Data management patterns
      4. Cross-cutting patterns
    4. Why Micronaut is the best choice for developing microservices
    5. Getting started with the Micronaut framework
      1. Installing the Micronaut CLI on mac OS
      2. Installing the Micronaut CLI on Windows
    6. Working on a hello world project in the Micronaut framework
      1. Creating a hello world project using the Micronaut CLI
      2. Creating a hello world project using Micronaut Launch
    7. Summary
    8. Questions
  9. Section 2: Microservices Development
  10. Chapter 2: Working on Data Access
    1. Technical requirements
    2. Integrating with persistence in the Micronaut framework
    3. Integrating with a relational database using an ORM (Hibernate) framework
      1. Generating the pet-owner schema in PostgreSQL
      2. Creating a Micronaut application for the pet-owner microservice
      3. Creating the entity classes
      4. Defining relationships among entities
      5. Creating data access repositories
      6. Creating services for entities
      7. Performing basic CRUD operations
      8. Wrapping up
    4. Integrating with a relational database using a persistence (MyBatis) framework
      1. Generating the pet-clinic schema in PostgreSQL
      2. Generating a Micronaut application for the pet-clinic microservice
      3. Defining a MyBatis factory
      4. Creating the entity classes
      5. Defining the mappers (repositories) for the entities
      6. Creating services for entities
      7. Performing basic CRUD operations
      8. Wrapping up
    5. Integrating with a NoSQL database (MongoDB)
      1. Creating a vet-reviews collection in MongoDB
      2. Generating a Micronaut application for the pet-clinic-reviews microservice
      3. Configuring MongoDB in Micronaut
      4. Creating the entity class
      5. Creating a data access repository
      6. Creating a service for the entity
      7. Performing basic CRUD operations
      8. Wrapping up
    6. Summary
    7. Questions
  11. Chapter 3: Working on RESTful Web Services
    1. Technical requirements
    2. Working on restful microservices in the Micronaut framework
    3. Using DTOs for the endpoint payloads
      1. Implementing DTOs
      2. Using MapStruct to define mappers
      3. Modifying the services to use DTOs
    4. Creating the restful endpoints for a microservice
      1. Creating an endpoint for retrieving a list of resources
      2. Creating an endpoint for retrieving a specific resource
      3. Creating an endpoint for inserting a resource
      4. Creating an endpoint for updating a resource
      5. Creating an endpoint for deleting a resource
    5. Using Micronaut's HTTP server APIs
      1. Binding HTTP requests in the Micronaut framework
      2. Validating data
      3. Handling errors
      4. Versioning the APIs
    6. Using Micronaut's HTTP client APIs
      1. Performing an HTTP PUT operation
      2. Performing an HTTP POST operation
      3. Performing an HTTP PUT operation
      4. Performing an HTTP DELETE operation
    7. Summary
    8. Questions
  12. Chapter 4: Securing the Microservices
    1. Technical requirements
    2. Working on RESTful microservices in the Micronaut framework
    3. The basics of Micronaut security
    4. Securing service endpoints using session authentication
      1. Implementing a basic authentication provider
      2. Configuring authorizations for the service endpoints
    5. Using JWT authentication to secure the service endpoints
      1. Setting up Keycloak as the identity provider
      2. Securing the pet-clinic microservice using token-based authentication
    6. Using OAuth to secure service endpoints
      1. Setting up Okta as the identity provider
      2. Enabling SSL in the Micronaut framework
      3. Securing the pet-clinic-reviews microservice using OAuth
    7. Summary
    8. Questions
  13. Chapter 5: Integrating Microservices Using Event-Driven Architecture
    1. Technical requirements
    2. Understanding event-driven architecture
      1. Event messaging or a pub/sub model in an event-driven architecture
      2. Event streaming in event-driven architecture
    3. Event streaming with the Apache Kafka ecosystem
    4. Integrating microservices using event streaming
      1. Setting up the Apache Kafka ecosystem locally
      2. Implementing an event-producer client in the pet-clinic-reviews microservice
      3. Implementing an event consumer client in the pet-clinic microservice
    5. Summary
    6. Questions
  14. Section 3: Microservices Testing
  15. Chapter 6: Testing Microservices
    1. Technical requirements
    2. Understanding the testing pyramid
    3. Unit testing in the Micronaut framework
      1. Unit testing using JUnit 5
    4. Service testing in the Micronaut framework
    5. Integration testing using Testcontainers
      1. Setting up the Testcontainers in the Micronaut application
      2. Writing integration tests using Testcontainers
    6. Summary
    7. Questions
  16. Section 4: Microservices Deployment
  17. Chapter 7: Handling Microservice Concerns
    1. Technical requirements
    2. Externalizing the application configuration
      1. Using distributed configuration management to externalize the configuration
    3. Documenting the service APIs
      1. Using Swagger to document the pet-owner service endpoints
    4. Implementing service discovery
      1. Implementing service discovery using Consul
    5. Implementing the API gateway
      1. Implementing the API gateway service
    6. Implementing the fault tolerance mechanisms
      1. Leveraging built-in mechanisms
    7. Summary
    8. Questions
  18. Chapter 8: Deploying Microservices
    1. Technical requirements
    2. Building the container artifacts
      1. Containerizing the Micronaut microservices using Jib
    3. Deploying the container artifacts
      1. Using docker-compose to deploy the pet-clinic services
    4. Summary
    5. Questions
  19. Section 5: Microservices Maintenance
  20. Chapter 9: Distributed Logging, Tracing, and Monitoring
    1. Technical requirements
    2. Distributed logging in Micronaut microservices
      1. Setting up ELK in Docker
      2. Integrating Logstash with Micronaut microservices
      3. Verifying the distributed logging in the pet-clinic application
    3. Distributed tracing in Micronaut microservices
      1. Implementing distributed tracing in Micronaut
      2. Verifying the distributed tracing in the pet-clinic application
    4. Distributed monitoring in Micronaut microservices
      1. Setting up Prometheus and Grafana in Docker
      2. Configuring microservices for distributed monitoring
      3. Verifying the distributed monitoring in the pet-clinic application
    5. Summary
    6. Questions
  21. Section 6: IoT with Micronaut and Closure
  22. Chapter 10: IoT with Micronaut
    1. Technical requirements
    2. Basics of IoT
    3. Working on the basics of Alexa skills
      1. Basics of Alexa skills
      2. Basics of intents
      3. Your first HelloWorld Alexa skill
      4. Testing your code
    4. Integrating Micronaut with Alexa
    5. Summary
    6. Questions
  23. Chapter 11: Building Enterprise-Grade Microservices
    1. Technical requirements
      1. Bringing it all together
    2. Architecting enterprise microservices
      1. Planning and analysis
      2. Design
      3. Develop
      4. Deploy
      5. Manage and maintain
    3. Understanding Micronaut's OpenAPI
      1. Scaling Micronaut
    4. Implementing Micronaut's microservices
    5. Summary
    6. Questions
  24. Assessment
    1. Chapter 10
    2. Chapter 11
    3. Why subscribe?
  25. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Building Microservices with Micronaut®
  • Author(s): Nirmal Singh, Zack Dawood
  • Release date: September 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800564237