其他分享
首页 > 其他分享> > OpenFaaS实战之八:自制模板(maven+jdk8)

OpenFaaS实战之八:自制模板(maven+jdk8)

作者:互联网

欢迎访问我的GitHub

https://github.com/zq2599/blog_demos

内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;

OpenFaaS实战系列文章链接

  1. 部署
  2. 函数入门
  3. Java函数
  4. 模板操作(template)
  5. 大话watchdog
  6. of-watchdog(为性能而生)
  7. java11模板解析
  8. OpenFaaS实战之八:自制模板(maven+jdk8)
  9. OpenFaaS实战之九:终篇,自制模板(springboot+maven+jdk8)

本篇概览

在这里插入图片描述

在这里插入图片描述

  1. 创建java项目,作为模板的基础源码
  2. 开发Dockerfile
  3. 完成模板配置并上传
  4. 验证模板

创建java项目

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bolingcavalry</groupId>
    <artifactId>java8maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>3.6.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>com.openfaas</groupId>
            <artifactId>model</artifactId>
            <version>0.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.openfaas</groupId>
            <artifactId>entrypoint</artifactId>
            <version>0.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.10</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.openfaas.entrypoint.App</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. openfaas的model和entrypoint这两个jar是整个服务可运行的基础;
  2. 有些常用的jar依赖也被加入了,您可以酌情自行增删;
  3. 插件maven-compiler-plugin用来指定编译时的JDK版本;
  4. 插件maven-dependency-pluginmaven-assembly-plugin用来将整个java代码和依赖库打包到一个jar文件中,这样制作Docker镜像会方便很多;
package com.openfaas.function;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.openfaas.model.IRequest;
import com.openfaas.model.IResponse;
import com.openfaas.model.Response;
import org.apache.commons.lang3.StringUtils;

