-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use quarkus with Google Cloud Run And CloudSQL #6634
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
707b9d1
Use of quarkus-agroal with GCP CloudSQL #5901
d4a8325
Use of quarkus-agroal with GCP CloudSQL #5901
1f9f77b
modified docs: updated dependencies
87c55f3
fixed documentation typos
c7b811c
Use quarkus with Google Cloud Run And CloudSQL
655d3fd
Use quarkus with Google Cloud Run And CloudSQL
708b113
Updated junit tests
32def54
updated tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
//// | ||
This guide is maintained in the main Quarkus repository | ||
and pull requests should be submitted there: | ||
https://github.com/quarkusio/quarkus/tree/master/docs/src/main/asciidoc | ||
//// | ||
= Quarkus - Deploying on Google cloud platform | ||
|
||
include::./attributes.adoc[] | ||
|
||
This guide covers: | ||
|
||
Connecting to a CloudSQL postgresql instance with quarkus-agroal and service account using CloudRun | ||
|
||
== Prerequisites | ||
|
||
For this guide you need: | ||
|
||
* roughly 1 hour | ||
* having access to an Google cloud platform project with owner rights. | ||
|
||
This guide will take as input an application developed in the link:datasource[datasource guide]. | ||
|
||
Make sure you have the application at hand working locally. | ||
|
||
|
||
== Solution | ||
|
||
We recommend to follow the instructions in the next sections and build the application step by step. | ||
|
||
== Connecting to CloudSQL | ||
|
||
Google CloudSQL managed service allows 4 kinds of connection : | ||
|
||
. Using public IP | ||
. Using private IP | ||
. Using Cloud SQL Proxy | ||
. Using service account | ||
|
||
The first two don't need more details, simply connect to your database following the link:datasource[datasource guide]. | ||
The third allows you to connect as if locally, but is not available for AppEngine or CloudRun where you only deploy one artifact. | ||
Please check link:https://cloud.google.com/sql/docs/postgres/external-connection-methods?hl=en[Connection options for external applications]. | ||
|
||
This guide will help you through the fourth possibility : connecting using service account. | ||
|
||
== Adding necessary dependencies to your application. | ||
|
||
You will need to add the _Cloud SQL Postgres Socket Factory_ and the PostgreSQL JDBC driver extension. | ||
|
||
.Example dependencies using Maven | ||
[source,xml, subs="attributes"] | ||
---- | ||
<dependency> | ||
<groupId>com.google.cloud.sql</groupId> | ||
<artifactId>postgres-socket-factory</artifactId> | ||
<version>1.0.15</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-jdbc-postgresql</artifactId> | ||
</dependency> | ||
---- | ||
|
||
== Configuring the CloudSQL instance | ||
|
||
If you haven't already please follow the link:https://cloud.google.com/sql/docs/postgres/create-instance[Creating instances guide]. | ||
|
||
Ensure you have a service account with _Cloud SQL Client_ role (minimum necessary role), and get your instance connection name (`PROJECT_ID:REGION:INSTANCE_ID`). | ||
|
||
== Configuring the application | ||
|
||
Configure your quarkus application as follows : | ||
|
||
[source,properties] | ||
-- | ||
quarkus.datasource.url=jdbc:postgresql:///${DB_NAME} | ||
quarkus.datasource.driver=org.postgresql.Driver | ||
quarkus.datasource.username = ${DB_USER} | ||
quarkus.datasource.password = ${DB_PASSWORD} | ||
quarkus.datasource.socket-factory=com.google.cloud.sql.postgres.SocketFactory | ||
quarkus.datasource.additional-jdbc-properties.cloudSqlInstance=project:zone:instance-name | ||
-- | ||
|
||
NOTE: To set the environment variables (DB_NAME, DB_USER, DB_PASSWORD) when deploying to CloudRun there is an option `ADD VARIABLE` | ||
|
||
WARNING: Don't forget to add your service account key to the deployed image, and to accept requests on the expected port | ||
|
||
[source,docker] | ||
-- | ||
FROM gcr.io/distroless/java:11 | ||
COPY target/*.jar /app/application-runner.jar | ||
COPY <your_service_account_key>.json /app | ||
WORKDIR /app | ||
CMD ["application-runner.jar","-Dquarkus.http.host=0.0.0.0", "-Dquarkus.http.port=$PORT"] | ||
-- | ||
|
||
NOTE: `$PORT` value will be injected automatically by CloudRun and is to be used for HealthCheck | ||
|
||
|
||
You should now be able to connect to your instance. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package io.quarkus.agroal.runtime; | ||
|
||
import java.time.Duration; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import io.agroal.api.configuration.AgroalConnectionFactoryConfiguration; | ||
|
@@ -124,4 +125,16 @@ public class DataSourceRuntimeConfig { | |
*/ | ||
@ConfigItem | ||
public Optional<String> validationQuerySql; | ||
|
||
/** | ||
* The socket factory driver that should be appended as a datasource url property | ||
*/ | ||
@ConfigItem | ||
public Optional<String> socketFactory; | ||
Comment on lines
+128
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed since the configuration is set as a JDBC property and could live in the additionalJdbcProperties Map below |
||
|
||
/** | ||
* General properties that should be applied for a JDBC | ||
*/ | ||
@ConfigItem | ||
public Map<String, String> additionalJdbcProperties; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
socketFactory
is a JDBC property, the best is to have it declared in theadditionalJdbcProperties
, like in:quarkus.datasource.additional-jdbc-properties.socketFactory=foobar