DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Use Spring Boot for Serverless Computing

How to Use Spring Boot for Serverless Computing

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

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
May. 16, 16 · Tutorial
Like (7)
Save
Tweet
Share
8.59K Views

Join the DZone community and get the full member experience.

Join For Free

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

Spring Framework Serverless computing Spring Boot Computing

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top Authentication Trends to Watch Out for in 2023
  • DevSecOps Benefits and Challenges
  • A Beginner's Guide to Back-End Development
  • Web Application Architecture: The Latest Guide

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: