Building Microservices with Spring
eBook - ePub

Building Microservices with Spring

Master design patterns of the Spring framework to build smart, efficient microservices

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

Building Microservices with Spring

Master design patterns of the Spring framework to build smart, efficient microservices

Book details
Book preview
Table of contents
Citations

About This Book

Learn and use the design patterns and best practices in Spring to solve common design problems and build user-friendly microservices

Key Features

  • Study the benefits of using the right design pattern in your toolkit
  • Manage your code easily with Spring's dependency injection pattern
  • Explore the features of Docker and Mesos to build successful microservices

Book Description

Getting Started with Spring Microservices begins with an overview of the Spring Framework 5.0, its design patterns, and its guidelines that enable you to implement responsive microservices at scale. You will learn how to use GoF patterns in application design. You will understand the dependency injection pattern, which is the main principle behind the decoupling process of the Spring Framework and makes it easier to manage your code. Then, you will learn how to use proxy patterns in aspect-oriented programming and remoting. Moving on, you will understand the JDBC template patterns and their use in abstracting database access.

After understanding the basics, you will move on to more advanced topics, such as reactive streams and concurrency. Written to the latest specifications of Spring that focuses on Reactive Programming, the Learning Path teaches you how to build modern, internet-scale Java applications in no time.

Next, you will understand how Spring Boot is used to deploying serverless autonomous services by removing the need to have a heavyweight application server. You'll also explore ways to deploy your microservices to Docker and managing them with Mesos.

By the end of this Learning Path, you will have the clarity and confidence for implementing microservices using Spring Framework.

This Learning Path includes content from the following Packt products:

  • Spring 5 Microservices by Rajesh R V
  • Spring 5 Design Patterns by Dinesh Rajput

What you will learn

  • Develop applications using dependency injection patterns
  • Build web applications using traditional Spring MVC patterns
  • Utilize the reactive programming pattern to build reactive web apps
  • Learn concurrency and handle multiple connections inside a web server
  • Use Spring Boot and Spring Cloud to develop microservices
  • Leverage reactive programming to build cloud-native applications

Who this book is for

Getting Started with Spring Microservices is ideal for Spring developers who want to use design patterns to solve common design problems and build cloud-ready, Internet-scale applications, and simple RESTful services.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
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 1000+ topics, we’ve got you covered! Learn more here.
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 here.
Yes, you can access Building Microservices with Spring by Dinesh Rajput, Rajesh R V in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781789958188
Edition
1

Building Microservices with Spring Boot

Developing microservices is not so tedious anymore--thanks to the powerful Spring Boot framework. Spring Boot is a framework for developing production-ready microservices in Java.
This chapter will move from the microservices theory explained in the previous chapters to hands-on practice by reviewing code samples. Here, we will introduce the Spring Boot framework, and explain how Spring Boot can help building RESTful microservices inline with the principles and characteristics discussed in the previous chapter. Finally, some of the features offered by Spring Boot for making the microservices production ready will be reviewed.
At the end of this chapter, you will have learned about the following:
  • Setting up the latest Spring development environment
  • Developing RESTful services using Spring Framework 5 and Spring Boot
  • Building reactive microservices using Spring WebFlux and Spring Messaging
  • Securing microservices using Spring Security and OAuth2
  • Implementing cross-origin microservices
  • Documenting Spring Boot microservices using Swagger
  • Spring Boot Actuator for building production-ready microservices

Setting up a development environment

