func/templates/springboot/cloudevents
Gunjan Vyas e465348210
chore: update springboot dependencies (#1183)
2022-08-23 12:05:17 +00:00
..
.mvn/wrapper Remove unnecesary files from Java template (#1008) 2022-05-06 15:06:29 +00:00
src Support using 'func invoke' for springboot templates (#920) 2022-03-23 09:31:18 -07:00
.editorconfig feat!: rename event templates to 'cloudevents' (#584) 2021-10-13 14:19:42 -07:00
.gitignore feat!: rename event templates to 'cloudevents' (#584) 2021-10-13 14:19:42 -07:00
README.md Support using 'func invoke' for springboot templates (#920) 2022-03-23 09:31:18 -07:00
manifest.yaml Update springboot http template (#914) 2022-03-22 08:31:12 -07:00
mvnw feat!: rename event templates to 'cloudevents' (#584) 2021-10-13 14:19:42 -07:00
mvnw.cmd feat!: rename event templates to 'cloudevents' (#584) 2021-10-13 14:19:42 -07:00
pom.xml chore: update springboot dependencies (#1183) 2022-08-23 12:05:17 +00:00

README.md

Function project

Welcome to your new Function project!

This sample project contains a single function based on Spring Cloud Function: echo.EchoFunction, which returns an echo of the data passed via CloudEvents.

Local execution

Make sure that Java 11 SDK is installed.

To start server locally run ./mvnw spring-boot:run. The command starts http server and automatically watches for changes of source code. If source code changes the change will be propagated to running server. It also opens debugging port 5005 so a debugger can be attached if needed.

To run tests locally run ./mvnw test.

The func CLI

It's recommended to set FUNC_REGISTRY environment variable.

# replace ~/.bashrc by your shell rc file
# replace docker.io/johndoe with your registry
export FUNC_REGISTRY=docker.io/johndoe
echo "export FUNC_REGISTRY=docker.io/johndoe" >> ~/.bashrc

Building

This command builds an OCI image for the function. By default, this will build a JVM image.

func build -v                  # build image

Note: If you want to enable the native build, you need to edit the func.yaml file and set the following BuilderEnv variable:

buildEnvs:
  - name: BP_NATIVE_IMAGE
    value: "true"

Running

This command runs the func locally in a container using the image created above.

func run

Deploying

This command will build and deploy the function into cluster.

func deploy -v # also triggers build

Function invocation

Spring Cloud Functions allows you to route CloudEvents to specific functions using the Ce-Type attribute. For this example, the CloudEvent is routed to the echo function. You can define multiple functions inside this project and then use the Ce-Type attribute to route different CloudEvents to different Functions. Check the src/main/resources/application.properties file for the functionRouter configurations. Notice that you can also use path-based routing and send the any event type by specifying the function path, for this example: "$URL/echo".

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)

func

Using func invoke command with CloudEvents Ce-Type routing:

func invoke --type "echo"

Using Path-Based routing:

func invoke --target "$URL/echo"

cURL

Using CloudEvents Ce-Type routing:

curl -v "$URL/" \
  -H "Content-Type:application/json" \
  -H "Ce-Id:1" \
  -H "Ce-Subject:Echo" \
  -H "Ce-Source:cloud-event-example" \
  -H "Ce-Type:echo" \
  -H "Ce-Specversion:1.0" \
  -w "\n" \
  -d "hello"

Using Path-Based routing:

curl -v "$URL/echo" \
  -H "Content-Type:application/json" \
  -H "Ce-Id:1" \
  -H "Ce-Subject:Echo" \
  -H "Ce-Source:cloud-event-example" \
  -H "Ce-Type:echo" \
  -H "Ce-Specversion:1.0" \
  -w "\n" \
  -d "hello"

HTTPie

Using CloudEvents Ce-Type routing:

echo hello | http -v "$URL/" \
  Content-Type:application/json \
  Ce-Id:1 \
  Ce-Subject:Echo \
  Ce-Source:cloud-event-example \
  Ce-Type:MyEvent \
  Ce-Specversion:1.0

Using Path-Based routing:

echo hello | http -v "$URL/echo" \
  Content-Type:application/json \
  Ce-Id:1 \
  Ce-Subject:Echo \
  Ce-Source:cloud-event-example \
  Ce-Type:MyEvent \
  Ce-Specversion:1.0

Cleanup

To remove the deployed function from your cluster, run:

func delete