Spring Cloud Function是一个具有以下高级目标的项目:
- 通过功能促进业务逻辑的实现。
- 将业务逻辑的开发生命周期与任何特定的运行时目标脱钩,以便可以将相同的代码作为Web终结点,流处理器或任务来运行。
- 支持跨无服务器提供程序的统一编程模型,以及独立运行(本地或在PaaS中)的能力。
- 在无服务器提供程序上启用Spring Boot功能(自动配置,依赖项注入,指标)。
它抽象出所有传输细节和基础结构,使开发人员可以保留所有熟悉的工具和流程,并专注于业务逻辑。
Spring Cloud Function功能:
- 编程风格的选择-反应式,命令式或混合式。
- POJO函数(即,如果符合@FunctionalInterface语义的内容,我们将其视为函数)
- 输入和输出的透明类型转换。
- 函数组成,包括将命令性函数与反应性组成。
- REST支持将功能公开为HTTP端点等。
- 通过Spring Cloud Stream框架向函数传递数据(通过Apache Kafka,Solace,RabbitMQ等)。
- 使用隔离的类加载器部署打包为JAR文件的功能,以在单个JVM中支持多版本部署。
- 特定于目标平台的部署打包功能(例如,Project Riff,AWS Lambda等)
- 适用于AWS Lambda,Microsoft Azure,Apache OpenWhisk以及其他“无服务器”服务提供商的适配器。
- 支持具有多个输入和输出的反应式功能,从而允许功能处理合并,联接和其他复杂的流操作。
创建一个 Spring Cloud Function 项目,编写和部署自定义函数,并通过 REST API 管理这些函数
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Spring-Cloud-Function</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Cloud Function Web -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
</dependency>
<!-- Spring Cloud Function Context -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-context</artifactId>
</dependency>
</dependencies>
</project>
FunctionCatalog:
FunctionCatalog
是 Spring Cloud Function 提供的一个组件,用于在应用程序上下文中查找和调用函数。- 它允许你通过函数名称动态地获取和调用函数
package com.et.controller;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.web.bind.annotation.*;
import java.util.function.Function;
public class FunctionController {
private final FunctionCatalog functionCatalog;
public FunctionController(FunctionCatalog functionCatalog) {
this.functionCatalog = functionCatalog;
}
"/function/{name}") (
public String applyFunction( String name, String input) {
Function<String, String> function = functionCatalog.lookup(Function.class, name);
if (function != null) {
return function.apply(input);
} else {
return "Function not found";
}
}
}
定义了一个 Spring 组件 CustomFunctions
,其中包含两个函数:toUpperCase
和 toLowerCase
。这些函数被注册为 Spring 的 Bean,可以在应用程序中被其他组件使用,特别是在使用 Spring Cloud Function 时,可以通过 FunctionCatalog
动态查找和调用这些函数。
package com.et.function;
import java.util.function.Function;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class CustomFunctions {
@Bean
public Function<String, String> toUpperCase() {
return value -> value.toUpperCase();
}
@Bean
public Function<String, String> toLowerCase() {
return value -> value.toLowerCase();
}
}
以上只是一些关键代码,所有代码请参见下面代码仓库
- https://github.com/Harries/springcloud-demo(spring cloud function)
- 启动Spring Cloud 应用
- 访问
http://localhost:8080/function/toUpperCase?input=hello
将返回HELLO
。 - 访问
http://localhost:8080/function/toLowerCase?input=HELLO
将返回hello
。
这个示例展示了如何创建一个 Spring Cloud Function 项目,编写和部署自定义函数,并通过 REST API 管理这些函数。你可以根据需要扩展这个示例,添加更多的函数或集成到不同的云平台上。包括像 Amazon AWS Lambda 这样的 FaaS(函数即服务,function as a service)平台。
- https://spring.io/projects/spring-cloud-function
- http://www.liuhaihua.cn/archives/711614.html
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/jjc/80099.html