Spring into action

Spring Cloud Function: Spring Boot with “all the benefits of serverless”

Jane Elizabeth
Spring Cloud Function
© Shutterstock / Vasabii

Spring Boot is going serverless with the new Spring Cloud Function. This new functional-based programming project is designed for serverless providers.

Spring has just announced their latest project: Spring Cloud Function. This new project provides a new programming model for Spring Boot applications, abstracting away all of the transport details and infrastructure.

Spring Cloud Function allows developers to keep all of their familiar Spring tools and processes, making it easier for them to focus firmly on business logic.

For Spring devs, Spring Cloud Function is a smaller, more familiar step than using FaaS APIs and UIs natively. For our function-focused devs, Spring Cloud Function requires no prior knowledge about Spring.

SEE MORE: Spring Boot – what it is and how to get started

According to Dave Syer, a Senior Engineering Consultant at Pivotal and Spring committer, Spring Cloud Function has “all the benefits of serverless,” but with full access to Spring like dependency injection, integrations, autoconfiguration, as well as build tools like testing, continuous delivery, and the ability run locally.

Spring Cloud Function specs

The goals of Spring Cloud Function are to:

  • Promote the implementation of business logic via functions.
  • Decouple the development lifecycle of business logic from any specific runtime target so that the same code can run as a web endpoint, a stream processor, or a task.
  • Support a uniform programming model across serverless providers, as well as the ability to run standalone (locally or in a PaaS).
  • Enable Spring Boot features (auto-configuration, dependency injection, metrics) on serverless providers.

Spring Cloud Function has four main features.

  1. Wrappers for @Beans of type FunctionConsumer and Supplier, exposing them to the outside world as either HTTP endpoints and/or message stream listeners/publishers with RabbitMQ, Kafka etc.
  2. Compiling strings which are Java function bodies into bytecode, and then turning them into @Beans that can be wrapped as above.
  3. Deploying a JAR file containing such an application context with an isolated classloader, so that you can pack them together in a single JVM.
  4. Adapters for AWS LambdaApache OpenWhisk and possibly other “serverless” service providers.

Spring Cloud Function promotes a programming model based on plain old functions. Specifically, this means the core interfaces defined in the java.util.function package:  Function, Consumer, and Supplier.

SEE MORE: Spring Boot tutorial: REST services and microservices

Also, Spring Cloud Function has support for compiling String based lambdas into function instances dynamically. This can be especially useful when prototyping or adding some trivial transformation logic, as the Spring Expression Language is commonly used today.

But why is Spring promoting this model? You can easily create Function, Consumer, and Supplier instances anyways. According to Mark Fisher,

It shouldn’t be much of a surprise to learn that the answer involves Inversion of Control. Over the years, everything from basic dependency injection to Spring’s ubiquitous use of the template pattern have been described by the Hollywood Principle: “don’t call us, we’ll call you”. An even more important example of Inversion of Control is the decoupling of business logic from deployment profile. The business logic in this case refers to functions, while the deployment profile could be a REST app, stream processing app, or finite task. Spring Cloud Function provides a JAR for each of those types, and in each case an auto-configured FunctionCatalog is used to locate the Functions, Consumers, and Suppliers within the ApplicationContext.

Likewise, to deploy a function as a stream-processor, only requires adding the “spring-cloud-function-stream” dependency that in turn builds upon Spring Cloud Stream. Just as Spring Cloud Stream provides a Binder abstraction that eliminates the need to define Channel Adapters, Spring Cloud Function eliminates the need to declare components like Service Activators, Transformers, or even the @StreamListener-annotated methods to which Spring Cloud Stream delegates. The “spring-cloud-function-stream” JAR itself provides all of that. It’s yet another case of taking Inversion of Control to another level.

Spring is a big fan of Alan Kay’s principle: “Simple things should be simple. Complex things should be possible.” And with Spring Cloud Function, they are moving even closer to their goal.

 

Visit the Spring blog or the Spring Cloud Function GitHub page for more information!

Author
Jane Elizabeth
Jane Elizabeth is an assistant editor for JAXenter.com

Comments
comments powered by Disqus