其他分享
首页 > 其他分享> > OpenFaaS实战之九:终篇,自制模板(springboot+maven+jdk8)

OpenFaaS实战之九:终篇,自制模板(springboot+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)

本篇概览

在这里插入图片描述

创建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 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-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>
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() {
        return "Hello world, " + new Date();
    }
}

在这里插入图片描述

server.port=8082

开发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 

# 前面用maven编译构建完毕后,这里将构建结果复制到指定位置用于提取文件
RUN cp /home/app/function/target/*.jar ./application.jar
# 通过工具spring-boot-jarmode-layertools从application.jar中提取拆分后的构建结果
RUN java -Djarmode=layertools -jar application.jar extract

# 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

# 前面提取命令执行成功后取得的文件,用于镜像中启动应用所需
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/ ./

# 指定容器的运行帐号
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 org.springframework.boot.loader.JarLauncher"

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

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

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

模板配置

  1. 新建一个文件夹,名为simplespringboot
  2. simplespringboot目录下新建文件template.yml,内容如下:
language: simplespringboot
welcome_message: |
  You have created a function using the java8 and maven and springboot template
  1. 将前面的Dockerfile文件复制到simplespringboot目录下;
  2. 前面咱们创建的springboot工程,最外层的文件夹名为jdk8mavenspringboot,请将此文件夹改名为function,然后将整个文件夹都复制到simplespringboot目录下;
  3. 此刻的simplespringboot目录下应该是这些内容:
[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
  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 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
[root@hedy 07]# faas new --list
Languages available as templates:
- dockerfile
- java11extend
- simplejava8
- simplespringboot
[root@hedy 07]# tree template/simplespringboot/
template/simplespringboot/
├── Dockerfile
├── function
│   ├── m2
│   │   └── settings.xml
│   ├── mvnw
│   ├── mvnw.cmd
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── com
│       │   │       └── bolingcavalry
│       │   │           └── jdk8mavenspringboot
│       │   │               ├── controller
│       │   │               │   └── Hello.java
│       │   │               └── Jdk8mavenspringbootApplication.java
│       │   └── resources
│       │       └── application.properties
│       └── test
│           └── java
│               └── com
│                   └── bolingcavalry
│                       └── jdk8mavenspringboot
│                           └── Jdk8mavenspringbootApplicationTests.java
└── template.yml

15 directories, 10 files
faas-cli new faas-simplespringbootdemo --lang simplespringboot -p bolingcavalry
[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 template
[root@hedy 07]# tree faas-simplespringbootdemo
faas-simplespringbootdemo
├── m2
│   └── settings.xml
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── bolingcavalry
    │   │           └── jdk8mavenspringboot
    │   │               ├── controller
    │   │               │   └── Hello.java
    │   │               └── Jdk8mavenspringbootApplication.java
    │   └── resources
    │       └── application.properties
    └── test
        └── java
            └── com
                └── bolingcavalry
                    └── jdk8mavenspringboot
                        └── Jdk8mavenspringbootApplicationTests.java

14 directories, 8 files

在这里插入图片描述

faas-cli build -f ./faas-simplespringbootdemo.yml
docker push bolingcavalry/faas-simplespringbootdemo:latest
faas-cli deploy -f faas-simplespringbootdemo.yml
[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
[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 2021

清理

faas-cli remove -f faas-simplespringbootdemo.yml

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

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

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

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

标签:java,springboot,maven,simplespringbootdemo,com,终篇,模板,faas
来源: https://www.cnblogs.com/bolingcavalry/p/15126602.html