4.0.5

微软Azure功能

Azure函数适配器,用于将Spring Cloud Function应用程序部署为本机 Azure Java 函数。

编程Azure Functions 模型广泛地依赖于 Java注释来定义函数的处理程序方法及其输入和输出类型。在编译时,带注释的类由提供的 Azure Maven/Gradle 插件处理,以生成必要的 Azure Function 绑定文件、配置和包工件。Azure 注释只是一种类型安全的方法,用于将 java 函数配置为被识别为 Azure 函数。

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

scf 天蓝色适配器
对于基于 Web 的函数应用程序,您可以将通用函数替换adapter-azure为专用的spring-cloud-function-adapter-azure-web。使用 Azure Web Adaptor,您可以将任何 Spring Web 应用程序部署为 Azure HttpTrigger 函数。该适配器隐藏了 Azure 注释的复杂性,并使用熟悉的Spring Web编程模型。有关更多信息,请参阅下面的Azure Web Adaptor部分。

1.Azure适配器

为 Azure Functions提供SpringSpring Cloud Function集成。

1.1. 依赖关系

为了启用 Azure Function 集成,请将 azure 适配器依赖项添加到您的pom.xmlbuild.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 集成。

1.2. 开发指南

使用@Component(或@Service) 注释将任何现有的 Azure Function 类(例如带有@FunctionName处理程序)转换为 Spring 组件。然后,您可以自动连接所需的依赖项(或Spring Cloud Function 组合的函数目录)并在 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", 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", 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是一个“组件”,Spring 框架将其视为自动检测和类路径扫描的候选者。
2 自动连接(如下)中定义的uppercase和beans 。functionCatalogHttpTriggerDemoApplication
3 @FunctionName注释标识指定的Azure函数处理程序。当被触发器(例如@HttpTrigger)调用时,函数会处理该触发器和任何其他输入,以产生一个或多个输出。
4 方法plainBean处理程序映射到一个 Azure 函数,该函数使用自动连接的uppercasespring bean 来计算结果。它演示了如何在 Azure 处理程序中使用“普通”Spring 组件。
5 方法springCloudFunction处理程序映射到另一个 Azure 函数,该函数使用自动连接的FunctionCatalog实例来计算结果。
6 展示如何利用 Spring Cloud Function Function Catalog组合 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 函数处理程序中使用的函数。

1.2.1. 功能目录

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

Azure 适配器可以自动连接任何 Spring 组件,例如uppercase上面的组件。但它们被视为普通的 Java 类实例,而不是规范的 Spring Cloud Functions!

要利用 Spring Cloud Function 并访问规范函数表示,您需要自动连接FunctionCatalog并在处理程序中使用它,就像上面的处理程序functionCatalog实例一样。springCloudFunction()

1.2.2. 访问 Azure ExecutionContext

有时需要访问 Azure 运行时以com.microsoft.azure.functions.ExecutionContext. 例如,此类需求之一是日志记录,因此它可以出现在 Azure 控制台中。

为此,AzureFunctionUtil.enhanceInputIfNecessary允许您添加 的实例ExecutionContext作为消息标头,以便您可以通过executionContext密钥检索它。

@FunctionName("myfunction")
public String execute(
	@HttpTrigger(name = "req", 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内联as 消息标头。contextAzureFunctionUtil.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 实例。

1.3. 配置

要在 Microsoft Azure 上运行函数应用程序,您必须提供必要的配置,例如function.jsonhost.json,并遵守强制 打包格式

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

Azure打包格式与默认的 Spring Boot 打包(例如 )不兼容uber jar。下面的禁用Spring Boot 插件部分解释了如何处理这个问题。

1.3.1. Azure Maven/Gradle 插件

Azure 提供MavenGradle插件来处理带注释的类、生成必要的配置并生成预期的包布局。插件用于设置平台、运行时和应用程序设置属性,如下所示:

梅文
<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 版本部署操作系统

1.3.2. 禁用 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>

1.3.3. 主级配置

指定Main-Class/Start-Class指向您的 Spring 应用程序入口点,例如上例中的HttpTriggerDemoApplication类。

您可以使用 Mavenstart-class属性或设置Main-Class以下属性MANIFEST/META-INFO

梅文
<properties>
	<start-class>YOUR APP MAIN CLASS</start-class>
	...
</properties>
摇篮
jar {
    manifest {
        attributes(
            "Main-Class": "YOUR-APP-MAIN-CLASS"
        )
    }
}
或者,您可以使用MAIN_CLASS环境变量显式设置类名。对于本地运行,请将MAIN_CLASS变量添加到文件中,对于 Azure 门户部署,请在应用程序设置local.settings.json中设置变量。
如果MAIN_CLASS未设置该变量,Azure 适配器会MANIFEST/META-INFO从类路径上找到的 jar 中查找属性,并选择第一个Main-Class:带有 a@SpringBootApplication@SpringBootConfiguration注释的属性。

1.3.4. 元数据配置

您可以使用共享的host.json文件来配置函数应用。

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

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

如果该文件不在项目顶部文件夹中,您需要相应地配置您的插件(如hostJsonmaven 属性)。

1.4. 样品

以下是您可以探索的各种 Spring Cloud Function Azure 适配器示例的列表:

2. Azure Web 适配器

adapter-azure对于纯粹的、基于 Web 的函数应用程序,您可以使用专门的spring-cloud-function-adapter-azure-web替换通用函数。Azure Web Adaptor 可以在内部使用 HttpTrigger 将任何 Spring Web 应用程序部署为本机 Azure 函数。它隐藏了 Azure 注释的复杂性,而是依赖于熟悉的Spring Web编程模型。

要启用 Azure Web Adaptor,请将适配器依赖项添加到您的pom.xmlbuild.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 Web Adapter

2.1. 样品

有关更多信息,请浏览以下 Azure Web Adaptor 示例:

3. 使用方法

构建和部署应用程序类型的通用Azure Adapter说明Azure Web Adapter

3.1. 建造

梅文
./mvnw -U clean package
摇篮
./gradlew azureFunctionsPackage

3.2. 本地运行

要在本地运行Azure Functions并部署到实时 Azure 环境,您需要Azure Functions Core Tools与 Azure CLI 一起安装(请参阅此处)。对于某些配置,您还需要Azurite 模拟器。

然后运行示例:

梅文
./mvnw azure-functions:run
摇篮
./gradlew azureFunctionsRun

3.3. 在 Azure 上运行

确保您已登录 Azure 帐户。

az login

并部署

梅文
./mvnw azure-functions:deploy
摇篮
./gradlew azureFunctionsDeploy

3.4. 本地调试

在调试模式下运行该函数。

梅文
./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"
		},
	]
}

4.FunctionInvoker(已弃用)

旧的FunctionInvoker编程模型已被弃用,并且今后将不再受支持。

有关函数集成方法的其他文档和示例,请遵循azure-sample README 和代码。