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



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.0modelVersion>
parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.4.3version>
relativePath/> <!-- lookup parent from repository -->
parent>
groupId>com.bolingcavalrygroupId>
artifactId>jdk8mavenspringbootartifactId>
version>0.0.1-SNAPSHOTversion>
name>jdk8mavenspringbootname>
description>Demo project for Spring Bootdescription>
properties>
java.version>1.8java.version>
properties>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-testartifactId>
scope>testscope>
dependency>
dependencies>
build>
plugins>
plugin>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-maven-pluginartifactId>
configuration>
layers>
enabled>trueenabled>
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
# 用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"]
language: simplespringboot
welcome_message: |
You have created a function using the java8 and maven and springboot template
[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


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
我是欣宸,期待与您一同畅游Java世界…
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1556/viewspace-2797574/ ,如需转载,请注明出处,否则将追究法律责任。