Documentation Microsoft ["Créez votre première fonction à l'aide de Java et Maven (aperçu)"](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first- J'ai essayé java-maven) dans l'environnement suivant.
Puisqu'il a fonctionné en toute sécurité, le mémo suivant.
J'ai installé Azure Functions Core Tools en faisant référence à Documentation. Conformément à la procédure, j'ai d'abord installé .NET Core.
Téléchargez le SDK .NET Core sur la page de téléchargement .NET Core (https://www.microsoft.com/net/download)
Poursuivez l'installation.
L'installation est terminée.
Installez Azure Functions Core Tools avec npm. J'ai utilisé npm 5.6.0 qui était déjà installé dans mon environnement.
npm install -g azure-functions-core-tools@core
Créez un modèle à l'aide des ["azure-functions-archetypes"] de Maven (https://github.com/Microsoft/azure-maven-archetypes).
Dans VS Code, sélectionnez ** "View" ** -> ** "Command Palette" ** - ** "Maven: Generate from Maven Archetype" ** et sélectionnez le dossier de destination.
** Sélectionnez "azure-functions-archetype" **.
Ensuite, le traitement suivant démarrera automatiquement.
PS D:\web\azure\AzureFunctionsJava> cmd /c mvn archetype:generate -DarchetypeArtifactId="azure-functions-archetype" -DarchetypeGroupId="com.microsoft.azure"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] Archetype [com.microsoft.azure:azure-functions-archetype:1.15] found in catalog remote
Downloading from central: https://repo.maven.apache.org/maven2/com/microsoft/azure/azure-functions-archetype/1.15/azure-functions-archetype-1.15.pom
Downloaded from central: https://repo.maven.apache.org/maven2/com/microsoft/azure/azure-functions-archetype/1.15/azure-functions-archetype-1.15.pom (1.5 kB at 4.3 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/microsoft/azure/azure-functions-archetype/1.15/azure-functions-archetype-1.15.jar
Downloaded from central: https://repo.maven.apache.org/maven2/com/microsoft/azure/azure-functions-archetype/1.15/azure-functions-archetype-1.15.jar (15 kB at 44 kB/s)
Réglez sur interactif. Ici, seuls groupId et artifactId sont spécifiés, et le reste est laissé vide par défaut.
Define value for property 'groupId' (should match expression '[A-Za-z0-9_\-\.]+'): tech.kikutaro
[INFO] Using property: groupId = tech.kikutaro
Define value for property 'artifactId' (should match expression '[A-Za-z0-9_\-\.]+'): AzureFunctionsJava
[INFO] Using property: artifactId = AzureFunctionsJava
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' tech.kikutaro: :
Define value for property 'appName' azurefunctionsjava-20180917211315850: :
Define value for property 'appRegion' westus: :
Define value for property 'resourceGroup' java-functions-group: :
Confirm properties configuration:
groupId: tech.kikutaro
groupId: tech.kikutaro
artifactId: AzureFunctionsJava
artifactId: AzureFunctionsJava
version: 1.0-SNAPSHOT
package: tech.kikutaro
appName: azurefunctionsjava-20180917211315850
appRegion: westus
resourceGroup: java-functions-group
Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: azure-functions-archetype:1.15
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: tech.kikutaro
[INFO] Parameter: artifactId, Value: AzureFunctionsJava
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: tech.kikutaro
[INFO] Parameter: packageInPathFormat, Value: tech/kikutaro
[INFO] Parameter: appName, Value: azurefunctionsjava-20180917211315850
[INFO] Parameter: resourceGroup, Value: java-functions-group
[INFO] Parameter: package, Value: tech.kikutaro
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: tech.kikutaro
[INFO] Parameter: appRegion, Value: westus
[INFO] Parameter: artifactId, Value: AzureFunctionsJava
[INFO] Project created from Archetype in dir: D:\web\azure\AzureFunctionsJava\AzureFunctionsJava
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:20 min
[INFO] Finished at: 2018-09-17T21:13:37+09:00
[INFO] ------------------------------------------------------------------------
Ceci complète le modèle. Bien qu'il soit appelé un modèle, le code suivant est écrit, vous pouvez donc le déplacer tel quel.
package tech.kikutaro;
import java.util.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
/**
* Azure Functions with HTTP Trigger.
*/
public class Function {
/**
* This function listens at endpoint "/api/HttpTrigger-Java". Two ways to invoke it using "curl" command in bash:
* 1. curl -d "HTTP Body" {your host}/api/HttpTrigger-Java
* 2. curl {your host}/api/HttpTrigger-Java?name=HTTP%20Query
*/
@FunctionName("HttpTrigger-Java")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger processed a request.");
// Parse query parameter
String query = request.getQueryParameters().get("name");
String name = request.getBody().orElse(query);
if (name == null) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
} else {
return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
}
}
}
Construire.
mvn clean package
Ci-dessous, certaines parties de téléchargement sont omises.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< tech.kikutaro:AzureFunctionsJava >------------------
[INFO] Building Azure Java Functions 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ AzureFunctionsJava ---
[INFO] Deleting D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ AzureFunctionsJava ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ AzureFunctionsJava ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ AzureFunctionsJava ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ AzureFunctionsJava ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\test-classes
[WARNING] /D:/web/azure/AzureFunctionsJava/AzureFunctionsJava/src/test/java/tech/kikutaro/FunctionTest.java: D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\src\test\java\tech\kikutaro\FunctionTest.Les opérations Java sont décochées ou dangereuses.
[WARNING] /D:/web/azure/AzureFunctionsJava/AzureFunctionsJava/src/test/java/tech/kikutaro/FunctionTest.java:Le détail est,-Xlint:Recon avec l'option décochée
Veuillez empiler.
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ AzureFunctionsJava ---
[INFO] Surefire report directory: D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running tech.kikutaro.FunctionTest
9 17, 2018 9:25:20 h technicien.kikutaro.Function run
information: Java HTTP trigger processed a request.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.302 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-dependency-plugin:3.1.1:copy-dependencies (copy-dependencies) @ AzureFunctionsJava ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ AzureFunctionsJava ---
[INFO] Building jar: D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\AzureFunctionsJava-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- azure-functions-maven-plugin:1.0.0-beta-6:package (package-functions) @ AzureFunctionsJava ---
Downloading from central: https://repo.maven.apache.org/maven2/com/microsoft/azure/azure-maven-plugin-lib/1.2.0/azure-maven-plugin-lib-1.2.0.pom
AI: INFO 17-09-2018 21:27, 1: Configuration file has been successfully found as resource
AI: INFO 17-09-2018 21:27, 1: Configuration file has been successfully found as resource
[INFO]
[INFO] Step 1 of 7: Searching for Azure Functions entry points
[INFO] 1 Azure Functions entry point(s) found.
[INFO]
[INFO] Step 2 of 7: Generating Azure Functions configurations
[INFO] Generation done.
[INFO]
[INFO] Step 3 of 7: Validating generated configurations
[INFO] Validation done.
[INFO]
[INFO] Step 4 of 7: Saving empty host.json
[INFO] Successfully saved to D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850\host.json
[INFO]
[INFO] Step 5 of 7: Saving configurations to function.json
[INFO] Starting processing function: HttpTrigger-Java
[INFO] Successfully saved to D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850\HttpTrigger-Java\function.json
[INFO]
[INFO] Step 6 of 7: Copying JARs to staging directoryD:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource to D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850
[INFO] Copied successfully.
[INFO]
[INFO] Step 7 of 7: Installing function extensions if needed
Writing C:\Users\kikuta\AppData\Local\Temp\tmpC1CE.tmp
info :paquet'Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator'Projet PackageReference'D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\extensions.csproj'J'ajoute à.
log : D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\extensions.Restauration du package csproj...
info :paquet'Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator'Est un projet'D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\extensions.csproj'Compatible avec tous les frameworks spécifiés de.
info :paquet'Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator'version'1.0.0'PackageReference est un fichier'D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\extensions.csproj'Mis à jour avec.
.Microsoft pour NET Core(R)Build Engine version 15.8.166+gd4e8d81a88
Copyright (C) Microsoft Corporation.All rights reserved.
D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\extensions.Restauration du package csproj...
Fichier MSBuild D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\obj\extensions.csproj.nuget.g.Générer des accessoires.
Fichier MSBuild D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\obj\extensions.csproj.nuget.g.Générer des cibles.
D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\extensions.La restauration de csproj est 971.Terminé en 41 ms.
extensions -> D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850\bin\extensions.dll
La construction a réussi.
0 avertissements
0 erreur
Temps écoulé 00:00:17.67
[INFO] Function extension installation done.
[INFO] Successfully built Azure Functions.
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:copy-resources (copy-resources) @ AzureFunctionsJava ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:07 min
[INFO] Finished at: 2018-09-17T21:28:15+09:00
[INFO] ------------------------------------------------------------------------
Je le ferai.
mvn azure-functions:run
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< tech.kikutaro:AzureFunctionsJava >------------------
[INFO] Building Azure Java Functions 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- azure-functions-maven-plugin:1.0.0-beta-6:run (default-cli) @ AzureFunctionsJava ---
AI: INFO 17-09-2018 22:00, 1: Configuration file has been successfully found as resource
AI: INFO 17-09-2018 22:00, 1: Configuration file has been successfully found as resource
[INFO] Azure Function App's staging directory found at: D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850
[INFO] Azure Functions Core Tools found.
%%%%%%
%%%%%%
@ %%%%%% @
@@ %%%%%% @@
@@@ %%%%%%%%%%% @@@
@@ %%%%%%%%%% @@
@@ %%%% @@
@@ %%% @@
@@ %% @@
%%
%
Azure Functions Core Tools (2.0.1-beta.38)
Function Runtime Version: 2.0.12050.0
Skipping 'AzureWebJobsStorage' from local settings as it's already defined in current environment variables.
Skipping 'AzureWebJobsDashboard' from local settings as it's already defined in current environment variables.
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using 'C:\Users\kikuta\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
info: Host.Startup[0]
Reading host configuration file 'D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850\host.json'info: Host.Startup[0]
Host configuration file read:
{
"version": "2.0"
}
[2018/09/17 13:00:43] Initializing Host.
[2018/09/17 13:00:43] Host initialization: ConsecutiveErrors=0, StartupCount=1
[2018/09/17 13:00:44] Starting JobHost
[2018/09/17 13:00:44] Starting Host (HostId=xxxxxxxxx-xxxxxxxx, InstanceId=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, Version=2.0.12050.0, ProcessId=11372, AppDomainId=1, Debug=False, FunctionsExtensionVersion=)
[2018/09/17 13:00:44] Starting language worker process:C:\Program Files\Java\jdk1.8.0_152\bin\java -jar "C:\Users\kikuta\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\java\azure-functions-java-worker.jar" --host 127.0.0.1 --port 36887 --workerId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --requestId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --grpcMaxMessageLength 134217728
[2018/09/17 13:00:44] C:\Program Files\Java\jdk1.8.0_152\bin\java process with Id=26080 started
[2018/09/17 13:00:44] Generating 1 job function(s)
[2018/09/17 13:00:44] Found the following functions:
[2018/09/17 13:00:44] Host.Functions.HttpTrigger-Java
[2018/09/17 13:00:44]
[2018/09/17 13:00:44] Host initialized (678ms)
[2018/09/17 13:00:44] Host started (695ms)
[2018/09/17 13:00:44] Job host started
Hosting environment: Production
Content root path: D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
Listening on http://0.0.0.0:7071/
Hit CTRL-C to exit...
Http Functions:
HttpTrigger-Java: http://localhost:7071/api/HttpTrigger-Java
[2018/09/17 13:00:45] Microsoft Azure Functions Java Runtime [build 1.1.0-beta9]
[2018/09/17 13:00:49] [INFO] {MessageHandler.handle}: Message generated by "StartStream.Builder"
[2018/09/17 13:00:49] Host lock lease acquired by instance ID '0000000000000000000000000xxxxxxx'.
[2018/09/17 13:00:57] Worker initialized
[2018/09/17 13:00:57] "HttpTrigger-Java" loaded (ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, Reflection: "D:\web\azure\AzureFunctionsJava\AzureFunctionsJava\target\azure-functions\azurefunctionsjava-20180917211315850\AzureFunctionsJava-1.0-SNAPSHOT.jar"::"tech.kikutaro.Function.run")
Lorsque j'ai accédé à "http: // localhost: 7071", ce qui suit était affiché.
De plus, j'ai confirmé l'affichage suivant sur "http: // localhost: 7071 / api / HttpTrigger-Java? Name = kikutaro".
Puisque nous avons pu confirmer l'opération localement, nous allons enfin la déployer sur Azure. D'abord depuis la connexion.
az login
Déployer.
mvn azure-functions:deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< tech.kikutaro:AzureFunctionsJava >------------------
[INFO] Building Azure Java Functions 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- azure-functions-maven-plugin:1.0.0-beta-6:deploy (default-cli) @ AzureFunctionsJava ---
AI: INFO 17-09-2018 22:07, 1: Configuration file has been successfully found as resource
AI: INFO 17-09-2018 22:07, 1: Configuration file has been successfully found as resource
[INFO] Authenticate with Azure CLI 2.0
[INFO] The specified function app does not exist. Creating a new function app...
[INFO] Successfully created the function app: azurefunctionsjava-20180917211315850
[INFO] Trying to deploy the function app...
[INFO] Successfully deployed the function app at https://azurefunctionsjava-20180917211315850.azurewebsites.net
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:59 min
[INFO] Finished at: 2018-09-17T22:09:03+09:00
[INFO] ------------------------------------------------------------------------
Si vous archivez le portail Azure, un groupe de ressources avec le nom spécifié dans Maven est créé.
Vous pouvez vérifier l'URL en cliquant sur la fonction créée.
Vérifiez comme local. Tout d'abord, l'URL supérieure.
Il se comporte de la même manière que local.
C'était plus facile que prévu.
Recommended Posts