Giter Site home page Giter Site logo

doom4s / function-demo-java-on-azure Goto Github PK

View Code? Open in Web Editor NEW

This project forked from archangelsdy/function-demo-java-on-azure

0.0 2.0 0.0 25 KB

Microsoft Azure Functions for Java (JavaOne Demo)

License: Creative Commons Attribution 4.0 International

Java 100.00%

function-demo-java-on-azure's Introduction

Walkthrough Steps

One-Time Environment Setup

Softwares & Extensions

Install Azure functions core tools using

npm i -g azure-functions-core-tools@core

Install Java Extension Pack for Visual Studio Code.

Configurations

Create a new storage account by executing (The UniqueID is just used to make sure your account name doesn't conflict with others, for example you can use the "current date & time", or "your alias + sequence number"; make sure the total length of the names are not exceeding 24 characters, and make sure you only use lower-case letters or numbers):

az group create -n BoothAzFunc<UniqueID> -l westus
az storage account create -n boothazfunc<UniqueID> -g BoothAzFunc<UniqueID> -l westus --sku Standard_LRS

Finally to get the storage connection string which will be used in the future:

az storage account show-connection-string -n boothazfunc<UniqueID> -g BoothAzFunc<UniqueID>

Copy it (Starting with DefaultEndpointsProtocol...) to somewhere.

Source Code

Go to your working directory and clone the sample code repository from:

git clone https://github.com/Microsoft/function-demo-java-on-azure.git
cd function-demo-java-on-azure

And make sure you replace the $UniqueId$ in walkthrough/pom.xml with the actual string of your unique ID. And you also need to replace $ResourceGroupName$ in walkthrough/pom.xml with the actual string of your resource group name (which should be BoothAzFunc<UniqueID>).

Then open walkthrough/local.settings.json and paste the connection string of your storage account to the value of AzureWebJobsStorage.

DEMO 1: Java Functions by Maven

Compiling and Running

Enter the folder walkthrough, and execute:

mvn clean package

Then you may run it by:

mvn azure-functions:run

The timer and the queue functions will be triggered once per 30 seconds. To trigger the HTTP function, you need to execute the following command in a new command line window:

curl -X POST -d "World" http://localhost:7071/api/hello

To terminate the app, press Ctrl + C.

Deployment

Try the following command to deploy the function app, and that's it.

mvn azure-functions:deploy

And you will find your functions app named walkthrough-<UniqueID> under Java Demos subscription.

You will also be able to find the URL of the deployed app within the last several lines of the command line output. So it is possible to verify that it is actually running on Azure.

curl -X POST -d "Azure World" <Deployed Host URL>/api/hello

DEMO 2: Java Functions by Visual Studio Code

Get to the built-in terminal by pressing the Ctrl + Backtick.

Compiling and Running

Enter the folder walkthrough in the built-in terminal, and execute:

mvn clean package

Then you may run it by:

mvn azure-functions:run

The timer and the queue functions will be triggered once per 30 seconds. To trigger the HTTP function, you need to execute the following command in a new command line window:

curl -X POST -d "World" http://localhost:7071/api/hello

To terminate the app, press Ctrl + C.

Deployment

Try the following command to deploy the function app, and that's it.

mvn azure-functions:deploy

And you will find your functions app named walkthrough-<UniqueID> under Java Demos subscription.

You will also be able to find the URL of the deployed app within the last several lines of the command line output. So it is possible to verify that it is actually running on Azure.

curl -X POST -d "Azure World" <Deployed Host URL>/api/hello

DEMO 3: Debugging Java Functions by Visual Studio Code

Get to the built-in terminal by pressing the Ctrl + Backtick.

Debugging

Enter the folder walkthrough in the built-in terminal, and execute:

mvn clean package

Navigate to the Debug page of Visual Studio Code by pressing Ctrl + Shift + D; then click the little gear icon (Open launch.json) besides "No Configurations" dropdown; and select "Java".

In the newly created launch.json, locate the Debug (Attach) block, and set the port to be 5005.

Now you run the functions app by:

mvn azure-functions:run

Wait for the functions host to start, and select the Debug (Attach) item in the dropdown list, and click the little green run icon (Start debugging) beside that dropdown list.

Now you can set the breakpoints in:

Those breakpoints will be hit when the corresponding function is triggered. And when any of them is hit, hover the mouse to some variables, like executionContext or timerInfo, to demostrate that the user could inspect all the execution environment here. And press F5 to continue running.

DEMO 4: Deploy Java Functions by Jenkins

Prerequisites

  1. Install the Azure Function Plugin for Jenkins. You can install/update the plugin in Jenkins Update Center (Manage Jenkins -> Manage Plugins, search Azure Function Plugin).
  2. Create an Azure Service Principal through Azure CLI or Azure Portal.
  3. Open Jenkins dashboard, go to Credentials, add a new Microsoft Azure Service Principal with the credential information you just created.

Deploy using Freestyle Project

You can deploy your Azure Function using a classic freestyle project:

  1. Create a new freestyle project in Jenkins, add necessary build steps to build your code. For example, if you use maven commands above you should get following directory structure after build:
target/azure-functions
-- walkthrough-prototype
    |-- Queue
        |-- function.json
    |-- Timer
        |-- function.json
    |-- hello
        |-- function.json
    |-- host.json
    |-- walkthrough-1.0-SNAPSHOT.jar
  1. Add a post-build action 'Publish an Azure Function'.
  2. Select your Azure credential in Azure Profile Configuration section.
  3. In App Configuration section, choose the resource group and function app in your subscription.
  4. Fill in 'Files' field with **/*.jar,**/*.json to select files we want to deploy.
  5. Fill in 'Source Directory' field with target/azure-functions.
  6. Save the project and build it, your function app will be deployed to Azure when build is completed.

Deploy using Pipeline

You can also use this plugin in pipeline (Jenkinsfile). Here are some samples to use the plugin in pipeline script:

To deploy a Java function app:

azureFunctionAppPublish azureCredentialsId: '<credential_id>',
    resourceGroup: '<resource_group_name>', appName: '<app_name>',
    filePath: '**/*.jar,**/*.json', sourceDirectory: 'target/azure-functions'

For advanced options, you can use Jenkins Pipeline Syntax tool to generate a sample script.

function-demo-java-on-azure's People

Contributors

microsoftopensource avatar pragnagopa avatar archangelsdy avatar xscript avatar msftgits avatar

Watchers

James Cloos avatar Vlajko Maric avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.