Software Architecture

Serverless

Serverless Architecture

  • Serverless architectures are application designs that incorporate third-party “Backend as a Service” (BaaS) services, and/or that include custom code run in managed, ephemeral containers on a “Functions as a Service” (FaaS) platform.
  • By using these ideas, and related ones like single-page applications, such architectures remove much of the need for a traditional always-on server component.
  • Serverless architectures may benefit from significantly reduced operational cost, complexity, and engineering lead time, at a cost of increased reliance on vendor dependencies and comparatively immature supporting services.
https://martinfowler.com/articles/serverless.html

JPetStore

传统上,架构里会包含一个部署了应用程序和前端的单体服务器。上述架构采用的是瘦客户端方式,所有的业务逻辑(如认证、会话管理、宠物管理等)都部署在服务器端。哪怕你将这个单体服务拆开成若干个微服务,也并没有改变这一点。

mybatis/jpetstore-6

Serverless

  • 删除原用户认证代码,使用第三方认证服务 (例如Auth0.)
  • 使用第三方数据服务(例如Google Firebase)管理产品数据
  • Client端基于 Single Page Application技术直接实现认证和数据访问展现
  • 将“查询”业务功能实现为一个“函数”,每次用户查询时启动函数运行
  • 将“购买”业务功能实现为另一个函数

无服务器架构

无服务器架构是指应用程序使用第三方管理的Function 和服务,因此不需要管理服务器。无服务器架构主要包含了两个方面:

  • BaaS(Backend as a Service,后端即服务):使用第三方服务(如 Firebase、Auth0)来达成目的。使用 BaaS 的应用程序通常是富客户端应用程序,如 SPA 或移动 App。客户端负责处理大部分的业务逻辑,其他部分则依赖外部服务,如认证、数据库、用户管理,等等。
  • FaaS(Function as a Service,Function 即服务):包含服务器端业务逻辑的无状态 Function。这些 Function 运行在独立的容器里,基于事件驱动,并由第三方厂商托管,如 AWS Lambda 或者 Azure Functions。

优点

  • 不需要管理服务器
  • 自动伸缩(Scale to Zero)
  • 没有运营成本
  • 成本由事件驱动
  • 具有较高的安全性

Function as a service (FaaS)

Function as a service is a category of cloud computing services that provides a platform allowing customers to develop, run, and manage application functionalities without the complexity of building and maintaining the infrastructure typically associated with developing and launching an app.

AWS Lambda was the first FaaS offering by a large public cloud vendor.

https://en.wikipedia.org/wiki/Function_as_a_service

Open Source Serverless Platforms

OpenFaaS


响应时间——FaaS 需要一些初始化时间。如果负载很小(比如一个小时只有一个事件),每个请求都会经历冷启动,导致整体响应变慢。

serverless.com

Spring Cloud Function

  • Spring Cloud Function 是来自 Pivotal 的 Spring 团队的新项目,它致力于促进函数作为主要的开发单元
  • 该项目提供了一个通用的模型,用于在各种平台上部署基于函数的软件,包括像 Amazon AWS Lambda 这样的 FaaS(函数即服务,function as a service)平台

函数式编程

https://www.bilibili.com/video/BV13K411p7Mf?p=2 2:15

sa-spring/spring-cloudfunction

函数风格

Imperative

@Bean
public Function<String, String> reverseString() {
    return value -> new StringBuilder(value).reverse().toString();
}

Reactive

@Bean
public Function<Flux<String>, Flux<String>> uppercase() {
    return flux -> flux.map(value -> value.toUpperCase());
}

Pojo Function


public class Greeter implements Function<String, String> {
 
    @Override
    public String apply(String s) {
        return "Hello " + s + ", and welcome to Spring Cloud Function!!!";
    }
}
spring.cloud.function.scan.packages=com.example.cloudfunction.functions

Invoke via Message

Features

  • Choice of programming styles - reactive, imperative or hybrid.
  • Function composition and adaptation (e.g., composing imperative functions with reactive).
  • Support for reactive function with multiple inputs and outputs allowing merging, joining and other complex streaming operation to be handled by functions.
  • Transparent type conversion of inputs and outputs.
  • Packaging functions for deployments, specific to the target platform
  • Adapters to expose function to the outside world as HTTP endpoints etc.
  • Deploying a JAR file containing such an application context with an isolated classloader, so that you can pack them together in a single JVM.

Target Platforms

Adapters for AWS Lambda, Microsoft Azure, Apache OpenWhisk and possibly other "serverless" service providers.

riff is an Open Source platform for building and running Functions, Applications, and Containers on Kubernetes. To get started running your own functions on riff, see our Docs. This project is sponsored by VMware