Skip to content

Commit 7cf32c1

Browse files
committed
update java samples
Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>
1 parent 7916404 commit 7cf32c1

19 files changed

Lines changed: 1080 additions & 96 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ We currently support two kinds of runtime, knative and async.
1313
### Knative runtime
1414

1515
- ["Hello world" in go](functions/knative/hello-world-go)
16-
- ["Hello world" in java](functions/knative/hello-world-java)
16+
- ["Hello world" in java](functions/knative/java/hello-world)
1717
- ["Hello world" in python](functions/knative/hello-world-python)
1818
- ["Hello world" in node](functions/knative/hello-world-node)
1919
- ["Hello world" in dotnet](functions/knative/hello-world-dotnet)
@@ -25,6 +25,7 @@ We currently support two kinds of runtime, knative and async.
2525
- Async bindings sample in [go](functions/async/bindings)
2626
- Async pubsub sample in [go](functions/async/pubsub)
2727
- Logs handler function [go](functions/async/logs-handler-function)
28+
- Java samples in [java](functions/async/java)
2829

2930
### Other samples
3031

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Autoscaling service based on queue depth
2+
3+
## Prerequisites
4+
5+
You can refer to the [Installation Guide](https://openfunction.dev/docs/getting-started/installation/) to setup OpenFunction.
6+
7+
Follow [this guide](../../../../Prerequisites.md#kafka) to install a Kafka server named `kafka-server` and a Topic named `sample-topic`.
8+
9+
Follow [this guide](../../../../Prerequisites.md#registry-credential) to create a registry credential.
10+
11+
## Deployment
12+
13+
The `cron-input-kafka-output-java` function will be triggered by Dapr's `bindings.cron` component at a frequency of once every 2 seconds. After being triggered, it will send a greeting to another service via Dapr's `bindings.kafka` component.
14+
15+
Modify the `spec.image` field in `cron-input-kafka-output.yaml` to your own container registry address:
16+
17+
```yaml
18+
apiVersion: core.openfunction.io/v1beta1
19+
kind: Function
20+
metadata:
21+
name: cron-input-kafka-output-java
22+
spec:
23+
image: "<your registry name>/cron-input-kafka-output-java:v1"
24+
```
25+
26+
Use the following commands to create this Function:
27+
28+
```shell
29+
kubectl apply -f cron-input-kafka-output.yaml
30+
```
31+
32+
Afterwards, use the following command to observe the log of the function:
33+
34+
```shell
35+
kubectl logs -f \
36+
$(kubectl get po -l \
37+
openfunction.io/serving=$(kubectl get functions cron-input-kafka-output-java -o jsonpath='{.status.serving.resourceRef}') \
38+
-o jsonpath='{.items[0].metadata.name}') \
39+
function
40+
```
41+
42+
You will be able to see messages similar to the following:
43+
44+
```shell
45+
plugin plugin-example:v1.0.0 exec pre hook for binding serving-2w6ft-component-cron-jsvxg at 2022-10-19 08:00:25.Z
46+
receive event:
47+
send to output sample
48+
plugin plugin-example:v1.0.0 exec post hook for binding serving-2w6ft-component-cron-jsvxg at 2022-10-19 08:00:25.Z
49+
```
50+
51+
Now we need to start the kafka-input function.
52+
53+
```shell
54+
kubectl apply -f ../kafka-input/kafka-input.yaml
55+
```
56+
57+
Use the following command to observe the log of the function:
58+
59+
```shell
60+
kubectl logs -f \
61+
$(kubectl get po -l \
62+
openfunction.io/serving=$(kubectl get functions kafka-input-java -o jsonpath='{.status.serving.resourceRef}') \
63+
-o jsonpath='{.items[0].metadata.name}') \
64+
function
65+
```
66+
67+
You will be able to see messages similar to the following:
68+
69+
```shell
70+
plugin plugin-example:v1.0.0 exec pre hook for binding serving-p7nll-component-target-topic-kjh7q at 2022-10-19 08:12:58.Z
71+
receive event: ""
72+
plugin plugin-example:v1.0.0 exec post hook for binding serving-p7nll-component-target-topic-kjh7q at 2022-10-19 08:12:58.Z
73+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
apiVersion: core.openfunction.io/v1beta1
2+
kind: Function
3+
metadata:
4+
name: cron-input-kafka-output-java
5+
annotations:
6+
plugins: |
7+
pre:
8+
- dev.openfunction.samples.plugins.ExamplePlugin
9+
post:
10+
- dev.openfunction.samples.plugins.ExamplePlugin
11+
spec:
12+
version: "v2.0.0"
13+
image: openfunctiondev/cron-input-kafka-output-java:v1
14+
imageCredentials:
15+
name: push-secret
16+
build:
17+
builder: openfunction/builder-java:v2-18
18+
env:
19+
FUNC_NAME: "dev.openfunction.samples.OpenFunctionImpl"
20+
FUNC_CLEAR_SOURCE: "true"
21+
srcRepo:
22+
url: "https://github.com/OpenFunction/samples.git"
23+
sourceSubPath: "functions/knative/java"
24+
revision: "main"
25+
serving:
26+
template:
27+
containers:
28+
- name: function # DO NOT change this
29+
imagePullPolicy: IfNotPresent
30+
runtime: "async"
31+
inputs:
32+
- name: cron
33+
component: cron
34+
outputs:
35+
- name: sample
36+
component: kafka-server
37+
operation: "create"
38+
bindings:
39+
cron:
40+
type: bindings.cron
41+
version: v1
42+
metadata:
43+
- name: schedule
44+
value: "@every 2s"
45+
kafka-server:
46+
type: bindings.kafka
47+
version: v1
48+
metadata:
49+
- name: brokers
50+
value: "kafka-server-kafka-brokers:9092"
51+
- name: topics
52+
value: "sample-topic"
53+
- name: consumerGroup
54+
value: "bindings-with-output"
55+
- name: publishTopic
56+
value: "sample-topic"
57+
- name: authRequired
58+
value: "false"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
apiVersion: core.openfunction.io/v1beta1
2+
kind: Function
3+
metadata:
4+
name: kafka-input-java
5+
annotations:
6+
plugins: |
7+
pre:
8+
- dev.openfunction.samples.plugins.ExamplePlugin
9+
post:
10+
- dev.openfunction.samples.plugins.ExamplePlugin
11+
spec:
12+
version: "v1.0.0"
13+
image: openfunctiondev/kafka-input-java:v1
14+
imageCredentials:
15+
name: push-secret
16+
build:
17+
builder: openfunction/builder-java:v2-18
18+
env:
19+
FUNC_NAME: "dev.openfunction.samples.OpenFunctionImpl"
20+
FUNC_CLEAR_SOURCE: "true"
21+
srcRepo:
22+
url: "https://github.com/OpenFunction/samples.git"
23+
sourceSubPath: "functions/knative/java"
24+
revision: "main"
25+
serving:
26+
runtime: async
27+
scaleOptions:
28+
minReplicas: 0
29+
maxReplicas: 10
30+
keda:
31+
scaledObject:
32+
pollingInterval: 15
33+
minReplicaCount: 0
34+
maxReplicaCount: 10
35+
cooldownPeriod: 60
36+
advanced:
37+
horizontalPodAutoscalerConfig:
38+
behavior:
39+
scaleDown:
40+
stabilizationWindowSeconds: 45
41+
policies:
42+
- type: Percent
43+
value: 50
44+
periodSeconds: 15
45+
scaleUp:
46+
stabilizationWindowSeconds: 0
47+
triggers:
48+
- type: kafka
49+
metadata:
50+
topic: sample-topic
51+
bootstrapServers: kafka-server-kafka-brokers.default.svc:9092
52+
consumerGroup: kafka-input
53+
lagThreshold: "20"
54+
inputs:
55+
- name: greeting
56+
component: target-topic
57+
bindings:
58+
target-topic:
59+
type: bindings.kafka
60+
version: v1
61+
metadata:
62+
- name: brokers
63+
value: "kafka-server-kafka-brokers:9092"
64+
- name: topics
65+
value: "sample-topic"
66+
- name: consumerGroup
67+
value: "kafka-input"
68+
- name: publishTopic
69+
value: "sample-topic"
70+
- name: authRequired
71+
value: "false"
72+
template:
73+
containers:
74+
- name: function # DO NOT change this
75+
imagePullPolicy: IfNotPresent
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Autoscaling service based on queue depth
2+
3+
## Prerequisites
4+
5+
You can refer to the [Installation Guide](https://openfunction.dev/docs/getting-started/installation/) to setup OpenFunction.
6+
7+
Follow [this guide](../../../../Prerequisites.md#kafka) to install a Kafka server named `kafka-server` and a Topic named `sample-topic`.
8+
9+
Follow [this guide](../../../../Prerequisites.md#registry-credential) to create a registry credential.
10+
11+
## Deployment
12+
13+
To configure the autoscaling demo we will deploy two functions: `subscriber` which will be used to process messages of the `sample-topic` queue in Kafka, and the `producer`, which will be publishing messages.
14+
15+
### Build and deploy Producer
16+
17+
Follow [this guide](../../pubsub/README.md#Build-and-deploy-Producer) to deploy a producer.
18+
19+
### Deploy Subscriber
20+
21+
Modify the ``spec.image`` field in `function-subscribe.yaml` to your own container registry address:
22+
23+
```yaml
24+
apiVersion: core.openfunction.io/v1beta1
25+
kind: Function
26+
metadata:
27+
name: autoscaling-subscriber-java
28+
spec:
29+
image: "<your registry name>/autoscaling-subscriber-java:v1"
30+
```
31+
32+
Use the following commands to create these Functions:
33+
34+
```shell
35+
kubectl apply -f function-subscriber.yaml
36+
```
37+
38+
Back in the initial terminal now, some 20-30 seconds after the `producer` starts, you should see the number of `subscriber` pods being adjusted by Keda based on the number of the `sample-topic` topic.
39+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
apiVersion: core.openfunction.io/v1beta1
2+
kind: Function
3+
metadata:
4+
name: autoscaling-subscriber-java
5+
annotations:
6+
plugins: |
7+
pre:
8+
- dev.openfunction.samples.plugins.ExamplePlugin
9+
post:
10+
- dev.openfunction.samples.plugins.ExamplePlugin
11+
spec:
12+
version: "v2.0.0"
13+
image: openfunctiondev/autoscaling-subscriber-java:v1
14+
imageCredentials:
15+
name: push-secret
16+
build:
17+
builder: openfunction/builder-java:v2-18
18+
env:
19+
FUNC_NAME: "dev.openfunction.samples.OpenFunctionImpl"
20+
FUNC_CLEAR_SOURCE: "true"
21+
# Use FUNC_GOPROXY to set the goproxy
22+
# FUNC_GOPROXY: "https://goproxy.cn"
23+
srcRepo:
24+
url: "https://github.com/OpenFunction/samples.git"
25+
sourceSubPath: "functions/knative/java"
26+
revision: "main"
27+
serving:
28+
runtime: "async"
29+
scaleOptions:
30+
keda:
31+
scaledObject:
32+
pollingInterval: 15
33+
minReplicaCount: 0
34+
maxReplicaCount: 10
35+
cooldownPeriod: 60
36+
advanced:
37+
horizontalPodAutoscalerConfig:
38+
behavior:
39+
scaleDown:
40+
stabilizationWindowSeconds: 45
41+
policies:
42+
- type: Percent
43+
value: 50
44+
periodSeconds: 15
45+
scaleUp:
46+
stabilizationWindowSeconds: 0
47+
triggers:
48+
- type: kafka
49+
metadata:
50+
topic: "sample-topic"
51+
bootstrapServers: kafka-server-kafka-brokers.default.svc.cluster.local:9092
52+
consumerGroup: autoscaling-subscriber
53+
lagThreshold: "20"
54+
template:
55+
containers:
56+
- name: function # DO NOT change this
57+
imagePullPolicy: IfNotPresent
58+
inputs:
59+
- name: producer
60+
component: kafka-server
61+
topic: "sample-topic"
62+
pubsub:
63+
kafka-server:
64+
type: pubsub.kafka
65+
version: v1
66+
metadata:
67+
- name: brokers
68+
value: "kafka-server-kafka-brokers:9092"
69+
- name: authRequired
70+
value: "false"
71+
- name: allowedTopics
72+
value: "sample-topic"
73+
- name: consumerID
74+
value: "autoscaling-subscriber"

functions/knative/hello-world-java/README.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)