|
对于最新稳定版本,请使用 spring-cloud-function 5.0.0! |
Microsoft Azure Functions
Azure function adapter for deploying春云函数应用程序作为原生 Azure Java 函数。
这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 Adapter
提供Spring & 春云函数Azure Functions integration for Azure Functions.
依赖
为了启用Azure Function集成,请将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函数类(例如,带有@FunctionNamehandler)被绑定为Spring组件。然后你可以自动布线所需的依赖关系(或Function Catalog for 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类是Spring框架中应考虑的“组件”,作为自动检测和类路径扫描的候选对象。 |
| 2 | 自动接线大写和函数目录定义在HttpTriggerDemoApplication(见下文) |
| 3 | @FunctionName注释标识指定的 Azure 函数处理器。当触发器调用时(例如@HttpTrigger),函数处理触发该输入及其他输入,产生一个或多个输出。 |
| 4 | 这plainBean方法处理程序映射到使用 自动有线的 Azure 函数大写用 Spring Bean 计算结果。它演示了如何在 Azure 处理程序中使用“普通”的 Spring 组件。 |
| 5 | 这springCloud功能方法处理程序映射到另一个Azure函数,该函数使用自动有线功能目录实例以计算结果。 |
| 6 | 展示了如何利用 Spring Cloud 函数函数目录 composition 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注释类被用作主级如主类配置中所述。 |
| 2 | 函数自动接线,并用于Azure函数处理程序。 |
功能目录
Spring Cloud 函数支持用户自定义函数的多种类型签名,同时提供一致的执行模型。为此,它使用函数目录将所有用户自定义函数转换为规范表示。
Azure 适配器可以自动接线任何 Spring 组件,例如大写以上。 但这些都被视为普通的 Java 类实例,而不是标准的 Spring Cloud 函数!
要利用Spring Cloud Function并访问规范函数表示,你需要自动接线功能目录并且在你的操作员中使用它,比如函数目录实例springCloudFunction()上方是控者。
Accessing Azure ExecutionContext
有时需要访问 Azure 运行时提供的目标执行上下文,形式为com.microsoft.azure.functions.ExecutionContext. 例如,其中一个需求是日志,这样它就能出现在 Azure 控制台中。
为此AzureFunctionUtil.enhanceInputIfNecessary允许你添加一个实例执行上下文作为Message头,你可以通过以下方式获取执行上下文钥匙。
@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用于联结上下文作为消息头,使用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 打包不兼容(例如超级罐).
下面的“禁用 Spring 启动插件”部分会解释如何处理这个问题。 |
Azure Maven/Gradle Plugins
-
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函数运行在Azure执行运行时,而不是在SpringBoot运行时! 此外,Azure 期望采用由 Azure Maven/Gradle 插件生成的特定打包格式,且与默认的 Spring Boot 打包不兼容。
你必须禁用 SpringBoot Maven/Gradle 插件,或者像这个 Maven 片段所示使用 Spring Boot Thin Launcher:
<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>
主级配置
具体说明主级/起始级指向你的 Spring 应用入口点,比如上面示例中的 HttpTriggerDemoApplication 类。
你可以用Maven起始级属性或设主级你的属性清单/元信息:
-
Maven
-
Gradle
<properties>
<start-class>YOUR APP MAIN CLASS</start-class>
...
</properties>
jar {
manifest {
attributes(
"Main-Class": "YOUR-APP-MAIN-CLASS"
)
}
}
或者你也可以使用MAIN_CLASSenvironment 变量用来显式设置类名。
对于局部运行,添加MAIN_CLASS变量为你的local.settings.json文件,Azure 门户部署时在应用设置中设置变量。 |
如果MAIN_CLASS变量未设置,Azure适配器查找清单/元信息从类路径上找到的jar中获取属性,并选择第一个主级:注释为 a@SpringBootApplication或@SpringBootConfiguration注解。 |
元数据配置
你可以用共享host.json文件来配置函数应用。
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
host.json元数据文件包含影响函数应用实例中所有功能的配置选项。
如果文件不在项目顶部文件夹,你需要相应配置插件(比如hostJsonMaven属性)。 |
Azure Web Adapter
对于纯粹的基于Web的功能应用,你可以替换通用的adapter-azure使用了专用的 Spring-Cloud-Function-Adapter-Azure-Web。
Azure Web 适配器可以作为 Azure 的原生函数部署任何 Spring Web 应用,内部使用 HttpTrigger。
它隐藏了 Azure 注释的复杂性,转而依赖熟悉的 Spring Web 编程模型。
要启用Azure Web Adapter,请在你的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示例README和代码。