免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1998 | 回复: 1
打印 上一主题 下一主题

Soalris下,能为MYSQL CLUSTER中的两个SQL 节点设置virtual IP吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-04 11:27 |只看该作者 |倒序浏览
Soalris下,能为MYSQL CLUSTER中的两个SQL 节点设置virtual IP吗?就像linux中,LVS+Keepalived或LVS+Heartbeat那种组合一样。

比如,mysql cluster,有两个SQL节点对外提供服务,我想做一个VIP指向这两个节点,当某个节点down掉的时候,VIP就只指向这一个节点,免得client端需要处理failover这样的问题,JDBC还要处理读取多个SQL 节点 IP。MYSQL的JDBC支持多IP,但是别的DB的JDBC不见得支持呀!!!



请大家给出solaris下virtual IP的解决方案。万分感谢!!!

论坛徽章:
0
2 [报告]
发表于 2010-03-04 15:57 |只看该作者
决定采用绑定IP的办法来做,代码如下:


import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Iterator;
import java.util.Vector;


public class VIPTestMain {
        public static void main(String args[])throws Exception{               
                addVirtualIP("10.80.1.218");
                Thread.sleep(5000);
                delVirtualIP("10.80.1.218");
        }

        /**
         * TODO: Only linux and SunOS are supported.
         *
         * @param vip
         * @throws Exception
         */
        public static void addVirtualIP(String vip)throws Exception{
                String ethernetCardDeviceName = getEthernetCardDeviceName();
                System.out.println("ethernetCardDeviceName="+ethernetCardDeviceName);
                //
                if(ethernetCardDeviceName==null){
                        return;
                }
                               
                String os_name = System.getProperty("os.name").toLowerCase();
                if(os_name.indexOf("sun")>=0 || os_name.indexOf("solaris")>=0){
                        String command1 = "ifconfig "+ethernetCardDeviceName.trim()+":1 plumb";
                        String command2 = "ifconfig "+ethernetCardDeviceName.trim()+":1 "+vip+" up";
                        System.out.println("command1="+command1);
                        System.out.println("command2="+command2);
                        Runtime.getRuntime().exec(command1);
                        Runtime.getRuntime().exec(command2);                       
                }else if(os_name.indexOf("linux")>=0){
                        String command = "ip addr add "+vip+"/32 dev "+ethernetCardDeviceName;
                        System.out.println("command="+command);
                        Runtime.getRuntime().exec(command);                       
                }
        }
        /**
         * TODO: Only linux and SunOS are supported.
         * @param vip
         * @throws Exception
         */
        public static void delVirtualIP(String vip)throws Exception{
                String ethernetCardDeviceName = getEthernetCardDeviceName();
                System.out.println("ethernetCardDeviceName="+ethernetCardDeviceName);
                //
                if(ethernetCardDeviceName==null){
                        return;
                }
                               
                String os_name = System.getProperty("os.name").toLowerCase();
                if(os_name.indexOf("sun")>=0 || os_name.indexOf("solaris")>=0){
                        String command = "ifconfig "+ethernetCardDeviceName.trim()+":1 unplumb";
                        System.out.println("command="+command);
                        Runtime.getRuntime().exec(command);
                }else if(os_name.indexOf("linux")>=0){
                        String command = "ip addr del "+vip+"/32 dev "+ethernetCardDeviceName;
                        System.out.println("command="+command);
                        Runtime.getRuntime().exec(command);                               
                }                       
        }
       
        /**
         * TODO: Only linux and SunOS are supported.
         * @return
         * @throws Exception
         */
        public static String getEthernetCardDeviceName()throws Exception{
                String os_name = System.getProperty("os.name").toLowerCase();
                String deviceName = null;
                //
                if(os_name.indexOf("sun")>=0 || os_name.indexOf("solaris")>=0){
                        String command = "ifconfig -a";
                        System.out.println("command="+command);
                       
                        Vector v = executeCommand(command);
                        //                       
                        Iterator it = v.iterator();
                        while(it.hasNext()){
                                String item = it.next().toString();
                                if(item!=null){
                                        item = item.trim().toLowerCase();
                                        if(item.indexOf("loopback")<0 && item.indexOf("broadcast")>=0){
                                                System.out.println("item="+item);
                                                if(item.indexOf(":")>=0){
                                                        deviceName = item.substring(0,item.indexOf(":")).trim();
                                                }
                                                break;
                                        }
                                }                               
                        }                       
                }else if(os_name.indexOf("linux")>=0){
                        String command = "ip addr show";
                        Vector v = executeCommand(command);
                        //               
                        /*String firstLine = v.get(0).toString().trim();
                        deviceName = firstLine.substring(0,firstLine.indexOf(" "));*/                       
                        Iterator it = v.iterator();
                        while(it.hasNext()){
                                String item = it.next().toString();
                                if(item!=null){
                                        item = item.trim().toLowerCase();
                                        if(item.indexOf("loopback")<0 && item.indexOf("broadcast")>=0){
                                                System.out.println("item="+item);
                                                String ss[] = item.split(":");
                                               
                                                if(ss.length>=3){
                                                        deviceName = ss[1].trim();
                                                }
                                                break;
                                        }
                                }                               
                        }
                }
               
                return deviceName;
        }
       
        public static Vector executeCommand(String command)throws Exception{
                Process pro =Runtime.getRuntime().exec(command);
                Vector v = new Vector();
                //
                InputStream in = pro.getInputStream();
                InputStreamReader isr = new InputStreamReader(pro.getInputStream());
                LineNumberReader reader = new LineNumberReader (isr);
                String line = null;
                while( (line=reader.readLine())!=null){
                        if(line!=null){
                                System.out.println("line="+line);
                                v.add(line);
                        }                         
                }
                System.out.println("=======================================");
                reader.close();
                isr.close();
                in.close();
               
                return v;
        }       
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP