Coding the example

Now, we will create a function that masks bank account numbers. Let's start by creating a new Spring Boot application from scratch, using the Spring Initializr website (https://start.spring.io):

Spring Initializr website

At the moment, there's no need to include additional dependencies as a part of the project. The project structure is pretty simple, and it will look as follows:

In order to write functions using Spring, we have to include the Spring Cloud Function project as a dependency; first, let's add some properties to specify the version that we are going to use, as follows:

  <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.11.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-
8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud-function.version>
1.0.0.BUILD-SNAPSHOT
</spring-cloud-function.version>

<reactor.version>3.1.2.RELEASE</reactor.version>
<wrapper.version>1.0.9.RELEASE</wrapper.version>
</properties>
Note that we will downgrade the Spring version to 1.5.11 RELEASE, because Spring Cloud Function is not currently ready to be used in Spring Boot 2.

Now, we will add the dependency, as follows:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-compiler</artifactId>
</dependency>

Then, we have to add an entry as a part of the dependency management section, to allow Maven to automatically resolve all transitive dependencies:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-dependencies</artifactId>
<version>${spring-cloud-function.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Finally, we will include some plugins that will allow us to wrap the coded functions, by adding the following entries as a part of the pom.xml file:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>${wrapper.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

We are now ready to implement a function to mask account numbers. Let's review the following code snippet:

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 reactor.core.publisher.Flux;

import java.util.function.Function;

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

@Bean
public Function<Flux<String>, Flux<String>> maskAccounts()
{

return flux ->
{

return flux
.map(value ->
value.replaceAll("\w(?=\w{4})", "*")
);

};
}
}

The @FunctionScan annotation is used to allow the Spring Function adapter to find the beans that will be deployed as functions in the cloud provider.

Once the function has been coded, we will register it using the application.properties file, as follows:

spring.cloud.function.stream.default-route: maskAccounts
spring.cloud.function.scan.packages: com.packtpub.maskaccounts

Now, it's time to execute the function locally, using the following steps:

  1. Generate the artifact:
$ mvn install
  1. Execute the generated artifact:
$ java -jar target/mask-accounts-0.0.1-SNAPSHOT.jar

You should now see output similar to the following:

Console output

Let's try to execute the function using the following CURL command:

$ curl -H "Content-Type: text/plain" http://localhost:8080/maskAccounts -d 37567979
%****7979

As a result, we will get a masked account number: ****7979.

In the next section, we will review how to deploy the function using different cloud providers.

In order to create an account on any cloud provider, such as AWS or Azure, you will need a credit or debit card, even if the provider offers free tiers.

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

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