Spring: Microservices with Spring Boot - Ranga Rao Karanam - E-Book

Spring: Microservices with Spring Boot E-Book

Ranga Rao Karanam

0,0
32,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Unlock the power of Spring Boot to build and deploy production-ready microservices


Key Features Get to know the advanced features of Spring Boot in order to develop and monitor applications Use Spring cloud to deploy and manage microservices on the cloud Look at embedded servers and deploy a test application to a PaaS Cloud platform Embedded with assessments that will help you revise the concepts you have learned in this bookBook Description


Microservices helps in decomposing applications into small services and move away from a single monolithic artifact. It helps in building systems that are scalable, flexible, and high resilient. Spring Boot helps in building REST-oriented, production-grade microservices.


This book is a quick learning guide on how to build, monitor, and deploy microservices with Spring Boot. You'll be first familiarized with Spring Boot before delving into building microservices. You will learn how to document your microservice with the help of Spring REST docs and Swagger documentation. You will then learn how to secure your microservice with Spring Security and OAuth2. You will deploy your app using a self-contained HTTP server and also learn to monitor a microservice with the help of Spring Boot actuator.


This book is ideal for Java developers who knows the basics of Spring programming and want to build microservices with Spring Boot.


This book is embedded with useful assessments that will help you revise the concepts you have learned in this book.


What you will learn Use Spring Initializr to create a basic spring project Build a basic microservice with Spring Boot Implement caching and exception handling Secure your microservice with Spring security and OAuth2 Deploy microservices using self-contained HTTP server Monitor your microservices with Spring Boot actuator Learn to develop more effectively with developer toolsWho this book is for


This book is aimed at Java developers who knows the basics of Spring programming and want to build microservices with Spring Boot.


Ranga Rao Karanam is a programmer, trainer, and architect. His areas of interest include cloud-native applications, microservices, evolutionary design, high-quality code, DevOps, BDD, TDD, and refactoring. He loves consulting for startups on developing scalable, component-based cloud-native applications, and following modern development practices such as BDD, continuous delivery, and DevOps. He loves the freedom the Spring Framework brings to developing enterprise Java applications. Ranga started in28minutes with the vision of creating high-quality courses on developing cloud-native Java applications. He is looking forward to enhancing his already considerable success--75,000 students on Udemy and 35,000 subscribers on YouTube.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 107

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Spring: Microservices with Spring Boot
Credits
Meet Your Expert
Preface
What's in It for Me?
What Will I Get from This Book?
Prerequisites
1. Building Microservices with Spring Boot
What is Spring Boot?
Building a Quick Prototype for a Microservice
Primary Goals
Nonfunctional Features
Spring Boot Hello World
Configure spring-boot-starter-parent
spring-boot-starter-parent
Configure pom.xml with the Required Starter Projects
Understanding Starter Projects
Configuring spring-boot-maven-plugin
Creating Your First Spring Boot Launch Class
The SpringApplication Class
The @SpringBootApplication Annotation
Running Our Hello World Application
Auto-configuration
Starter Projects
What is REST?
First REST Service
Simple Method Returning String
Unit Testing
Integration Testing
Simple REST Method Returning an Object
Executing a Request
Unit Testing
Integration Testing
Get Method with Path Variables
Executing a Request
Unit Testing
Integration Testing
Creating a Todo Resource
Request Methods, Operations, and Uris
Beans and Services
Retrieving a Todo List
Executing the Service
Unit Testing
Integration Testing
Retrieving Details for a Specific Todo
Executing the Service
Unit Testing
Integration Testing
Adding A Todo
Postman
Executing the POST Service
Unit Testing
Integration Testing
Spring Initializr
Creating Your First Spring Initializr Project
pom.xml
FirstSpringInitializrApplication.java Class
FirstSpringInitializrApplicationTests Class
A Quick Peek into Auto-Configuration
Summary
Assessments
2. Extending Microservices
Exception Handling
Spring Boot Default Exception Handling
Non-Existent Resource
Resource Throwing an Exception
Throwing a Custom Exception
Customizing the Exception Message
Response Status
HATEOAS
Sending HATEOAS Links in Response
Spring Boot Starter HATEOAS
Validation
Enabling Validation on the Controller Method
Defining validations on the Bean
Unit Testing Validations
Documenting REST Services
Generating a Swagger Specification
Swagger UI
Customizing Swagger Documentation Using Annotations
Securing REST Services with Spring Security
Adding Spring Security Starter
Basic Authentication
Integration Testing
Unit Testing
OAuth 2 Authentication
High-level flow
Implementing OAuth 2 authentication for Our Service
Setting up Authorization and Resource Servers:
Executing OAuth Requests
Obtaining an Access Token
Executing the Request Using the Access Token
Integration Test
Internationalization
Caching
Spring-boot-starter-cache
Enabling Caching
Caching Data
JSR-107 Caching Annotations
Auto-Detection Order
Summary
Assessments
3. Advanced Spring Boot Features
Externalised Configuration
Customizing Frameworks Through application.properties
Logging
Embedded Server Configuration
Spring MVC
Spring Starter Security
Data Sources, JDBC, and JPA
Other Configuration Options
Custom Properties in Application.Properties
Configuration properties - Type-Safe Configuration Management
Profiles
Profiles-Based Bean Configuration
Other Options for Application Configuration Values
YAML Configuration
Embedded Servers
Switching to Jetty and Undertow
Building a WAR file
Developer Tools
Live Reload
Spring Boot Actuator
HAL Browser
Configuration Properties
Environment Details
Health
Mappings
Beans
Metrics
Auto-Configuration
Debugging
Deploying an Application to Cloud
Cloud Foundry
Summary
Assessments
4. Assessment Answers
Lesson 1: Building Microservices with Spring Boot
Lesson 2: Extending Microservices
Lesson 3: Advanced Spring Boot Features

