此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-function 4.1.4spring-doc.cadn.net.cn

Microsoft Azure 函数

用于部署的 Azure 函数适配器Spring Cloud Function应用程序作为本机 Azure Java Functions。spring-doc.cadn.net.cn

Azure Functions 编程模型广泛地中继 Java 注释,用于定义函数的处理程序方法及其输入和输出类型。 在编译时,提供的 Azure Maven/Gradle 插件会处理带批注的类,以生成必要的 Azure 函数绑定文件、配置和包项目。 Azure 注释只是一种类型安全的方法,用于将 Java 函数配置为被识别为 Azure 函数。spring-doc.cadn.net.cn

spring-cloud-function-adapter-azure 扩展了基本编程模型以提供 Spring 和 Spring Cloud Function 支持。 借助适配器,您可以使用依赖项注入构建 Spring Cloud Function 应用程序,然后将必要的服务自动连接到您的 Azure 处理程序方法中。spring-doc.cadn.net.cn

SCF 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 FunctionAzure Functions 的集成。spring-doc.cadn.net.cn

依赖

为了启用 Azure 函数集成,请将 azure 适配器依赖项添加到您的pom.xmlbuild.gradle文件:spring-doc.cadn.net.cn

<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 函数类(例如,使用@FunctionNamehandlers) 导入到 Spring 组件中。 然后,您可以自动连接所需的依赖项(或 Spring Cloud 函数组合的函数目录),并在 Azure 函数处理程序中使用它们。spring-doc.cadn.net.cn

@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 指示MyAzureFunctionclass 是 Spring 框架视为自动检测和 Classpath 扫描候选者的“组件”。
2 自动连接uppercasefunctionCatalogHttpTriggerDemoApplication(下图)。
3 @FunctionName 注释标识指定的 Azure 函数处理程序。 当由触发器调用时(例如@HttpTrigger)、函数处理该触发器和任何其他输入,以生成一个或多个输出。
4 plainBean方法处理程序映射到使用自动连接的uppercaseSpring Bean 来计算结果。 它演示了如何在 Azure 处理程序中使用“纯”Spring 组件。
5 springCloudFunction方法处理程序映射到另一个 Azure 函数,该函数使用自动连接的FunctionCatalog实例来计算结果。
6 演示如何利用 Spring Cloud 函数目录组合 API。
使用 com.microsoft.azure.functions.annotation.* 包中包含的 Java 注释将输入和输出绑定到您的方法。

Azure 处理程序中使用的业务逻辑的实现类似于常见的 Spring 应用程序:spring-doc.cadn.net.cn

@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 @SpringBootApplicationannotated 类用作Main-ClassMain Class Configuration 中所述。
2 函数自动连接并在 Azure 函数处理程序中使用。

函数目录

Spring Cloud Function 支持用户定义函数的一系列类型签名,同时提供一致的执行模型。 为此,它使用 Function Catalog 将所有用户定义的函数转换为规范表示。spring-doc.cadn.net.cn

Azure 适配器可以自动连接任何 Spring 组件,例如uppercase以上。 但是这些被视为普通的 Java 类实例,而不是规范的 Spring Cloud Functions!spring-doc.cadn.net.cn

要利用 Spring Cloud Function 并访问规范函数表示,您需要自动连接FunctionCatalog并在您的处理程序中使用它,例如functionCatalog实例springCloudFunction()处理程序。spring-doc.cadn.net.cn

访问 Azure ExecutionContext

有时需要访问 Azure 运行时以com.microsoft.azure.functions.ExecutionContext. 例如,其中一个需求是日志记录,因此它可以显示在 Azure 控制台中。spring-doc.cadn.net.cn

为此,AzureFunctionUtil.enhanceInputIfNecessary允许您添加ExecutionContext作为 Message 标头,以便您可以通过executionContext钥匙。spring-doc.cadn.net.cn

@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_CONTEXTheader 键。

