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()); } }