Cloud SQL 连接来自 App Engine

huangapple go评论69阅读模式
英文:

Cloud SQL connect from Appengine

问题

我想连接到GCP中的Cloud SQL MySQL实例,从我的Java AppEngine项目中。我不明白配置部分的"INSTANCE_UNIX_SOCKET"是什么意思。我希望从我的Java AppEngine项目连接到这个实例。我在我的构建路径中有"mysqlsocketfactoryconnector"的JAR包。

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;

public class ConnectorConnectionPoolFactory extends ConnectionPoolFactory {

  // 注意:将凭据保存在环境变量中很方便,但不安全,建议考虑更安全的解决方案,如Cloud Secret Manager(https://cloud.google.com/secret-manager)来保护凭据的安全。
  private static final String INSTANCE_CONNECTION_NAME =
      System.getenv("INSTANCE_CONNECTION_NAME");
  private static final String INSTANCE_UNIX_SOCKET = System.getenv("INSTANCE_UNIX_SOCKET");
  private static final String DB_USER = System.getenv("DB_USER");
  private static final String DB_PASS = System.getenv("DB_PASS");
  private static final String DB_NAME = System.getenv("DB_NAME");

  public static DataSource createConnectionPool() {
    // 配置对象指定了连接池的行为。
    HikariConfig config = new HikariConfig();

    // 以下URL等同于设置以下配置选项:
    // jdbc:mysql:///<DB_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&
    // socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<DB_USER>&password=<DB_PASS>
    // 有关构建Cloud SQL JDBC Socket Factory的JDBC URL的更多信息,请参阅以下链接
    // https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#creating-the-jdbc-url

    // 配置要连接的实例和数据库用户。
    config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
    config.setUsername(DB_USER); // 例如:"root","mysql"
    config.setPassword(DB_PASS); // 例如:"my-password"

    config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
    config.addDataSourceProperty("cloudSqlInstance", INSTANCE_CONNECTION_NAME);

    // Java不本地支持Unix套接字,因此需要使用Cloud SQL Java Connector进行连接。当设置INSTANCE_UNIX_SOCKET时,连接器将调用一个外部包,以启用Unix套接字连接。
    // 注意:对于Java用户,Cloud SQL Java Connector可以提供经过身份验证的连接,通常优于使用Unix套接字与Cloud SQL代理。有关详细信息,请参阅https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory。
    if (INSTANCE_UNIX_SOCKET != null) {
      config.addDataSourceProperty("unixSocketPath", INSTANCE_UNIX_SOCKET);
    }

    // ... 在此处指定其他连接属性。
    // ...

    // 使用配置对象初始化连接池。
    return new HikariDataSource(config);
  }
}
英文:

I want to coonnect to cloud sql mysql instance in gcp from my java appengine project.
I am not able to under stand what this is INSTANCE_UNIX_SOCKET in the configuration section.
I want to connect to this instance from my java appengine project. I have mysqlsocketfactoryconnector jar in my build path

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;

public class ConnectorConnectionPoolFactory extends ConnectionPoolFactory {

  // Note: Saving credentials in environment variables is convenient, but not
  // secure - consider a more secure solution such as
  // Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
  // keep secrets safe.
  private static final String INSTANCE_CONNECTION_NAME =
      System.getenv(&quot;INSTANCE_CONNECTION_NAME&quot;);
  private static final String INSTANCE_UNIX_SOCKET = System.getenv(&quot;INSTANCE_UNIX_SOCKET&quot;);
  private static final String DB_USER = System.getenv(&quot;DB_USER&quot;);
  private static final String DB_PASS = System.getenv(&quot;DB_PASS&quot;);
  private static final String DB_NAME = System.getenv(&quot;DB_NAME&quot;);

