Vertica7 Native Connection Load Balance,vertica7native

來源:互聯網
上載者:User

Vertica7 Native Connection Load Balance,vertica7native

原文連結:Vertica7 Native Connection Load Balance


在Vertica7以前的版本中,Vertica是通過Linux的Virtual IP來實現串連的負載平衡的,但是在Vertica7x中,Vertica本身提供了串連的負載平衡功能,這個功能用起來也是非常方便的,下面就來看看怎麼使用這個功能。

1. 首先安裝N個Vertica7的節點,然後用資料庫管理員的身份運行下面的命令來讓Vertica使用這個功能

SELECT SET_LOAD_BALANCE_POLICY('ROUNDROBIN');

如果需要disable這個功能,可以通過下面的命令
SELECT SET_LOAD_BALANCE_POLICY('NONE');

如果需要查看當前是否enable了這個功能可以使用下面的命令查看
SELECT GET_LOAD_BALANCE_POLICY();

伺服器端的設定就這麼多,下面看看用戶端需要哪些步驟。

2. 首先必須使用vertica7的JDBC驅動,可以從Vertica的官方網站上下載。

3. 寫個測試程式來測試連接,如下
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties; public class VerticaTest {    private static String USERNAME = "<user>";    private static String PASSWORD = "<password>";    private static String URL = "jdbc:vertica://<ip>:5433/<db>";         public static void main(String[] args) throws Exception {        Class.forName("com.vertica.jdbc.Driver");        Properties props = new Properties();        props.put("user", USERNAME);        props.put("password", PASSWORD);        props.put("ConnectionLoadBalance", 1);        for (int x = 1; x <= 10; x++) {            try {                Connection conn = DriverManager.getConnection(URL, props);                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());                conn.close();            } catch (SQLException ex) {                ex.printStackTrace();            }        }    }}

這裡特別注意下面一句,這一句設定了一個串連屬性告訴Vertica的JDBC驅動使用負載平衡功能。

props.put("ConnectionLoadBalance", 1);

這裡特別說一下,如果伺服器上沒有enable負載平衡功能,那麼這個屬性也不會影響正常的功能,只不過不使用負載平衡功能罷了。

4. 測試
運行這段代碼,可以看到每次串連到的Vertica節點都會按照順序變動。

另外也可以通過下面的sql來查詢當前所有串連的情況。

select node_name, client_hostname from sessions;







相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.