Welcome to my GitHub

Github.com/zq2599/blog…

Content: all original article classification summary and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;

Links to OpenFaaS Field series articles

  1. The deployment of
  2. Introduction to the function
  3. Java functions
  4. Template operation (Template)
  5. Big watchdog
  6. Of -watchdog(For performance)
  7. Java11 template parsing
  8. Maven +jdk8
  9. Maven +maven+jdk8

This paper gives an overview of

  • As the final article in the OpenFaaS Real World series, we have done enough about theory and real world in the first eight articles. Let’s conclude the series with a useful template.
  • Maven + JDk8 makes a Java template: The JDK version is 8, the compilation and construction tool is Maven, and the function is to provide web services by writing handler. Java. This template is not practical, in the actual development of Java programmers like to use springboot framework, so today our task is to create a custom template. Jdk8, Maven, SpringBoot, etc.
  • The specific actual combat content is shown in the figure below. First complete the blue part on the left to complete the template, and then execute the green part on the right to develop a function to verify that the template meets expectations:

  • Okay, a little less routine, a little more sincerity, no gossip and just get to work;

Creating a Java project

  • The most important thing to do when creating a template is to provide complete template code.
  • Create a springboot project named JDk8MavenSpringBoot and use JDK8:

  • The basic Settings of the project are as follows:

  • Project of pom. The XML content as follows, note that the spring – the boot – maven – the plugin adds a plugin configuration parameter configuration. The layers, enabled, and this is used when making mirror, make the required jar files can extract image content:

      
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <! -- lookup parent from repository -->
    </parent>
    <groupId>com.bolingcavalry</groupId>
    <artifactId>jdk8mavenspringboot</artifactId>
    <version>0.0.1 - the SNAPSHOT</version>
    <name>jdk8mavenspringboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <layers>
                        <enabled>true</enabled>
                    </layers>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code
  • Add a controller as a symbolic demo:
package com.bolingcavalry.jdk8mavenspringboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;

@RestController
public class Hello {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(a) {
        return "Hello world, " + newDate(); }}Copy the code
  • In the pom. XML directory, create a new folder m2 and add the Maven configuration file settings. XML. This file is used during the FaaS development process to create an image (Java project will be compiled and built during the image creation). This will make the image much faster. I have configured ali Cloud image here, but it still takes more than two minutes (as shown below), so if you have The Nexus3 private server, you must give priority to:

  • Modify the configuration file SRC/main/resources/application. The properties, add a line of port configuration, this is fwatchdog forwarded to port:
server.port=8082
Copy the code
  • At this point, the coding work has been completed, visible this is a common Springboot project, the next thing to consider is how to make Docker image, that is, the preparation of Dockerfile;

Develop Dockerfile

  • We have experienced the previous actual situation, the development of FaaS will be compiled to build the code into a mirror, so the corresponding Dockerfile should also be ready, the following is the complete Dockerfile content:
#Use maven images as the base images for compiling and building Java projectsMaven: 3.6.3-openJDK-8 as Builder WORKDIR /home/app
#Copy the entire project to the /home/app directory
COPY . /home/app/

#Go to the pom. XML directory and run the build command, specifying the m2/settings. XML file as the configuration file.
#Configure the private server in settings. XML, otherwise the build will be slow
RUN cd function && mvn clean package -U -DskipTests --settings ./m2/settings.xml 

