码迷,mamicode.com
首页 > 其他好文 > 详细

关于vertica的Connection Failover

时间:2016-08-23 19:04:08      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

最近在生产中发现vertica有个别节点老师宕机,实际业务中有Python通过odbc连接vertica,还有jdbc连接vertica。加入你连接的那个节点正好是down的节点或者是standby节点,那么就会连接失败,程序就会报错,影响正常业务。其实vertica官方文档已经给出了解决方法。

ODBC Connection Failover

官方给出的代码如下:

[VMartBadNode]
Description=VMart Vertica Database
Driver=/opt/vertica/lib64/libverticaodbc.so
Database=VMart
Servername=badnode.example.com
BackupServerNode=node02.example.com,node03.example.com

其中上面的BackupServerNode就是备用的IP

JDBC Connection Failover

代码:

import java.sql.*;
import java.util.Properties;

public class ConnectionFailoverExample {
    public static void main(String[] args) {
        // Assume using JDBC 4.0 driver on JVM 6+. No driver loading needed.
        Properties myProp = new Properties();
        myProp.put("user", "dbadmin");
        myProp.put("password", "vertica");
        // Set two backup hosts to be used if connecting to the first host
        // fails. All of these hosts will be tried in order until the connection
        // succeeds or all of the connections fail.
        myProp.put("BackupServerNode", "VerticaHost02,VerticaHost03");
        Connection conn;
        try {
            // The connection string is set to try to connect to a known
            // bnad host (in this case, a host that never existed). 
            conn = DriverManager.getConnection(
                    "jdbc:vertica://BadVerticaHost:5433/vmart", myProp);
            System.out.println("Connected!");
            // Query system to table to see what node we are connected to. 
            // Assume a single row in response set.            
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(
                    "SELECT node_name FROM v_monitor.current_session;");      
            rs.next();
            System.out.println("Connected to node " + rs.getString(1).trim());
            // Done with connection.
            conn.close();
        } catch (SQLException e) {
            // Catch-all for other exceptions
            e.printStackTrace();
        }
    }
}

另外也可以通过jdbc连接串来实现:

jdbc:vertica://192.168.111.25:5433/DBname?ConnectionLoadBalance=1&BackupServerNode=192.168.111.12:5433,192.168.111.13:5433

其中ConnectionLoadBalance是vertica的负载均衡,如果在库中没有开启该功能的话,此时是没有作用的,不影响。
开启ConnectionLoadBalance的话,需要管理员账户

SELECT SET_LOAD_BALANCE_POLICY(‘ROUNDROBIN‘);

取消该功能:

SELECT SET_LOAD_BALANCE_POLICY(‘NONE‘);

查询是否开启:

SELECT GET_LOAD_BALANCE_POLICY();

通过这次设置,加了几个备用连接节点,确保业务正常。

关于vertica的Connection Failover

标签:

原文地址:http://blog.csdn.net/windyqcf/article/details/52292947

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!