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
处理程序)转换为 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 Framework 将其视为自动检测和类路径扫描的候选者。 |
2 | 自动连接uppercase 和functionCatalog bean 中定义的HttpTriggerDemoApplication (见下)。 |
3 | @FunctionName批注标识指定的 Azure 函数处理程序。
当由触发器(例如@HttpTrigger ),函数处理该触发器以及任何其他输入,以生成一个或多个输出。 |
4 | 这plainBean 方法处理程序映射到使用 Auto-Wired 的 Azure 函数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 注释类用作Main-Class 如主类配置中所述。 |
2 | 函数自动连接并在 Azure 函数处理程序中使用。 |
功能目录
Spring Cloud Function 支持用户定义函数的一系列类型签名,同时提供一致的执行模型。 为此,它使用函数目录将所有用户定义的函数转换为规范表示。
Azure 适配器可以自动连接任何 Spring 组件,例如uppercase
以上。
但这些被当作普通的 Java 类实例,而不是规范的 Spring Cloud Functions!
要利用 Spring Cloud 函数并访问规范函数表示,您需要自动连接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 标头键。 |
现在,您可以从消息标头中检索 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 ).
下面的禁用 Spring Boot 插件部分解释了如何处理此问题。 |
Azure Maven/Gradle 插件
-
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 门户部署,请在“应用设置”中设置变量。 |
如果MAIN_CLASS 变量未设置,则 Azure 适配器会查找MANIFEST/META-INFO 属性,并选择第一个Main-Class: 用@SpringBootApplication 或@SpringBootConfiguration 注解。 |
元数据配置
可以使用共享host.json文件来配置函数应用。
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
host.json 元数据文件包含影响函数应用实例中所有函数的配置选项。
如果该文件不在项目顶部文件夹中,则需要相应地配置插件(例如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 编程模型已弃用,今后将不受支持。 |
有关函数集成方法的其他文档和示例,请遵循 azure-sample 自述文件和代码。