欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > Spring Cloud Function快速入门Demo

Spring Cloud Function快速入门Demo

2025/2/24 19:02:50 来源:https://blog.csdn.net/dot_life/article/details/143441154  浏览:    关键词:Spring Cloud Function快速入门Demo

1.什么是Spring Cloud Function?

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以及其他“无服务器”服务提供商的适配器。
  • 支持具有多个输入和输出的反应式功能,从而允许功能处理合并,联接和其他复杂的流操作。

2.代码工程

实验目标

创建一个 Spring Cloud Function 项目,编写和部署自定义函数,并通过 REST API 管理这些函数

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

controller

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;@RestController
public class FunctionController {private final FunctionCatalog functionCatalog;public FunctionController(FunctionCatalog functionCatalog) {this.functionCatalog = functionCatalog;}@GetMapping("/function/{name}")public String applyFunction(@PathVariable String name, @RequestParam String input) {Function<String, String> function = functionCatalog.lookup(Function.class, name);if (function != null) {return function.apply(input);} else {return "Function not found";}}
}

function

定义了一个 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 {@Beanpublic Function<String, String> toUpperCase() {return value -> value.toUpperCase();}@Beanpublic Function<String, String> toLowerCase() {return value -> value.toLowerCase();}
}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • GitHub - Harries/springcloud-demo: Spring Cloud tutorial about hystrix,eureka,config,admin,skywalking(spring cloud function)

3.测试

  • 启动Spring Cloud 应用
  • 访问 http://localhost:8080/function/toUpperCase?input=hello 将返回 HELLO
  • 访问 http://localhost:8080/function/toLowerCase?input=HELLO 将返回 hello

4.总结

这个示例展示了如何创建一个 Spring Cloud Function 项目,编写和部署自定义函数,并通过 REST API 管理这些函数。你可以根据需要扩展这个示例,添加更多的函数或集成到不同的云平台上。包括像 Amazon AWS Lambda 这样的 FaaS(函数即服务,function as a service)平台。

5.引用

  • Spring Cloud Function
  • http://www.liuhaihua.cn/archives/711614.html

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词