此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-function 4.1.4! |
Microsoft Azure 函数
用于部署的 Azure 函数适配器Spring Cloud Function
应用程序作为本机 Azure Java Functions。
这Azure Functions
编程模型广泛地中继 Java 注释,用于定义函数的处理程序方法及其输入和输出类型。
在编译时,提供的 Azure Maven/Gradle 插件会处理带批注的类,以生成必要的 Azure 函数绑定文件、配置和包项目。
Azure 注释只是一种类型安全的方法,用于将 Java 函数配置为被识别为 Azure 函数。
spring-cloud-function-adapter-azure 扩展了基本编程模型以提供 Spring 和 Spring Cloud Function 支持。 借助适配器,您可以使用依赖项注入构建 Spring Cloud Function 应用程序,然后将必要的服务自动连接到您的 Azure 处理程序方法中。
对于基于 Web 的函数应用程序,您可以将泛型adapter-azure 使用专门的 spring-cloud-function-adapter-azure-web.
使用 Azure Web 适配器,您可以将任何 Spring Web 应用程序部署为 Azure HttpTrigger 函数。
此适配器隐藏了 Azure 注释的复杂性,并改用熟悉的 Spring Web 编程模型。
有关详细信息,请遵循下面的 Azure Web 适配器部分。 |
Azure 适配器
提供Spring
& Spring Cloud Function
Azure Functions 的集成。
依赖
为了启用 Azure 函数集成,请将 azure 适配器依赖项添加到您的pom.xml
或build.gradle
文件:
-
Maven
-
Gradle
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-azure</artifactId>
</dependency>
</dependencies>
dependencies {
implementation 'org.springframework.cloud:spring-cloud-function-adapter-azure'
}
版本4.0.0+ 是必需的。将适配器放在类路径上会激活 Azure Java Worker 集成。 |
开发指南
使用@Component
(或@Service
) 注释来转换任何现有的 Azure 函数类(例如,使用@FunctionName
handlers) 导入到 Spring 组件中。
然后,您可以自动连接所需的依赖项(或 Spring Cloud 函数组合的函数目录),并在 Azure 函数处理程序中使用它们。
@Component (1)
public class MyAzureFunction {
// Plain Spring bean - not a Spring Cloud Functions!
@Autowired private Function<String, String> uppercase; (2)
// The FunctionCatalog leverages the Spring Cloud Function framework.
@Autowired private FunctionCatalog functionCatalog; (2)
@FunctionName("spring") (3)
public String plainBean( (4)
@HttpTrigger(name = "req",
methods = { HttpMethod.POST },
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
ExecutionContext context) {
return this.uppercase.apply(request.getBody().get());
}
@FunctionName("scf") (3)
public String springCloudFunction( (5)
@HttpTrigger(name = "req",
methods = { HttpMethod.POST },
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
ExecutionContext context) {
// Use SCF composition. Composed functions are not just spring beans but SCF such.
Function composed = this.functionCatalog.lookup("reverse|uppercase"); (6)
return (String) composed.apply(request.getBody().get());
}
}
1 | 指示MyAzureFunction class 是 Spring 框架视为自动检测和 Classpath 扫描候选者的“组件”。 |
2 | 自动连接uppercase 和functionCatalog 在HttpTriggerDemoApplication (下图)。 |
3 | @FunctionName 注释标识指定的 Azure 函数处理程序。
当由触发器调用时(例如@HttpTrigger )、函数处理该触发器和任何其他输入,以生成一个或多个输出。 |
4 | 这plainBean 方法处理程序映射到使用自动连接的uppercase Spring Bean 来计算结果。
它演示了如何在 Azure 处理程序中使用“纯”Spring 组件。 |
5 | 这springCloudFunction 方法处理程序映射到另一个 Azure 函数,该函数使用自动连接的FunctionCatalog 实例来计算结果。 |
6 | 演示如何利用 Spring Cloud 函数目录组合 API。 |
使用 com.microsoft.azure.functions.annotation.* 包中包含的 Java 注释将输入和输出绑定到您的方法。 |
Azure 处理程序中使用的业务逻辑的实现类似于常见的 Spring 应用程序:
@SpringBootApplication (1)
public class HttpTriggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(HttpTriggerDemoApplication.class, args);
}
@Bean
public Function<String, String> uppercase() { (2)
return payload -> payload.toUpperCase();
}
@Bean
public Function<String, String> reverse() { (2)
return payload -> new StringBuilder(payload).reverse().toString();
}
}
1 | 这@SpringBootApplication annotated 类用作Main-Class 如 Main Class Configuration 中所述。 |
2 | 函数自动连接并在 Azure 函数处理程序中使用。 |
函数目录
Spring Cloud Function 支持用户定义函数的一系列类型签名,同时提供一致的执行模型。 为此,它使用 Function Catalog 将所有用户定义的函数转换为规范表示。
Azure 适配器可以自动连接任何 Spring 组件,例如uppercase
以上。
但是这些被视为普通的 Java 类实例,而不是规范的 Spring Cloud Functions!
要利用 Spring Cloud Function 并访问规范函数表示,您需要自动连接FunctionCatalog
并在您的处理程序中使用它,例如functionCatalog
实例springCloudFunction()
处理程序。
访问 Azure ExecutionContext
有时需要访问 Azure 运行时以com.microsoft.azure.functions.ExecutionContext
.
例如,其中一个需求是日志记录,因此它可以显示在 Azure 控制台中。
为此,AzureFunctionUtil.enhanceInputIfNecessary
允许您添加ExecutionContext
作为 Message 标头,以便您可以通过executionContext
钥匙。
@FunctionName("myfunction")
public String execute(
@HttpTrigger(name = "req",
methods = { HttpMethod.POST },
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
ExecutionContext context) {
Message message =
(Message) AzureFunctionUtil.enhanceInputIfNecessary(request.getBody().get(), context); (1)
return this.uppercase.apply(message);
}
1 | 利用AzureFunctionUtil 实用程序内联context 作为消息标头,使用AzureFunctionUtil.EXECUTION_CONTEXT header 键。 |
现在,您可以从消息标头中检索 ExecutionContext:
@Bean
public Function<Message<String>, String> uppercase(JsonMapper mapper) {
return message -> {
String value = message.getPayload();
ExecutionContext context =
(ExecutionContext) message.getHeaders().get(AzureFunctionUtil.EXECUTION_CONTEXT); (1)
. . .
}
}
1 | 从标头中检索 ExecutionContext 实例。 |
配置
要在 Microsoft Azure 上运行函数应用程序,您必须提供必要的配置,例如function.json
和host.json
,并坚持强制打包格式。
通常,Azure Maven(或 Gradle)插件用于从带注释的类生成必要的配置,并生成所需的包格式。
Azure 打包格式与默认的 Spring Boot 打包不兼容(例如uber jar ).
下面的 Disable Spring Boot Plugin 部分解释了如何处理此问题。 |
Azure Maven/Gradle 插件
Azure 提供 Maven 和 Gradle 插件来处理带批注的类、生成必要的配置并生成预期的包布局。 插件用于设置 platform、runtime 和 app-settings 属性,如下所示:
-
Maven
-
Gradle
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<version>1.22.0 or higher</version>
<configuration>
<appName>YOUR-AZURE-FUNCTION-APP-NAME</appName>
<resourceGroup>YOUR-AZURE-FUNCTION-RESOURCE-GROUP</resourceGroup>
<region>YOUR-AZURE-FUNCTION-APP-REGION</region>
<appServicePlanName>YOUR-AZURE-FUNCTION-APP-SERVICE-PLANE-NAME</appServicePlanName>
<pricingTier>YOUR-AZURE-FUNCTION-PRICING-TIER</pricingTier>
<hostJson>${project.basedir}/src/main/resources/host.json</hostJson>
<runtime>
<os>linux</os>
<javaVersion>11</javaVersion>
</runtime>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>~4</value>
</property>
</appSettings>
</configuration>
<executions>
<execution>
<id>package-functions</id>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
plugins {
id "com.microsoft.azure.azurefunctions" version "1.11.0"
// ...
}
apply plugin: "com.microsoft.azure.azurefunctions"
azurefunctions {
appName = 'YOUR-AZURE-FUNCTION-APP-NAME'
resourceGroup = 'YOUR-AZURE-FUNCTION-RESOURCE-GROUP'
region = 'YOUR-AZURE-FUNCTION-APP-REGION'
appServicePlanName = 'YOUR-AZURE-FUNCTION-APP-SERVICE-PLANE-NAME'
pricingTier = 'YOUR-AZURE-FUNCTION-APP-SERVICE-PLANE-NAME'
runtime {
os = 'linux'
javaVersion = '11'
}
auth {
type = 'azure_cli'
}
appSettings {
FUNCTIONS_EXTENSION_VERSION = '~4'
}
// Uncomment to enable local debug
// localDebug = "transport=dt_socket,server=y,suspend=n,address=5005"
}
禁用 Spring Boot 插件
预期中,Azure Functions 在 Azure 执行运行时中运行,而不是在 SpringBoot 运行时中运行! 此外,Azure 需要由 Azure Maven/Gradle 插件生成的特定打包格式,该格式与默认的 Spring Boot 打包不兼容。
您必须禁用 SpringBoot Maven/Gradle 插件或使用 Spring Boot Thin Launcher,如以下 Maven 代码段所示:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
</dependency>
</dependencies>
</plugin>
主类配置
指定Main-Class
/Start-Class
指向您的 Spring 应用程序入口点,例如上面示例中的 HttpTriggerDemoApplication 类。
您可以使用 Mavenstart-class
属性或设置Main-Class
属性MANIFEST/META-INFO
:
-
Maven
-
Gradle
<properties>
<start-class>YOUR APP MAIN CLASS</start-class>
...
</properties>
jar {
manifest {
attributes(
"Main-Class": "YOUR-APP-MAIN-CLASS"
)
}
}
或者,您可以使用MAIN_CLASS 环境变量显式设置类名。
对于本地运行,请添加MAIN_CLASS 变量设置为local.settings.json 文件,对于 Azure 门户部署,请在 App Settings 中设置变量。 |
如果MAIN_CLASS 变量,则 Azure 适配器会查找MANIFEST/META-INFO 属性,并选择第一个Main-Class: 用 a@SpringBootApplication 或@SpringBootConfiguration 注解。 |
元数据配置
可以使用共享host.json文件来配置函数应用。
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
host.json元数据文件包含影响函数应用实例中所有函数的配置选项。
如果文件不在项目 top 文件夹中,则需要相应地配置插件(如hostJson maven 属性)。 |
Azure Web 适配器
对于基于 Web 的纯函数应用程序,您可以将泛型adapter-azure
使用专门的 spring-cloud-function-adapter-azure-web.
Azure Web 适配器可以在内部使用 HttpTrigger 将任何 Spring Web 应用程序部署为本机 Azure 函数。
它隐藏了 Azure 注释的复杂性,而是依赖于熟悉的 Spring Web 编程模型。
要启用 Azure Web 适配器,请将适配器依赖项添加到您的pom.xml
或build.gradle
文件:
-
Maven
-
Gradle
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-azure-web</artifactId>
</dependency>
</dependencies>
dependencies {
implementation 'org.springframework.cloud:spring-cloud-function-adapter-azure-web'
}
在本地运行
要在Azure Functions
,并且要部署到实时 Azure 环境,您将需要Azure Functions Core Tools
与 Azure CLI 一起安装(请参阅此处)。
对于某些配置,您还需要 Azurite 模拟器。
然后运行示例:
-
Maven
-
Gradle
./mvnw azure-functions:run
./gradlew azureFunctionsRun
在 Azure 上运行
确保您已登录 Azure 帐户。
az login
并部署
-
Maven
-
Gradle
./mvnw azure-functions:deploy
./gradlew azureFunctionsDeploy
本地调试
在调试模式下运行函数。
-
Maven
-
Gradle
./mvnw azure-functions:run -DenableDebug
// If you want to debug your functions, please add the following line
// to the azurefunctions section of your build.gradle.
azurefunctions {
...
localDebug = "transport=dt_socket,server=y,suspend=n,address=5005"
}
或者,将JAVA_OPTS
值添加到local.settings.json
喜欢这个:
{
"IsEncrypted": false,
"Values": {
...
"FUNCTIONS_WORKER_RUNTIME": "java",
"JAVA_OPTS": "-Djava.net.preferIPv4Stack=true -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=127.0.0.1:5005"
}
}
以下是VSCode
远程调试配置:
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Attach to Remote Program",
"request": "attach",
"hostName": "localhost",
"port": "5005"
},
]
}
FunctionInvoker(已弃用)
遗产FunctionInvoker 编程模型已弃用,今后将不再受支持。 |
有关 Function Integration 方法的其他文档和示例,请遵循 azure-sample README 和代码。