云服务器内容精选

  • JDBC通过ssl方式连接doris(验证证书) 在应用层进行代码重试和负载均衡时,代码重试需要应用自己多个配置doris前端节点地址。比如发现一个连接异常退出,就自动在其他连接上进行重试。 前提条件:集群必须开启HTTPS。 下载证书请在集群详情页面下载。 在已安装mysql客户端的ecs服务器上先执行以下命令,导入服务器证书。 your_certificate_path:自定义证书存放路径。 your_truststore_name:自定义truststore名称。 your_truststore_password:自定义 truststore密码。 keytool -importcert -alias MySQLCACert -file your_certificate_path -keystore your_truststore_name -storepass your_truststore_password 运行该命令的过程中,需要手动输入yes,如下所示: 图1 运行图 执行以下代码样例。 以下java代码中your_truststore_path为truststore文件路径,your_truststore_password为上述命令设置的truststore密码。 public class Main { private static String URL = "jdbc:mysql:loadbalance://" + "[FE1_host]:[FE1_port],[FE2_host]:[FE2_port],[FE3_host]:[FE3_port]/[your_database]?" + "loadBalanceConnectionGroup=first&ha.enableJMX=true"; static Connection getNewConnection() throws SQLException, ClassNotFoundException { Class.forName("com.mysql.cj.jdbc.Driver"); System.setProperty("javax.net.ssl.trustStore","your_truststore_path"); System.setProperty("javax.net.ssl.trustStorePassword","your_truststore_password"); String user = "your_username"; String password = "your_password"; Properties props = new Properties(); props.setProperty("user", user); props.setProperty("password", password); props.setProperty("useSSL", "true"); props.setProperty("requireSSL", "true"); props.setProperty("verifyServerCertificate", "true"); props.setProperty("sslMode", "VERIFY_CA"); return DriverManager.getConnection(URL, props); } public static void main(String[] args) throws Exception { Connection c = getNewConnection(); try { System.out.println("begin print"); String query = "your sqlString"; c.setAutoCommit(false); Statement s = c.createStatement(); ResultSet resultSet = s.executeQuery(query); while(resultSet.next()) { int id = resultSet.getInt(1); System.out.println("id is: "+id); } System.out.println("end print"); Thread.sleep(Math.round(100 * Math.random())); c.close(); } catch (Exception e) { e.printStackTrace(); } } } 父主题: 通过JDBC方式连接Doris