  public static DataSource createConnectionPool() {
    // The configuration object specifies behaviors for the connection pool.
    HikariConfig config = new HikariConfig();

    // The following URL is equivalent to setting the config options below:
    // jdbc:mysql:///&lt;DB_NAME&gt;?cloudSqlInstance=&lt;INSTANCE_CONNECTION_NAME&gt;&amp;
    // socketFactory=com.google.cloud.sql.mysql.SocketFactory&amp;user=&lt;DB_USER&gt;&amp;password=&lt;DB_PASS&gt;
    // See the link below for more info on building a JDBC URL for the Cloud SQL JDBC Socket Factory
    // https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#creating-the-jdbc-url

    // Configure which instance and what database user to connect with.
    config.setJdbcUrl(String.format(&quot;jdbc:mysql:///%s&quot;, DB_NAME));
    config.setUsername(DB_USER); // e.g. &quot;root&quot;, &quot;mysql&quot;
    config.setPassword(DB_PASS); // e.g. &quot;my-password&quot;

    config.addDataSourceProperty(&quot;socketFactory&quot;, &quot;com.google.cloud.sql.mysql.SocketFactory&quot;);
    config.addDataSourceProperty(&quot;cloudSqlInstance&quot;, INSTANCE_CONNECTION_NAME);

    // Unix sockets are not natively supported in Java, so it is necessary to use the Cloud SQL
    // Java Connector to connect. When setting INSTANCE_UNIX_SOCKET, the connector will 
    // call an external package that will enable Unix socket connections.
    // Note: For Java users, the Cloud SQL Java Connector can provide authenticated connections
    // which is usually preferable to using the Cloud SQL Proxy with Unix sockets.
    // See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details.
    if (INSTANCE_UNIX_SOCKET != null) {
      config.addDataSourceProperty(&quot;unixSocketPath&quot;, INSTANCE_UNIX_SOCKET);
    }


    // ... Specify additional connection properties here.
    // ...

    // Initialize the connection pool using the configuration object.
    return new HikariDataSource(config);
  }
}

答案1

得分: 1

您可以在这里忽略INSTANCE_UNIX_SOCKET - 我们已将其添加到示例代码中,以供可能想要连接到Unix套接字的人使用。在几乎所有情况下,您将希望直接使用套接字工厂并绕过Unix套接字。所以只需这样做:

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;

public class ConnectorConnectionPoolFactory extends ConnectionPoolFactory {
  private static final String INSTANCE_CONNECTION_NAME =
      System.getenv("INSTANCE_CONNECTION_NAME");
  private static final String DB_USER = System.getenv("DB_USER");
  private static final String DB_PASS = System.getenv("DB_PASS");
  private static final String DB_NAME = System.getenv("DB_NAME");

  public static DataSource createConnectionPool() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
    config.setUsername(DB_USER); // 例如:"root"、"mysql"
    config.setPassword(DB_PASS); // 例如:"my-password"

    config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
    config.addDataSourceProperty("cloudSqlInstance", INSTANCE_CONNECTION_NAME);
    return new HikariDataSource(config);
  }
}
英文:

You can ignore INSTANCE_UNIX_SOCKET here -- we've added it to the sample code for people who might want to connect to the Unix socket. In almost all cases, you'll want to use the socket factory directly and bypass the Unix socket. So just do this:

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import javax.sql.DataSource;

public class ConnectorConnectionPoolFactory extends ConnectionPoolFactory {
  private static final String INSTANCE_CONNECTION_NAME =
      System.getenv(&quot;INSTANCE_CONNECTION_NAME&quot;);
  private static final String INSTANCE_UNIX_SOCKET = System.getenv(&quot;INSTANCE_UNIX_SOCKET&quot;);
  private static final String DB_USER = System.getenv(&quot;DB_USER&quot;);
  private static final String DB_PASS = System.getenv(&quot;DB_PASS&quot;);
  private static final String DB_NAME = System.getenv(&quot;DB_NAME&quot;);

  public static DataSource createConnectionPool() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl(String.format(&quot;jdbc:mysql:///%s&quot;, DB_NAME));
    config.setUsername(DB_USER); // e.g. &quot;root&quot;, &quot;mysql&quot;
    config.setPassword(DB_PASS); // e.g. &quot;my-password&quot;

    config.addDataSourceProperty(&quot;socketFactory&quot;, &quot;com.google.cloud.sql.mysql.SocketFactory&quot;);
    config.addDataSourceProperty(&quot;cloudSqlInstance&quot;, INSTANCE_CONNECTION_NAME);
    return new HikariDataSource(config);
  }
}

huangapple
  • 本文由 发表于 2023年3月3日 18:56:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定