Azure adapter

In this section, we will review how to deploy the previously coded function to Azure, which is a cloud provider supported by Microsoft. Azure supports functions by using Microsoft Azure Functions (https://azure.microsoft.com/en-us/services/functions/).

The Azure adapter is a layer coded over the Spring Cloud Function project. You can find the source of the project on GitHub (https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-azure).

Let's start by adding the following properties as a part of the pom.xml file, in the properties section:

<functionAppName>function-mask-account-azure</functionAppName><functionAppRegion>westus</functionAppRegion>
<
start-class>
com.packtpub.maskaccounts.MaskAccountsApplication
</start-class>

Now, let's add the required dependencies for this adapter, as follows:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-azure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-java-core</artifactId>
<version>1.0.0-beta-2</version>
<scope>provided</scope>
</dependency>

Then, we will add some plugins to allow the adapter to work, as follows:

<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-functions-maven-plugin</artifactId>
<configuration>
<resourceGroup>java-functions-group</resourceGroup>
<appName>${functionAppName}</appName>
<region>${functionAppRegion}</region>
<appSettings>
<property>
<name>FUNCTIONS_EXTENSION_VERSION</name>
<value>beta</value>
</property>
</appSettings>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/azure-
functions/${functionAppName}
</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/azure</directory>
<includes>
<include>**</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>azure</shadedClassifierName>
<outputDirectory>${project.build.directory}/azure-
functions/${functionAppName}</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>azure</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<inherited>false</inherited>
<configuration>
<attach>false</attach>
<descriptor>${basedir}/src/assembly/azure.xml</descriptor>
<outputDirectory>${project.build.directory}/azure-
functions</outputDirectory>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${functionAppName}</finalName>
</configuration>
</execution>
</executions>
</plugin>

Finally, we will create an adapter that should extend from the AzureSpringBootRequestHandler class. The extended class will provide us with the input and output types that enable Azure functions to inspect the class and perform any JSON conversion to consume/produce data:

public class Handler 
extends AzureSpringBootRequestHandler<Flux<String>,Flux<String>> {

public
Flux<String> execute
(Flux<String>in, ExecutionContext context) {
return handleRequest(in, context);
}
}

Now, we will modify the coded function that resides in the MaskAccountsApplication.java file; we will change the input and output of the function, in order to use a plain old Java object with setters and getters:

package com.packtpub.maskaccounts;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.function.context.FunctionScan;
import org.springframework.context.annotation.Bean;

import java.util.function.Function;

@FunctionScan
@SpringBootApplication
public class MaskAccountsApplication {

public static void main(String[] args) {
SpringApplication.run(MaskAccountsApplication.class, args);
}

@Bean
public Function<In, Out> maskAccount() {
return value -> new Out(value.mask());
}
}

class In {

private String value;

In() {
}

public In(String value) {
this.value = value;
}

public String mask() {
return value.replaceAll("\w(?=\w{4})", "*");
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

class Out {

private String value;

Out() {
}

public Out(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

Then we have to add a JSON configuration for Azure tooling, so we are going to create a JSON file named function.json, in a new folder behind the src/main folder, with the name of the function (maskAccount). This file will be used to let Azure know about the function that we want to deploy, by specifying the Java class that will be used as the entry point. The src folder should look as follows:

The content of the function.json file will be as follows:

{
"scriptFile": "../mask-accounts-azure-1.0.0.BUILD-SNAPSHOT-azure.jar",
"entryPoint": "com.packtpub.maskaccounts.Handler.execute",
"bindings": [
{
"type": "httpTrigger",
"name": "in",
"direction": "in",
"authLevel": "anonymous",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"name": "$return",
"direction": "out"
}
],
"disabled": false
}
The JSON files can be created with the Maven plugin for a non-Spring function, but the tooling doesn't work with the current version of the adapter.

Before generating the artifact that will be deployed, we have to create an assembly file, which is required by the Azure Maven plugin that we are using.

The assembly file should be placed in the src/assembly directory; the file will be named azure.xmland will include the following content:

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>azure</id>
<formats>
<format>zip</format>
</formats>
<baseDirectory></baseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/azure-functions/${functionAppName}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*-azure.jar</include>
<include>**/*.json</include>
</includes>
</fileSet>
</fileSets>
</assembly>

Now, the JAR file can be created by using the following Maven goals:

$ mvn clean package

The function can be deployed locally for testing purposes, running the JAR file as a regular Java application by using the following command:

$ java -jar target/mask-accounts-azure-0.0.1-SNAPSHOT.jar

You will then see that the application is running, as follows:

The output of the Spring application, running locally

Let's try out the function using the following curl command:

$ curl -H "Content-Type: text/plain" localhost:8080/maskAccount -d '{"value": "37567979"}'

You will see the following output:

Alternatively, we can deploy our function to Azure using Azure Functions Core Tools.

To do so, first, you have to install all of the required tools using the information provided at https://github.com/azure/azure-functions-core-tools#installing. Once the required tools have been installed, you can log in to Azure using the following command in your Terminal: 

$ az login

After you have entered your credentials, you will see the following output on the console:

Deploying the coded function to Azure is pretty simple; you only have to execute the following Maven:

$ mvn azure-functions:deploy

Now, you can try out the deployed function using the following curl command:

$ curl https://<azure-function-url-from-the-log>/api/maskAccount -d '{"value": "37567979"}'

The <azure-function-url-from-the-log> is the URL that you will get after running the mvn azure-functions:deploy command. For example, in the following screenshot, you can see the https://function-mask-account-azure.azurewebsites.net/ URL: 

After executing the curl command, the output received will be as follows:

Output processing

We can also test the same function on the Azure Functions console, just as we did with AWS Lambda.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.69