Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Gebruik Durable Functions, een functie van Azure Functions, om stateful serverloze werkstromen te schrijven in TypeScript. In deze snelstartgids kloont u een voorbeeldtoepassing en voert u deze uit, waarin twee veelvoorkomende orkestratiepatronen worden gedemonstreerd:
- Functiekoppeling: Roept activiteiten opeenvolgend aan (Tokio → Seattle → Londen).
- Fan-out/fan-in: roept activiteiten parallel aan in vijf steden en voegt vervolgens de resultaten samen.
Aan het einde hebt u beide orchestraties lokaal draaien met de Durable Task Scheduler-emulator en kunt u hun status bekijken in het dashboard.
- Kloon en bereid het Hello Cities-voorbeeldproject voor.
- Stel de Durable Task Scheduler emulator en Azurite in voor lokale ontwikkeling.
- Voer de functie-app uit en start beide orchestraties.
- Controleer de orchestratiestatus en uitvoer in het dashboard van Durable Task Scheduler.
Prerequisites
- Node.js 20+ geïnstalleerd.
- Azure Functions Core Tools v4 of hoger.
- Docker voor het uitvoeren van de emulator en Azurite.
- Kloon de Durable Task Scheduler GitHub repository om het snelstartvoorbeeld te gebruiken.
De emulator voor Durable Task Scheduler instellen
De Durable Task Scheduler-emulator biedt een lokale ontwikkelomgeving, zodat u indelingen kunt testen zonder een Azure abonnement. De Functions-host vereist ook Azurite voor lokale opslag.
Start beide containers op:
docker run -d --name dtsemulator -p 8080:8080 -p 8082:8082 \
mcr.microsoft.com/dts/dts-emulator:latest
docker run -d --name azurite -p 10000:10000 -p 10001:10001 -p 10002:10002 \
mcr.microsoft.com/azure-storage/azurite
Tip
Zodra de emulator actief is, kunt u het Durable Task Scheduler-dashboard openen op http://localhost:8082 om orchestraties te bewaken.
Het snelstartvoorbeeld uitvoeren
Navigeer naar de voorbeeldmap Hello Cities:
cd samples/durable-functions/typescript/HelloCitiesInstalleer afhankelijkheden en bouw het project:
npm install npm run buildControleer of het
local.settings.jsonbestand de volgende configuratie bevat:{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "node", "DURABLE_TASK_SCHEDULER_CONNECTION_STRING": "Endpoint=http://localhost:8080;TaskHub=default;Authentication=None" } }Start de functie-app:
func startStart in een aparte terminal de orkestratie van functiechaining:
$response = Invoke-RestMethod -Method POST -Uri http://localhost:7071/api/StartChaining $responseHet antwoord bevat status-URL's voor de orchestratie-instantie. Kopieer de waarde
statusQueryGetUrien voer deze uit om het resultaat te controleren:Invoke-RestMethod -Uri $response.statusQueryGetUriStart de fan-out/fan-in-orchestratie:
$response = Invoke-RestMethod -Method POST -Uri http://localhost:7071/api/StartFanOutFanIn Invoke-RestMethod -Uri $response.statusQueryGetUri
Verwachte uitvoer
De POST-aanvraag retourneert een JSON-antwoord met status-URL's. Voorbeeld:
{
"id": "<instanceId>",
"statusQueryGetUri": "http://localhost:7071/runtime/webhooks/durabletask/instances/<instanceId>?code=...",
"sendEventPostUri": "...",
"terminatePostUri": "...",
"purgeHistoryDeleteUri": "..."
}
Wanneer u een query uitvoert op statusQueryGetUri en de runtimeStatus van de orchestratie Completed is, kunt u de resultaten van de begroeting vinden in het veld output. De ketenorkestratie retourneert:
{
"name": "chainingOrchestration",
"runtimeStatus": "Completed",
"output": ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
}
De fan-out/fan-in-orchestratie retourneert:
{
"name": "fanOutFanInOrchestration",
"runtimeStatus": "Completed",
"output": ["Hello Tokyo!", "Hello Seattle!", "Hello London!", "Hello Paris!", "Hello Berlin!"]
}
Tip
Als runtimeStatusRunning of Pending weergeeft, wacht dan even en voer de statusQueryGetUri opnieuw uit.
Open het dashboard van Durable Task Scheduler op http://localhost:8082 om de orchestrationstatus en uitvoeringsgeschiedenis te bekijken.
De code begrijpen
In het voorbeeld wordt het Node.js v4-programmeermodel gebruikt, waarbij alle functies in één bestand (src/functions/helloCities.ts) zijn gedefinieerd.
Activiteitsfunctie
De sayHello activiteit heeft een plaatsnaam en retourneert een begroeting:
df.app.activity("sayHello", {
handler: (city: string): string => {
return `Hello ${city}!`;
},
});
Orchestratorfuncties
De chaining orchestrator roept sayHello opeenvolgend aan voor drie steden:
const chainingOrchestrator: OrchestrationHandler = function* (
context: OrchestrationContext
) {
const outputs: string[] = [];
outputs.push(yield context.df.callActivity("sayHello", "Tokyo"));
outputs.push(yield context.df.callActivity("sayHello", "Seattle"));
outputs.push(yield context.df.callActivity("sayHello", "London"));
return outputs;
};
df.app.orchestration("chainingOrchestration", chainingOrchestrator);
De fan-out/fan-in orchestrator plant activiteiten parallel:
const fanOutFanInOrchestrator: OrchestrationHandler = function* (
context: OrchestrationContext
) {
const cities: string[] = ["Tokyo", "Seattle", "London", "Paris", "Berlin"];
// Fan-out: schedule all activities in parallel
const tasks = cities.map((city) => context.df.callActivity("sayHello", city));
// Fan-in: wait for all to complete
const results: string[] = yield context.df.Task.all(tasks);
return results;
};
df.app.orchestration("fanOutFanInOrchestration", fanOutFanInOrchestrator);
Clientfuncties
Door HTTP geactiveerde clientfuncties starten elke orchestratie. Bijvoorbeeld de koppelingsstarter:
app.http("StartChaining", {
route: "StartChaining",
methods: ["POST"],
authLevel: "anonymous",
extraInputs: [df.input.durableClient()],
handler: async (
request: HttpRequest,
context: InvocationContext
): Promise<HttpResponse> => {
const client = df.getClient(context);
const instanceId = await client.startNew("chainingOrchestration");
context.log(`Started chaining orchestration with ID = '${instanceId}'.`);
return client.createCheckStatusResponse(request, instanceId);
},
});
Configuratie
In het voorbeeld wordt de Durable Task Scheduler-emulator gebruikt als de back-end voor opslag. Dit is geconfigureerd in host.json:
{
"version": "2.0",
"logging": {
"logLevel": {
"DurableTask.Core": "Warning"
}
},
"extensions": {
"durableTask": {
"hubName": "default",
"storageProvider": {
"type": "azureManaged",
"connectionStringName": "DURABLE_TASK_SCHEDULER_CONNECTION_STRING"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
De hulpbronnen opschonen
Stop de containers van de emulator wanneer u klaar bent:
docker stop dtsemulator azurite && docker rm dtsemulator azurite
Volgende stappen
- Meer informatie over common Durable Functions app-patronen.
- Meer informatie over Durable Functions-opslagproviders.