Spring: Microservices with Spring Boot

Spring: Microservices with Spring Boot

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: March 2018

Production reference: 1090318

Published by Packt Publishing Ltd.

Livery Place, 35 Livery Street

Birmingham B3 2PB, UK.

ISBN: 978-1-78913-258-8

www.packtpub.com

Credits

This book is a blend of text and quizzes, all packaged up keeping your journey in mind. It includes content from the following Packt product:

Mastering Spring 5.0 by Ranga Rao Karanam

Meet Your Expert

We have the best work of the following esteemed author to ensure that your learning journey is smooth:

Ranga Rao Karanam is a programmer, trainer, and architect. His areas of interest include cloud native applications, microservices, evolutionary design, high-quality code, DevOps, BDD, TDD, and refactoring. He loves consulting for startups on developing scalable, component-based cloud native applications, and following modern development practices such as BDD, continuous delivery, and DevOps. He loves the freedom the Spring Framework brings to developing enterprise Java applications. Ranga started in28minutes with the vision of creating high-quality courses on developing cloud native Java applications. He is looking forward to enhancing his already considerable success--75,000 students on Udemy and 35,000 subscribers on YouTube.

Preface

Nowadays developers are facing competitive pressures and it impacts the way they build applications, which includes faster delivery, scalability, and high performance. Microservices helps in decomposing applications into small services and move away from a single monolithic artifact. In that case, we can build systems that are scalable, flexible, and high resilient. Spring Boot helps in building such REST-oriented, production-grade microservices.

So, if you want to build microservices with Spring Boot, you are in the right path.

What's in It for Me?

Maps are vital for your journey, especially when you're holidaying in another continent. When it comes to learning, a roadmap helps you in giving a definitive path for progressing towards the goal. So, here you're presented with a roadmap before you begin your journey.

This book is meticulously designed and developed in order to empower you with all the right and relevant information on Spring Boot. We've created this Learning Path for you that consists of three lessons:

Lesson 1, Building Microservices with Spring Boot, covers the basics of Spring Boot and REST services. You will explore different features of Spring Boot and create a few REST services with great tests.

Lesson 2, Extending Microservices, covers how to add features such as exception handling, caching, and internationalization to your application. You will learn the best practices of documenting REST services using Swagger. You will look at the basics of securing your microservices with Spring Security.

Lesson 3, Advanced Spring Boot Features, takes a look at the advanced features in Spring Boot. You will learn how to monitor a microservice with a Spring Boot Actuator. You will then learn how to deploy the microservice to Cloud. You will also learn how to develop more effectively with the developer tools provided by Spring Boot.

What Will I Get from This Book?

Use Spring Initializr to create a basic Spring projectBuild a basic microservice with Spring BootImplement caching and exception handlingSecure your microservice with Spring security and OAuth2Deploy microservices using self-contained HTTP serverMonitor your microservices with Spring Boot actuatorLearn to develop more effectively with developer tools

Prerequisites

This book is aimed at Java developers who knows the basics of Spring programming and want to build microservices with Spring Boot. Some of the prerequisites that is required before you begin this book are:

