-
If you haven't already, set up a Java Development Environment (including google-cloud-sdk and maven utilities) by following the Java setup guide and creating a project.
-
You can use MySQL or PostgreSQL instance for this sample. Create a 2nd Gen Cloud SQL Instance by following corresponding instructions: MySQL / PostgreSQL. Note the connection string, database user, and database password that you create.
-
Create a database for your application by following corresponding instructions: MySQL / PostgreSQL. Note the database name.
-
Assign your connection details in the following format:
r2dbc:gcp:<'mysql' or 'postgres'>://<user>:<password>@<connection_name>/<db_name>
to an environment variable
CLOUD_SQL_CONNECTION_STRING
.Example for MySQL:
export CLOUD_SQL_CONNECTION_STRING=r2dbc:gcp:mysql://user:123456@my-project:us-central1:r2dbctest/testdb
Example for PostgreSQL:
export CLOUD_SQL_CONNECTION_STRING=r2dbc:gcp:postgres://user:123456@my-project:us-central1:r2dbctest/testdb
The schema will be created automatically when the application starts.
To run this application locally, run the following command inside the project folder:
mvn spring-boot:run
Navigate to http://localhost:8080
to verify your application is running correctly.
To run on GAE-Standard, create an AppEngine project by following the setup for these instructions and verify that appengine-maven-plugin has been added in your build section as a plugin.
Edit src/main/appengine/app.yaml
to set CLOUD_SQL_CONNECTION_STRING
to your connection string.
The following command will deploy the application to your Google Cloud project:
mvn clean package appengine:deploy
See the Cloud Run documentation for more details on connecting a Cloud Run service to Cloud SQL.
-
Create an environment variable with your GCP project id:
export PROJECT_ID=[YOUR_PROJECT_ID]
-
Build the container image and push it to Google Container Registry (GCR):
mvn clean package com.google.cloud.tools:jib-maven-plugin:2.8.0:build
-Dimage=gcr.io/[YOUR_PROJECT_ID]/r2dbc-sample -DskipTests
```
-
Deploy the service to Cloud Run:
gcloud run deploy r2dbc-sample \ --image gcr.io/$PROJECT_ID/r2dbc-sample \ --platform managed \ --memory 512Mi \ --set-env-vars CLOUD_SQL_CONNECTION_STRING=$CLOUD_SQL_CONNECTION_STRING
Take note of the URL output at the end of the deployment process.
It is recommended to use the Secret Manager integration for Cloud Run instead of using environment variables for the SQL configuration. The service injects the SQL credentials from Secret Manager at runtime via an environment variable.
Create secrets via the command line:
echo -n "my-awesome-project:us-central1:my-cloud-sql-instance" | \ gcloud secrets versions add INSTANCE_CONNECTION_NAME_SECRET --data-file=-
Deploy the service to Cloud Run specifying the env var name and secret name:
gcloud beta run deploy SERVICE --image gcr.io/[YOUR_PROJECT_ID]/run-sql \ --add-cloudsql-instances [INSTANCE_CONNECTION_NAME] \ --update-secrets INSTANCE_CONNECTION_NAME=[INSTANCE_CONNECTION_NAME_SECRET]:latest
-
Navigate to the URL noted in Step 2.
For more details about using Cloud Run see http://cloud.run. Review other Java on Cloud Run samples.