Microservices Deployment Cookbook - Vikram Murugesan - E-Book

Microservices Deployment Cookbook E-Book

Vikram Murugesan

0,0
39,59 €

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

Mehr erfahren.
Beschreibung

This book will help any team or organization understand, deploy, and manage microservices at scale. It is driven by a sample application, helping you gradually build a complete microservice-based ecosystem. Rather than just focusing on writing a microservice, this book addresses various other microservice-related solutions: deployments, clustering, load balancing, logging, streaming, and monitoring.

The initial chapters offer insights into how web and enterprise apps can be migrated to scalable microservices. Moving on, you’ll see how to Dockerize your application so that it is ready to be shipped and deployed. We will look at how to deploy microservices on Mesos and Marathon and will also deploy microservices on Kubernetes. Next, you will implement service discovery and load balancing for your microservices. We’ll also show you how to build asynchronous streaming systems using Kafka Streams and Apache Spark.

Finally, we wind up by aggregating your logs in Kafka, creating your own metrics, and monitoring the metrics for the microservice.

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

EPUB
MOBI

Seitenzahl: 383

Veröffentlichungsjahr: 2017

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

Microservices Deployment Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Why subscribe?
Customer Feedback
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Building Microservices with Java
Introduction
Creating a project template using STS and Maven
Getting ready
How to do it...
There's more...
Writing microservices with Spring Boot
Getting ready
How to do it...
Writing REST APIs with Spring MVC
Getting ready
How to do it...
Writing microservices with WildFly Swarm
Getting ready
How to do it...
Writing microservices with Dropwizard
Getting ready
How to do it...
Writing REST APIs with SparkJava
Getting ready
How to do it...
Conclusion
2. Containerizing Microservices with Docker
Building an executable JAR using Maven Shade plugin
Getting ready
How to do it...
Building an executable JAR using the Spring Boot Maven plugin
Getting ready
How to do it...
Installing and setting up Docker
Getting ready
How to do it...
Writing your Dockerfile
Getting ready
How to do it...
Building your Docker image
Getting ready
How to do it...
Running your microservice inside a Docker container
Getting ready
How to do it...
Pushing your image to Docker Hub
Getting ready
How to do it...
3. Deploying Microservices on Mesos
Introduction
Setting up a Mesos cluster using Docker
Getting ready
Zookeeper
Mesos masters and Mesos slaves
Mesos frameworks
How to do it...
Understanding the Mesos and Marathon interface
Getting ready
How to do it...
The Mesos interface
The Mesos home page
Frameworks
The Marathon web UI
Deploying your microservice to Mesos using Marathon
Getting ready
How to do it...
Configuring ports in Marathon
Getting ready
How to do it...
Configuring volumes in Marathon
Getting ready
How to do it...
Configuring environment variables in Marathon
Getting ready
How to do it...
Scaling your microservice in Marathon
Getting ready
How to do it...
Destroying your microservice in Marathon
Getting ready
How to do it...
Monitoring your microservice logs in Marathon
Getting ready
How to do it...
Monitoring your microservice logs in Mesos
Getting ready
How to do it...
Managing your microservice using Marathon's REST API
Getting ready
How to do it...
4. Deploying Microservices on Kubernetes
Introduction
Kubernetes master
API server
etcd
Scheduler
Controller manager
Kubernetes node
Setting up Kubernetes cluster using Docker
Getting ready
How to do it...
Understanding the Kubernetes dashboard
Getting ready
How to do it...
Deploying your microservice on Kubernetes
Getting ready
How to do it...
Configuring ports in Kubernetes
Getting ready
How to do it...
Configuring volumes in Kubernetes
Getting ready
How to do it...
Configuring environment variables in Kubernetes
Getting ready
How to do it...
Scaling your microservice in Kubernetes
Getting ready
How to do it...
Destroying your microservice in Kubernetes
Getting ready
How to do it...
Monitoring your microservice logs in Kubernetes
Getting ready
How to do it...
5. Service Discovery and Load Balancing Microservices
Introduction
Setting up Zookeeper using Docker
Getting ready
How to do it...
Load balancing microservices using Zookeeper
Getting ready
How to do it...
Setting up Consul using Docker
Getting ready
How to do it...
Understanding the concepts of Consul
Implementing service discovery using Spring Cloud Consul
Getting ready
How to do it...
Load balancing your microservice using Spring Cloud Consul
Getting ready
How to do it...
Load balancing your microservice using Nginx and Consul
Getting ready
How to do it...
Load balancing your microservice using Marathon LB
How it works...
6. Monitoring Microservices
Introduction
Configuring Spring Boot Actuator metrics
Getting ready
How to do it...
Understanding Spring Boot Actuator metrics
Getting ready
How to do it...
Creating custom metrics using Dropwizard
Getting ready
How to do it...
Setting up Graphite using Docker
Getting ready
How to do it...
Using the Graphite interface
Getting ready
How to do it...
Tree view
Search
Auto-Completer
Graphite
Exporting Dropwizard metrics over to Graphite
Getting ready
How to do it...
Exporting Spring Boot Actuator metrics over to Graphite
Getting ready
How to do it...
Setting up Grafana using Docker
Getting ready
How to do it...
Configuring Grafana to use Graphite
Getting ready
How to do it...
Configuring Grafana dashboards to view metrics
Getting ready
How to do it...
7. Building Asynchronous Streaming Systems with Kafka and Spark
Introduction
Setting up Kafka using Docker
Kafka
Point-to-point mechanism
Pub-sub mechanism
Kafka terminology
Brokers
Topics
Partitions
Producers and consumers
Getting ready
How to do it...
Creating Kafka topics to stream data
Getting ready
How to do it...
Writing a streaming program using Kafka Streams
Getting ready
How to do it...
Improving the performance of the Kafka Streams program
Getting ready
How to do it...
Writing a streaming program using Apache Spark
Getting ready
How to do it...
Improving the performance of the Spark job
How to do it...
Aggregating logs into Kafka using Log4J
Getting ready
How to do it...
Integrating Kafka with log management systems
How it works...
8. More Clustering Frameworks - DC/OS, Docker Swarm, and YARN
Introduction
Deploying infrastructure with DC/OS
Getting ready
How to do it...
Deploying containers with Docker Swarm
Getting ready
How to do it...
Deploying containers on YARN
Getting ready
How it works...

Microservices Deployment Cookbook

Microservices Deployment Cookbook

Copyright © 2017 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: January 2017

Production reference: 1240117

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham 

B3 2PB, UK.

ISBN 978-1-78646-943-4

www.packtpub.com

Credits

Author

Vikram Murugesan

Copy Editor

Madhusudan Uchil

Reviewer

Kishore Kumar Yekkanti

Project Coordinator

Judie Jose

Commissioning Editor

Kartikey Pandey

Proofreader

Safis Editing

Acquisition Editor

Vijin Boricha

Indexer

Pratik Shirodkar

Content Development Editor

Amedh Gemraram Pohad

Graphics

Kirk D'Penha

Technical Editors

Prashant Chaudhari

Vishal Kamal Mewada

Production Coordinator

Melwyn Dsa

  

About the Author

Vikram Murugesan is a software architect who has over 10 years of experience building distributed systems and products. He currently works as a principal architect with Egen Solutions Inc. In his current job, he focuses on building platforms based on JVM (Java, Scala, and Groovy), big data, and cloud technologies. He is a passionate programmer and is interested in learning new technologies. He is also interested in coaching, mentoring, and building scalable teams that build great software.

I would like to take a moment to thank everyone that has been a huge support during the course of writing this book. Firstly, thanks to Mr. Raghu Potini, who motivated me to write this book and has been supportive throughout the writing process. Without his support and motivation, this book would have not been possible. Secondly, I would like to thank Mr. Andrew Leasck, who has been my inspiration since the beginning of my career. When I started writing the book, I did not know much about the publishing process or the amount of team work it needs. While working with the Packt Publishing team, they made me realize that it requires enormous amount of team effort, coordination and patience. The Packt Publishing team made it look so simple, but behind the scenes, they put so much effort and thoughts into giving life to this book. Without them, this would have not been possible. Hats off to the Packt team members that helped me during this process. Everyone has a role model in life. My father has always been my role model and an inspiration. Thanks to my father, Mr. Murugesan, who would have been really proud about this book. Special thanks to my mother, Vijayarani, wife, Subamalar, and daughther, Sreesha, who have been very patient and supportive during the course of writing this book.

About the Reviewer

Kishore Kumar Yekkanti is an seasoned developer who worked across various domains and technologies over the past 10 years. He played key roles in various product and agile consulting companies such as Stayzilla, Thoughtworks, and, currently at CurrencyFair. His domain expertise spans the finance, supply chain, e-commerce, cloud, infrastructure management, health, retail, ICT4D, and entertainment industries. He is passionate about open source software and is a core contributor to many humanitarian open source projects. His current focus is on scaling microservices in highly distributed applications that are deployed using container-based systems in the cloud. Kishore is also a core reviewer for another microservices book called Developing Microservices with Node.js.

I would like thank my wife, Jyothsna, and my daughter, Dhruti, who put up with me all along irrespective of my crazy schedules.

www.PacktPub.com

For support files and downloads related to your book, please visit www.PacktPub.com.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

https://www.packtpub.com/mapt

Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

Why subscribe?

Fully searchable across every book published by PacktCopy and paste, print, and bookmark contentOn demand and accessible via a web browser

Customer Feedback

Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously—that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn.

You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: [email protected].

Preface

The goal of this book is to introduce you to some of the most popular and newest technologies and frameworks that will help you build and deploy microservices at scale. With the current evolution in this space, it is really difficult to keep up with all the new frameworks and tools. If you are an open source fan like me, you would already know that you have to spend a lot of time in trying out these new frameworks and libraries in order to understand their potential and the exact problem that they are trying to solve. Of course, each framework would have been built for a specific purpose, and you will often end up in a situation where you don’t have a silver bullet for all your microservice concerns. In this book, you will learn some of the most commonly used frameworks and technologies that help you build and deploy microservices at scale.

Throughout this book, we will be sticking to a specific application and will try to build upon that application. For example, we will be using the same application to configure service discovery, monitoring, streaming, log management, and load balancing. So by the end of this book, you will have a fully loaded microservice that demonstrates every aspect of a microservice.

This book covers several libraries and frameworks that help you build and deploy microservices. After reading this book, you will not be an expert on all of them, but you will know where to start and how to proceed. That’s the whole intention of this book. I hope you'll like it. Good luck microservicing!

What this book covers

Chapter 1, Building Microservices with Java, shows you how to build Java-based RESTful microservices using frameworks such as Spring Boot, Wildfly Swarm, Dropwizard, and Spark Java . This chapter will also show you how to write RESTful APIs using Spring MVC and Spark Java.

Chapter 2, Containerizing Microservices with Docker, shows you how to package your application using Maven plugins such as the Maven Shade plugin and Spring Boot Maven plugin. This chapter will also show you how to install Docker on your local computer. You will also learn how to containerize your application using Docker and later push your microservice’s Docker image to the public Docker Hub.

Chapter 3, Deploying Microservices on Mesos, shows you how to orchestrate a Dockerized Mesos cluster with Marathon on your local machine. You will also learn how to deploy your Dockerized microservice to a Mesos cluster using Marathon. Later, you will learn how to scale your microservice; configure ports, volumes, and environment variables; and view container logs in Marathon. Finally you will learn how to use Marathon's REST API for managing your microservice.

Chapter 4, Deploying Microservices on Kubernetes, shows you how to orchestrate a Dockerized Kubernetes cluster using Minikube on your local machine. You will also learn how to deploy your Dockerized microservice to a Kubernetes cluster using the Kubernetes dashboard as well as kubectl. Later, you will learn how to scale your microservice; configure ports, volumes, and environment variables; and view container logs in Kubernetes using the dashboard as well as kubectl.

Chapter 5, Service Discovery and Load Balancing Microservices, shows you how you to run a Dockerized Zookeeper instance on your local machine. You will learn how to implement service discovery and load balancing using Zookeeper. This chapter also introduces you to Consul, where you will be running a Dockerized Consul instance on your local machine. Later, you will learn how to implement service discovery and load balancing using Consul and Spring Cloud. You will also learn how to implement service discovery and load balancing using Consul and Nginx.

Chapter 6, Monitoring Microservices, shows you how to configure Spring Boot Actuator and gives you an overview of all the metrics that are exposed by Spring Boot Actuator. You will also learn how to create your own metrics using the Dropwizard metrics library and later expose them via Spring Boot Actuator. Later, you will learn how to run a Dockerized Graphite instance on your local machine. The metrics that you created using Dropwizard will then be published to Graphite. Finally, you will learn how to run a Dockerized Grafana instance on your local machine and then use it to expose your metrics in the form of dashboards.