To crystalize microservices' concepts, a couple of microservices will be built. For that, it is assumed that the following components are installed:
  • JDK 1.8 (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
  • Spring Tool Suite 3.8.2 (STS) (https://spring.io/tools/sts/all)
  • Maven 3.3.1 (https://maven.apache.org/download.cgi)
Alternately, other IDEs like IntelliJ IDEA/NetBeans/Eclipse could be used. Similarly, alternate build tools like Gradle can be used. It is assumed that Maven repository, class path, and other path variables are set properly for running STS and Maven projects.
This chapter is based on the following versions of Spring libraries:
  • Spring Framework 5.0.0.RC1
  • Spring Boot 2.0.0. M1
The focus of this chapter is not to explore the full features of Spring Boot, but to understand some of the essential and important features of Spring Boot, required when building microservices.

Spring Boot for building RESTful microservices

Spring Boot is a utility framework from the Spring team for bootstrapping Spring-based applications and microservices quickly and easily. The framework uses an opinionated approach over configurations for decision making, thereby reducing the effort required on writing a lot of boilerplate code and configurations. Using the 80-20 principle, developers should be able to kick start a variety of Spring applications with many default values. Spring Boot further presents opportunities to the developers for customizing applications by overriding auto-configured values.
Spring Boot not only increases the speed of development, but also provides a set of production-ready ops features such as health checks and metrics collections. Since Spring Boot masks many configuration parameters and abstracts many lower level implementations, it minimizes the chances of errors to a certain extent. Spring Boot recognizes the nature of the application based on the libraries available in the classpath, and runs the auto-configuration classes packaged in those libraries.
Often, many developers mistakenly see Spring Boot as a code generator, but, in reality, it is not. Spring Boot only auto-configures build files, for example, pom files in the case of Maven. It also sets properties, such as data source properties, based on certain opinionated defaults.
Consider the following dependencies in the file pom.xml:
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
For instance, in the preceding case, Spring Boot understands that the project is set to use the Spring Data JPA and HSQL database. It automatically configures the driver class and other connection parameters.
One of the great outcomes of Spring Boot is that it almost eliminates the need to have traditional XML configurations. Spring Boot also enables microservices development by packaging all the required runtime dependencies into a fat executable jar.

Getting started with Spring Boot

These are the different ways in which Spring Boot-based application development can be started:
  • By using Spring Boot CLI as a command-line tool
  • By using IDEs like Spring Tool Suite (STS), which provide Spring Boot, supported out of the box
  • By using the Spring Initializr project at http://start.spring.io
  • By using SDKMAN! (The Software Development Kit Manager) from http://sdkman.io
The first three options will be explored in this chapter, developing a variety of example services.

Developing a Spring Boot microservice

The easiest way to develop and demonstrate Spring Boot's capabilities is by using the Spring Boot CLI, a command-line tool.
The following are the steps to set up and run Spring Boot CLI:
  1. Install the Spring Boot command-line tool by downloading the spring-boot-cli-2.0.0.BUILD-M1-bin.zip file from the following location URL:
    https://repo.spring.io/milestone/org/springframework/boot/spring-boot-cli/2.0.0.M1/
  1. Unzip the file into a directory of choice. Open a terminal window, and change the terminal prompt to the bin folder.
Ensure that the /bin folder is added to the system path so that Spring Boot can be run from any location. Otherwise, execute from the bin folder by using the command ./spring.
  1. Verify the installation with the following command. If successful, the Spring CLI version will be printed on the console as shown:
 $spring –-version
Spring CLI v2.0.0.M1
  1. As the next step, a quick REST service will be developed in groovy, which is supported out of the box in Spring Boot. To do so, copy and paste the following code using any editor of choice, and save it as myfirstapp.groovy into any folder:
 @RestController
class HelloworldController {
@RequestMapping("/")
String sayHello(){
return "Hello World!"
}
}
  1. In order to run this groovy application, go to the folder where myfirstapp.groovy is saved, and execute the following command. The last few lines of the server startup log will be as shown in the following command snippet:
 $spring ...

Table of contents

  1. Title Page
  2. Copyright
  3. About Packt
  4. Contributors
  5. Preface
  6. Getting Started with Spring Framework 5.0 and Design Patterns
  7. Overview of GOF Design Patterns - Core Design Patterns
  8. Wiring Beans using the Dependency Injection Pattern
  9. Spring Aspect Oriented Programming with Proxy and Decorator pattern
  10. Accessing a Database with Spring and JDBC Template Patterns
  11. Improving Application Performance Using Caching Patterns
  12. Implementing Reactive Design Patterns
  13. Implementing Concurrency Patterns
  14. Demystifying Microservices
  15. Related Architecture Styles and Use Cases
  16. Building Microservices with Spring Boot
  17. Scale Microservices with Spring Cloud Components
  18. Logging and Monitoring Microservices
  19. Containerizing Microservices with Docker
  20. Scaling Dockerized Microservices with Mesos and Marathon
  21. Other Books You May Enjoy