表格存储服务 CLOUDTABLE-ClickHouse安全通道加密:JDBC连接

时间:2024-09-26 09:00:53

JDBC连接

public void run()
        throws InterruptedException {
    final ClickHouseProperties clickHouseProperties = new ClickHouseProperties();
    // 认证用的密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
    // 本示例以密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量CK_PASSWORD
    String password = System.getenv("CK_PASSWORD");
    clickHouseProperties.setSslRootCertificate("/etc/ssl/certificate.crt");
    clickHouseProperties.setSsl(true);
    clickHouseProperties.setSslMode("strict");
    clickHouseProperties.setUser("test");
    clickHouseProperties.setPassword(password);
    clickHouseProperties.setSocketTimeout(2 * 3600 * 1000);
    final BalancedClickhouseDataSource dataSource = new BalancedClickhouseDataSource("xxxx.mycloudtable.com:8443/default?ssl=true", clickHouseProperties);
    try {
        final ClickHouseConnection conn = dataSource.getConnection();
        conn.createStatement().executeQuery("select now()");
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

clickHouseProperties.setSslRootCertificate("/etc/ssl/certificate.crt");中的/etc/ssl/certificate.crt指存放证书路径。

support.huaweicloud.com/usermanual-cloudtable/cloudtable_01_0381.html