Your First Function
source link: https://cloud.google.com/functions/docs/first-java
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Your First Function: Java
This guide takes you through the process of writing a Cloud Function using the Java runtime. There are two types of Cloud Functions:
- An HTTP function, which you invoke from standard HTTP requests.
- A background function, which you use to handle events from your Cloud infrastructure, such as messages on a Cloud Pub/Sub topic, or changes in a Cloud Storage bucket.
The document shows how to create a simple HTTP function and build it using either Maven or Gradle.
Learn more: For more details, read about HTTP functions and background functions.
Guide structure
Creating a GCP project using Cloud SDK
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
Note: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project. -
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Cloud Functions and Cloud Build APIs.
- Install and initialize the Cloud SDK.
- Update and install
gcloud
components:gcloud components update
Need a command prompt? You can use the Google Cloud Shell. The Google Cloud Shell is a command line environment that already includes the Google Cloud SDK, so you don't need to install it. The Google Cloud SDK also comes preinstalled on Google Compute Engine Virtual Machines.
Prepare your development environment.Creating a function
This section describes how to create a function.
Create a directory on your local system for the function code:
Linux or Mac OS X:
mkdir ~/helloworld
cd ~/helloworldWindows:
mkdir %HOMEPATH%\helloworld
cd %HOMEPATH%\helloworldCreate the project structure to contain the source directory and source file.
mkdir -p src/main/java/functions
touch src/main/java/functions/HelloWorld.javaAdd the following contents to the
HelloWorld.java
file:
package functions;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.BufferedWriter;
import java.io.IOException;
public class HelloWorld implements HttpFunction {
// Simple function to return "Hello World"
@Override
public void service(HttpRequest request, HttpResponse response)
throws IOException {
BufferedWriter writer = response.getWriter();
writer.write("Hello World!");
}
}This example function outputs the greeting "Hello World!"
Specifying dependencies
The next step is to set up dependencies:
Change directory to the helloworld
directory you created above, and create
a pom.xml
file:
cd ~/helloworld
touch pom.xml
To manage dependencies using Maven, specify the dependencies in
the <dependencies>
section inside the pom.xml file
of your project. For this exercise, copy the following contents
into your pom.xml
file.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.cloud.functions</groupId>
<artifactId>functions-hello-world</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<!-- Required for Function primitives -->
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!--
Google Cloud Functions Framework Maven plugin
This plugin allows you to run Cloud Functions Java code
locally. Use the following terminal command to run a
given function locally:
mvn function:run -Drun.functionTarget=your.package.yourFunction
-->
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.9.7</version>
<configuration>
<functionTarget>functions.HelloWorld</functionTarget>
</configuration>
</plugin>
</plugins>
</build>
</project>
See helloworld for a complete sample based on Maven.
Learn more: For more details, read about specifying dependencies.Building and testing locally
Before deploying the function, you can build and test it locally:
Run the following command to confirm that your function builds:
mvn compile
Another option is to use the mvn package
command to compile your Java
code, run any tests, and package the code up in a JAR file within the target
directory. You can learn more about the Maven build lifecycle
here.
To test the function, run the following command:
mvn function:run
If testing completes successfully, it displays the URL you can visit in your web
browser to see the function in action:
http://localhost:8080/
. You should see a Hello World!
message.
Alternatively, you can send requests to this function using curl
from another
terminal window:
curl localhost:8080
# Output: Hello World!
Deploying the function
To deploy the function with an HTTP trigger, run the following
command in the helloworld
directory:
gcloud functions deploy my-first-function --entry-point functions.HelloWorld --runtime java11 --trigger-http --memory 512MB --allow-unauthenticated
where my-first-function
is the registered name by which your function
will be identified in the Cloud Console, and --entry-point
specifies
your function's fully qualified class name (FQN).
Testing the deployed function
When the function finishes deploying, take note of the
httpsTrigger.url
property or find it using the following command:gcloud functions describe my-first-function
It should look like this:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/my-first-function
Visit this URL in your browser. You should see a
Hello World!
message.
Viewing logs
Using the command-line tool
Logs for Cloud Functions are viewable in the Cloud Logging UI, and
via the gcloud
command-line tool.
To view logs for your function with the gcloud
tool, use the
logs read
command, followed by
the name of the function:
gcloud functions logs read my-first-function
The output should resemble the following:
LEVEL NAME EXECUTION_ID TIME_UTC LOGNote: There is typically a slight delay between when log entries are created and when they show up in Cloud Logging.
D my-first-function k2bqgroszo4u 2020-07-24 18:18:01.791 Function execution started
D my-first-function k2bqgroszo4u 2020-07-24 18:18:01.958 Function execution took 168 ms, finished with status code: 200
...
Using the Logging dashboard
You can also view logs for Cloud Functions from the Cloud Console.
Learn more: For more details, read about writing, viewing, and responding to logs.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK