首页 > 解决方案 > 如何将 GCP Cloud Run Java Spring 连接到 Cloud SQL SQL Server

问题描述

我有一个小问题,我想将 Java Spring App 部署到 Cloud RUN 并从 CLOUD SQL SQL SERVER 获取连接,我知道可以通过 unix socket 连接 MySQL 和 Postgresql ( https://cloud.google.com/sql /docs/mysql/connect-run?hl=es-419)但对于 SQL Server 没有驱动程序喷射。

另一种方法是连接 Proxy Like ( https://medium.com/@petomalina/connecting-to-cloud-sql-from-cloud-run-dcff2e20152a ) 我试过但我不能,即使在部署脚本时,它告诉我正在为我的实例 id 监听 127.0.0.1 但是当我尝试连接时我不能。

这是我的码头文件

# 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"]

我的 java 应用程序我有这种连接方式,在本地 PC 中找到了代理

            @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));

        }

我想从我的 SQL Server 连接我的公共 IP 或私有 IP 但我也找不到相关信息,你有什么建议吗?

标签: sql-serverspring-bootgoogle-cloud-platformgoogle-cloud-sqlgoogle-cloud-run

解决方案


Cloud SQL 代理以2 种模式工作:Unix 套接字和 TCP

在电脑上使用时,应使用 TCP 模式,并且可以在 localhost IP 中连接。但是,对于 Cloud Run,使用的是 unix 套接字模式,并且没有可以使用此连接模式的 SQL 服务器客户端。

因此,您必须使用 Cloud SQL IP 将 Cloud SQL 实例连接到 Cloud Run。

对于您的本地测试,请继续在 TCP 模式下使用 Cloud SQL 代理

对于 Cloud Run,我建议您使用 SQL 服务器的私有 IP。

  1. 在您的 VPC 中公开您的实例
  2. 在正确的区域中创建无服务器 VPC 连接器
  3. 无服务器 VPC 连接器附加到您的 Cloud Run 服务
  4. 在您的代码中使用 Cloud SQL 私有 IP 连接您的数据库。

推荐阅读