Building Serverless Microservices With Java
What is Serverless Architecture?
The phrase “serverless” doesn’t mean servers are no longer required. It solely proposes that developers no longer have to think that much about them. Going serverless lets developers shift their focus from the server level to the task level which is writing codes.
What does it mean to have servers?
First, let’s talk about what it means to have servers (virtual servers) providing the computing power required by your application. Owning servers comes with responsibilities -
Managing how the primitives (functions in the case of applications, or objects when it comes to storage) map to server primitives (CPU, memory, disk etc.).
Own provisioning (and therefore paying) for the capacity to handle your application’s projected traffic, independent of whether there’s actual traffic or not.
Own managing reliability and availability constructs like redundancy, failover, retries etc.
Advantages of going Serverless
Why should one move to serverless architecture can be adequately described through its benefits:
PaaS and Serverless - A user of traditional PaaS has to specify the number of resources - such as dynos for Heroku or gears for OpenShift - for the application. The Serverless platform will take care of finding a server where the code is to run and to scale up when necessary.
Lower operational and development costs - The containers used to run these functions are decommissioned as soon as the execution ends. And the execution is metered in units of 100 ms, You don't pay anything when your code isn't running.
Fits with microservices, which can be implemented as functions.
Serverless architectures refer to applications that significantly depend on third-party services (known as Backend as a Service or "BaaS") or on custom code that's run in ephemeral containers (Function as a Service or "FaaS"). But there are cons related to moving your application to FaaS which is discussed in our next post: Building Serverless Microservices with Python
The simplest way of thinking about FaaS is that it changes thinking from "build a framework to sit on a server to react to multiple events to "build/use micro-functionality to react to a single event."
How to migrate to a Microservices Architecture?
In a simple definition, Microservices are independently scalable, independently deployable systems that communicate over some protocols HTTP (XML, JSON), Thrift, Protocol Buffers etc. Microservices are Single Responsibility Principle at code base level.
Below are some of the factors that can be followed to build Microservices:
One code per app/service: There is always a one-to-one correlation between the codebase and the service.
Explicitly declare and isolate dependencies: This can be done by using packaging systems.
Use environment variables to store configurations.
Strictly separate build, release and run stages.
Treat logs as event streams. Route log event stream to analysis system such as Splunk for log analysis.
Keep development, staging, and production as similar as possible.
Benefits of Microservices Architecture
Microservices Architectures have lots of very real and significant benefits:
Systems built in this way are inherently loosely coupled.
The services themselves are very simple, focusing on doing one thing well.
Multiple developers and teams can deliver independently under this model.
They are a great enabler for continuous delivery, allowing frequent releases whilst keeping the rest of the system available and stable.
In this post, we will implement a Nexastack function which integrates with a database(MongoDB used here). We are going to implement this new function in Java using Spring Framework. So, Let’s get started -
We are going to build an Employee Service consisting of a function to show Employees information from the database. For Demo purpose, we are here implementing one function “GetEmployee”.
1. Setting up MongoDB Instance
Install MongoDB and configure it to get started.
Create Database EmployeeDB
Create table Employee
Insert some records into the table for the demo.
Write a file “config.properties” to setup configuration on the serverless architecture
2. Create Spring Microservice
3. Create Deployment Package
To create a function, first create a function deployment package, a JAR file consisting of your code and any dependencies. We have to build a JAR file named “get-employees-api-0.0.1-SNAPSHOT.jar” containing java code to show employees.
1. Create a directory, for example, project-dir.
2. Save all of your Java source files at the root level of this directory. The folder named “app” serves this objective in the demo.
3. Add any of the extra libraries used in a folder “lib”.
4. Zip the content of the project-dir directory, which is your deployment package.
4. Setup Microservices on NexaStack
Fill the entries and upload your function.
The port number field must be filled with a port number on which you want your service to run.
Once we are finished with uploading the function, Service is active to test
Click on "Test" and your service is up in few seconds on the port number specified in the previous step.
Serverless Computing: The Future of Cloud
Above shown is just a demo of Serverless computing and there is a lot to add into the field. Improved ability to respond to a software event (the shopper's click), retrieve the analyst's code, and quickly run it might prove instrumental in converting your shoppers into buyers. As said, "Software is eating the world. Companies that can develop software most effectively will be among those that succeed" at thriving in such a world. There would be no long pause, no shifting to another application, no launch of a virtual server in the cloud. Most applications would be built out of multiple microservices. For a company using them, there would be dozens or hundreds of microservices, sitting in the public cloud until demanded, then running in an expedient burst to produce results.
Till then, explore how going serverless can help your business attain new heights and Contact Us for the best possible solution.
Product NexaStack - Unified DevOps Platform Provides monitoring of Kubernetes, Docker, OpenStack infrastructure, Big Data Infrastructure and uses advanced machine learning techniques for Log Mining and Log Analytics.
Product ElixirData - Modern Data Integration Platform Enables enterprises and Different agencies for Log Analytics and Log Mining.
Product Akira.AI is an Automated & Knowledge Drive Artificial Intelligence Platform that enables you to automate the Infrastructure to train and deploy Deep Learning Models on Public Cloud as well as On-Premises.
Get 1 Hour Free Assessment for DevOps, Big Data Strategy, and Data Science. CONTACT US NOW