
# JDBC通过ssl方式连接Doris集群（验证证书）
在应用层进行代码重试和负载均衡时，代码重试需要应用自己多个配置Doris前端节点地址。比如发现一个连接异常退出，就自动在其他连接上进行重试。
![](https://support.huaweicloud.com/devg-cloudtable/public_sys-resources/note_3.0-zh-cn.png)
前提条件：集群必须开启HTTPS。
下载证书请在集群详情页面下载。
1. 在已安装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
   ```
   
2. 运行该命令的过程中，需要手动输入yes，如下所示：
   图1运行图   
   ![](https://support.huaweicloud.com/devg-cloudtable/zh-cn_image_0000002376557097.png "点击放大") 
3. 执行以下代码样例。
   以下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");
         // 认证用的密码直接写到代码中有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
         // 本示例以密码保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量
         String storePassword = System.getenv("STORE_PASSWORD");
         String userPassword = System.getenv("USER_PASSWORD");
         System.setProperty("javax.net.ssl.trustStore","your_truststore_path");
         System.setProperty("javax.net.ssl.trustStorePassword",storePassword);
         String user = "your_username";
         Properties props = new Properties();
         props.setProperty("user", user);
         props.setProperty("password", userPassword);
         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();
         }
     }
   }
   ```
   
 
