Going Serverless with Spring

Image Source : Google Images

Let’s assume a scenario, wherein we don’t have to worry about infrastructure while writing & deploying code. Sounds promising, right? Well this is what we call as Serverless Computing. To put up in Wikipedia’s words, Serverless computing is a cloud computing execution model in which the cloud provider allocates machine resources on demand, taking care of the servers on behalf of their customers. Serverless takes the benefits of modern day cloud computing paradigm thus allowing the developers to focus on writing their business logic rather then worrying about underlying infrastructure. The platform providing the infrastructure takes care of security, scaling, memory runtimes etc. Another out of the box benefit of using serverless architecture is that you pay for what you use, thereby aiming towards automated cost management.

Before going ahead, first let’s make sure that we are on the same page. Serverless does not(I repeat does not) in any way means that we are not using servers. Obviously we need servers for running our code. What serverless does is, it allows you to give your entire focus on writing awesome code rather then worrying about aspects such as maintaining virtual or physical servers, operating systems, memory management etc.

Spring Boot & Serverless

  • Enabling Spring Boot features into cloud vendors.
  • Enabling features of auto-configuration, dependency injection, metrics on serverless platforms.
  • Provides set of programming styles such as reactive, imperative or hybrid.
  • Capability to export functions pertaining business logic as REST endpoints.
  • Capability to handle streams of data from platform such as Apache Kafka, RabbitMQ, etc.

Let’s Code

Initializing Spring Boot Project


Adding Required Dependency

Creating our serverless function

Serverless URL status

The Function<String, String> wrapper for our getUrlStatus() function takes in a String type argument(URL in our case) and returns a String type value(Status of that URL). There are however two other types of wrapper also which are Consumer<T> and Supplier<T>.

Supplier<T> wrapper does not accept any input param and returns a value of type T. In simpler terms it can be understood as Kafka Producer whereas Consumer<T> accepts a input of type T and returns nothing. It can be understood as Kafka consumer which just consumes the incoming value.

The coding part is done and all that is left is to start the application and check if the serverless function is working or not. Open your cmd/terminal and execute a curl command.

check the URL’s after -d

We can also create functions by creating a class which implements the Function interface. We need to override the apply() method in order for our function to work. Lets convert the same code that we wrote into a class.

Class based implementation

To check if it is working or not open up the cmd/terminal and fire up a bunch of curl commands like below.

Note- The name of our function in this case will be camel case conversion of our class name.

So this was, one can say a 101 article on serverless implementation using Spring boot. Although this does not prove that whether we can use Spring serverless for production grade applications or not but it definitely is worth trying. As mentioned on multiple places Spring cloud functions allows developers to use the same code as Stream, Cloud Function and Web Endpoint. The future holds exciting possibilities for Serverless Computing.

An astrophile, trying to learn about universe.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store