-
OpenFaaS实战之三:Java函数
本篇概览
- 本文是《OpenFaaS实战》系列的第三篇,经过前文实战,咱们掌握了函数开发和部署的要领,作为一名Java程序员,当然迫切的希望用Java编写OpenFaaS函数,于是就有了本文;
- 本文开发一个Java函数,功能是解析请求body中的JSON字符串,再加上JVM进程ID、IP地址、当前时间一起拼成字符串,包装在JSON中返回;
- 平时写java代码会用到各种二方库,这里引入jackson的库,作为OpenFaaS添加依赖的参考;
源码下载
- 本篇实战中的源码可在GitHub下载到,地址和链接信息如下表所示(https://github.com/zq2599/blog_demos):
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
- 这个git项目中有多个文件夹,本章的应用在openfaas文件夹下,如下图红框所示:
- openfaas里面有多个子文件夹,本篇的源码在currenttime中,如下图红框:
创建函数
- 执行以下命令,即可创建名为faas-currenttime的函数,此函数的镜像前缀是bolingcavalry,语言类型为java11:
faas-cli new faas-currenttime --lang java11 -p bolingcavalry
- 控制台响应如下:
[root@node1 20]# faas-cli new faas-currenttime --lang java11 -p bolingcavalry
2020/11/20 15:47:50 No templates found in current directory.
2020/11/20 15:47:50 Attempting to expand templates from https://github.com/openfaas/templates.git
2020/11/20 15:47:56 Fetched 12 template(s) : [csharp dockerfile go java11 java11-vert-x node node12 php7 python python3 python3-debian ruby] from https://github.com/openfaas/templates.git
Folder: faas-currenttime created.
___ _____ ____
/ _ \ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|
Function created in folder: faas-currenttime
Stack file written: faas-currenttime.yml
Notes:
You have created a function using the java11 template which uses an LTS
version of the OpenJDK.
- 当前目录已经新增了文件faas-currenttime.yml和文件夹faas-currenttime
- 文件夹faas-currenttime的内容如下,可见是个gradle工程:
faas-currenttime
├── build.gradle
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
├── main
│ └── java
│ └── com
│ └── openfaas
│ └── function
│ └── Handler.java
└── test
└── java
└── HandlerTest.java
- 打开build.gradle文件,添加下图红框中的内容,即jackson和common库的依赖:
- 进入文件夹faas-currenttime/src/main/java/com/openfaas/function/,可见已创建了默认的业务功能类Handler.java,打开看看OpenFaaS给的默认代码啥样的,如下所示:
package com.openfaas.function;
import com.openfaas.model.IHandler;
import com.openfaas.model.IResponse;
import com.openfaas.model.IRequest;
import com.openfaas.model.Response;
public class Handler extends com.openfaas.model.AbstractHandler {
public IResponse Handle(IRequest req) {
Response res = new Response();
res.setBody("Hello, world!");
return res;
}
}
- 把Handler.java的内容用以下代码替换掉,替换后的函数,其功能是取得请求参数,再把当前JVM的进程ID、IP地址、当前时间都拼接到一个字符串中返回,需要重点关注的有两点:将请求参数反序列化成Map实例,以及将Map序列化成JSON字符串返回:
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;
}
}
- 至此编码完成,接下来是制作镜像和部署;
部署
- 在faas-currenttime.yml所在目录执行以下命令,即可开始制作镜像,制作过程中会有gradle的编译过程,如果编译失败会中断镜像制作:
faas-cli build -f ./faas-currenttime.yml
- 镜像制作成功时,控制台输出类似如下信息:
Step 27/30 : ENV fprocess="java -XX:+UseContainerSupport com.openfaas.entrypoint.App"
---> Running in 0f50636cc747
Removing intermediate container 0f50636cc747
---> 54a5c9a193c8
Step 28/30 : EXPOSE 8080
---> Running in 3252f165af15
Removing intermediate container 3252f165af15
---> c05afc826ec5
Step 29/30 : HEALTHCHECK --interval=5s CMD [ -e /tmp/.lock ] || exit 1
---> Running in 4106410be0a2
Removing intermediate container 4106410be0a2
---> 6d95b73b5f33
Step 30/30 : CMD ["fwatchdog"]
---> Running in 1606dbcd7003
Removing intermediate container 1606dbcd7003
---> 99a519ab82fd
Successfully built 99a519ab82fd
Successfully tagged bolingcavalry/faas-currenttime:latest
Image: bolingcavalry/faas-currenttime:latest built.
[0] < Building faas-currenttime done in 34.94s.
[0] Worker done.
Total build time: 34.94s
- 将镜像推送到镜像仓库,以便Kubernetes可以下载到此镜像,我这里用的是hub.docker.com,因为我的ID是bolingcavalry,所执行以下命令即可推送成功:
docker push bolingcavalry/faas-currenttime:latest
- 执行以下命令部署函数到OpenFaaS:
faas-cli deploy -f faas-currenttime.yml
- 控制台响应如下,可见部署已经开始,并且给出了endpoint:
[root@node1 20]# faas-cli deploy -f faas-currenttime.yml
Deploying: faas-currenttime.
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Deployed. 202 Accepted.
URL: http://192.168.133.187:31112/function/faas-currenttime.openfaas-fn
- 打开web端,在页面上可见新增的函数,验证操作如下图所示,可见入参的JSON内容可以被正常解析:
- 也可以在控制台用curl命令测试:
[root@node1 20]# curl \
> -H "Content-Type: application/json" \
> -X POST \
> --data '{"name":"Jerry}' \
> http://192.168.133.187:31112/function/faas-currenttime
{"success":true,"message":"Hello anonymous, response from [10.233.90.79], PID [11], 2020-11-20 02:14:46"}
- 执行命令faas-cli deploy -f faas-currenttime.yml开始部署,控制台已经接受了部署请求,并给出了函数的endpoint:
[root@node1 20]# faas-cli deploy -f faas-currenttime.yml
Deploying: faas-currenttime.
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Deployed. 202 Accepted.
URL: http://192.168.133.187:31112/function/faas-currenttime.openfaas-fn
清理
- 删除函数的命令如下,依旧是faas-currenttime.yml所在目录:
faas-cli remove -f faas-currenttime.yml
- 至此,最基本的Java函数的开发、部署、验证都已经完成,如果您也打算用Java开发OpenFaaS函数,希望本文能给您一些参考;
出处:https://www.cnblogs.com/bolingcavalry/p/15068687.html
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比