Hands-On Microservices with Node.js: Build, test, and deploy robust microservices in JavaScript
()
About this ebook
Learn essential microservices concepts while developing scalable applications with Express, Docker, Kubernetes, and Docker Swarm using Node 10
Key FeaturesWrite clean and maintainable code with JavaScript for better microservices developmentDive into the Node.js ecosystem and build scalable microservices with Seneca, Hydra, and Express.jsDevelop smart, efficient, and fast enterprise-grade microservices implementationBook Description
Microservices enable us to develop software in small pieces that work together but can be developed separately; this is one reason why enterprises have started embracing them. For the past few years, Node.js has emerged as a strong candidate for developing microservices because of its ability to increase your productivity and the performance of your applications.
Hands-On Microservices with Node.js is an end-to-end guide on how to dismantle your monolithic application and embrace the microservice architecture - right from architecting your services and modeling them to integrating them into your application. We'll develop and deploy these microservices using Docker. Scalability is an important factor to consider when adding more functionality to your application, and so we delve into various solutions, such as Docker Swarm and Kubernetes, to scale our microservices. Testing and deploying these services while scaling is a real challenge; we'll overcome this challenge by setting up deployment pipelines that break up application build processes in several stages. Later on, we'll take a look at serverless architecture for our microservices and its benefits against traditional architecture. Finally, we share best practices and several design patterns for creating efficient microservices.
What you will learnLearn microservice conceptsExplore different service architectures, such as Hydra and SenecaUnderstand how to use containers and the process of testingUse Docker and Swarm for continuous deployment and scalingLearn how to geographically spread your microservicesDeploy a cloud-native microservice to an online providerKeep your microservice independent of online providersWho this book is for
This book is for JavaScript developers seeking to utilize their skills to build microservices and move away from the monolithic architecture. Prior knowledge of Node.js is assumed.
Diogo Resende is a developer with more than 15 years of experience. He has worked with Node.js almost from the beginning. His computer education and experience in many industries and telecommunication projects have given him a wide background knowledge of other architecture components and approaches that influence the overall performance of an application.
Diogo Resende
Diogo Resende is a passionate developer obsessed with perfection in everything he works on. He loves everything about the Internet of Things, which is the ability to connect everything together and always be connected to the world. He studied computer science and graduated in engineering. At that time, he deepened his knowledge of computer networking and security, software development, and cloud computing. Over the past 10 years, Diogo has embraced different challenges to develop applications and services to connect people with embedded devices around the world, building a bridge between old and uncommon protocols and the Internet of today. ThinkDigital has been his employer and a major part of his life for the last few years. It offers services and expertise in areas such as computer networking and security, automation, smart metering, and fleet management and intelligence. Diogo has also published many open source projects. You can find them all, with an MIT license style, on his personal GitHub page under the username dresende.
Read more from Diogo Resende
Node.js High Performance Rating: 0 out of 5 stars0 ratings
Related to Hands-On Microservices with Node.js
Related ebooks
Docker and Kubernetes for Java Developers Rating: 0 out of 5 stars0 ratingsSails.js Essentials Rating: 0 out of 5 stars0 ratingsHands-On Microservices with Kubernetes: Build, deploy, and manage scalable microservices on Kubernetes Rating: 0 out of 5 stars0 ratingsDeveloping Microservices with Node.js Rating: 0 out of 5 stars0 ratingsBeginning DevOps with Docker: Automate the deployment of your environment with the power of the Docker toolchain Rating: 0 out of 5 stars0 ratingsJavaScript Cloud Native Development Cookbook: Deliver serverless cloud-native solutions on AWS, Azure, and GCP Rating: 0 out of 5 stars0 ratingsSpring 5.0 By Example: Grasp the fundamentals of Spring 5.0 to build modern, robust, and scalable Java applications Rating: 0 out of 5 stars0 ratingsCloud Native Python: Build and deploy resilent applications on the cloud using microservices, AWS, Azure and more Rating: 0 out of 5 stars0 ratingsMicroservices Development Cookbook: Design and build independently deployable modular services Rating: 0 out of 5 stars0 ratingsTypeScript Microservices: Build, deploy, and secure Microservices using TypeScript combined with Node.js Rating: 0 out of 5 stars0 ratingsDeveloping with Docker Rating: 0 out of 5 stars0 ratingsLearn Qt 5: Build modern, responsive cross-platform desktop applications with Qt, C++, and QML Rating: 0 out of 5 stars0 ratingsDocker for Serverless Applications: Containerize and orchestrate functions using OpenFaas, OpenWhisk, and Fn Rating: 0 out of 5 stars0 ratingsNext.js Quick Start Guide: Server-side rendering done right Rating: 0 out of 5 stars0 ratingsCloud Native programming with Golang: Develop microservice-based high performance web apps for the cloud with Go Rating: 0 out of 5 stars0 ratingsNode.js for Beginners: A comprehensive guide to building efficient, full-featured web applications with Node.js Rating: 0 out of 5 stars0 ratingsMobX Quick Start Guide: Supercharge the client state in your React apps with MobX Rating: 0 out of 5 stars0 ratingsVuex Quick Start Guide: Centralized State Management for your Vue.js applications Rating: 0 out of 5 stars0 ratingsMastering Microservices with Java: Build enterprise microservices with Spring Boot 2.0, Spring Cloud, and Angular, 3rd Edition Rating: 0 out of 5 stars0 ratingsMicroservices with Clojure: Develop event-driven, scalable, and reactive microservices with real-time monitoring Rating: 0 out of 5 stars0 ratings
Programming For You
SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Windows 11 For Dummies Rating: 0 out of 5 stars0 ratingsExcel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsBeginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Java: Programming: Your Step by Step Guide to Easily Learn Java in 7 Days Rating: 4 out of 5 stars4/5The Most Concise Step-By-Step Guide To ChatGPT Ever Rating: 3 out of 5 stars3/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsCoding with JavaScript For Dummies Rating: 0 out of 5 stars0 ratingsBlender 3D Printing Essentials Rating: 0 out of 5 stars0 ratingsC All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5
Reviews for Hands-On Microservices with Node.js
0 ratings0 reviews
Book preview
Hands-On Microservices with Node.js - Diogo Resende
Hands-On Microservices with Node.js
Build, test, and deploy robust microservices in JavaScript
Diogo Resende
BIRMINGHAM - MUMBAI
Hands-On Microservices with Node.js
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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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.
Commissioning Editor: Kunal Chaudhari
Acquisition Editor: Nigel Fernandes
Content Development Editor: Arun Nadar
Technical Editor: Leena Patil
Copy Editor: Safis Editing
Project Coordinator: Sheejal Shah
Proofreader: Safis Editing
Indexer: Tejal Daruwale Soni
Graphics: Jason Monteiro
Production Coordinator: Deepika Naik
First published: June 2018
Production reference: 1270618
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78862-021-5
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
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 service@packtpub.com 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.
Contributors
About the author
Diogo Resende is a developer with more than 15 years of experience. He has worked with Node.js almost from the beginning. His computer education and experience in many industries and telecommunication projects have given him a wide background knowledge of other architecture components and approaches that influence the overall performance of an application.
About the reviewer
Bruno Joseph D'mello works at Accion labs as a senior software developer. He has 6 years of experience in web application development, in domains such as entertainment, social media, enterprise, and IT services. Bruno follows Kaizen and enjoys the freedom of architecting new things on the web. He has also contributed some of his knowledge by authoring books such as Web Development in Node.js and MongoDB - Second Edition, What You Need to Know about Node.js, and JavaScript and JSON Essentials.
Thanks to my family for their patience and encouragement.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Table of Contents
Title Page
Copyright and Credits
Hands-On Microservices with Node.js
PacktPub.com
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Conventions used
Get in touch
Reviews
The Age of Microservices
Introducing microservices
Introducing Node.js
Modules
Arrow functions
Classes
Promises and async/await
Spread and rest syntax
Default function parameters
Destructuring
Template literals
Advantages of using Node.js
Node.js Package Manager
Asynchronous I/O
Community
From monolith to microservices
Patterns of microservices
Decomposable
Autonomous
Scalable
Communicable
Disadvantages of microservices
Summary
Modules and Toolkits
Express
Micro
Seneca
Hydra
Summary
Building a Microservice
Using Express
Uploading images
Checking an image exists in the folder
Downloading images
Using route parameters
Generating thumbnails
Playing around with colors
Refactor routes
Manipulating images
Using Hydra
Using Seneca
Plugins
Summary
State and Security
State
Storing state
MySQL
RethinkDB
Redis
Conclusion
Security
Summary
Testing
Types of testing methodologies
Using frameworks
Integrating tests
Using chai
Adding code coverage
Covering all code
Mocking our services
Summary
Deploying Microservices
Using virtual machines
Using containers
Deploying using Docker
Creating images
Defining a Dockerfile
Managing containers
Cleaning containers
Deploying MySQL
Using Docker Compose
Mastering Docker Compose
Summary
Scaling, Sharding, and Replicating
Scaling your network
Replicating our microservice
Deploying to swarm
Creating services
Running our service
Sharding approach
Replicating approach
Sharding and replicating
Moving to Kubernetes
Deploying with Kubernetes
Summary
Cloud-Native Microservices
Preparing for cloud-native
Going cloud-native
Creating a new project
Deploying a database service
Creating a Kubernetes cluster
Creating our microservice
Deploying our microservice
Summary
Design Patterns
Choosing patterns
Architectural patterns
Front Controller
Layered
Service Locator
Observer
Publish-Subscribe
Using patterns
Planning your microservice
Obstacles when developing
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
Preface
This book is an end-to-end guide on how to split your monolithic Node.js application into several microservices. We'll cover some of the toolkits available, such as Express, Hydra, and Seneca, and create a simple microservice. We'll introduce you to continuous integration using Mocha to add a test suite, we'll use chai to test the HTTP interface, and we'll use nyc to see the test coverage.
We'll cover the concept of containers and use Docker to make our first deployment. We'll then use other tools, such as Docker Swarm, to help us to scale our service. We'll see how to do the same using Kubernetes, both locally and be using Google Cloud Platform, always using the same minimal microservice architecture and with minimal changes to the code.
Who this book is for
The book is targeted at people who know the basics of Node.js and want to enter the world of microservices, get to know its advantages and techniques, and understand why it's so popular. It can also be useful for developers in other similar programming languages, such as Java or C#.
What this book covers
Chapter 1, Age of Microservices, covers the evolution of computing and how development has changed and shifted from paradigm to paradigm depending on processing capacity and user demand, ultimately resulting in the age of microservices.
Chapter 2, Modules and Toolkits, introduces you to some modules that help you create a microservice, detailing different approaches: from very raw and simple modules, such as Micro and Express, to full toolkits, such as Hydra and Seneca.
Chapter 3, Building a Microservice, covers the development of a simple microservice using the most common module, Express, with a very simple HTTP interface.
Chapter 4, State and Security, covers the development of our microservice: from using the server filesystem to moving to a more structured database service, such as MySQL.
Chapter 5, Testing, shows how to use Mocha and chai to add test coverage to our previous microservice.
Chapter 6, Deploying Microservices, introduces you to Docker and helps you create a container image to use to run our microservice.
Chapter 7, Scaling, Sharding, and Replicating, covers the concept of replication when using Docker Swarm and Kubernetes locally to scale our microservice.
Chapter 8, Cloud-Native Microservices, shows how to migrate our microservice from the local Kubernetes to Google Cloud Platform, as an example of a fully cloud-native microservice.
Chapter 9, Design Patterns, enumerates some of the most common architectural design patterns and reviews the continuous integration and deployment loop used throughout the book.
To get the most out of this book
You should have basic Node.js skills and be somewhat comfortable with the language. We will cover Docker and Kubernetes, and it can be helpful to know the concepts of containers—but it's not mandatory.
You need to have Node.js (and npm) installed. We recommend using the current stable version, but you're free to use a previous version if it's an LTS one, with possible adaptions. If you want to deploy Kubernetes locally, you'll need to install it later on.
Download the example code files
You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at www.packtpub.com.
Select the SUPPORT tab.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box and follow the onscreen instructions.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR/7-Zip for Windows
Zipeg/iZip/UnRarX for Mac
7-Zip/PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Hands-On-Microservices-with-Node.js. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Conventions used
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in the text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: Arrow functions are a shorter expression function syntax without their own function scope, meaning this reference will point to the parent scope.
A block of code is set as follows:
function start() {
this.uptime = process.uptime();
setTimeout(() => {
console.log(this.uptime);
}, 5000);
}
start();
Any command-line input or output is written as follows:
npm init –y
npm install express –save
Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: Go to the Tables section on top to see the databases.
Warnings or important notes appear like this.
Tips and tricks appear like this.
Get in touch
Feedback from our readers is always welcome.
General feedback: Email feedback@packtpub.com and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Reviews
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packtpub.com.
The Age of Microservices
Decades ago, more specifically in 1974, Intel introduced 8080 to the world, which is an 8-bit processor with a 2 MHz clock speed and 64 KB of memory. This processor was used in Altair and began the revolution in personal computers.
It was sold pre-assembled or as a kit for hobbyists. It was the first computer to have enough power to actually be used for calculations. Even though it had some poor design choices and needed an engineering major to be able to use and program it, it started the spread of personal computers to the general public.
The technology evolved rapidly and the processor industry followed Moore's law, almost doubling speed every two years. Processors were still single core, with a low-efficiency ratio (power consumption per clock cycle). Because of this, servers usually did one specific job, called a service, like serving HTTP pages or managing a Lightweight Directory Access Protocol (LDAP) directory. Services were the monolith, with very few components, and were compiled altogether to be able to take the most out of the hardware processor and memory.
In the 90s, the internet was still only available for the few. Hypertext, based on HTML and HTTP, was in its infancy. Documents were simple and browsers developed language and protocol as they pleased. Competition for market share was ferocious between Internet Explorer and Netscape. The latter introduced JavaScript, which Microsoft copied as JScript:
Simple single-core servers
After the turn of the century, processor speed continued to increase, memory grew to generous sizes, and 32-bit became insufficient for allocating memory addresses. The all-new 64-bit architecture appeared and personal computer processors hit the 100 W consumption mark. Servers gained muscle and were able to handle different services. Developers still avoided breaking the service into parts. Interprocess communication was considered slow and services were kept in threads, inside a single process.
The internet was starting to become largely available. Telcos started offering triple play, which included the internet bundled with television and phone services. Cellphones became part of the revolution and the age of the smartphone began.
JSON appeared as a subset of the JavaScript language, although it's considered a language-independent data format. Some web services began to support the format.
The following is an example of servers with a couple of services running, but still having only one processor.
Powerful but single-core servers
Processor evolution then shifted. Instead of the increased speed that we were used to, processors started to appear with two cores, and then four cores. Eight cores followed, and it seemed the evolution of the computer would follow this path for some time.
This also meant a shift in architecture in the development paradigms. Relying on the system to take advantage of all processors is unwise. Services started to take advantage of this new layout and now it's common to see services having at least one processor per core. Just look at any web server or proxy, such as Apache or Nginx.
The