Monday 16 May 2016

How to Use Spring Boot for Serverless Computing






by Sarang Nagmote



Category - Cloud Computing
More Information & Updates Available at: http://vibranttechnologies.co.in




Serverless Computing is a relatively new technology which allows developers to build event-driven code which scales and for which you only pay the time it’s running. OpenWhisk is IBM’s serverless computing offering hosted on Bluemix.
With OpenWhisk developers can implement actions via JavaScript, Swift and Docker. Via Docker other programming languages like Java can be used. This article describes how you can use Spring Boot to develop an action, how to build a Docker image, and how to register and trigger the serverless action.
As starting point I’m using the simple REST sample from the Spring website Building a RESTful Web Service. Follow the tutorial to set up this sample in less than 15 minutes.
In order to read the input parameters that are passed into the action, I created a class OpenWhiskRequestBody.
public class OpenWhiskRequestBody { private String authKey; private Greeting value; public String getAuthKey() { return authKey; } public void setAuthKey(String authKey) { this.authKey = authKey; } public Greeting getValue() { return value; } public void setValue(Greeting value) { this.value = value; }}
Next, I modified GreetingController.java slightly. I changed the path to ‘run’ and added some code to read and return data.
@RestControllerpublic class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @RequestMapping(method=RequestMethod.POST, path="/run", produces={"application/json"}) public Greeting greeting(@RequestBody OpenWhiskRequestBody request) { return new Greeting(counter.incrementAndGet(), String.format(template, request.getValue().getContent())); } @RequestMapping(method=RequestMethod.POST, path="/init", produces={"application/json"}) public ResponseEntity init() { return ResponseEntity.ok("{}"); }}
Here is the Dockerfile.
FROM java:8VOLUME /tmpADD target/gs-rest-service-0.1.0.jar app.jarRUN bash -c touch /app.jarENV SPRING_PROFILES_ACTIVE dockerENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
Invoke these commands to run the code. At this point images need to be pushed to DockerHub.
mvn packagedocker build -t nheidloff/open-whisk-docker-spring .docker push nheidloff/open-whisk-docker-springwsk action create --docker docker-spring nheidloff/open-whisk-docker-springwsk action invoke --blocking --result docker-spring --param name niklas
Here is the output of the action in the dashboard.
Image title

No comments:

Post a Comment