Reader small image

You're reading from  Microservices with Clojure

Product typeBook
Published inJan 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788622240
Edition1st Edition
Languages
Right arrow
Author (1)
Anuj Kumar
Anuj Kumar
author image
Anuj Kumar

Anuj Kumar is the co-founder and chief architect of FORMCEPT, a data analytics startup based in Bangalore, India. He has more than 10 years of experience in designing large-scale distributed systems for storage, retrieval, and analytics. He has been in industry hacking, mainly in the area of data integration, data quality, and data analytics using NLP and machine learning techniques. He has published research papers at ACM conferences, got a few patents granted, and has spoken at TEDx. Prior to FORMCEPT, he has worked with the Oracle Server Technologies division in Bangalore, India.
Read more about Anuj Kumar

Right arrow

Chapter 5. REST APIs for Microservices

"Coming together is a beginning; keeping together is progress; working together is success."

- Henry Ford

One of the modes of interaction among microservices is direct messaging via APIs. REST APIs are one of the ways to make the direct messaging possible among microservices. REST APIs enable interoperability among microservices irrespective of the technology stack in which they are implemented. In this chapter, we will cover the following topics:

  • The concept of REST
  • How to define REST URIs with appropriate methods with status codes
  • How to use REST-based HTTP URIs using utilities such as cURL
  • REST APIs for the Helping Hands application

Introducing REST


REST stands for representational state transfer, and defines an architectural style for distributed hypermedia systems. It focuses on creating independent component implementations that are stateless and scale well. Applications that support REST do not store any information about the client state on the server side. Such applications require clients to maintain the state themselves and use the REST-style implementations, such as HTTP APIs exposed by the application, to transfer the state between them and the server. Clients who use the REST API may query the server for the latest state and represent the same state at the client side, thus keeping the client and server in sync.

The state of an application is defined by the state of the entities of the system. Entities can be related to the concept of resources in REST architecture. A resource is a key abstraction of information in REST. It is a conceptual mapping to a set of entities that is defined by the resource identifier...

RESTful APIs


Web service APIs that conform to REST architecture is called RESTful APIs. Microservices mostly implement the HTTP-based RESTful APIs that are stateless and have a base URI and a media type (https://en.wikipedia.org/wiki/Media_type) for the representation of resources. It also supports predefined standard operations that are mapped to HTTP methods such as GET, POST, PUT, DELETE, and more.

For example, as shown in the following table, the Order service may define an API /orders to get access to the orders that it maintains. It can support a GET method to look up all the orders or get a specific order by specifying the order ID. It can also allow clients to create new orders by using the POST method or create an order with a specific ID by using the PUT method. Similarly, it can support the PUT method to update order details and the DELETE method to delete an order by specifying the order ID explicitly.

REST APIs for Helping Hands


The Helping Hands application has Consumer and Provider services that expose the REST APIs to manage consumers and providers for the application. Each service provider in the application can register one or more services that are managed by Service APIs. Apart from these services, there is an Order service that manages all the orders placed by the consumers for the service and served by the providers of the service. The Helping Hands application also provides an application-wide Lookup service that provides a single API to look up services and orders by vicinity. All APIs provided by the Helping Hands microservices also handle errors with appropriate error messages in the response and the corresponding status code.

Consumer and Provider APIs

The APIs provided by the Consumer and Provider services target the consumers and providers resources of the system, respectively:

URI

HTTP method

Operation

Description

GET https://server/orders

GET

Read

Gets all the...

Summary


In this chapter, we learned about the concept of REST and how to design RESTful APIs with an easy-to-understand naming convention. We also learned about various request methods and status codes, and when to use what. At the end, we revisited the Helping Hands application to list down the REST APIs that will be required for the application across microservices.

In the next chapter, we will take a look at a Clojure framework called Pedestal (http://pedestal.io/) that we will be using to design microservices for the Helping Hands application. Pedestal will also be used to expose REST APIs for various microservices operations.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Microservices with Clojure
Published in: Jan 2018Publisher: PacktISBN-13: 9781788622240
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Anuj Kumar

Anuj Kumar is the co-founder and chief architect of FORMCEPT, a data analytics startup based in Bangalore, India. He has more than 10 years of experience in designing large-scale distributed systems for storage, retrieval, and analytics. He has been in industry hacking, mainly in the area of data integration, data quality, and data analytics using NLP and machine learning techniques. He has published research papers at ACM conferences, got a few patents granted, and has spoken at TEDx. Prior to FORMCEPT, he has worked with the Oracle Server Technologies division in Bangalore, India.
Read more about Anuj Kumar

URI

Method

Params

Description

/consumers

POST

Details (JSON)

Creates a new consumer and returns the consumer...