This is question 4 of a game intended to demonstrate serverless and event-driven features on Kubernetes, using Knative and CloudEvents.
- For more information on the game, visit this page.
- For instructions on deploying the entire system, visit this page.
Question 4 is a Spring Boot project relying on Spring Cloud Function, Spring Native, and the CloudEvents Java SDK. The project has been initialized using the Knative func plugin.
$ http <url> player="jon-snow" sessionId="game-blahblah" message="Spring loves Knative"
HTTP/1.1 200 OK
Content-Length: 98
Content-Type: application/json
accept-encoding: gzip, deflate
connection: keep-alive
user-agent: HTTPie/3.1.0
{
"player": "jon-snow"
"level": "devoxxuk-question-4",
"levelScore": 33,
"sessionId": "game-blahblah",
"gameTime": "2022-04-19T11:40:46.04108"
}
Make sure you have a Java 17 distribution installed.
You can run the application locally as follows.
./gradlew bootRun
To run the tests run the following command.
./gradlew test
It's recommended to set the FUNC_REGISTRY
environment variable.
export FUNC_REGISTRY=<registry>/<username>
echo "export FUNC_REGISTRY=<registry>/<username>" >> ~/.bashrc
where <registry>
is a container registry URI (for example, ghcr.io
) and username
is your account name on
that registry.
This command builds an OCI image for the function. By default, this will build a GraalVM native image.
func build -v
Note: If you want to disable the native build, you need to edit the func.yaml
file and
remove (or set to false) the following BuilderEnv variable:
buildEnvs:
- name: BP_NATIVE_IMAGE
value: "true"
This command runs the func locally in a container using the image created above.
func run
This command will build and deploy the function into cluster.
func deploy -v
For the examples below, please be sure to set the URL
variable to the route of your function.
You get the route by following command.
func info
Note the value of Routes: from the output, set $URL
to its value.
TIP:
If you use kn
then you can set the url by:
# kn service describe <function name> and show route url
export URL=$(kn service describe $(basename $PWD) -ourl)
Then, call the function as follows.
http $URL player="jon-snow" sessionId="game-blahblah" message="Spring loves Knative"
To clean the deployed function run:
func delete