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
As a spring boot developer, One wouldn’t prefer diving in to some new technology just to explore the world of serverless computing. Spring framework provides us with robust set of functionality that comes in handy while writing our serverless functions. Spring Cloud Function provides us with a set of features that let us help in building serverless capabilities in our spring boot application. Spring cloud functions uses java.util.function as its underlying package to deliver a set of functionalities which are
- 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.
Grab your Cup of Coffee. It’s time to write some code. We will create a spring boot application which will be a basic one wherein we will give our function a URL and as response it will tell us whether that URL is Up & Running or not. We will start with the obvious first step of creating a Spring boot project from Spring Initializer.
Initializing Spring Boot Project
Go to start.spring.io and fill in all the details corresponding to your project. We don’t have to add any dependency as of now. We can do that later in pom.xml.
Adding Required Dependency
For adapting serverless capabilities we need spring-cloud-starter-function-web dependency. It enables serverless function execution in our local dev environment.
Creating our serverless function
While creating our serverless functions that acts as an endpoint to incoming calls, we need to annotate them with @ Bean annotation will expose them as an endpoint. The purpose of our function is to take in a URL and check by pinging it to know whether the URL is up & running or not.
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.
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.
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.