Chapter 7, Building Asynchronous Streaming Systems with Kafka and Spark, shows you how to set up and run a Dockerized Kafka broker on your local machine. You will learn how to create topics in Kafka and build Kafka Streams application in our microservice that will stream data asynchronously. You will build a similar Spark Streaming job that will have the ability to stream data asynchronously. You will get an overview of improving the performance of your streaming application. Later, you will learn how to aggregate your application logs into a Kafka topic and then explore the possibilities of integrating it with popular log-management systems.

Chapter 8, More Clustering Frameworks - DC/OS, Docker Swarm, and YARN, will give you an overview of other popular clustering frameworks in the market. You will get a high-level idea of Mesosphere’s DC/OS, Docker Swarm, and Apache YARN. You will also get to see how DC/OS and Docker Swarm can be used to deploy microservices on a larger scale.

What you need for this book

You will need the following software and hardware to execute the recipes on this book.

Hardware:

Desktop or laptop with at least 16 GB memory and a 4-core CPU

Software:

Java Development KitApache MavenSpring Tool Suite

Who this book is for

This book is for Java developers that would like to learn how to build microservices, deploy them on a clustered environment, monitor them, and manage them at scale. Familiarity with Java is a plus, as most of the recipes in this book are based on Java.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Start the GeoLocationApplication.java class as a Spring Boot application from your STS IDE."

A block of code is set as follows:

<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-all</artifactId> <version>1.1.2.RELEASE</version> </dependency> -->

Any command-line input or output is written as follows:

curl -H "Content-Type: application/json" -X GET http://localhost:8080/geolocation

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "The next button that we would want to use most of the time is the Short URL button."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

Log in or register to our website using your e-mail address and password.Hover the mouse pointer on the SUPPORT tab at the top.Click on Code Downloads & Errata.Enter the name of the book in the Search box.Select the book for which you're looking to download the code files.Choose from the drop-down menu where you purchased this book from.Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR / 7-Zip for WindowsZipeg / iZip / UnRarX for Mac7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Microservices-Deployment-Cookbook. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.

Chapter 1. Building Microservices with Java

In this chapter, we will cover the following recipes:

Creating a project template using STS and MavenWriting microservices with Spring BootWriting REST APIs with Spring MVCWriting microservices with WildFly SwarmWriting microservices with DropwizardWriting REST APIs with SparkJava

Microservices have gained a lot of traction recently. A microservice-based architecture is one way of designing your software. In such an architecture, applications are broken down into smaller services so that they can be deployed and managed separately. This takes away a lot of pain points that occur in traditional monolithic applications. With that being said, microservices can be built using any programming language. In fact, there are many libraries and frameworks that help programmers build microservices using Java, Scala, C#, JavaScript, Python, Ruby, and so on. In this book, we will focus more on building and deploying microservices with Java.

Introduction

In a traditional microservice-based design, monolithic applications will be broken down into smaller services that can talk to other services either in a synchronous or asynchronous model, based on the need and use case. The first question that anyone would have when breaking down monolithic applications is "what are the potential services that my application can be broken down into?" There is no rule of thumb or straight-forward answer to this. But usually, one looks for independent functionalities. Each and every functionality can be considered to be built as its own service.

To illustrate this, let's take a look at an example application and see how it could be broken down into smaller, manageable and deployable microservices. The sample application we will be looking at is a biker tracking application. This application will have the following functionalities:

Web interface to monitor the user's progress on a mapREST API to consume the user's geolocation data constantlyAnalytics code to perform calculations for biking route suggestions, weather predictions, biking gear suggestions, calories burnt, water intake, and so on

Let's take a look at how this application might have been designed as a monolithic application:

As you can see, the whole application is bundled as one artifact and therefore promotes a single point of failure (SPOF). If for some reason the analytics code crashes your JVM, we will lose the web interface, REST APIs, and analytics as a whole. Now, let's take a look at how this might be broken down into manageable microservices:

In this architecture diagram, you can see that each and every functionality is deployed as its own microservice. The service implementations have been broken down into a Notification Service, which will take care of sending notifications to the users, and the Geo Location Tracker Service, which keeps track of the geolocation (latitude and longitude) information of all the users. The Analytics code has been broken down into its own microservices. So if one type of analytics microservice goes down, the other microservices will keep functioning properly. You might have noticed that the REST APIs are missing. They are actually not missing, but integrated into their respective microservices.

