Microservices with Clojure
eBook - ePub

Microservices with Clojure

Anuj Kumar, Michael Vitz

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

Microservices with Clojure

Anuj Kumar, Michael Vitz

Book details
Book preview
Table of contents
Citations

About This Book

The common patterns and practices of the microservice architecture and their application using the Clojure programming language.About This Book• Relevance of the microservice architecture and benefits of Clojure's functional and simple features to implement it.• Learn best practices and common principles to avoid common pitfalls while developing microservices.• Learn how to use Pedestal to build your next microservices, secure them using JWT, and monitor them using the ELK stackWho This Book Is ForYou should have a working knowledge of programming in Clojure. However, no knowledge of RESTful architecture, microservices, or web services is expected. If you are looking to apply techniques to your own projects, taking your first steps into microservice architecture, this book is for you.What You Will Learn• Explore the pros and cons of monolithic and microservice architectures• Use Clojure to effectively build a reallife application using Microservices• Gain practical knowledge of the Clojure Pedestal framework and how to use it to build Microservices• Explore various persistence patterns and learn how to use Apache Kafka to build event-driven microservice architectures• Secure your Microservices using JWT• Monitor Microservices at scale using the ELK stack• Deploy Microservices at scale using container orchestration platforms such as KubernetesIn DetailThe 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.Style and approachThis book highlights the merits of the microservice architecture and its implementation with Clojure. Learn to implement microservices by migrating a monolithic application to a microservice-based architecture.

Frequently asked questions

How do I cancel my subscription?
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.
Can/how do I download books?
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.
What is the difference between the pricing plans?
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.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is Microservices with Clojure an online PDF/ePUB?
Yes, you can access Microservices with Clojure by Anuj Kumar, Michael Vitz in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788626316

Building Microservices for Helping Hands

"It's not the ideas; it's design, implementation and hard work that make the difference."
- Michael Abrash
Identifying bounded context is the first step towards building a successful microservices-based architecture. Designing for scale and implementing them with the right technology stack is the next and the most crucial step in building a microservices-based application. This chapter brings together all the design decisions taken in the first part of the book (Chapter 2, Microservices Architecture and Chapter 3, Microservices for Helping Hands Application) and describes the steps to implement them using the Pedestal framework (Chapter 6, Introduction to Pedestal). In this chapter, you will learn how to:
  • Implement Hexagonal design for microservices
  • Create scalable microservices for Helping Hands using Pedestal
  • Implement workflows for microservices using the Pedestal interceptor chain
  • Implement the lookup service of Helping Hands to search for services and generate reports

Implementing Hexagonal Architecture

Hexagonal Architecture (http://alistair.cockburn.us/Hexagonal+architecture), as shown in the following diagram, aims to decouple the business logic from the persistence and the service layer. Clojure provides the concept of a protocol (https://clojure.org/reference/protocols) that can be used to define the interfaces, that act as ports of Hexagonal Architecture. These ports can then be implemented by the adapters, resulting in a decoupled implementation that can be swapped based on the requirement. Execution of these adapters can then be triggered via Pedestal interceptors based on the business logic.

Designing the interceptor chain and context

The interceptor chain must be defined for each microservice of the Helping Hands application separately. Each interceptor chain may consist of interceptors that authenticate the request, validate the data models, and apply the business logic. Interceptors can also be added to interact with the Persistence layer and to generate events as well. The list of probable interceptors that may be a part of Helping Hands services include:
  • Auth: Used to authenticate and authorize requests received by the API endpoints exposed by the microservice.
  • Validation (data model): Used to validate the request parameters and map the external data model with the internal data model as expected by the business logic and underlying persistent store.
  • Business logic: One or more interceptors to implement the business logic. These interceptors process the request parameters received by the API endpoints.
  • Persistence: Persist the changes using one or more adapters that implement the Port Protocol defined for the microservice data model to be persisted.
  • Events: Generate events asynchronously both for other microservices as well as for monitoring and reporting. These interceptors are added at the end of the chain to generate changelog events of the persistent store for other microservices to consume.
Pedestal context is a Clojure map that contains all the details related to the interceptor chain, request parameters, headers, and more. The same context map can also be used to share data with other interceptors in the chain. Instead of adding a key to the context map directly, it is recommended to keep a parent key, such as tx-data, that contains a map of keys that are related to the data being processed by the microservice. It may also contain the validated user details for other microservices to consume.

Creating a Pedestal project

To start with the implementation, create a project by the name of helping-hands and initialize it with a Pedestal template as discussed earlier in Chapter 6, Introduction to Pedestal. Once the project template is initialized, update the project.clj file and other configuration parameters as per the development environment setup of the playground application of Chapter 4, Development Environment. Once configured, the project.clj file should contain all the required dependencies and plugins, as shown here:
(defproject helping-hands "0.0.1-SNAPSHOT"
:description "Helping Hands Application"
:url "https://www.packtpub.com/application-development/microservices-clojure"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[io.pedestal/pedestal.service "0.5.3"]

;; Remove this line and uncomment one of the next lines to
;; use Immutant or Tomcat instead of Jetty:
[io.pedestal/pedestal.jetty "0.5.3"]
;; [io.pedestal/pedestal.immutant "0.5.3"]
;; [io.pedestal/pedestal.tomcat "0.5.3"]

[ch.qos.logback/logback-classic "1.1.8" :exclusions [org.slf4j/slf4j-api]]
[org.slf4j/jul-to-slf4j "1.7.22"]
[org.slf4j/jcl-over-slf4j "1.7.22"]
[org.slf4j/log4j-over-slf4j "1.7.22"]]
:min-lein-version "2.0.0"
:source-paths ["src/clj"]
:java-source-paths ["src/jvm"]
:test-paths ["test/clj" "test/jvm"]
:resource-paths ["config", "resources"]
:plugins [[:lein-codox "0.10.3"]
;; Code Coverage
[:lein-cloverage "1.0.9"]
;; Unit test docs
[test2junit "1.2.2"]]
:codox {:namespaces :all}
:test2junit-output-dir "target/test-reports"
;; If you use HTTP/2 or ALPN, use the java-agent to pull in the correct alpn-boot dependency
;:java-agents [[org.mortbay.jetty.alpn/jetty-alpn-agent "2.0.5"]]
:profiles {:provided {:depend...

Table of contents