丁云鹏 1 سال پیش
والد
کامیت
a1ec5347a9

+ 11 - 0
recommend-server-service/pom.xml

@@ -126,6 +126,17 @@
             <groupId>com.aliyun.openservices</groupId>
             <artifactId>aliyun-log-logback-appender</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.aliyun.openservices</groupId>
+            <artifactId>aliyun-log-producer</artifactId>
+            <version>0.3.10</version>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun.openservices</groupId>
+            <artifactId>aliyun-log</artifactId>
+            <version>0.6.35</version>
+            <classifier>jar-with-dependencies</classifier>
+        </dependency>
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>

+ 7 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/RecommendService.java

@@ -83,6 +83,8 @@ public class RecommendService {
     private PreViewedService preViewedService;
     @Autowired
     private FlowPoolService flowPoolService;
+    @Autowired
+    private StatisticsLogService statisticsLogService;
 
     @PostConstruct
     public void init() {
@@ -127,6 +129,7 @@ public class RecommendService {
         stopwatch.reset().start();
         updateCache(request, param, videos);
 
+        logStatisticsInfo(param, videos);
         // 更新position
         List<VideoProto> vps = new ArrayList<>();
         for (int i = 0; i < videos.size(); i++) {
@@ -150,6 +153,10 @@ public class RecommendService {
 
     }
 
+    private void logStatisticsInfo(RecommendParam param, List<Video> videos) {
+        statisticsLogService.log();
+    }
+
     private RecommendResponse specialMidRecommend(RecommendRequest request) {
         log.info("hit special mid recommend request={}", JSONUtils.toJson(request));
         if (request == null) {

+ 58 - 0
recommend-server-service/src/main/java/com/tzld/piaoquan/recommend/server/service/StatisticsLogService.java

@@ -0,0 +1,58 @@
+package com.tzld.piaoquan.recommend.server.service;
+
+import com.aliyun.openservices.aliyun.log.producer.LogProducer;
+import com.aliyun.openservices.aliyun.log.producer.Producer;
+import com.aliyun.openservices.aliyun.log.producer.ProducerConfig;
+import com.aliyun.openservices.aliyun.log.producer.ProjectConfig;
+import com.aliyun.openservices.log.common.LogItem;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.MapUtils;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import java.util.Map;
+
+@Service
+@Slf4j
+public class StatisticsLogService {
+
+    @Value("${aliyun.log.project}")
+    private String project;
+    @Value("${aliyun.log.endpoint}")
+    private String endpoint;
+    @Value("${aliyun.log.accessKeyId}")
+    private String accessKeyId;
+    @Value("${aliyun.log.accessKeySecret}")
+    private String accessKeySecret;
+
+    private String logStore = "statistics-log";
+
+    private Producer producer;
+
+
+    @PostConstruct
+    public void init() {
+        ProducerConfig producerConfig = new ProducerConfig();
+        producer = new LogProducer(producerConfig);
+        producer.putProjectConfig(new ProjectConfig(project, endpoint, accessKeyId, accessKeySecret));
+    }
+
+    public void log(Map<String, String> data) {
+        if (MapUtils.isEmpty(data)) {
+            return;
+        }
+        try {
+            LogItem logItem = new LogItem();
+            data.entrySet().stream().forEach(e -> {
+                logItem.PushBack(e.getKey(), e.getValue());
+            });
+            producer.send(project, logStore, logItem);
+        } catch (InterruptedException e) {
+            log.warn("The current thread has been interrupted during send logs.");
+        } catch (Exception e) {
+            log.error("Failed to send logs", e);
+        }
+    }
+
+}