0
votes

I have a little problem, I want to deploy a Java Spring App to Cloud RUN and take connection from CLOUD SQL SQL SERVER , I know that can connect via unix socket for MySQL and Postgresql (https://cloud.google.com/sql/docs/mysql/connect-run?hl=es-419) but for SQL Server do not has the drivers jet.

Another way is to connect for Proxy Like (https://medium.com/@petomalina/connecting-to-cloud-sql-from-cloud-run-dcff2e20152a) I tried but I can't, even that when deploy the script, it's tell me that is listenning for 127.0.0.1 for my instance id but when a tried to connect I can't.

here is my docker file

# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.5-jdk-8-alpine as builder

# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src
COPY ohJpo-2.1.0.jar .

# download the cloudsql proxy binary
# RUN wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O ./build/cloud_sql_proxy
# RUN chmod +x ./build/cloud_sql_proxy

COPY cloud_sql_proxy /build/cloud_sql_proxy
RUN chmod +x /build/cloud_sql_proxy

# copy the wrapper script and credentials
COPY run.sh /build/run.sh
COPY credentials.json /build/credentials.json

# Build a release artifact.
RUN mvn install:install-file -Dfile=/app/ohJpo-2.1.0.jar -DgroupId=ovenfo -DartifactId=ohJpo -Dversion=2.1.0 -Dpackaging=jar
RUN mvn package -DskipTests


# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk8:jdk8u202-b08-alpine-slim



RUN /build/cloud_sql_proxy -instances=idInstanceID=tcp:1433 -credential_file=/build/credentials.json & sleep 10

COPY --from=builder /app/target/helloworld-*.jar /helloworld.jar

# Run the web service on container startup.
CMD ["java","-Djava.security.egd=file:/dev/./urandom","-Dserver.port=${PORT}","-jar","/helloworld.jar"]

I my java application I have this way to connect, in local PC with proxy found wit

            @GetMapping("/pruebacuatro")
        String pruebacuatro() {

            Map<String, String> config = new HashMap<String, String>();

            config.put("type", "SQLSERVER");
            config.put("url", "127.0.0.1");
            config.put("db", "bd");
            config.put("username", "user");
            config.put("password", "pass");

            Object data = null;
            Jpo miJpo = null;
            try {
                miJpo = new Jpo(config);
                Procedure store = miJpo.procedure("seg.menu_configuraciones");
                data = store.ejecutar();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(miJpo != null) {
                    try {
                        miJpo.finalizar();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            return "Contents  json: "+(new Gson().toJson(data));

        }

I want to connect with my public IP or Private IP from my SQL Server But also I can't find information about that, Do you have any suggestion?

1
Cloud Run does not support IP addressing. You need to use a connection string. cloud.google.com/sql/docs/mysql/connect-run#javaJohn Hanley

1 Answers

2
votes

Cloud SQL proxy work in 2 modes: Unix socket and TCP

When you use it on your computer, you should use the TCP mode and you can connect to it in the localhost IP. However, with Cloud Run, it's the unix socket mode which is used and there isn't SQL server client that can use this connexion mode.

Thus, you have to use the Cloud SQL IP to connect your Cloud SQL instance to your Cloud Run.

For your local tests, continue to use Cloud SQL proxy in TCP mode

For Cloud Run, I recommend you to use the private IP of your SQL server.

  1. Expose your instance in your VPC
  2. Create a serverless VPC connector in the correct region
  3. Attach the Serverless VPC connector to your Cloud Run service
  4. Use the Cloud SQL private IP in your code to connect your DB.