通过中间服务器连接到 Redshift

connect to Redshift via an intermediate server

我正在开发一个 Java 应用程序,它连接到 Redshift 数据库以 运行 大量查询,而这些查询不会 运行 在我们的硬件上。该应用程序还会消耗我们数据中心内的各种内部非 AWS 资源(例如我们 NAS、Oracle、MySQL 等上的文件)。

遗憾的是,由于某些网络路由限制,应用程序无法直接连接到 Redshift。我可以通过 SSH 手动连接到我们的生产 Redshift 集群到属于我们的 VPC 的中间 EC2 实例 - 我希望以编程方式执行此操作。

在我的测试环境中,它没有相同的路由限制,我可以使用这样的数据源进行连接:

@Bean(name="dataSourceRedshift")
public DataSource dataSourceRedshift() throws SQLException {
    SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
    dataSource.setDriver(new com.amazon.redshift.jdbc41.Driver());
    dataSource.setUrl("jdbc:postgresql://" + redshiftHost + ":" + redshiftPort + "/" + redshiftDatabase);
    dataSource.setUsername(redshiftUser);
    dataSource.setPassword(redshiftPass);
    return dataSource;
}

在我们的生产环境中,我无法直接连接到 Redshift,有没有办法调整数据源 bean(如上)以通过 EC2 实例设置 SSH 隧道?如果没有,'hop' 的最佳途径是什么?

我偶然发现了一种非常简单的方法来创建通过 SSH 建立隧道的数据源(由 Lucas Theisen 提供:https://github.com/lucastheisen/jsch-extension):

@Bean(name="dataSourceRedshift")
public DataSource dataSourceRedshift() throws SQLException, JSchException {
    SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
    dataSource.setDriver(new com.amazon.redshift.jdbc41.Driver());
    dataSource.setUrl("jdbc:postgresql://" + redshiftHost + ":" + redshiftPort + "/" + redshiftDatabase);
    dataSource.setUsername(redshiftUser);
    dataSource.setPassword(redshiftPass);

    DefaultSessionFactory defaultSessionFactory = new DefaultSessionFactory();

    TunneledDataSourceWrapper tunneledDataSource = new TunneledDataSourceWrapper(
            new TunnelConnectionManager(
                    defaultSessionFactory,
                    redshiftTunnel ),
            dataSource );

    return tunneledDataSource;
}

redshiftTunnel 字符串所在的位置:

awoolford@localhost->awoolford@{{ ec2 instance in our VPC }}|127.0.0.1:5439:{{ redshift endpoint }}:5439