【问题标题】:How to connect GCP Cloud Run Java Spring to Cloud SQL SQL Server如何将 GCP Cloud Run Java Spring 连接到 Cloud SQL SQL Server
【发布时间】:2020-08-31 18:04:10
【问题描述】:

我有一个小问题,我想将 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 但是当我尝试连接时我不能。

这是我的 docker 文件

# 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-server spring-boot google-cloud-platform google-cloud-sql google-cloud-run


【解决方案1】:

Cloud SQL 代理在 2 modes 中工作:Unix 套接字和 TCP

在电脑上使用时,应使用 TCP 模式,并且可以在 localhost IP 中连接。但是,对于 Cloud Run,它使用的是 unix socket 模式,并且没有 SQL server 客户端可以使用这种连接模式。

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

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

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

  1. Expose your instance in your VPC
  2. Create a serverless VPC connector 在正确的地区
  3. 附上Serverless VPC connector to your Cloud Run service
  4. 在您的代码中使用 Cloud SQL 私有 IP 连接您的数据库。

【讨论】:

  • 我建议在本地通过授权 IP 使用 Cloud SQL 代理,即使它是个人代理。
  • 你完全正确。当我回答时,我没有想到我的想法......我更新我的答案
  • 有效!!!非常感谢你,像你一样,我可以建立联系
猜你喜欢
  • 2020-07-01
  • 2020-07-01
  • 2019-10-28
  • 2021-01-05
  • 1970-01-01
  • 2019-09-05
  • 2018-12-15
  • 2020-08-18
  • 2020-06-15
相关资源
最近更新 更多