google-cloud-run quarkus google-cloud-sql - google-cloud-sql

I deploy a native quarkus application under cloud run. This application need to connect to a cloud sql postgresql database.
On the configuration panel's cloud run, i create a Cloud SQL connections (db-instance-name eq. cloud sql) and some variable as DB_USER, DB_PASSWORD, DB_NAME
On Quarkus, i define properties as below :
quarkus.datasource.jdbc.url=jdbc:postgresql:///${DBNAME}:5432
quarkus.datasource.driver=org.postgresql.Driver
quarkus.datasource.username=${DB_USER}
quarkus.datasource.password=${DB_PASSWORD}
My pom.xml
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>postgres-socket-factory</artifactId>
<version>1.0.16</version>
</dependency>
When cloud run is starting, an exception occured :
WARN [io.agr.pool] (Agroal_18109070341) Datasource '<default>': Connection to :5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
I have to say that cloud run is deployed by cloud build.
Could you help me to fix this issue...
Thanks

You need to use a unix_socket file looks like the following
postgres://user:password#/databasename?unix_sock=/cloudsql/projectshortcod:us-central1:pg-instance-name
you probably need to put this in the URL field of you library, but make sure that the library supports unix_socket databases.

I got this working (for MySQL instead of Postgres; but should be similar) without using a unix_socket, but simply using a quarkus.datasource.jdbc.url=jdbc:mysql:///DB-NAME?ipTypes=PRIVATE&cloudSqlInstance=PROJECT-NAME:REGION:demo&socketFactory=com.google.cloud.sql.mysql.SocketFactory (and an quarkus.datasource.username= and quarkus.datasource.password=, as always).
The ?ipTypes=PRIVATE& is to connect to a Cloud SQL with only a private instead of a public IP from within GCP; omit that if you want to connect to a public IP, e.g. from your developer machine at home or work.
The trickiest part wasn't so much this but to set up all the required pre-requisites on GCP for this connection to work from a Quarkus service running on AppEngine Standard.
Quarkus Issue #9985 is for about documenting this.

Related

Connecting Postgres instance to AppEngine - Google Cloud, with SpringBoot

