业务场景: 新上线一个线上运行版本,测试运行一段时间验证是否可用。测试运行版本要保证与之前服务的数据一致,故做了个日志转发,把发送到现运行版本的请求全部转发给测试版本一份。程序完全基于jvm自有jar包实现,单java文件就可执行,轻量,如果想要做其他的日志处理,比如保存mysql,日志备份等,只需要实现LogProcessing接口即可,方便可扩展。
我这里是直接获取日志之后做了个转发。单个转发速度太慢,做了个多线程版本的转发。
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
/**
* 日志监听类,可以做请求转发,日志入库,日志备份等
* 未依赖任何第三方jar包,基于jvm就可直接执行
* 执行步骤:
* javac RequestListen.java
* nohup java -cp . RequestListen info.log http://127.0.0.1:8080/ 0 >> listen.log 2>&1 &
* @author ZhangShaobo
* @date 2019/7/16 17:07
* @param
* @return
*/
public class RequestListen {
// 指定上次文件读取位置
private long lastTimeFileSize = 0;
public void realtimesShowLog(File logFile, LogProcessing logProcessing) throws FileNotFoundException {
final RandomAccessFile randomAccessFile = new RandomAccessFile(logFile,"r");
new Thread(() -> {
for(;;){
try {
randomAccessFile.seek(lastTimeFileSize);
String tmp = "";
while( (tmp = randomAccessFile.readLine())!= null) {
String line = new String(tmp.getBytes("ISO8859-1"));
logProcessing.process(line);
}
lastTimeFileSize = randomAccessFile.length();
System.out.println("当前执行位置:"+ lastTimeFileSize);
Thread.sleep(10000);
} catch (IOException e) {
e.printStackTrace();
continue;
} catch (InterruptedException e) {
e.printStackTrace();
continue;
}
}
}).start();
}
/**
* 主函数
* @param args 0:监听的文件地址。1: 转发的请求地址。 2.读取的文件位置
*/
public static void main(String[] args) {
RequestListen listen = new RequestListen();
if (args.length < 3) {
System.err.println("param can't be null ! args[0]: listen file path, args[1] request url, args[2] lastTimeFileSize!");
return ;
}
File file = new File(args[0]);
String urlstr = args[1];
listen.lastTimeFileSize = Long.parseLong(args[2]);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100, 200, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1000), new ThreadPoolExecutor.CallerRunsPolicy());
try {
listen.realtimesShowLog(file, new LogProcessingForward(urlstr, threadPoolExecutor));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
/**
* 日志程序处理接口
*/
interface LogProcessing {
void process(String line);
}
/**
* 日志处理转发实现类
*/
class LogProcessingForward implements LogProcessing {
private String urlstr;
private ThreadPoolExecutor threadPoolExecutor;
public LogProcessingForward(String urlstr, ThreadPoolExecutor threadPoolExecutor) {
this.urlstr = urlstr;
this.threadPoolExecutor = threadPoolExecutor;
}
@Override
public void process(String line) {
threadPoolExecutor.submit(new ForwardHandle(line, urlstr));
}
}
/**
* 日志转发线程
*/
class ForwardHandle implements Runnable {
private String line;
private String urlstr;
public ForwardHandle(String line, String urlstr) {
this.line = line;
this.urlstr = urlstr;
}
@Override
public void run() {
if (!line.startsWith("20") || line.length() < 19) {
return ;
}
String dateStr = line.substring(0, 19);
String[] split1 = line.split(" - ");
String[] split = null;
if (line.contains("request:")) {
split = split1[split1.length-1].split("request\\:");
} else if (line.contains("request :")) {
split = split1[split1.length-1].split("request \\:");
}
if (split == null || split.length < 2) {
return ;
}
String key = split[0].trim();
// if (!key.equals("details") && !key.equals("syncDetails") && !key.equals("rollback")) {
// return ;
// }
String jsonObject = split[1].trim();
HttpURLConnection con = null;
OutputStream os = null;
String concat = urlstr.concat(key);
System.out.println(dateStr + ", request url: "+concat +", param: "+ jsonObject);
try {
URL url = new URL(concat);
con = (HttpURLConnection) url.openConnection();
//设置请求类型
con.setRequestMethod("POST");
if (key.equals("rollback")) {
//设置请求需要返回的数据类型和字符集类型
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=GBK");
jsonObject = "_id=" + jsonObject;
} else {
//设置请求需要返回的数据类型和字符集类型
con.setRequestProperty("Content-Type", "application/json;charset=GBK");
}
//允许写出
con.setDoOutput(true);
//允许读入
con.setDoInput(true);
//不使用缓存
con.setUseCaches(false);
//获得输出流,并将其封装为字符流
PrintWriter out = new PrintWriter(con.getOutputStream());
//按字节的方式打印输出字符,并写入数组的某一部分
out.print(jsonObject);
//刷新输出流的缓冲
out.flush();
//得到响应码
con.getResponseCode();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (con != null) {
con.disconnect();
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
}
}
}