Now let's not waste any more time and jump directly into building one part of this application. To be able to illustrate the extensive concepts that this book offers, I have chosen the geolocation tracker service as our example microservice. This service will be responsible for collecting the geolocation of all users of this application and then storing them in a data store.

Creating a project template using STS and Maven

Creating a project for your microservice is no different than creating a simple Java project. We will use Maven as our build framework as it is considered to be one of the most popular build frameworks. If you are comfortable using other frameworks, such as Gradle, SBT, or Ivy, feel free to use them. But keep in mind that the recipes throughout this book will use Maven extensively. Unless you are an expert in your preferred framework, I strongly recommend using Maven.

Getting ready

In order to create your microservice project, you will need the following software. Follow the instructions on their respective websites to install them:

JDK 1.8+Maven 3.3.9+Spring Tool Suite (STS) 3.8.0+

Make sure both Java and Maven are in your PATH variable so that you can use the java and mvn commands on every terminal shell without having to set PATH each time. Spring Tool Suite is a sophisticated version of Eclipse that has lot of Spring plugins and extensions. If you are familiar with other IDEs, feel free to use them. But for familiarity, this book will use STS for all recipes.

How to do it...

After you have installed the above-mentioned software, open Spring Tool Suite. The first time you open it, you will be requested to choose a workspace. Go ahead and enter your workspace location. In this recipe, we will learn how to create a template Maven project using STS and Maven. STS comes with Maven Integration out of the box. So we don't have to configure it any further. After your STS IDE has completed startup, follow the below instructions to create a new Maven project:

In your STS window, right-click anywhere on the Package Explorer, select New, and then select Maven Project, as shown in the following screenshot:This will open a popup that will let you chose the type of Maven project you would like to create. We will skip the archetype selection by checking the box that says Create a simple project (skip archetype selection) and then hit Next:In the next window, enter the following details to create your project:
Group Id: com.packt.microservicesArtifact Id: geolocationName: geolocation
After you have entered the details, hit Finish:This will create a simple Maven JAR module with all the required directories in place. Depending on your IDE settings, STS configures your new project with the default Java version. If you have not set any defaults, it will configure your project with Java 1.5. You can verify this by checking your project structure in STS. The following screenshot shows that STS uses Java 1.5 for your project:We will use Java 8's lambda expressions in other chapters. So let's change the Java version from 1.5 to 1.8. In order to change the Java version, we will configure the maven-compiler-plugin in the pom.xmlfile. Add the following section of code to your pom.xml file's project section: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> Save your pom.xml file, right-click on your project, choose Maven, and then hit Update Project... or use the keyboard shortcut Alt + F5. This will automatically change your project's Java version to 1.8.Our microservice project is now ready to play with.

There's more...

If you are more comfortable using the command line to create Maven projects, issue the following command in your terminal to create the new project:

mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes \ -DgroupId=com.packt.microservices -DartifactId=geolocation \ -Dname=geolocation

After Maven creates the project, you should be able to import your project into your IDE. As this is something out of the scope of this book, we will not be looking at how to import an existing Maven project into your IDE.

Writing microservices with Spring Boot

Now that our project is ready, let's look at how to write our microservice. There are several Java-based frameworks that let you create microservices. One of the most popular frameworks from the Spring ecosystem is the Spring Boot framework. In this recipe, we will look at how to create a simple microservice application using Spring Boot.

Getting ready

Any application requires an entry point to start the application. For Java-based applications, you can write a class that has the main method and run that class as a Java application. Similarly, Spring Boot requires a simple Java class with the main method to run it as a Spring Boot application (microservice). Before you start writing your Spring Boot microservice, you will also require some Maven dependencies in your pom.xml file.

How to do it...

Create a Java class called com.packt.microservices.geolocation.GeoLocationApplication.java and give it an empty main method: package com.packt.microservices.geolocation;    public class GeoLocationApplication {    public static void main(String[] args) {    // left empty intentionally    }    } Now that we have our basic template project, let's make our project a child project of Spring Boot's spring-boot-starter-parent pom module. This module has a lot of prerequisite configurations in its pom.xml file, thereby reducing the amount of boilerplate code in our pom.xml file. At the time of writing this, 1.3.6.RELEASE was the most recent version: <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.3.6.RELEASE</version>    </parent> After this step, you might want to run a Maven update on your project as you have added a new parent module. If you see any warnings about the version of the maven-compiler  plugin, you can either ignore it or just remove the <version>3.5.1</version> element. If you remove the version element, please perform a Maven update afterward.Spring Boot has the ability to enable or disable Spring modules such as Spring MVC, Spring Data, and Spring Caching. In our use case, we will be creating some REST APIs to consume the geolocation information of the users. So we will need Spring MVC. Add the following dependencies to your pom.xml file: <dependencies>    <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>    </dependency>    </dependencies> We also need to expose the APIs using web servers such as Tomcat, Jetty, or Undertow. Spring Boot has an in-memory Tomcat server that starts up as soon as you start your Spring Boot application. So we already have an in-memory Tomcat server that we could utilize.Now let's modify the GeoLocationApplication.java class to make it a Spring Boot application: package com.packt.microservices.geolocation;    import org.springframework.boot.SpringApplication;    import org.springframework.boot.autoconfigure .SpringBootApplication;    @SpringBootApplication    public class GeoLocationApplication {    public static void main(String[] args) {     SpringApplication.run(GeoLocationApplication.class, args);    }    }

As you can see, we have added an annotation, @SpringBootApplication, to our class. The @SpringBootApplication annotation reduces the number of lines of code written by adding the following three annotations implicitly:

@Configuration@ComponentScan@EnableAutoConfiguration

If you are familiar with Spring, you will already know what the first two annotations do. @EnableAutoConfiguration is the only annotation that is part of Spring Boot. The AutoConfiguration package has an intelligent mechanism that guesses the configuration of your application and automatically configures the beans that you will likely need in your code.

You can also see that we have added one more line to the main method, which actually tells Spring Boot the class that will be used to start this application. In our case, it is GeoLocationApplication.class. If you would like to add more initialization logic to your application, such as setting up the database or setting up your cache, feel free to add it here.

Now that our Spring Boot application is all set to run, let's see how to run our microservice. Right-click on GeoLocationApplication.java from Package Explorer, select Run As, and then select Spring Boot App. You can also choose Java Application instead of Spring Boot App. Both the options ultimately do the same thing. You should see something like this on your STS console:If you look closely at the console logs, you will notice that Tomcat is being started on port number 8080. In order to make sure our Tomcat server is listening, let's run a simple curl command. cURL is a command-line utility available on most Unix and Mac systems. For Windows, use tools such as Cygwin or even Postman. Postman is a Google Chrome extension that gives you the ability to send and receive HTTP requests. For simplicity, we will use cURL. Execute the following command on your terminal: curl http://localhost:8080This should give us an output like this: {"timestamp":1467420963000,"status":404,"error":"Not Found","message":"No message available","path":"/"}

This error message is being produced by Spring. This verifies that our Spring Boot microservice is ready to start building on with more features. There are more configurations that are needed for Spring Boot, which we will perform later in this chapter along with Spring MVC.

Writing microservices with WildFly Swarm

WildFly Swarm is a J2EE application packaging framework from RedHat that utilizes the in-memory Undertow server to deploy microservices. In this recipe, we will create the same GeoLocation API using WildFly Swarm and JAX-RS.

To avoid confusion and dependency conflicts in our project, we will create the WildFly Swarm microservice as its own Maven project. This recipe is just here to help you get started on WildFly Swarm. When you are building your production-level application, it is your choice to either use Spring Boot, WildFly Swarm, Dropwizard, or SparkJava based on your needs.

Getting ready

Similar to how we created the Spring Boot Maven project, create a Maven WAR module with the groupId com.packt.microservices and name/artifactId geolocation-wildfly. Feel free to use either your IDE or the command line. Be aware that some IDEs complain about a missing web.xml file. We will see how to fix that in the next section.

How to do it...

Before we set up the WildFly Swarm project, we have to fix the missing web.xml error. The error message says that Maven expects to see a web.xml file in your project as it is a WAR module, but this file is missing in your project. In order to fix this, we have to add and configure maven-war-plugin. Add the following code snippet to your pom.xml file's project section: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> After adding the snippet, save your pom.xml file and perform a Maven update. Also, if you see that your project is using a Java version other than 1.8, follow the Creating a project template using STS and Maven recipe to change the Java version to 1.8. Again, perform a Maven update for the changes to take effect.