-
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/testdbExample 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:runNavigate 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:deploySee 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 compile com.google.cloud.tools:jib-maven-plugin:2.5.2:build \ -Dimage=gcr.io/$PROJECT_ID/r2dbc-sample -
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_STRINGTake note of the URL output at the end of the deployment process.
-
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.