Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use Spring Boot for Serverless Computing

DZone's Guide to

How to Use Spring Boot for Serverless Computing

Check out this tutorial on serverless computing using Spring Boot and Docker.

· Cloud Zone
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

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.

@RestController
public 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:8
VOLUME /tmp
ADD target/gs-rest-service-0.1.0.jar app.jar
RUN bash -c 'touch /app.jar'
ENV SPRING_PROFILES_ACTIVE docker
ENTRYPOINT ["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 package
docker build -t nheidloff/open-whisk-docker-spring .
docker push nheidloff/open-whisk-docker-spring
wsk action create --docker docker-spring nheidloff/open-whisk-docker-spring
wsk action invoke --blocking --result docker-spring --param name niklas 

Here is the output of the action in the dashboard.

Image title

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
openwhisk ,serverless architecture ,bluemix

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}