如何将 GCP 云 运行 Java Spring 连接到云 SQL SQL 服务器
How to connect GCP Cloud Run Java Spring to Cloud SQL SQL Server
我有一个小问题,我想部署一个 Java Spring 应用程序到云 运行 并从云 SQL SQL 服务器获取连接,我知道 MySQL 和 Postgresql (https://cloud.google.com/sql/docs/mysql/connect-run?hl=es-419) 可以通过 unix 套接字连接,但是 SQL 服务器没有驱动程序 jet。
另一种方式是为 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 服务器连接到我的 public IP 或私有 IP 但我也找不到相关信息,你有什么建议吗?
Cloud SQL 代理在 2 modes 中工作:Unix 套接字和 TCP
在你的电脑上使用时,应该使用TCP模式,你可以在本地主机IP中连接到它。但是,对于 Cloud 运行,它使用的是 unix 套接字模式,没有 SQL 服务器客户端可以使用这种连接模式。
因此,您必须使用 Cloud SQL IP 将您的 Cloud SQL 实例连接到您的 Cloud 运行。
对于您的本地测试,继续在 TCP 模式下使用云 SQL 代理
对于云 运行,我建议您使用 SQL 服务器的私有 IP。
- Expose your instance in your VPC
- Create a serverless VPC connector 在正确的地区
- 附上 Serverless VPC connector to your Cloud Run service
- 在您的代码中使用云 SQL 私有 IP 连接您的数据库。
我有一个小问题,我想部署一个 Java Spring 应用程序到云 运行 并从云 SQL SQL 服务器获取连接,我知道 MySQL 和 Postgresql (https://cloud.google.com/sql/docs/mysql/connect-run?hl=es-419) 可以通过 unix 套接字连接,但是 SQL 服务器没有驱动程序 jet。
另一种方式是为 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 服务器连接到我的 public IP 或私有 IP 但我也找不到相关信息,你有什么建议吗?
Cloud SQL 代理在 2 modes 中工作:Unix 套接字和 TCP
在你的电脑上使用时,应该使用TCP模式,你可以在本地主机IP中连接到它。但是,对于 Cloud 运行,它使用的是 unix 套接字模式,没有 SQL 服务器客户端可以使用这种连接模式。
因此,您必须使用 Cloud SQL IP 将您的 Cloud SQL 实例连接到您的 Cloud 运行。
对于您的本地测试,继续在 TCP 模式下使用云 SQL 代理
对于云 运行,我建议您使用 SQL 服务器的私有 IP。
- Expose your instance in your VPC
- Create a serverless VPC connector 在正确的地区
- 附上 Serverless VPC connector to your Cloud Run service
- 在您的代码中使用云 SQL 私有 IP 连接您的数据库。