![](/img/trans.png)
[英]Standalone Java application in Google Cloud App Engine connecting to Google Cloud SQL database
[英]Connecting to Google Cloud SQL (PostgreSQL) locally and through App Engine Flex
本地:我的本地環境是Mac OS。 我正在嘗試使用Java 8連接到我的Cloud SQL(PostgreSQL)數據庫。我已將IP列入白名單,並且能夠使用pgadmin連接到數據庫。 我的服務帳戶還是托管數據庫和應用程序引擎環境的項目的項目所有者。
這是我用來本地連接的教程:
https://cloud.google.com/sql/docs/postgres/connect-external-app#java
和我的實際代碼:
private static void jdbcUrl = String.format(
"jdbc:postgresql://google/%s?socketFactory=com.google.cloud.sql.postgres.SocketFactory"
+ "&cloudSqlInstance=%s",
Configuration.getDatabaseName(),
Configuration.getInstanceConnectionName());
public static String getDbConnection() throws Exception {
LOGGER.info(jdbcUrl);
Connection connection = DriverManager.getConnection(jdbcUrl, Configuration.getUsername(), Configuration.getPassword());
...}
這是在我的代碼中生成的url調用(出於明顯的目的,我已經更改了原始代碼):
jdbc:postgresql://google/users?socketFactory=com.google.cloud.sql.postgres.SocketFactory&cloudSqlInstance=my-project:europe-west1:my-db
由於出現此錯誤,我無法連接此呼叫:
Nov 29, 2018 7:13:11 PM com.google.cloud.sql.postgres.SocketFactory createSocket
INFO: Connecting to Cloud SQL instance [my-project:europe-west1:my-db] via ssl socket.
Nov 29, 2018 7:13:11 PM com.google.cloud.sql.core.SslSocketFactory getInstance
INFO: First Cloud SQL connection, generating RSA key pair.
Nov 29, 2018 7:13:12 PM com.google.cloud.sql.core.SslSocketFactory fetchInstanceSslInfo
INFO: Obtaining ephemeral certificate for Cloud SQL instance [my-project:europe-west1:my-db].
Nov 29, 2018 7:13:13 PM com.google.cloud.sql.core.SslSocketFactory createAndConfigureSocket
INFO: Connecting to Cloud SQL instance [my-project:europe-west1:my-db] on IP [30.100.40.150].
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.ri.ccm.utils.DbConnection.getDbConnection(DbConnection.java:23)
at com.ri.ccm.routes.DummyRoute.serviceCategoryDetail(DummyRoute.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:76)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:148)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:191)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:200)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:103)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:493)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:415)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:104)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:277)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:272)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:268)
at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
at org.glassfish.jersey.internal.Errors.process(Errors.java:268)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:833)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1634)
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1621)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:541)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1593)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1239)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:481)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1562)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1141)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:213)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:118)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:564)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:320)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at com.google.cloud.sql.core.SslSocketFactory.createAndConfigureSocket(SslSocketFactory.java:210)
at com.google.cloud.sql.core.SslSocketFactory.create(SslSocketFactory.java:160)
at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:96)
at org.postgresql.core.PGStream.<init>(PGStream.java:62)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
... 64 more
但是,我可以通過此調用連接並獲取數據:
jdbc:postgresql://30.100.40.150/users
唯一的問題是,我必須將每個ip(0.0.0.0/24)列入白名單,才能使該網址在App Engine Flex(包括我的)中正常工作! 現在,出於安全原因,我顯然不想這樣做。
通過App Engine Flex:
jdbc:postgresql://30.100.40.150/users
我可以在App Engine Flex中連接此呼叫^,但再次必須將每個IP列入白名單才能使其正常工作。 該文檔指出,我無需將App Engine Flex中的IP列入白名單即可使用(它應該只使用服務帳戶)。 我猜原來的電話,在文檔中做到了。 但是,這不起作用。 所以我停滯不前。
拜托,因為上帝的愛,有人可以幫助我。 連接數據庫並不難!
我要做的就是通過本地環境和App Engine Flex環境連接到遠程Cloud SQL PostgreSQL數據庫,而不必將每個IP地址都列入白名單(並以“正確”的方式進行)。 我正在使用maven配置文件來區分環境。
我也嘗試過設置Cloud SQL代理供本地使用,但它不起作用,並且出現此錯誤:
couldn't connect to "my-project:europe-west1:my-db": read tcp 10.75.120.100:49663->30.100.40.150:3307: read: connection reset by peer
我的想法已經用完了,這可能是防火牆端口問題嗎? 請幫忙 :(
使用Cloud SQL代理服務器或套接字工廠時,需要出站tcp端口3307訪問,但使用本機PostgreSQL JDBC驅動程序時則不需要。 確保Mac或本地網絡上沒有防火牆,阻止出站端口3307。
您是否在本地環境中安裝了Google Cloud SDK,並且已通過身份驗證? 您可以通過輸入以下命令進行測試:
gcloud projects list
JDBC套接字工廠提供了客戶端代理軟件的替代方法,並且需要像Cloud SQL Proxy一樣啟用Cloud SQL API。 套接字工廠提供與代理相同的加密級別,並使用Cloud SDK憑據進行身份驗證,因此必須安裝和認證Cloud SDK。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.