java - 通过中间服务器连接到 Redshift

标签 java amazon-web-services ssh amazon-ec2 amazon-redshift

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

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

enter image description here

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

@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 隧道?如果没有,“跳过”的最佳方式是什么?

最佳答案

我偶然发现了一种非常简单的方法来创建通过 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

关于java - 通过中间服务器连接到 Redshift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37332096/

相关文章:

java - hibernate 按计数排序连接表并返回 pojo 列表

java - java中的Println空参数

amazon-web-services - AWS——我如何知道什么在使用服务相关角色?

regex - 用于备份服务器上传文件夹的 Bash 脚本

git - 无法克隆 git repo 并收到错误 ssh_askpass : exec(/usr/bin/ssh-askpass): No such file or directory Host key verification failed

java - Postgresql:无法使用 EclipseLink 插入路径字段

java - 如何在基类上创建调用其他方法的java方法?

ruby-on-rails - 是否可以只有一个领事服务器来为 Rails 应用程序提供 key 对?

amazon-web-services - 笔记应用程序的 DynamoDB 分区键选择

ubuntu - 为 hadoop 设置无密码 ssh