现在,您可以从消息标头中检索 ExecutionContext:spring-doc.cadn.net.cn

@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.jsonhost.json,并坚持强制打包格式spring-doc.cadn.net.cn

通常,Azure Maven(或 Gradle)插件用于从带注释的类生成必要的配置,并生成所需的包格式。spring-doc.cadn.net.cn

Azure 打包格式与默认的 Spring Boot 打包不兼容(例如uber jar). 下面的 Disable Spring Boot Plugin 部分解释了如何处理此问题。

Azure Maven/Gradle 插件

Azure 提供 MavenGradle 插件来处理带批注的类、生成必要的配置并生成预期的包布局。 插件用于设置 platform、runtime 和 app-settings 属性,如下所示:spring-doc.cadn.net.cn

<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"
}

有关运行时配置的更多信息:Java 版本部署作系统spring-doc.cadn.net.cn

禁用 Spring Boot 插件

预期中,Azure Functions 在 Azure 执行运行时中运行,而不是在 SpringBoot 运行时中运行! 此外,Azure 需要由 Azure Maven/Gradle 插件生成的特定打包格式,该格式与默认的 Spring Boot 打包不兼容。spring-doc.cadn.net.cn

您必须禁用 SpringBoot Maven/Gradle 插件或使用 Spring Boot Thin Launcher,如以下 Maven 代码段所示:spring-doc.cadn.net.cn

<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 类。spring-doc.cadn.net.cn

您可以使用 Mavenstart-class属性或设置Main-Class属性MANIFEST/META-INFO:spring-doc.cadn.net.cn

<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文件来配置函数应用。spring-doc.cadn.net.cn

{
	"version": "2.0",
	"extensionBundle": {
		"id": "Microsoft.Azure.Functions.ExtensionBundle",
		"version": "[4.*, 5.0.0)"
	}
}

host.json元数据文件包含影响函数应用实例中所有函数的配置选项。spring-doc.cadn.net.cn

如果文件不在项目 top 文件夹中,则需要相应地配置插件(如hostJsonmaven 属性)。

Azure Web 适配器

对于基于 Web 的纯函数应用程序,您可以将泛型adapter-azure使用专门的 spring-cloud-function-adapter-azure-web. Azure Web 适配器可以在内部使用 HttpTrigger 将任何 Spring Web 应用程序部署为本机 Azure 函数。 它隐藏了 Azure 注释的复杂性,而是依赖于熟悉的 Spring Web 编程模型。spring-doc.cadn.net.cn

要启用 Azure Web 适配器,请将适配器依赖项添加到您的pom.xmlbuild.gradle文件:spring-doc.cadn.net.cn

<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 Web Adapter也。spring-doc.cadn.net.cn

Azure 示例

有关详细信息,请浏览以下内容 Azure Web 适配器示例:spring-doc.cadn.net.cn

用法

构建和部署两者的通用说明Azure AdapterAzure Web Adapter应用程序类型。spring-doc.cadn.net.cn

构建

./mvnw -U clean package
./gradlew azureFunctionsPackage

在本地运行

要在Azure Functions,并且要部署到实时 Azure 环境,您将需要Azure Functions Core Tools与 Azure CLI 一起安装(请参阅此处)。 对于某些配置,您还需要 Azurite 模拟器spring-doc.cadn.net.cn

然后运行示例:spring-doc.cadn.net.cn

./mvnw azure-functions:run
./gradlew azureFunctionsRun

在 Azure 上运行

确保您已登录 Azure 帐户。spring-doc.cadn.net.cn

az login
./mvnw azure-functions:deploy
./gradlew azureFunctionsDeploy

本地调试

在调试模式下运行函数。spring-doc.cadn.net.cn

./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喜欢这个:spring-doc.cadn.net.cn

{
	"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远程调试配置:spring-doc.cadn.net.cn

{
	"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 和代码。spring-doc.cadn.net.cn