Update the azure adapter doc

- Add explanation for usiing Spring Cloud Function via the catalog (vs. plain bean)
 - minor text corrections.
This commit is contained in:
Christian Tzolov
2023-03-03 10:36:35 +01:00
parent ebd8b3138b
commit 69e29e347d

View File

@@ -6,36 +6,64 @@ The https://azure.microsoft.com[Azure] adapter bootstraps a Spring Cloud Functio
Azure Functions has quite a unique and invasive programming model, involving annotations in user code that are specific to the Azure platform. Azure Functions has quite a unique and invasive programming model, involving annotations in user code that are specific to the Azure platform.
However, it is important to understand that because of the style of integration provided by Spring Cloud Function, this annotation-based programming model is simply a type-safe way to configure your simple java function (function that has no awareness of Azure) to be recognized as Azure function. However, it is important to understand that because of the style of integration provided by Spring Cloud Function, this annotation-based programming model is simply a type-safe way to configure your simple java function (function that has no awareness of Azure) to be recognized as Azure function.
All you need to annotate the your class with `@Component` or `@Service` annotations, auto-wire the required Spring Cloud Function beans, define and configure your Azure function handler. This Azure handler method provides input and output types as annotated method parameters (enabling Azure to inspect the class and create JSON bindings). All you need to annotate the your class with `@Component` or `@Service` annotations, auto-wire the required Spring beans (or the `FunctionCatalog` when using Spring Cloud Function), define and configure your Azure function handler. This Azure handler method provides input and output types as annotated method parameters (enabling Azure to inspect the class and create JSON bindings).
[source,java] [source,java]
---- ----
@Component @Component
public class MyAzureFunction { public class MyAzureFunction {
@Autowired /**
private Function<String, String> uppercase; * Plain Spring bean (not Spring Cloud Functions!)
*/
@Autowired
private Function<String, String> uppercase;
@FunctionName("ditest") /**
public String execute( * The FunctionCatalog leverages the Spring Cloud Function framework.
*/
@Autowired
private FunctionCatalog functionCatalog;
@FunctionName("bean")
public String plainBean(
@HttpTrigger(name = "req", methods = { HttpMethod.GET, @HttpTrigger(name = "req", methods = { HttpMethod.GET,
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
ExecutionContext context) { ExecutionContext context) {
return this.uppercase.apply(request.getBody().get()); return this.uppercase.apply(request.getBody().get());
} }
@FunctionName("scf")
public String springCloudFunction(
@HttpTrigger(name = "req", methods = { HttpMethod.GET,
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");
return (String) composed.apply(request.getBody().get());
}
} }
---- ----
Aside form providing configuration via Azure annotation, inside the body of this handler method we make use of the `uppercase` bean to compute the result. The `plainBean` method will be mapped to the `bean` Azure function and when executed this method uses of the plain `uppercase` bean to compute the result.
The actual user function you're delagating to looks like this The `springCloudFunction` method, mapped to the `scf` Azure function shows how to use the Spring Cloud Function composition.
The actual Spring defined functions you're delegating to looks like this:
[source,java] [source,java]
---- ----
@Bean @Bean
public Function<String, String> uppercase() { public Function<String, String> uppercase() {
return payload -> payload.toUpperCase(); return payload -> payload.toUpperCase();
}
@Bean
public Function<String, String> reverse() {
return payload -> new StringBuilder(payload).reverse().toString();
} }
---- ----
@@ -213,7 +241,7 @@ and deploy
Run the function in debug mode. Run the function in debug mode.
---- ----
./mvnw azure-functions:deploy -DenableDebug ./mvnw azure-functions:run -DenableDebug
---- ----
Alternatively and the `JAVA_OPTS` value to your `local.settings.json` like this: Alternatively and the `JAVA_OPTS` value to your `local.settings.json` like this: