Quarkus:谁说 Java 不能用来跑 Serverle

why-not-java

想到这个标题的时候,我第一时间想到的就是星爷的《唐伯虎点秋香》的这一幕。

当讨论起世界上最好的开发语言是什么的时候,Java 的粉丝们总会遇到这种场景:

吹:“Java 语法简单,容易上手!”

黑:“Java 启动慢,性能差,耗资源!”

吹:“Java 有世界上最多的程序员!”

黑:“Java 启动慢,性能差,耗资源!”

吹:“Java 生态好!”

黑:“Java 启动慢,性能差,耗资源!”

吹:“滚!”

今天我们继续说说 Quarkus,应“云”而生的 Java 框架。今天算是第三篇了,没看过的同学可以回顾一下:

上一篇的结尾预告:试试 Quarkus 在 ArgoCD 中的应用,看下 Serverless 上的使用体验。不过不想用 ArgoCD 了,因为这 workflow 这种场景实在体现不出 Quarkus 到底有多快。但又想做 Serverless,那就想到了 Knative Serving 了。

其实,还有一个原因是比较懒,上次的镜像还可以直接拿来用。

TL;DR

废话不多说,先上结论。Quarkus 与 Spring 首个请求的响应耗时:2.5s vs 5.7s

注:为了忽略拉取镜像的时间差异,提前 pull 镜像。

验证

环境准备

  • Kubernetes 1.18+ via minikube
  • Istio 1.9.2
  • Knative 0.22.0
  • Knative CLI (brew 安装)
  • watch (brew 安装)

环境的安装准备参考官方的文档。

镜像

资源镜像就使用上一篇文章构建的,但需要做下调整:

1
2
shell复制代码docker tag quarkus/quarkus-getting-started:distroless dev.local/quarkus/quarkus-getting-started:distroless
docker tag spring/spring-getting-started:latest dev.local/spring/spring-getting-started:latest

注:knative 会忽略 dev.local 镜像的预加载,不会在创建 knative service 的时候拉取。

然后使用 minikube image load 加载到 minikube环境中:

1
2
shell复制代码minikube image load dev.local/quarkus/quarkus-getting-started:distroless
minikube image load dev.local/spring/spring-getting-started:latest

knative 配置(可选)

修改 istio-system namespace 下的 configmap config-domain,增加新的 domain:nip.io

注:这个操作纯属个人喜好,不喜欢那个 example.com,可跳过。

获取 Istio Ingress 地址

使用命令获取 Ingress 的访问方式,这里 http2/80 后的 http://192.168.64.2:31608 就是我们需要的,记下这个 ip 和端口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
shell复制代码minikube service list

|------------------|----------------------------|-------------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|------------------|----------------------------|-------------------|---------------------------|
| default | kubernetes | No node port |
| istio-system | istio-egressgateway | No node port |
| istio-system | istio-ingressgateway | status-port/15021 | http://192.168.64.2:32431 |
| | | http2/80 | http://192.168.64.2:31608 |
| | | https/443 | http://192.168.64.2:31795 |
| | | tcp/31400 | http://192.168.64.2:31369 |
| | | tls/15443 | http://192.168.64.2:30293 |
| istio-system | istiod | No node port |
| istio-system | knative-local-gateway | No node port |
| knative-eventing | broker-filter | No node port |
| knative-eventing | broker-ingress | No node port |
| knative-eventing | eventing-webhook | No node port |
| knative-eventing | imc-dispatcher | No node port |
| knative-serving | activator-service | No node port |
| knative-serving | autoscaler | No node port |
| knative-serving | autoscaler-bucket-00-of-01 | No node port |
| knative-serving | autoscaler-hpa | No node port |
| knative-serving | controller | No node port |
| knative-serving | istio-webhook | No node port |
| knative-serving | webhook | No node port |
| kube-system | kube-dns | No node port |
|------------------|----------------------------|-------------------|---------------------------|

创建 Knative service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
yaml复制代码#quarkus
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello-quarkus
namespace: default
spec:
template:
spec:
containers:
- image: dev.local/quarkus/quarkus-getting-started:distroless
imagePullPolicy: Never
---
#spring
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: hello-spring
namespace: default
spec:
template:
spec:
containers:
- image: dev.local/spring/spring-getting-started:latest
imagePullPolicy: Never

通过 cli kn 命令查看下 service 的信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
shell复制代码kn service describe hello-quarkus -n default

Name: hello-quarkus
Namespace: default
Age: 21s
URL: http://hello-quarkus.default.nip.io

Revisions:
100% @latest (hello-quarkus-00001) [1] (21s)
Image: dev.local/quarkus/quarkus-getting-started:distroless

Conditions:
OK TYPE AGE REASON
++ Ready 9s
++ ConfigurationsReady 10s
++ RoutesReady 9s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
shell复制代码kn service describe hello-spring -n default

Name: hello-spring
Namespace: default
Age: 44s
URL: http://hello-spring.default.nip.io

Revisions:
100% @latest (hello-spring-00001) [1] (44s)
Image: dev.local/spring/spring-getting-started:latest

Conditions:
OK TYPE AGE REASON
++ Ready 31s
++ ConfigurationsReady 32s
++ RoutesReady 31s

从描述信息中可以拿到服务的访问地址,分别是 http://hello-quarkus.default.nip.iohttp://hello-spring.default.nip.io

接下来就需要在本地主机的 hosts 中加入解析:

1
2
arduino复制代码192.168.64.2    hello-quarkus.default.nip.io
192.168.64.2 hello-spring.default.nip.io

测试

上面操作完之后,就可以使用下面的地址访问服务了。

hello-quarkus.default.nip.io:31608/hello/greet…
hello-spring.default.nip.io:31608/hello/greet…

在测试的过程中,可以通过 watch -n 1 'kubectl get po -n default | grep hello' 命令来查看 pod 的创建和销毁。

2021-04-24 09.07.56

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%