Working knowledge on JavaBasic knowledge on Spring programming

Chapter 1. Building Microservices with Spring Boot

As we discussed in the last lesson, we are moving toward architectures with smaller, independently deployable microservices. This would mean that there will be a huge number of smaller microservices developed.

An important consequence is that we would need to be able to quickly get off the ground and get running with new components.

Spring Boot aims to solve the problem of getting off fast with a new component. In this lesson, we will start with understanding the capabilities Spring Boot brings to the table. We will answer the following questions:

Why Spring Boot?What are the features that Spring Boot provides?What is auto-configuration?What is Spring Boot not?What happens in the background when you use Spring Boot?How do you use Spring Initializr to create new Spring Boot projects?How do you create basic RESTful services with Spring Boot?

What is Spring Boot?

First of all, let's start with clearing out a few misconceptions about Spring Boot:

Spring Boot is not a code generation framework. It does not generate any code.Spring Boot is neither an application server, nor is it a web server. It provides good integration with different ranges of applications and web servers.Spring Boot does not implement any specific frameworks or specifications.

These questions still remain:

What is Spring Boot?Why has it become so popular in the last couple of years?

To answer these questions, let's build a quick example. Let's consider an example application that you want to quickly prototype.

Building a Quick Prototype for a Microservice

Let's say we want to build a microservice with Spring MVC and use JPA (with Hibernate as the implementation) to connect to the database.

Let's consider the steps in setting up such an application:

Decide which versions of Spring MVC, JPA and Hibernate to use.Set up a Spring context to wire all the different layers together.Set up a web layer with Spring MVC (including Spring MVC configuration):
Configure beans for DispatcherServlet, handler, resolvers, view resolvers, and so on
Set up Hibernate in the data layer:
Configure beans for SessionFactory, data source, and so on
Decide and implement how to store your application configuration, which varies between different environments.Decide how you would want to do your unit testing.Decide and implement your transaction management strategy.Decide and implement how to implement security.Set up your logging framework.Decide and implement how you want to monitor your application in production. Decide and implement a metrics management system to provide statistics about the application.Decide and implement how to deploy your application to a web or application server.

At least a few of the steps mentioned have to be completed before we can start with building our business logic. And this might take a few weeks at the least.

When we build microservices, we would want to make a quick start. All the preceding steps will not make it easy to develop a microservice. And that's the problem Spring Boot aims to solve.

The following quote is an extract from the Spring Boot website (http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-documentation):

Spring Boot makes it easy to create stand-alone, production-grade Spring based applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration

Spring Boot enables developers to focus on the business logic behind their microservice. It aims to take care of all the nitty-gritty technical details involved in developing microservices.

Primary Goals

The primary goals of Spring Boot are as follows:

Enable quickly getting off the ground with Spring-based projects.Be opinionated. Make default assumptions based on common usage. Provide configuration options to handle deviations from defaults.Provide a wide range of nonfunctional features out of the box.Do not use code generation and avoid using a lot of XML configuration.

Nonfunctional Features

A few of the nonfunctional features provided by Spring Boot are as follows:

Default handling of versioning and configuration of a wide range of frameworks, servers, and specificationsDefault options for application securityDefault application metrics with possibilities to extendBasic application monitoring using health checksMultiple options for externalized configuration

Spring Boot Hello World

We will start with building our first Spring Boot application in this lesson. We will use Maven to manage dependencies.

The following steps are involved in starting up with a Spring Boot application:

Configure spring-boot-starter-parent in your pom.xml file.Configure the pom.xml file with the required starter projects.Configure spring-boot-maven-plugin to be able to run the application.Create your first Spring Boot launch class.

Let's start with step 1, configuring the starter projects.

Configure spring-boot-starter-parent

Let's start with a simple pom.xml file with spring-boot-starter-parent:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mastering.spring</groupId> <artifactId>springboot-example</artifactId> <version>0.0.1-SNAPSHOT</version> <name>First Spring Boot Example</name> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M1</version> </parent> <properties> <java.version>1.8</java.version> </properties> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>

The first question is this: why do we need spring-boot-starter-parent?

A spring-boot-starter-parent dependency contains the default versions of Java to use, the default versions of dependencies that Spring Boot uses, and the default configuration of the Maven plugins.

Note

The spring-boot-starter-parent dependency is the parent POM providing dependency and plugin management for Spring Boot-based applications.

Let's look at some of the code inside spring-boot-starter-parent