tidb 作爲mysql從庫,使用Syncer同步,Syncer監控重啓

mysql 作爲tidb 從庫,使用Syncer同步binlog  經常發生Syncer掛掉的現象,故寫了一個程序進行監聽Syncer日誌,如果2分鐘內無日誌內容更新,則重啓Syncer,以達到同步穩定性的保障

 

核心代碼如下:

/**
 * 實時輸出日誌信息
 * @param logFile 日誌文件
 * @throws IOException
 */
public void realtimeShowLog(File logFile,Date date) throws IOException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    final String log=simpleDateFormat.format(date)+"*****************************";

    //指定文件可讀可寫
    final RandomAccessFile randomFile = new RandomAccessFile(logFile,"rw");
    //啓動一個線程每10秒鐘讀取新增的日誌信息
    ScheduledExecutorService exec =
            Executors.newScheduledThreadPool(1);
    exec.scheduleWithFixedDelay(new Runnable(){
        public void run() {
            System.out.println("-----------------定時開始"+log);
            boolean isRead=true;
            try {
                //獲得變化部分的
                randomFile.seek(lastTimeFileSize);
                String tmp = "";
                while( (tmp = randomFile.readLine())!= null) {
                    isRead=false;
                    System.out.println(new String(tmp.getBytes("ISO8859-1")));
                }
                lastTimeFileSize = randomFile.length();
            } catch (Exception e) {
                System.out.println("RuntimeException-----------------讀取文件異常"+e.getMessage());
                throw new RuntimeException(e);
            }
            if(isRead){
                System.out.println("-----------------開始監控"+log);

                String lastTime=(String)WoflUtils.getRedisUtil().get("syncerLastTime");

                String nowTime="";
                Date nowDate=new Date();
                Date date = null;
                try {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    nowTime=simpleDateFormat.format(nowDate);
                    if(lastTime==null||lastTime.trim().length()==0){
                        System.out.println("-----------第一次執行"+log);
                        WoflUtils.getRedisUtil().set("syncerLastTime", nowTime, 3000);
                    }else{
                        date = simpleDateFormat.parse(lastTime);
                       Long diff= TimeUtils.getDatePoor(TimeUtils.MIN,nowDate,date);
                        System.out.println("相差時間-----------"+diff+"分鐘"+log);
                       if(diff>3){
                           System.out.println(lastTime+"斷開-----------重啓同步"+log);
                           WoflUtils.getRedisUtil().set("syncerLastTime", nowTime, 3000);
                           try {
                               SyncerRestat sr= new SyncerRestat();
                               List<String> cmds= new ArrayList<>();
                               cmds.add("cd /data1/tidb-enterprise-tools-latest-linux-amd64");
                               cmds.add(" nohup   ./bin/syncer -config config.toml >>/data1/tidb-enterprise-tools-latest-linux-amd64/syncer.log 2>&1 & ");
                               List<String>rs =sr.executeNewFlow(cmds);
                               if(rs!=null){
                                    for(String line:rs ){
                                        System.out.println("執行命令:--------  "+line);
                                    }
                                }
                               WoflUtils.getRedisUtil().set("syncerLastTime", nowTime, 3000);
                               sr.restart();
                               System.out.println("關閉定時1:--------  ");
                               exec.shutdown();
                               System.out.println("關閉定時2:--------  ");
                           } catch (Exception e) {
                               e.printStackTrace();
                               System.out.println(lastTime+"重啓同步失敗-----------"+e.getMessage());
                           }


                       }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("-----------發生異常"+e.getMessage());
                }

            }

        }
    }, 0, 30, TimeUnit.SECONDS);
}

    public void restart()  {
        LogView view = new LogView();
       final File tmpLogFile = new File("/data1/tidb-enterprise-tools-latest-linux-amd64/syncer.log");
        //final File tmpLogFile = new File("d:/localhost.2019-10-17.log");
        try {
            view.realtimeShowLog(tmpLogFile,new Date());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("系統找不到指定的路徑--"+e.getMessage());
        }
    }


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章