引入mongodb,使用队列+单独线程记录访问日志
This commit is contained in:
@@ -8,6 +8,10 @@
|
|||||||
<source name="redis" value="org.redkalex.cache.RedisCacheSource" xxx="16">
|
<source name="redis" value="org.redkalex.cache.RedisCacheSource" xxx="16">
|
||||||
<node addr="redishost" port="6379"/>
|
<node addr="redishost" port="6379"/>
|
||||||
</source>
|
</source>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<property name="mongo.host" value="127.0.0.1"/>
|
||||||
|
</properties>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
||||||
<server protocol="HTTP" host="0.0.0.0" port="80" root="root">
|
<server protocol="HTTP" host="0.0.0.0" port="80" root="root">
|
||||||
|
@@ -11,7 +11,7 @@ javax.level = INFO
|
|||||||
com.sun.level = INFO
|
com.sun.level = INFO
|
||||||
sun.level = INFO
|
sun.level = INFO
|
||||||
jdk.level = INFO
|
jdk.level = INFO
|
||||||
|
org.mongodb.driver.level = SEVERE
|
||||||
|
|
||||||
java.util.logging.FileHandler.level = FINER
|
java.util.logging.FileHandler.level = FINER
|
||||||
#10M
|
#10M
|
||||||
|
BIN
lib/bson-3.7.1.jar
Normal file
BIN
lib/bson-3.7.1.jar
Normal file
Binary file not shown.
BIN
lib/mongodb-driver-3.7.1.jar
Normal file
BIN
lib/mongodb-driver-3.7.1.jar
Normal file
Binary file not shown.
BIN
lib/mongodb-driver-core-3.7.1.jar
Normal file
BIN
lib/mongodb-driver-core-3.7.1.jar
Normal file
Binary file not shown.
BIN
libs/redbbs.jar
BIN
libs/redbbs.jar
Binary file not shown.
5
pom.xml
5
pom.xml
@@ -37,6 +37,11 @@
|
|||||||
<artifactId>enjoy</artifactId>
|
<artifactId>enjoy</artifactId>
|
||||||
<version>3.4</version>
|
<version>3.4</version>
|
||||||
</dependency>-->
|
</dependency>-->
|
||||||
|
<!--<dependency>
|
||||||
|
<groupId>org.mongodb</groupId>
|
||||||
|
<artifactId>mongodb-driver</artifactId>
|
||||||
|
<version>3.7.1</version>
|
||||||
|
</dependency>-->
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@@ -20,6 +20,7 @@ import org.redkale.util.AnyValue;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.lxyer.bbs.base.kit.RetCodes.RET_USER_UNLOGIN;
|
import static com.lxyer.bbs.base.kit.RetCodes.RET_USER_UNLOGIN;
|
||||||
|
|
||||||
@@ -53,6 +54,9 @@ public class BaseServlet extends HttpServlet {
|
|||||||
@Resource
|
@Resource
|
||||||
protected CommentService commentService;
|
protected CommentService commentService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
protected TaskQueue<Map> logQueue;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(HttpContext context, AnyValue config) {
|
public void init(HttpContext context, AnyValue config) {
|
||||||
@@ -87,6 +91,24 @@ public class BaseServlet extends HttpServlet {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Kv visLog = new Kv();//ip、time、userid、uri、para
|
||||||
|
visLog.set("ip", request.getRemoteAddr());
|
||||||
|
request.getHost();
|
||||||
|
visLog.set("time", System.currentTimeMillis());
|
||||||
|
visLog.set("userid", currentid);
|
||||||
|
visLog.set("uri", request.getRequestURI());
|
||||||
|
|
||||||
|
Kv para = Kv.create();
|
||||||
|
for (String key : request.getParameterNames()){
|
||||||
|
para.set(key, request.getParameter(key));
|
||||||
|
}
|
||||||
|
visLog.set("para", para);
|
||||||
|
try {
|
||||||
|
logQueue.put(visLog);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*if ("/".equals(uri)){
|
/*if ("/".equals(uri)){
|
||||||
finish("/front/index.html", Kv.by("",""));
|
finish("/front/index.html", Kv.by("",""));
|
||||||
|
69
src/com/lxyer/bbs/base/TaskQueue.java
Normal file
69
src/com/lxyer/bbs/base/TaskQueue.java
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package com.lxyer.bbs.base;
|
||||||
|
|
||||||
|
import com.mongodb.MongoClient;
|
||||||
|
import com.mongodb.client.MongoCollection;
|
||||||
|
import com.mongodb.client.MongoDatabase;
|
||||||
|
import org.bson.Document;
|
||||||
|
import org.redkale.util.AnyValue;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by liangxianyou at 2018/6/20 22:54.
|
||||||
|
*/
|
||||||
|
public class TaskQueue<T extends Object> extends BaseService implements Runnable {
|
||||||
|
|
||||||
|
@Resource(name = "property.mongo.host")
|
||||||
|
private String mongoHost;
|
||||||
|
|
||||||
|
protected static LinkedBlockingQueue queue = new LinkedBlockingQueue();
|
||||||
|
|
||||||
|
private static MongoClient mongoClient;
|
||||||
|
private static MongoDatabase redbbs;
|
||||||
|
private static MongoCollection<Document> visLog;
|
||||||
|
|
||||||
|
public TaskQueue() {
|
||||||
|
new Thread(this).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(AnyValue config) {
|
||||||
|
mongoClient = new MongoClient(mongoHost, 27017);
|
||||||
|
redbbs = mongoClient.getDatabase(winos ? "redbbs_dev": "redbbs");
|
||||||
|
visLog = redbbs.getCollection("vis_log");
|
||||||
|
}
|
||||||
|
|
||||||
|
public T take() throws InterruptedException {
|
||||||
|
return (T) queue.take();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void put(T t) throws InterruptedException {
|
||||||
|
queue.put(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (true){
|
||||||
|
Map take = (Map) take();
|
||||||
|
|
||||||
|
take.put("ftime", String.format("%1$tY%1$tm%1$td%1$tH%1$tM", take.get("time")));
|
||||||
|
visLog.insertOne(new Document(take));
|
||||||
|
|
||||||
|
//在这里处理日志数据[访问量]
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public static void main(String[] args) {
|
||||||
|
//测试mongodb 连通性
|
||||||
|
FindIterable<Document> documents = visLog.find().limit(10);
|
||||||
|
documents.forEach((Block<? super Document>) x->{
|
||||||
|
System.out.println(x);
|
||||||
|
});
|
||||||
|
}*/
|
||||||
|
}
|
Reference in New Issue
Block a user