更新时间:2025-01-09 GMT+08:00
JDBC通过ssl方式连接doris(无需验证证书)
在应用层进行代码重试和负载均衡时,代码重试需要应用自己多个配置doris前端节点地址。比如发现一个连接异常退出,就自动在其他连接上进行重试。
![](https://support.huaweicloud.com/devg-cloudtable/public_sys-resources/note_3.0-zh-cn.png)
前提条件:集群必须开启HTTPS。
下载证书请在集群详情页面下载。
样例代码:
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"); // 认证用的密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以密码保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量 String password = System.getenv("USER_PASSWORD"); String user = "your_username"; Properties props = new Properties(); props.setProperty("user", user); props.setProperty("password", password); props.setProperty("useSSL", "true"); props.setProperty("requireSSL", "true"); 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