#After the previous compilation and build with Maven, the build results are copied to the specified location to extract the file
RUN cp /home/app/function/target/*.jar ./application.jar
#Use the spring-boot-Jarmode-layerTools to extract the split build result from application.jar
RUN java -Djarmode=layertools -jar application.jar extract

#Ofwatchdog Contains the binary watchdog file, which is used during image creationThe FROM openfaas/of - watchdog: 0.7.6 as watchdog
#The OpenJDK image is the runtime environment for the container
FROM openjdk:8-jre-slim as ship

#For security reasons, do not use root accounts and groups when running containers in production
RUN addgroup --system app \
    && adduser --system --ingroup app app

#Copy the binary file fwatchdog from the of-watchdog image, which is the startup process for the container
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog

#Grant executable permission
RUN chmod +x /usr/bin/fwatchdog

WORKDIR /home/app

#The file obtained after the preceding command is successfully executed is used to start applications in the image
COPY --from=builder /home/app/dependencies/ ./
COPY --from=builder /home/app/spring-boot-loader/ ./
COPY --from=builder /home/app/snapshot-dependencies/ ./
COPY --from=builder /home/app/application/ ./

#Specifies the operation account for the container
user app

#Specifies the working directory for the container
WORKDIR /home/app/

#Fwatchdog forwards a Web request to the port on which the Java process listensENV upstream_url = "http://127.0.0.1:8082"
#The running mode is HTTP
ENV mode="http"

#The command to pull up a business process, in this case the Java process
ENV fprocess="java org.springframework.boot.loader.JarLauncher"

#The port exposed by the container, that is, the port on which the fwatchdog process listens
EXPOSE 8080

#Health check
HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1

#The container startup command, in this case, executes the binary file fwatchdog
CMD ["fwatchdog"]
Copy the code
  • The script above contains operations of extracting and copying extracted content, which is a feature provided by Springboot for containerization officially from version 2.3. For details, please refer to experience SpringBoot(2.3) Application making Docker Image (Official Scheme).

The template configuration

  • Now that the materials are ready to be submitted to Github, they can be used as OpenFaaS templates.
  1. Create a new folder called SimplespringBoot;
  2. Simplespringboot creates a new file, template.yml, with the following contents:
language: simplespringboot
welcome_message: | You have created a function using the java8 and maven and springboot templateCopy the code
  1. Copy the previous Dockerfile file to the SimplespringBoot directory.
  2. Simplespringboot: simplespringBoot: simplespringBoot: simplespringBoot: simplespringBoot: simplespringBoot: simplespringBoot: simplesPringBoot
  3. The simplespringBoot directory should look like this at this point:
[root@hedy 003]# tree simplespringboot
simplespringboot
├── Dockerfile
├── function
│   ├── HELP.md
│   ├── jdk8mavenspringboot.iml
│   ├── m2
│   │   └── settings.xml
│   ├── mvnw
│   ├── mvnw.cmd
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── com
│       │   │       └── bolingcavalry
│       │   │           └── jdk8mavenspringboot
│       │   │               ├── controller
│       │   │               │   └── Hello.java
│       │   │               └── Jdk8mavenspringbootApplication.java
│       │   └── resources
│       │       ├── application.properties
│       │       ├── static
│       │       └── templates
│       └── test
│           └── java
│               └── com
│                   └── bolingcavalry
│                       └── jdk8mavenspringboot
│                           └── Jdk8mavenspringbootApplicationTests.java
└── template.yml

17 directories, 12 files
Copy the code
  1. Upload all of this to Github, where I’m at github.com/zq2599/open… , there are already four templates, the newly added ones are shown in the red box below:

  • At this point, the template is made, then verify that the template is available;

Verify the template

  • The next thing to do is the green section on the right:

  • Log in to a computer with OpenFaaS installed, find a clean directory, and run the following command to download all the templates from Github:
faas template pull https://github.com/zq2599/openfaas-templates
Copy the code
  • The console responds with the following message indicating that four templates have been downloaded, as expected:
[root@hedy 07]# faas template pull https://github.com/zq2599/openfaas-templates
Fetch templates from repository: https://github.com/zq2599/openfaas-templates at 
2021/03/07 20:30:24 Attempting to expand templates from https://github.com/zq2599/openfaas-templates
2021/03/07 20:30:29 Fetched 4 template(s) : [dockerfile java11extend simplejava8 simplespringboot] from https://github.com/zq2599/openfaas-templates
Copy the code
  • Use faas new –list to view the list as follows:
[root@hedy 07]# faas new --list
Languages available as templates:
- dockerfile
- java11extend
- simplejava8
- simplespringboot
Copy the code
  • Look at the content of the template/simplespringboot directory, and upload in front of the same:
[root @ hedy 07] # tree template/simplespringboot/template/simplespringboot / ├ ─ ─ Dockerfile ├ ─ ─ function │ ├ ─ ─ m2 │ │ └ ─ ─ Settings. The XML │ ├ ─ ─ MVNW │ ├ ─ ─ MVNW. CMD │ ├ ─ ─ pom. The XML │ └ ─ ─ the SRC │ ├ ─ ─ the main │ │ ├ ─ ─ Java │ │ │ └ ─ ─ com │ │ │ └ ─ ─ Cavalry │ ├─ ├─ Java │ ├─ Java │ ├─ Java │ ├─ Jdk8mavenspringbootApplication. Java │ │ └ ─ ─ resources │ │ └ ─ ─ application. The properties │ └ ─ ─ the test │ └ ─ ─ Java │ └ ─ ─ com │ └ ─ ─ bolingcavalry │ └ ─ ─ jdk8mavenspringboot │ └ ─ ─ Jdk8mavenspringbootApplicationTests. Java └ ─ ─ the template. The yml 15 directories, 10 filesCopy the code
  • Now that you have the template, you can create a function called faas-Simplespringbootdemo:
faas-cli new faas-simplespringbootdemo --lang simplespringboot -p bolingcavalry
Copy the code
  • Faas-simplespringbootdemo = faas-simplespringBootdemo = faas-simplespringBootDemo
[root@hedy 07]# faas-cli new faas-simplespringbootdemo --lang simplespringboot -p bolingcavalry Folder: faas-simplespringbootdemo created. ___ _____ ____ / _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___| | | | | '_ \ / _ \ '_ \| |_ /  _` |/ _` \___ \ | |_| | |_) | __/ | | | _| (_| | (_| |___) | \___/| .__/ \___|_| |_|_| \__,_|\__,_|____/ |_| Function created in folder: faas-simplespringbootdemo Stack file written: faas-simplespringbootdemo.yml Notes: You have created a function using the java8 and maven and springboot templateCopy the code
  • The faas-SimplespringBootDemo folder contains the following contents:
[root@hedy 07]# ├─ m │ ├─ ├─ MVNW ├─ mvnw.cmd [root@hedy 07]# ├─ MVNW ├─ mvnw.cmd ├ ─ ─ pom. XML └ ─ ─ the SRC ├ ─ ─ the main │ ├ ─ ─ Java │ │ └ ─ ─ com │ │ └ ─ ─ bolingcavalry │ │ └ ─ ─ jdk8mavenspringboot │ │ ├ ─ ─ The controller │ │ │ └ ─ ─ Hello. Java │ │ └ ─ ─ Jdk8mavenspringbootApplication. Java │ └ ─ ─ resources │ └ ─ ─ application. The properties └ ─ ─ the test └ ─ ─ Java └ ─ ─ com └ ─ ─ bolingcavalry └ ─ ─ jdk8mavenspringboot └ ─ ─ Jdk8mavenspringbootApplicationTests. Java 14 directories, 8 filesCopy the code
  • Now we are ready to develop the business. For testing purposes, we have modified the Hello. Java interface to return content, as shown in the red box below:

  • To start building, execute the following command:
faas-cli build -f ./faas-simplespringbootdemo.yml
Copy the code
  • After the build, push the image to the image warehouse so that Kubernetes can download the image. I used hub.docker.com here, because my ID is BolingCavalry, I can push the image successfully by executing the following command (docker login command must be executed first) :
docker push bolingcavalry/faas-simplespringbootdemo:latest
Copy the code
  • Execute the following command to deploy the function to OpenFaaS:
faas-cli deploy -f faas-simplespringbootdemo.yml
Copy the code
  • The console responds with the following, indicating that deployment has started and an endpoint is given:
[root@hedy 07]# faas-cli deploy -f faas-simplespringbootdemo.yml
Deploying: faas-simplespringbootdemo.
WARNING! You are not using an encrypted connection to the gateway, consider using HTTPS.

Deployed. 202 Accepted.
URL: http://192.168.50.75:31112/function/faas-simplespringbootdemo.openfaas-fn
Copy the code
  • Delete from the console using curl:
[root @ hedy 07] # curl http://192.168.50.75:31112/function/faas-simplespringbootdemo.openfaas-fn/hello Hello world 123456789, Sun Mar 07 13:17:06 UTC 2021Copy the code
  • At this point, the validation template is complete, as expected

Clean up the

  • The following command is used to delete the function: faas-simplespringbootdemo.yml
faas-cli remove -f faas-simplespringbootdemo.yml
Copy the code
  • So far, self-made Springboot + Maven + JDK8 template, from development to verification we have gone all over, our OpenFaaS practical series is also a successful conclusion, I hope this series can bring some reference to your Serverless road, that will be my honor;

You are not alone, Xinchen original accompany all the way

  1. Java series
  2. Spring series
  3. The Docker series
  4. Kubernetes series
  5. Database + middleware series
  6. The conversation series

Welcome to pay attention to the public number: programmer Xin Chen

Wechat search “programmer Xin Chen”, I am Xin Chen, looking forward to enjoying the Java world with you…

Github.com/zq2599/blog…