Skip to content

Commit fd9b6d5

Browse files
committed
Streamline the azure adoc
1 parent 3bafcc5 commit fd9b6d5

File tree

2 files changed

+59
-36
lines changed
  • docs/src/main/asciidoc/adapters
  • spring-cloud-function-samples/function-sample-azure-http-trigger-gradle

2 files changed

+59
-36
lines changed

docs/src/main/asciidoc/adapters/azure-intro.adoc

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ files:
4141
.Gradle
4242
----
4343
dependencies {
44-
implementation "org.springframework.cloud:spring-cloud-function-adapter-azure:<last version>"
44+
implementation 'org.springframework.cloud:spring-cloud-function-adapter-azure'
4545
}
4646
----
4747
====
@@ -50,8 +50,8 @@ NOTE: version `4.0.0+` is required. Having the adapter on the classpath activate
5050

5151
=== Function Implementation
5252

53-
You can use the `@Component` (or `@Service`) annotation to turn any exiting Azure Function class (e.g. class with `@FunctionName` annotated handlers) into a Spring component.
54-
Then you can auto-wire the required dependencies (or the https://docs.spring.io/spring-cloud-function/docs/current/reference/html/spring-cloud-function.html#_function_catalog_and_flexible_function_signatures[FunctionCatalog] instance for Spring Cloud Function) and use those inside your Azure function handler.
53+
Use the `@Component` (or `@Service`) annotation to turn any exiting Azure Function class (e.g. with `@FunctionName` handlers) into a Spring component.
54+
Then you can auto-wire the required dependencies (or the https://docs.spring.io/spring-cloud-function/docs/current/reference/html/spring-cloud-function.html#_function_catalog_and_flexible_function_signatures[FunctionCatalog] for Spring Cloud Function composition) and use those inside the Azure function handlers.
5555

5656
[source,java]
5757
----
@@ -64,39 +64,39 @@ public class MyAzureFunction {
6464
// The FunctionCatalog leverages the Spring Cloud Function framework.
6565
@Autowired private FunctionCatalog functionCatalog; // <2>
6666
67-
@FunctionName("spring")
68-
public String plainBean( // <3>
67+
@FunctionName("spring") // <3>
68+
public String plainBean( // <4>
6969
@HttpTrigger(name = "req", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
7070
ExecutionContext context) {
7171
7272
return this.uppercase.apply(request.getBody().get());
7373
}
7474
75-
@FunctionName("scf")
76-
public String springCloudFunction( // <4>
75+
@FunctionName("scf") // <3>
76+
public String springCloudFunction( // <5>
7777
@HttpTrigger(name = "req", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
7878
ExecutionContext context) {
7979
8080
// Use SCF composition. Composed functions are not just spring beans but SCF such.
81-
Function composed = this.functionCatalog.lookup("reverse|uppercase"); // <5>
81+
Function composed = this.functionCatalog.lookup("reverse|uppercase"); // <6>
8282
8383
return (String) composed.apply(request.getBody().get());
8484
}
8585
}
8686
----
87-
Azure's programming-model uses the https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption#java-function-basics[@FunctionName] method annotation to identify the designated function handlers.
88-
When invoked by a trigger (such as `@HttpTrigger`), functions process that trigger, and any other inputs, to produce one or more outputs.
89-
90-
TIP: Use the Java annotations included in the https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.functions.annotation?view=azure-java-stable[com.microsoft.azure.functions.annotation.*] package to bind input and outputs to your methods.
9187

9288
<1> Indicates that the `MyAzureFunction` class is a "component" to be considered by the Spring Framework as a candidate for auto-detection and classpath scanning.
9389
<2> Auto-wire the `uppercase` and `functionCatalog` beans defined in the `HttpTriggerDemoApplication` (below).
94-
<3> The `plainBean` method handler is mapped to an Azure function that uses of the auto-wired `uppercase` spring bean to compute the result.
90+
<3> The https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption#java-function-basics[@FunctionName] annotation identifies the designated Azure function handlers.
91+
When invoked by a trigger (such as `@HttpTrigger`), functions process that trigger, and any other inputs, to produce one or more outputs.
92+
<4> The `plainBean` method handler is mapped to an Azure function that uses of the auto-wired `uppercase` spring bean to compute the result.
9593
It demonstrates how to use "plain" Spring components in your Azure handlers.
96-
<4> The `springCloudFunction` method handler is mapped to another Azure function, that uses the auto-wired `FunctionCatalog` instance to compute the result.
97-
<5> Shows how to leverage the Spring Cloud Function https://docs.spring.io/spring-cloud-function/docs/current/reference/html/spring-cloud-function.html#_function_catalog_and_flexible_function_signatures[FunctionCatalog] composition API.
94+
<5> The `springCloudFunction` method handler is mapped to another Azure function, that uses the auto-wired `FunctionCatalog` instance to compute the result.
95+
<6> Shows how to leverage the Spring Cloud Function https://docs.spring.io/spring-cloud-function/docs/current/reference/html/spring-cloud-function.html#_function_catalog_and_flexible_function_signatures[FunctionCatalog] composition API.
9896

99-
The actual Spring defined beans used inside the handlers looks like this:
97+
TIP: Use the Java annotations included in the https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.functions.annotation?view=azure-java-stable[com.microsoft.azure.functions.annotation.*] package to bind input and outputs to your methods.
98+
99+
The implementation of the business logic used inside the Azure handlers looks like a common Spring application:
100100

101101
[source,java]
102102
----
@@ -118,7 +118,7 @@ public class HttpTriggerDemoApplication {
118118
}
119119
}
120120
----
121-
<1> the `@SpringBootApplication` annotated class should be used as Main-Class (or Start-Class) in you Maven/Gradle configurations as explained in <<star-class-configuration, star class configuration>>.
121+
<1> The `@SpringBootApplication` annotated class is used as a `Main-Class` (or `Start-Class`) as explained in <<star-class-configuration, star class configuration>>.
122122
<2> Functions auto-wired and used in the Azure function handlers.
123123

124124
==== Accessing Azure ExecutionContext
@@ -130,7 +130,7 @@ For that purpose the `AzureFunctionUtil.enhanceInputIfNecessary` allow you to ad
130130

131131
[source,java]
132132
----
133-
@FunctionName("ditest")
133+
@FunctionName("myazurefunction")
134134
public String execute(
135135
@HttpTrigger(name = "req", authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
136136
ExecutionContext context) {
@@ -159,20 +159,16 @@ public Function<Message<String>, String> uppercase(JsonMapper mapper) {
159159
----
160160
<1> Retrieve the ExecutionContext instance from the header.
161161

162-
=== Configuration and Project Layout
163-
164-
You don't need the Spring Cloud Function Web at runtime in Azure, so you can exclude this before you create the JAR you deploy to Azure, but it won't be used if you include it, so it doesn't hurt to leave it in.
165-
A function application on Azure is an archive generated either by the Maven (`azure-functions-maven-plugin`) or the Gradle(`azure-functions-gradle-plugin`) plugins.
166-
The function lives in the JAR file generated by this project.
162+
=== Project Layout
167163

168-
The sample creates it as an executable jar, using the thin layout, so that Azure can find the handler classes. If you prefer you can just use a regular flat JAR file.
169-
The dependencies should *not* be included.
170-
171-
In order to run Spring Cloud Function applications on Microsoft Azure, you have to use Maven or Gradle plugins offered by Azure.
172-
173-
Provide the Azure-specific configuration for your application, specifying the `resourceGroup`, `appName` and other optional properties.
164+
In order to run Spring Cloud Function applications on Microsoft Azure, you have to use the Maven or Gradle plugins offered by Azure.
165+
Later imposes a specific https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption#folder-structure[package archive structure] that interferes with the `standard` Spring Boot package jars.
166+
The <<disable-spring-boot-layout,Disable Spring Boot Layout>> section below explains how to handle this.
167+
You have to provide Azure specific configurations such as the `resourceGroup`, `appName` and other optional properties.
174168
More information about the runtime configurations: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption#java-versions[Java Versions], https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-java?tabs=bash%2Cconsumption#specify-the-deployment-os[Deployment OS].
175169

170+
==== Azure Maven/Gradle Plugin
171+
176172
Sample Azure Function (Maven/Gradle) configuration would like like:
177173

178174
====
@@ -252,6 +248,16 @@ azurefunctions {
252248

253249
The complete plugin documentation is available at the https://github.com/microsoft/azure-maven-plugins/tree/develop/azure-functions-maven-plugin[Azure Maven] and https://github.com/microsoft/azure-gradle-plugins/tree/master/azure-functions-gradle-plugin[Azure Gradle] repositories.
254250

251+
252+
==== Disable Spring Boot Layout
253+
254+
The Azure Functions come with their own (non-Boot) execution runtime that imposes its own packaging format generated by the Azure's maven/gradle plugins.
255+
Therefore we can't use the (standard) Spring Boot packaging.
256+
You have to either disable the Spring-Boot plugin all together or opt for the https://github.com/dsyer/spring-boot-thin-launcher[spring-boot-thin-launcher] instead.
257+
This https://github.com/spring-cloud/spring-cloud-function/blob/3bafcc59175fb61e323e393487d413441287a450/spring-cloud-function-samples/function-sample-azure-http-trigger/pom.xml#L97-L107[snipped] illustrates how to use the `spring-boot-thin-launcher` for Maven.
258+
259+
==== Provide Main-Class
260+
255261
[[star-class-configuration]]
256262
Next you must specify the `Start-Class` or `Main-Class` to point to your application main class.
257263

@@ -271,31 +277,48 @@ Next you must specify the `Start-Class` or `Main-Class` to point to your applica
271277
jar {
272278
manifest {
273279
attributes(
274-
"Main-Class": "YOUR APP MAIN CLASS"
280+
"Main-Class": "YOUR-APP-MAIN-CLASS"
275281
)
276282
}
277283
}
278284
----
279285
====
280286

281-
IMPORTANT: The main class provided must be annotated by `@SpringBootApplication` or `@SpringBootConfiguration` annotation.
287+
Alternatively you can explicitly set the main class using the `MAIN_CLASS` environment variable.
288+
289+
For local runs, you can set the `MAIN_CLASS` in your the `local.settings.json`:
290+
291+
[source,json]
292+
----
293+
{
294+
"IsEncrypted": false,
295+
"Values": {
296+
... ,
297+
"MAIN_CLASS": "YOUR-APP-MAIN-CLASS"
298+
}
299+
}
300+
----
282301

302+
IMPORTANT: When not set via the `MAIN_CLASS` variable, the Azure adapter will try to retrieve the main class from the `MANIFEST/META-INFO` sections of all dependencies.
303+
The first main class annotated with a `@SpringBootApplication` or `@SpringBootConfiguration` is selected.
283304

284-
You will also have to ensure that the files to be scanned by the plugin can be found in the Azure functions staging directory (see the https://github.com/microsoft/azure-maven-plugins[plugin repository] for more details on the staging directory and it's default location).
305+
==== Configuration Metadata
285306

286-
Add the `host.json` configuration file:
307+
You can use a shared https://learn.microsoft.com/en-us/azure/azure-functions/functions-host-json[host.json] file to configure the function app.
287308

288309
[source,json]
289310
----
290311
{
291312
"version": "2.0",
292313
"extensionBundle": {
293314
"id": "Microsoft.Azure.Functions.ExtensionBundle",
294-
"version": "[3.*, 4.0.0)"
315+
"version": "[4.*, 5.0.0)"
295316
}
296317
}
297318
----
298319

320+
The host.json metadata file contains configuration options that affect all functions in a function app instance.
321+
299322
TIP: If the file is not in the project top folder you need to configure your plugins accordingly (like `hostJson` maven attribute).
300323

301324
=== Samples
@@ -334,7 +357,7 @@ files:
334357
.Gradle
335358
----
336359
dependencies {
337-
implementation "org.springframework.cloud:spring-cloud-function-adapter-azure-web:<last-version>"
360+
implementation 'org.springframework.cloud:spring-cloud-function-adapter-azure-web'
338361
}
339362
----
340363
====

spring-cloud-function-samples/function-sample-azure-http-trigger-gradle/host.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"version": "2.0",
33
"extensionBundle": {
44
"id": "Microsoft.Azure.Functions.ExtensionBundle",
5-
"version": "[3.*, 4.0.0)"
5+
"version": "[4.*, 5.0.0)"
66
}
77
}

0 commit comments

Comments
 (0)