Going serverless with microservices

If you're working in the software industry, you've probably already heard of serverless services and design. Serverless architecture takes the Microservices-based approach to the next level. With the use of serverless architecture, we are completely moving away from managing any hardware. This also means that we do not need to worry about our scalability needs as scaling up or down will be taken care of by the service provider.

Let's take a quick look at how can we create a serverless implementation of a Microservice that can be used to sum up any two numbers. For the sake of this example, we will use the AWS Lambda serverless implementation, but you can use equivalent services in Microsoft Azure, Google Cloud, or other providers.

First, we will create a class file for the Lambda function's implementation:

package com.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;

/**
* Class to implement simple hello world example
*
*/
public class LambdaMethodHandler implements RequestStreamHandler {

public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
JSONObject responseJson = new JSONObject();
int num1 =0;
int num2 = 0;
String responseCode = "200";

try {
// First parse the request
JSONParser parser = new JSONParser();
JSONObject event = (JSONObject)parser.parse(reader);

if (event.get("queryStringParameters") != null) {
JSONObject queryStringParameters = (JSONObject)event.get("queryStringParameters");
if ( queryStringParameters.get("num1") != null) {
String temp = (String)queryStringParameters.get("num1");
num1 = Integer.parseInt(temp);
}
if ( queryStringParameters.get("num2") != null) {
String temp = (String)queryStringParameters.get("num2");
num2 = Integer.parseInt(temp);
}
}

// Prepare the response. If name was provided use that else use default.
String sum = "Sum is " + (num1+num2);

JSONObject responseBody = new JSONObject();
responseBody.put("message", sum);

JSONObject headerJson = new JSONObject();
responseJson.put("isBase64Encoded", false);
responseJson.put("statusCode", responseCode);
responseJson.put("headers", headerJson);
responseJson.put("body", responseBody.toString());

} catch(ParseException parseException) {
responseJson.put("statusCode", "400");
responseJson.put("exception", parseException);
}

OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(responseJson.toJSONString());
writer.close();
}

}

Next, we will execute the build using the following command:

mvn clean package shade:shade

Go to Amazon Lambda. From there, select the AWS account, choose Lambda service | Create functionAuthor from scratch, and provide the required values.

The following screenshot shows the screen from AWS Lambda:

Next, it will ask us to upload the .Jar file we created for our sum function:

Update the handler function, under the Handler section.  For example, in this case, provide com.test.LambdaMethodHandler::handleRequest.

After uploading .Jar, the last step is to configure the API gateway. The following screenshot shows the configuration screen:

After filling in the details, we will be provided with the URL,which  can be used to call the Microservice we created.

The following screenshot shows the API URL provided by AWS:

In this example, AWS provided us the following URL to call our service: 

https://91nvlnpp85.execute-api.us-east-1.amazonaws.com/default/SumFunction?num1=1&num2=4

The advantage of this approach is that we are not worried about where the service is deployed, or how to scale up or down. The cloud service provider will automatically take care of all our scaling needs. In this model, you only need to pay for the infrastructure you are using; in other words, you will be charged based on the number of calls your Microservice is receiving.

Though a serverless based approach looks very good, you need to be careful when selecting a solution approach. One needs to look at the pros and cons of any solution being finalized. The serverless approach might have some limitations from the service provider end, for example, currently Amazon has a limitation that a service should run in a maximum of five minutes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.246.245