I've nearly got a SpringBoot app running on Google Cloud services that is connected to a Postgres instance.
I've ran through the steps on their guide, located here and have gotten to the point where I need to set my variables up for the app to find the database instance:
The problem encountered is two fold:
I don't know where and how to set these
My server logs report this error:
meaning that the Spring application is trying to find the database like it would on my local.
I set the following values in the app.yaml (assuming this is where they should be?)
runtime: java11
instance_class: F1
env_variables:
SQL_USER: quickstart-user
SQL_PASSWORD: <password>
SQL_DATABASE: quickstart_db
INSTANCE_CONNECTION_NAME: quickstart-instance
So, my question(s) are:
Is this the correct place to set them?
If not, do I need a appengine-web.xml file instead (And does anyone have an example of what this looks like, I can't find one)
How do I stop the app from looking for the local database?
Thanks

MicroProfile LRA on Wildfly - How to setup LRA coordinator host and port on client application runing on WildFly

I have introduced LRA on a MicroProfile application already running on WildFly AS.
To get the LRA working I have added the following depedency on my application pom.xml
<dependency>
<groupId>org.jboss.narayana.rts</groupId>
<artifactId>narayana-lra</artifactId>
<version>5.10.6.Final</version>
</dependency>
and I have created an LRA coordinator running on the same host ad listening on port 8080.
The application works as expected.
Now I want to move LRA coordinator on a remote host, but I'm not able to configure my application to point to it (on new host and port).
I have tried to put in my microprofile-config.properties the following parameters:
mp.lra.http.host=<new_host>
mp.lra.http.port=<new_port>
but without effect.
Can anyone suggest me hot to configure LRA coordinator host and port on client application?
Thanks in advance
Narayana doesn't support MicroProfile Config yet even if it is something that it probably should. The properties you want to set are defined only as system properties (i.e., read with System.getProperty(String, String).
Another issue is that the properties you are looking for are defined as lra.http.host and lra.http.port respectively. MP LRA made a deliberate decision to remove all coordinator references from the specification to not specify the implementation architectures (saga can also be implemented as an orchestration pattern).
So you need to set these system properties for instance when you are starting the WildFly server:
bin/standalone.sh -Dlra.http.host=lra-coordinator.com -Dlra.http.port=7777
Finally, if you ever move to the latest Narayana releases, these properties were merged only into single property lra.coordinator.url which is however still read only from system properties.

What is a schema manager in Spring's Hibernate and how I set it?

Some tables were dropped at development environment during a spring-data-jpa project test and the developer responsible for it said it only used spring.jpa.hibernate.ddl-auto=update. Checking here, I suspected he had it set as create-drop, but since he said he didn't use, I went for this piece of information:
Spring Boot chooses a default value for you based on whether it thinks your database is embedded.
It defaults to create-drop if no schema manager has been detected
Since his application actually managed to connect to our development environment PostgreSQL database (since it dropped some tables), I start to think he maybe could have forgotten to set hbm2ddl.auto and automatically it went as create-drop.
Is it possible to connect to PostgreSQL and don't have a valid schema manager defined? Which are the most common schema managers?
PS: at this application #DataSource is set this way:
#Bean
public DataSource dataSource() {
Class.forName("org.postgresql.Driver");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(someUrlConnectionStringFromApplicationDotPropertiesFile);
return dataSource;
}
like String someUrlConnectionStringFromApplicationDotPropertiesFile = "jdbc:postgresql://ipAddress:port/dbName?user=user&password=pass". I know spring-data-jpa sets everything with spring jpa properties automatically without this method, this is currently an unchangeable legacy code :(
Spring Boot said they are Higher-level Database Migration Tool (Flyway or Liquibase).
ref: https://docs.spring.io/spring-boot/docs/2.7.x/reference/html/howto.html#howto.data-access.jpa-properties, https://docs.spring.io/spring-boot/docs/2.7.x/reference/html/howto.html#howto.data-access.jpa-properties
You can also configure them through Spring Boot documentation or their own docs. ref: https://docs.spring.io/spring-boot/docs/2.7.x/reference/html/howto.html#howto.data-initialization.migration-tool
For example, configure Flyway community version through maven (https://flywaydb.org/download/community):
<build>
...
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>9.12.0</version>
</plugin>
...
</build>

Resolving properties via eureka in Spring Cloud

I have a simple #EnableSidecar for postgres app. with virtual-host-name: postgres.vip and sidecar.port: 5432. Can I in another #EnableDiscoveryClient app. have spring cloud resolve #Value("${postgres.vip.port}") or do I have to do the resolving manually via a EurekaClient?
#Value("${postgres.vip.port}") won't work. You can use EurekaClient directly or use org.springframework.cloud.client.discovery.DiscoveryClient which is an abstraction.

Rest Client Timeout - Implementation specific?

I am using Wildfly and I started to write some clients to connect to other backends. Now I want to set some timeouts to my stubs to avoid that my application performance suffers from slow or not responding backends.
The connection works very well, but when I wanted to set the timeout I realized that I have to fall back to the client implementation so I used resteasy (used in Wildfly afaik).
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.8.Final</version>
<scope>provided</scope>
</dependency>
Is there really no standard way to accomplish that?
The resteasy documentation is quite short. Does anyone have a good source with examples for setting a timeout there?
greetings
m
PS:
Client client = ClientBuilder.newClient();
Builder request = client.target(ADRESS_BALANCE).request();
return request.get(BigInteger.class);
RestEasy way to set socketTimeout:
ResteasyClientBuilder builder = (ResteasyClientBuilder) ClientBuilder.newBuilder();
builder.socketTimeout(60, TimeUnit.SECONDS);
If you would like to use standard way check this answer