栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

OpenFaaS实战之三:Java函数,Java知识体系结构图

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenFaaS实战之三:Java函数,Java知识体系结构图

| || | |) | / | | | | (| | (| |) |

_/| ./ ___|| ||| _,|_,|___/

|_|

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.

  1. 当前目录已经新增了文件faas-currenttime.yml和文件夹faas-currenttime

  2. 文件夹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

  1. 打开build.gradle文件,添加下图红框中的内容,即jackson和common库的依赖:

  1. 进入文件夹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;

}

}

  1. 把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();

public static String getIpAddress() {

try {

Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();

InetAddress ip = null;

while (allNetInterfaces.hasMoreElements()) {

NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();

if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {

continue;

} else {

Enumeration 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 “”;

}

private static String getPID() {

return Manage

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

mentFactory

.getRuntimeMXBean()

.getName()

.split("@")[0];

}

private String getUserName(IRequest req) {

// 如果从请求body中取不到userName,就用

String userName = null;

try {

Map mapFromStr = mapper.readValue(req.getBody(),

new TypeReference>() {});

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 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;

}

}

  • 至此编码完成,接下来是制作镜像和部署;
部署
  1. 在faas-currenttime.yml所在目录执行以下命令,即可开始制作镜像,制作过程中会有gradle的编译过程,如果编译失败会中断镜像制作:

faas-cli build -f ./faas-currenttime.yml

  1. 镜像制作成功时,控制台输出类似如下信息:

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

  1. 将镜像推送到镜像仓库,以便Kubernetes可以下载到此镜像,我这里用的是hub.docker.com,因为我的ID是bolingcavalry,所执行以下命令即可推送成功:

docker push bolingcavalry/faas-currenttime:latest

  1. 执行以下命令部署函数到OpenFaaS:

faas-cli deploy -f faas-currenttime.yml

  1. 控制台响应如下,可见部署已经开始,并且给出了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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/673195.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号