Skip to content

Commit 981825f

Browse files
authored
add examples for spring cloud function on Azure (#697)
* add examples for spring cloud function * remove license info * add hello world example * renmae folder from examples to samples
1 parent efa713a commit 981825f

16 files changed

Lines changed: 790 additions & 0 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,8 @@ pkg/
273273

274274
# Mac specific gitignore
275275
.DS_Store
276+
277+
# Azure Functions
278+
local.settings.json
279+
bin/
280+
obj/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Build output
2+
target/
3+
*.class
4+
5+
# Log file
6+
*.log
7+
8+
# BlueJ files
9+
*.ctxt
10+
11+
# Mobile Tools for Java (J2ME)
12+
.mtj.tmp/
13+
14+
# Package Files #
15+
*.jar
16+
*.war
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
25+
# IDE
26+
.idea/
27+
*.iml
28+
.settings/
29+
.project
30+
.classpath
31+
.vscode/
32+
33+
# macOS
34+
.DS_Store
35+
36+
# Azure Functions
37+
local.settings.json
38+
bin/
39+
obj/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM springcloudstream/azure-functions-java17:1.0.0
2+
3+
COPY ./target/azure-functions /src/java-function-app
4+
5+
RUN mkdir -p /home/site/wwwroot && \
6+
cd /src/java-function-app && \
7+
cd $(ls -d */|head -n 1) && \
8+
cp -a . /home/site/wwwroot
9+
10+
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
11+
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
== Running Locally `
2+
You can run this Azure function locally, similar to other Spring Cloud Function samples, however
3+
this time by using the Azure Maven plugin, as the Microsoft Azure functions execution context must be available.
4+
5+
NOTE: To run locally on top of Azure Functions, and to deploy to your live Azure environment, you will need the Azure Functions Core Tools installed along with the Azure CLI (see https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-java?tabs=bash%2Cazure-cli%2Cbrowser#configure-your-local-environment[here] for details).
6+
7+
.Follow these steps to build and run locally:
8+
[source,bash]
9+
----
10+
mvn clean package
11+
mvn azure-functions:run
12+
----
13+
.console output
14+
[source,bash]
15+
----
16+
[INFO] Azure Function App's staging directory found at: /Users/cbono/repos/spring-cloud-function/spring-cloud-function-samples/function-sample-azure/target/azure-functions/spring-cloud-function-samples
17+
4.0.3971
18+
[INFO] Azure Functions Core Tools found.
19+
20+
Azure Functions Core Tools
21+
Core Tools Version: 4.0.3971 Commit hash: d0775d487c93ebd49e9c1166d5c3c01f3c76eaaf (64-bit)
22+
Function Runtime Version: 4.0.1.16815
23+
24+
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
25+
Request starting HTTP/2 POST http://127.0.0.1:53836/AzureFunctionsRpcMessages.FunctionRpc/EventStream application/grpc -
26+
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
27+
Executing endpoint 'gRPC - /AzureFunctionsRpcMessages.FunctionRpc/EventStream'
28+
[2022-04-11T03:04:05.143Z] OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.
29+
[2022-04-11T03:04:05.247Z] Worker process started and initialized.
30+
31+
Functions:
32+
33+
echo: [GET,POST] http://localhost:7071/api/echo
34+
35+
echoStream: [GET,POST] http://localhost:7071/api/echoStream
36+
37+
uppercase: [GET,POST] http://localhost:7071/api/uppercase
38+
39+
uppercaseReactive: [GET,POST] http://localhost:7071/api/uppercaseReactive
40+
41+
For detailed output, run func with --verbose flag.
42+
[2022-04-11T03:04:10.163Z] Host lock lease acquired by instance ID '000000000000000000000000BEFE21CF'.
43+
44+
----
45+
46+
.Test the _uppercase_ function using the following _curl_ command:
47+
[source,bash]
48+
----
49+
curl -H "Content-Type: application/json" localhost:7071/api/uppercase -d '{"greeting": "hello", "name": "foo"}'
50+
----
51+
.curl response
52+
[source,json]
53+
----
54+
{
55+
"greeting": "HELLO",
56+
"name": "FOO"
57+
}
58+
----
59+
Notice that the URL is of the format `<function-base-url>/api/<function-name>`).
60+
61+
The `uppercase` function signature is `Function<Message<String>, String> uppercase()`. The implementation of `UppercaseHandler` (which extends `FunctionInvoker`) copies the HTTP headers of the incoming request into the input message's _MessageHeaders_ which makes them accessible to the function if needed.
62+
63+
NOTE: Implementation of `FunctionInvoker` (your handler), should contain the least amount of code. It is really a type-safe way to define
64+
and configure function to be recognized as Azure Function.
65+
Everything else should be delegated to the base `FunctionInvoker` via `handleRequest(..)` callback which will invoke your function, taking care of
66+
necessary type conversion, transformation etc. One exception to this rule is when custom result handling is required. In that case, the proper post-process method can be overridden as well in order to take control of the results processing.
67+
68+
.UppercaseHandler.java
69+
[source,java]
70+
----
71+
@FunctionName("uppercase")
72+
public String execute(
73+
@HttpTrigger(
74+
name = "req",
75+
methods = {HttpMethod.GET, HttpMethod.POST},
76+
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
77+
ExecutionContext context
78+
) {
79+
Message<String> message = MessageBuilder.withPayload(request.getBody().get())
80+
.copyHeaders(request.getHeaders()).build();
81+
return handleRequest(message, context);
82+
}
83+
----
84+
85+
86+
The `echo` function does the same as the `uppercase` less the actual uppercasing. However, the important difference to notice is that function itself
87+
takes primitive `String` as its input (i.e., `public Function<String, String> echo()`) while the actual handler passes instance of `Message` the same way as with `uppercase`. The framework recognizes that you only care about the payload and extracts it from the `Message` before calling the function.
88+
89+
There is also a reactive version of _uppercase_ (named _uppercaseReactive_) which will produce the same result, but
90+
demonstrates and validates the ability to use reactive functions with Azure.
91+
92+
== Running on Azure
93+
94+
NOTE: The Azure Java functions runtime does not yet support Java 17 but Spring Cloud Function 4.x requires it. To get around this limitation we deploy to Azure in a custom Docker container. Once https://github.com/Azure/azure-functions-java-worker/issues/548[Azure supports] Java 17 we can move back to using non-Docker deployments.
95+
96+
==== Custom Docker Image
97+
The steps below describe the process to create a custom Docker image which is suitable for deployment on Azure and contains the 4.x Azure Functions runtime, the MS Java 17 JVM, and the sample functions in this repo.
98+
99+
====== Image name
100+
Pick an image name for the Docker container (eg. `onobc/function-sample-azure-java17:1.0.0`) and update the _pom.xml_ `functionDockerImageName` property with the image name.
101+
102+
TIP: By default it is expected that the image name is a publicly accessible image on Docker Hub. However, other registries and credentials can be configured as described https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supporte-runtime[here].
103+
104+
.Rebuild the functions (pom.xml was updated):
105+
[source,bash]
106+
----
107+
mvn clean package
108+
----
109+
.Build the Docker image:
110+
[source,bash]
111+
----
112+
docker build -t <image-name> .
113+
----
114+
115+
Test the Docker image locally by starting the container and issuing a request.
116+
117+
.Start the function runtime locally in Docker:
118+
[source,bash]
119+
----
120+
docker run -p 8080:80 <image-name>
121+
----
122+
123+
.console output
124+
[source,bash]
125+
----
126+
cbono@cbono-a01 function-sample-azure % docker run -p 8080:80 onobc/function-sample-azure-java17:1.0.0
127+
info: Host.Triggers.Warmup[0]
128+
Initializing Warmup Extension.
129+
info: Host.Startup[503]
130+
Initializing Host. OperationId: 'e7317c18-4daa-4d69-bf38-beaa51e1a012'.
131+
info: Host.Startup[504]
132+
Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=e7317c18-4daa-4d69-bf38-beaa51e1a012
133+
info: Microsoft.Azure.WebJobs.Hosting.OptionsLoggingService[0]
134+
LoggerFilterOptions
135+
{
136+
"MinLevel": "None",
137+
"Rules": [
138+
{
139+
"ProviderName": null,
140+
"CategoryName": null,
141+
"LogLevel": null,
142+
"Filter": "<AddFilter>b__0"
143+
},
144+
{
145+
"ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
146+
"CategoryName": null,
147+
"LogLevel": "None",
148+
"Filter": null
149+
},
150+
{
151+
"ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
152+
"CategoryName": null,
153+
"LogLevel": null,
154+
"Filter": "<AddFilter>b__0"
155+
}
156+
]
157+
}
158+
...
159+
...
160+
...
161+
info: Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostHttpRoutesManager[0]
162+
Initializing function HTTP routes
163+
Mapped function route 'api/echo' [GET,POST] to 'echo'
164+
Mapped function route 'api/echoStream' [GET,POST] to 'echoStream'
165+
Mapped function route 'api/uppercase' [GET,POST] to 'uppercase'
166+
Mapped function route 'api/uppercaseReactive' [GET,POST] to 'uppercaseReactive'
167+
168+
info: Host.Startup[412]
169+
Host initialized (65ms)
170+
info: Host.Startup[413]
171+
Host started (81ms)
172+
info: Host.Startup[0]
173+
Job host started
174+
Hosting environment: Production
175+
Content root path: /azure-functions-host
176+
Now listening on: http://[::]:80
177+
Application started. Press Ctrl+C to shut down.
178+
info: Microsoft.Azure.WebJobs.Script.Workers.Rpc.RpcFunctionInvocationDispatcher[0]
179+
Worker process started and initialized.
180+
info: Host.General[337]
181+
Host lock lease acquired by instance ID '000000000000000000000000C4043012'.
182+
----
183+
184+
.Test the _uppercase_ function using the following _curl_ command:
185+
[source,bash]
186+
----
187+
curl -H "Content-Type: application/json" localhost:8080/api/uppercase -d '{"greeting": "hello", "name": "foo"}'
188+
----
189+
.curl response
190+
[source,json]
191+
----
192+
{
193+
"greeting": "HELLO",
194+
"name": "FOO"
195+
}
196+
----
197+
198+
.Push the image to Docker registry:
199+
[source,bash]
200+
----
201+
docker push <image-name>
202+
----
203+
At this point the custom image has been created and pushed to the configured Docker registry.
204+
205+
==== Deploy to Azure
206+
To deploy the functions to your live Azure environment, including automatic provisioning of an _HTTPTrigger_ for each function, do the following.
207+
208+
.Login to Azure:
209+
[source,bash]
210+
----
211+
az login
212+
----
213+
214+
.Deploy to Azure:
215+
[source,bash]
216+
----
217+
mvn azure-functions:deploy
218+
----
219+
.console output
220+
[source,bash]
221+
----
222+
[INFO] ---------------< io.spring.sample:function-sample-azure >---------------
223+
[INFO] Building function-sample-azure 4.0.0.RELEASE
224+
[INFO] --------------------------------[ jar ]---------------------------------
225+
[INFO]
226+
[INFO] --- azure-functions-maven-plugin:1.16.0:deploy (default-cli) @ function-sample-azure ---
227+
Auth type: AZURE_CLI
228+
Default subscription: SCDF-Azure(b80d18******)
229+
Username: cbono@vmware.com
230+
[INFO] Subscription: SCDF-Azure(*******)
231+
[INFO] Reflections took 123 ms to scan 6 urls, producing 24 keys and 486 values
232+
[INFO] Start creating Resource Group(java-functions-group) in region (West US)...
233+
[INFO] Resource Group(java-functions-group) is successfully created.
234+
[INFO] Reflections took 1 ms to scan 3 urls, producing 12 keys and 12 values
235+
[INFO] Creating app service plan java-functions-app-service-plan...
236+
[INFO] Successfully created app service plan java-functions-app-service-plan.
237+
[INFO] Start creating Application Insight (spring-cloud-function-samples)...
238+
[INFO] Application Insight (spring-cloud-function-samples) is successfully created. You can visit https://ms.portal.azure.com/********providers/Microsoft.Insights/components/spring-cloud-function-samples to view your Application Insights component.
239+
[INFO] Creating function app spring-cloud-function-samples...
240+
[INFO] Set function worker runtime to java.
241+
[INFO] Ignoring decoding of null or empty value to:com.azure.resourcemanager.storage.fluent.models.StorageAccountInner
242+
[INFO] Successfully created function app spring-cloud-function-samples.
243+
[INFO] Skip deployment for docker app service
244+
[INFO] ------------------------------------------------------------------------
245+
[INFO] BUILD SUCCESS
246+
[INFO] ------------------------------------------------------------------------
247+
[INFO] Total time: 01:30 min
248+
[INFO] Finished at: 2022-04-04T19:06:24-05:00
249+
[INFO] ------------------------------------------------------------------------
250+
----
251+
252+
TIP: When deployed as a Docker container the function urls are not written to the console. You will need to inspect the functions in the Azure Portal to find the urls.
253+
254+
==== Inspect in Azure Portal
255+
256+
Navigate to the https://portal.azure.com/#blade/HubsExtension/BrowseResource/resourceType/Microsoft.Web%2Fsites/kind/functionapp[Function App] dashboard in the Azure portal and then:
257+
258+
* click on your function app (`"spring-cloud-function-samples"` by default)
259+
* click the left nav `"Functions"` link
260+
* click the `"uppercase"` function
261+
262+
====== Function Url
263+
Click the `"Get Function Url"` link to see the function's url.
264+
265+
====== Test via Portal
266+
* click on the left nav `"Code and Test"`
267+
* click on `"Test/Run"` at top of page
268+
* enter the following input json in the `"Body"` section on the right-hand side:
269+
270+
[source,json]
271+
----
272+
{
273+
"greeting": "hello",
274+
"name": "foo"
275+
}
276+
----
277+
* click "Run" and the output should look like:
278+
279+
[source,json]
280+
----
281+
{
282+
"greeting": "HELLO",
283+
"name": "FOO"
284+
}
285+
----
286+
287+
===== Test via cURL
288+
Armed w/ the function url from above, issue the following curl command in another terminal:
289+
290+
[source,bash]
291+
----
292+
curl -H "Content-Type: application/json" https://spring-cloud-function-samples.azurewebsites.net/api/uppercase -d '{"greeting": "hello", "name": "foo"}'
293+
----
294+
.curl response
295+
[source,json]
296+
----
297+
{
298+
"greeting": "HELLO",
299+
"name": "FOO"
300+
}
301+
----
302+
303+
TIP: The Azure dashboard provides a plethora of information about your functions, including but not limited to execution count, memory consumption and execution time.
304+
305+
306+
==== Custom Result Handling
307+
308+
As noted above, the implementation of `FunctionInvoker` (your handler), should contain the least amount of code possible. However, if custom result handling needs to occur there is a set of methods (named `postProcess**`) that can be overridden in link:../../spring-cloud-function-adapters/spring-cloud-function-adapter-azure/src/main/java/org/springframework/cloud/function/adapter/azure/FunctionInvoker.java[FunctionInvoker.java].
309+
310+
One such example can be seen in link:src/main/java/example/ReactiveEchoCustomResultHandler.java[ReactiveEchoCustomResultHandler.java].
311+
312+
Once the function is deployed it can be tested using _curl_:
313+
314+
[source,bash]
315+
----
316+
curl -H "Content-Type: application/json" localhost:7071/api/echoStream -d '["hello","peepz"]'
317+
----
318+
.result
319+
[source,bash]
320+
----
321+
Kicked off job for [hello, peepz]
322+
----
323+
The custom result handling takes the Flux returned from the `echoStream` function and adds logging, uppercase mapping, and then subscribes to the publisher. The Azure logs output the following:
324+
325+
[source,bash]
326+
----
327+
[2022-03-01T01:36:57.439Z] 2022-02-28 19:36:57.439 INFO 20587 --- [pool-2-thread-2] o.s.boot.SpringApplication : Started application in 0.466 seconds (JVM running for 57.906)
328+
[2022-03-01T01:36:57.462Z] BEGIN echo post-processing work ...
329+
[2022-03-01T01:36:57.462Z] HELLO
330+
[2022-03-01T01:36:57.462Z] PEEPZ
331+
[2022-03-01T01:36:57.463Z] END echo post-processing work
332+
[2022-03-01T01:36:57.463Z] Function "echoStream" (Id: 678cff0b-d958-4fab-967b-e19e0d5d67e8) invoked by Java Worker
333+
----

0 commit comments

Comments
 (0)