Giter VIP home page Giter VIP logo

code-as-a-function's Introduction

About

These are examples of applications to run in a serverless environment at GCP

Description

code-as-a-function

This is the minimal app to run as a Cloud Function at GCP

springboot-as-a-function

This is an example of minimal Spring Boot app to run as a Cloud Function at GCP

Local testing

export MAIN_CLASS=functions.WordCounterApp
export PROJECT_ID=<project_id>
mvn -pl springboot-as-a-function function:run

In other console invoke the following command:

./send_local_message.sh "hello big world"

and see how Function processes the event (must count all 3 words in message). In real situation the message from Pub/Sub topic will be forwarded to app.

Deployment

  1. Create a new Pub/Sub topic to be linked to Function and a topic to publish the results to
gcloud config set project <project_id>
gcloud pubsub topics create cf-trigger
gcloud pubsub topics create cf-results
  1. Build the app by running:
mvn -pl springboot-as-a-function package

This command will build in the target/deploy directory the JAR file to deploy to GCP

  1. Deploy the JAR that you created by running
gcloud functions deploy sbaaf-gcp-background \
--entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
--runtime java11 \
--trigger-topic cf-trigger \
--source ./springboot-as-a-function/target/deploy \
--set-env-vars PROJECT_ID=<project_id>
--memory 512MB

where the parameter --trigger-topic which topic will trigger the function invocation when new messages are published to it.

In some cases this command may be need to enabling service [cloudfunctions.googleapis.com] on project. The deployment normally takes 1-3 min. The deployed function is available at https://console.cloud.google.com/functions/list?project=project_id

  1. Invoke the background function by publishing a message to topic, the outcome must be the same as in Local testing:
gcloud pubsub topics publish cf-trigger --message="hello big world"

The result of execution should be visible in logs, and in cf-results topic

References

[1] https://docs.spring.io/spring-cloud-function/docs/current/reference/html/gcp.html

[2] https://github.com/spring-cloud/spring-cloud-function

code-as-a-function's People

Contributors

akaliutau avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.