import java.lang.management.ManagementFactory;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class Handler extends com.openfaas.model.AbstractHandler {

    private static final String PARAM_USER_NAME = "name";

    private static final String RESPONSE_TEMPLETE = "Hello %s, response from [%s], PID [%s], %s";

    private ObjectMapper mapper = new ObjectMapper();

    /**
     * 获取本机IP地址
     * @return
     */
    public static String getIpAddress() {
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
                    continue;
                } else {
                    Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        ip = addresses.nextElement();
                        if (ip != null && ip instanceof Inet4Address) {
                            return ip.getHostAddress();
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("IP地址获取失败" + e.toString());
        }
        return "";
    }

    /**
     * 返回当前进程ID
     * @return
     */
    private static String getPID() {
        return ManagementFactory
                .getRuntimeMXBean()
                .getName()
                .split("@")[0];
    }


    private String getUserName(IRequest req) {
        // 如果从请求body中取不到userName,就用
        String userName = null;

        try {
            Map<String, Object> mapFromStr = mapper.readValue(req.getBody(),
                    new TypeReference<Map<String, Object>>() {});

            if(null!=mapFromStr && mapFromStr.containsKey(PARAM_USER_NAME)) {
                userName = String.valueOf(mapFromStr.get(PARAM_USER_NAME));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        // 如果从请求body中取不到userName,就给个默认值
        if(StringUtils.isBlank(userName)) {
            userName = "anonymous";
        }

        return userName;
    }

    public IResponse Handle(IRequest req) {

        String userName = getUserName(req);

        System.out.println("1. ---" + userName);

        // 返回信息带上当前JVM所在机器的IP、进程号、时间
        String message = String.format(RESPONSE_TEMPLETE,
                userName,
                getIpAddress(),
                getPID(),
                new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ).format(new Date()));

        System.out.println("2. ---" + message);

        // 响应内容也是JSON格式,所以先存入map,然后再序列化
        Map<String, Object> rlt = new HashMap<>();
        rlt.put("success", true);
        rlt.put("message", message);

        String rltStr = null;

        try {
            rltStr = mapper.writeValueAsString(rlt);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Response res = new Response();
        res.setContentType("application/json;charset=utf-8");
        res.setBody(rltStr);

	    return res;
    }
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

开发Dockerfile

# 用maven镜像作为基础镜像,用于编译构建java项目
FROM maven:3.6.3-openjdk-8 as builder

WORKDIR /home/app

# 将整个项目都复制到/home/app目录下
COPY . /home/app/

# 进入pom.xml所在目录执行构建命令,指定m2/settings.xml文件作为配置文件,
# 请在settings.xml中配置好私服,否则构建速度极慢
RUN cd function && mvn clean package -U -DskipTests --settings ./m2/settings.xml 

# of-watchdog里面有二进制文件watchdog,制作镜像时要用到
FROM openfaas/of-watchdog:0.7.6 as watchdog

# openjdk镜像是容器的运行环境
FROM openjdk:8-jre-slim as ship

# 为了安全起见,在生产环境运行容器时不要用指root帐号和群组
RUN addgroup --system app \
    && adduser --system --ingroup app app

# 从of-watchdog镜像中复制二进制文件fwatchdog,这是容器的启动进程
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog

# 赋予可执行权限
RUN chmod +x /usr/bin/fwatchdog

WORKDIR /home/app

# 前面用maven编译构建完毕后,这里将构建结果复制到镜像中
COPY --from=builder /home/app/function/target/java8maven-1.0-SNAPSHOT-jar-with-dependencies.jar ./java8maven-1.0-SNAPSHOT-jar-with-dependencies.jar
# 指定容器的运行帐号
user app

# 指定容器的工作目录
WORKDIR /home/app/

# fwatchdog收到web请求后的转发地址,java进程监听的就是这个端口
ENV upstream_url="http://127.0.0.1:8082"

# 运行模式是http
ENV mode="http"

# 拉起业务进程的命令,这里就是启动java进程
ENV fprocess="java -jar java8maven-1.0-SNAPSHOT-jar-with-dependencies.jar"

# 容器对外暴露的端口,也就是fwatchdog进程监听的端口
EXPOSE 8080

# 健康检查
HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1

# 容器启动命令,这里是执行二进制文件fwatchdog
CMD ["fwatchdog"]

模板配置

  1. 新建一个文件夹,名为simplejava8
  2. simplejava8目录下新建文件template.yml,内容如下:
language: simplejava8
welcome_message: |
  You have created a function using the java8 and maven template
  1. 将前面的Dockerfile文件复制到simplejava8目录下;
  2. 前面咱们创建的maven工程,最外层的文件夹名为java8maven,请将此文件夹改名为function,然后将整个文件夹都复制到simplejava8目录下;
  3. 此刻的simplejava8目录下应该是这些内容:
[root@hedy 002]# tree simplejava8
simplejava8
├── Dockerfile
├── function
│   ├── java8maven.iml
│   ├── m2
│   │   └── settings.xml
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── com
│       │   │       └── openfaas
│       │   │           └── function
│       │   │               └── Handler.java
│       │   └── resources
│       └── test
│           └── java
└── template.yml

11 directories, 6 files
  1. 将这些内容全部上传到github上,我这里路径是https://github.com/zq2599/openfaas-templates/tree/master/template,这里面已经有三个模板了,本次新增的如下图红框:

在这里插入图片描述

验证模板

在这里插入图片描述

faas template pull https://github.com/zq2599/openfaas-templates
[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 08:44:29 Attempting to expand templates from https://github.com/zq2599/openfaas-templates
2021/03/07 08:44:32 Fetched 3 template(s) : [dockerfile java11extend simplejava8] from https://github.com/zq2599/openfaas-templates
[root@hedy 07]# faas new --list
Languages available as templates:
- dockerfile
- java11extend
- simplejava8
[root@hedy 07]# tree template/simplejava8/
template/simplejava8/
├── Dockerfile
├── function
│   ├── java8maven.iml
│   ├── m2
│   │   └── settings.xml
│   ├── pom.xml
│   └── src
│       └── main
│           └── java
│               └── com
│                   └── openfaas
│                       └── function
│                           └── Handler.java
└── template.yml

8 directories, 6 files
faas-cli new faas-simplejava8demo --lang simplejava8 -p bolingcavalry
[root@hedy 07]# faas-cli new faas-simplejava8demo --lang simplejava8 -p bolingcavalry
Folder: faas-simplejava8demo created.
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|


Function created in folder: faas-simplejava8demo
Stack file written: faas-simplejava8demo.yml

Notes:
You have created a function using the java8 and maven template
[root@hedy 07]# ls
faas-simplejava8demo  faas-simplejava8demo.yml  template
[root@hedy 07]# tree faas-simplejava8demo
faas-simplejava8demo
├── java8maven.iml
├── m2
│   └── settings.xml
├── pom.xml
└── src
    └── main
        └── java
            └── com
                └── openfaas
                    └── function
                        └── Handler.java

7 directories, 4 files

在这里插入图片描述

faas-cli build -f ./faas-simplejava8demo.yml
docker push bolingcavalry/faas-simplejava8demo:latest
faas-cli deploy -f faas-simplejava8demo.yml
[root@hedy 07]# faas-cli deploy -f faas-simplejava8demo.yml
Deploying: faas-simplejava8demo.
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-simplejava8demo.openfaas-fn

在这里插入图片描述

[root@hedy 07]# curl \
> -H "Content-Type: application/json" \
> -X POST \
> --data '{"name":"Jerry}' \
> http://192.168.50.75:31112/function/faas-simplejava8demo
{"success":true,"foo":"bar","message":"Hello anonymous, response from [10.244.0.168], PID [14], 2021-03-07 03:32:15"}

清理

faas-cli remove -f faas-simplejava8demo.yml

你不孤单,欣宸原创一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 数据库+中间件系列
  6. DevOps系列

欢迎关注公众号:程序员欣宸

微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos

标签:java,之八,simplejava8demo,maven,jdk8,com,模板,faas
来源: https://www.cnblogs.com/bolingcavalry/p/15121916.html