Serverless with Spring
1. Introduction:
In this article, we have gone through the functional interface implementation feature introduced in Java8.
Now, we will learn how to leverage functional interface implementation using spring boot.
2. Tool(s)/Framework(s):
- Any java project tool suite like Spring tool suit, eclipse, IntelliJ
- JDK 8 or above
- Spring boot 2.x & above
3. Prerequisites:
- Java functional interface
- Lambda
4. Spring Cloud Function Setup
4.1 Maven Dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
</dependency>
4.2 Writing Spring Cloud Function
Writing a spring cloud function like supplier, consumer, or function is very simple. We can define bean for each type of function & we can access this functional implementation via its method name.
For Ex:
/**
* Function implementation.
*
* @return Function
*/
@Bean
public Function<String, String> welcome() {
return (userName) -> “Welcome “ + userName + “ to the functional programming”;
}
4.3 Testing
We can verify the spring cloud function written above with curl command as given below.
curl http://localhost:8080/welcome -H “Content-Type: text/plain” -d apurv
5. Key takeaways
The spring cloud function is a really powerful feature that abstracts business logic from all of transport details & infrastructure. It might be in its earlier stage but there are many benefits via which we can,
- use all spring boot features like auto-configuration, dependency injection & many more.
- build cloud-agnostic unified programming model, across serverless providers.
- leverage same code & can run as a web endpoint, a stream processor, or a task.
6. Git Repo
https://github.com/shethaptech/spring-boot.git
7. References:
https://spring.io/projects/spring-cloud-function#support