Book Image

Microservices with Clojure

By : Anuj Kumar
Book Image

Microservices with Clojure

By: Anuj Kumar

Overview of this book

The microservice architecture is sweeping the world as the de facto pattern with which to design and build scalable, easy-tomaintain web applications. This book will teach you common patterns and practices, and will show you how to apply these using the Clojure programming language. This book will teach you the fundamental concepts of architectural design and RESTful communication, and show you patterns that provide manageable code that is supportable in development and at scale in production. We will provide you with examples of how to put these concepts and patterns into practice with Clojure. This book will explain and illustrate, with practical examples, how teams of all sizes can start solving problems with microservices. You will learn the importance of writing code that is asynchronous and non-blocking and how Pedestal helps us do this. Later, the book explains how to build Reactive microservices in Clojure that adhere to the principles underlying the Reactive Manifesto. We finish off by showing you various ways to monitor, test, and secure your microservices. By the end, you will be fully capable of setting up, modifying, and deploying a microservice with Clojure and Pedestal.
Table of Contents (18 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Preface

The microservice architecture is sweeping the world as the de facto pattern for building scalable and easy-to-maintain web-based applications. This book will teach you common patterns and practices, showing you how to apply them using the Clojure programming language. It will teach you the fundamental concepts of architectural design and RESTful communication, and show you patterns that provide manageable code that is supportable in development and at scale in production. This book will provide you with examples of how to put these concepts and patterns into practice with Clojure.

Whether you are planning a new application or working on an existing monolith, this book will explain and illustrate with practical examples how teams of all sizes can start solving problems with microservices. You will understand the importance of writing code that is asynchronous and non-blocking, and how Pedestal helps us do this. Later, the book explains how to build Reactive microservices in Clojure, which adhere to the principles underlying the Reactive Manifesto. We finish off by showing you various techniques to monitor, test, and secure your microservices. By the end, you will be fully capable of setting up, modifying, and deploying a microservice with Clojure and Pedestal.

Who this book is for

If you are looking forward to migrate your existing monolithic applications to microservices or taking your first steps into microservice architecture, then this book is for you. You should have a working knowledge of programming in Clojure. However, no knowledge of RESTful architecture, microservices, or web services is expected.

What this book covers

Chapter 1, Monolithic Versus Microservices, introduces monolithic and microservice architecture and discusses when to use what. It also covers the possible migration plans of moving from monolithic applications to microservices.

Chapter 2, Microservices Architecture, covers the basic building blocks of microservice architecture and its related features. It discusses how to set up messaging and contracts, and manage data flows among microservices.

Chapter 3, Microservices for Helping Hands Application, introduces a sample Helping Hands application and describes the steps that will be taken in the rest of the book to build the application using microservices. Further, the chapter compares and contrasts the benefits of using a microservices-based architecture compared with a monolithic one.

Chapter 4, Development Environment, covers Clojure and REPL at a high level and introduces the concepts of Leiningen and Boot—the two major build tools for any Clojure project. The emphasis will be on Leiningen with a basic introduction to Boot on how to set up a Clojure project for implementing microservices.

Chapter 5, REST APIs for Microservices, covers the basics of the REST architectural style, various HTTP methods, when to use what, and how to give meaningful names to RESTful APIs of microservices. It also covers the naming conventions for REST APIs using the Helping Hands application as an example.

Chapter 6, Introduction to Pedestal, covers the Clojure Pedestal framework in detail with all the relevant features provided by Pedestal, including interceptors and handlers, routes, WebSockets, server-sent events, and chain providers.

Chapter 7, Achieving Immutability with Datomic, gives an overview of the Datomic database along with its architecture, data model, transactions, and Datalog query language.

Chapter 8, Building Microservices for Helping Hands, is a step-by-step, hands-on guide to build and test microservices for the Helping Hands application using the Pedestal framework.

Chapter 9, Configuring Microservices, covers the basics of microservices configuration and discusses how to create configurable microservices using frameworks such as Omniconf. It also explains the steps to manage the application state effectively using available state-management frameworks such as Mount.

Chapter 10Event-Driven Patterns for Microservices, covers the basics of event-driven architectures and shows how to use Apache Kafka as a messaging system and event store. Further, it discusses how to use Apache Kafka brokers and set up consumer groups for the effective coordination of microservices.

Chapter 11Deploying and Monitoring Secured Microservices, covers the basics of microservices authentication using JWT and how to set up a real-time monitoring system using the ELK Stack. It also explains the basic concepts of containers and orchestration frameworks such as Kubernetes.

To get the most out of this book

The Java Development Kit (JDK) is required to run and develop applications using Clojure. You can get the JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html. It is also recommended that you use a text editor or an integrated development environment (IDE) of your choice for implementation. Some of the examples in the chapters require Linux as an operating system.

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:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. 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/Microservices-with-Clojure. 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 text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The persistence protocol ServiceDB consists of upsertentity, and delete functions."

A block of code is set as follows:

{
  "query": {
    "term": {
      "status": "O"
    }
  }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

(defn home-page
  [request]
(log/counter ::home-hits 1)
  (ring-resp/response "Hello World!"))

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

% lein run
Noname | Hello, World!

% lein run Clojure
Clojure | Hello, World!

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: "Click on the Create a visualization button."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] 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 [email protected].

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 [email protected] 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.