浏览代码

提交代码

tianboguang 2 年之前
父节点
当前提交
94206bbabb

+ 6 - 0
pom.xml

@@ -40,6 +40,12 @@
         </dependency>
 
         <dependency>
+            <groupId>com.squareup.okhttp3</groupId>
+            <artifactId>okhttp</artifactId>
+            <version>3.14.4</version>
+        </dependency>
+
+        <dependency>
             <groupId>com.yomahub</groupId>
             <artifactId>tlog-web-spring-boot-starter</artifactId>
             <version>1.3.6</version>

+ 12 - 1
src/main/java/com/fdkankan/tracking/controller/TrackingController.java

@@ -3,13 +3,18 @@ package com.fdkankan.tracking.controller;
 import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.tracking.entit.TrackingEntity;
 import com.fdkankan.tracking.service.TrackingService;
+import com.fdkankan.tracking.utils.IpUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.ObjectUtils;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletRequest;
+import java.util.Date;
+
 @RestController
 @RequestMapping("/log")
 public class TrackingController {
@@ -21,7 +26,13 @@ public class TrackingController {
 
 
     @RequestMapping("save")
-    public void saveLog(@RequestBody TrackingEntity entity){
+    public void saveLog(@RequestBody TrackingEntity entity, HttpServletRequest request){
+        entity.setUserAgent(request.getHeader("user-agent"));
+        entity.setUserIP(IpUtils.getIPAddress(request));
+        entity.setRequestTime(new Date());
+        if(!ObjectUtils.isEmpty(entity.getUserIP())){
+            entity.setUserIpAdress(IpUtils.getUserAddress(entity.getUserIP()));
+        }
         log.info("收到打点请求:{}", JSONObject.toJSONString(entity));
         trackingService.save(entity);
     }

+ 7 - 5
src/main/java/com/fdkankan/tracking/entit/TrackingEntity.java

@@ -1,11 +1,13 @@
 package com.fdkankan.tracking.entit;
 
+import java.util.Date;
+
 public class TrackingEntity {
     private String traceId;
     private String requestType;
     private String channel;
     private String userId;
-    private String time;
+    private Date requestTime;
     private String requestUrl;
     private String requestData;
     private String TrackingType;
@@ -54,12 +56,12 @@ public class TrackingEntity {
         this.userId = userId;
     }
 
-    public String getTime() {
-        return time;
+    public Date getRequestTime() {
+        return requestTime;
     }
 
-    public void setTime(String time) {
-        this.time = time;
+    public void setRequestTime(Date requestTime) {
+        this.requestTime = requestTime;
     }
 
     public String getRequestUrl() {

+ 11 - 1
src/main/java/com/fdkankan/tracking/service/TrackingService.java

@@ -1,15 +1,25 @@
 package com.fdkankan.tracking.service;
 
 import com.fdkankan.tracking.entit.TrackingEntity;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
 @Service
 public class TrackingService {
 
+    private static final Logger log = LoggerFactory.getLogger(TrackingService.class);
+
+    @Value("${es.host:http://120.24.144.164:18201}")
+    private String esHost;
+
     private RestTemplate restTemplate = new RestTemplate();
 
     public void save(TrackingEntity entity) {
-        restTemplate.postForEntity("http://120.24.144.164:18201/tracking/log?pretty&pretty", entity, String.class);
+        String url = esHost.concat("/tracking/log?pretty&pretty");
+        log.info("es host:{}", url);
+        restTemplate.postForEntity(url, entity, String.class);
     }
 }

+ 85 - 0
src/main/java/com/fdkankan/tracking/utils/IpUtils.java

@@ -0,0 +1,85 @@
+package com.fdkankan.tracking.utils;
+
+import com.alibaba.fastjson.JSONObject;
+import org.springframework.http.*;
+import org.springframework.web.client.RestTemplate;
+
+import javax.servlet.http.HttpServletRequest;
+
+public class IpUtils {
+
+    private static RestTemplate restTemplate = new RestTemplate();
+
+    public static String getIPAddress(HttpServletRequest request) {
+        String ip = null;
+
+        //X-Forwarded-For:Squid 服务代理
+        String ipAddresses = request.getHeader("X-Forwarded-For");
+
+        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
+            //Proxy-Client-IP:apache 服务代理
+            ipAddresses = request.getHeader("Proxy-Client-IP");
+        }
+
+        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
+            //WL-Proxy-Client-IP:weblogic 服务代理
+            ipAddresses = request.getHeader("WL-Proxy-Client-IP");
+        }
+
+        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
+            //HTTP_CLIENT_IP:有些代理服务器
+            ipAddresses = request.getHeader("HTTP_CLIENT_IP");
+        }
+
+        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
+            //X-Real-IP:nginx服务代理
+            ipAddresses = request.getHeader("X-Real-IP");
+        }
+
+        //有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
+        if (ipAddresses != null && ipAddresses.length() != 0) {
+            ip = ipAddresses.split(",")[0];
+        }
+
+        //还是不能获取到,最后再通过request.getRemoteAddr();获取
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
+            ip = request.getRemoteAddr();
+        }
+        return ip;
+    }
+
+    public static void main(String[] args) {
+        getUserAddress(null);
+    }
+
+    public static String getUserAddress(String ip) {
+        try {
+//            OkHttpClient client = new OkHttpClient().newBuilder()
+//                    .build();
+//            MediaType mediaType = MediaType.parse("text/plain");
+//            RequestBody body = RequestBody.create(mediaType, "");
+//            Request request = new Request.Builder()
+//                    .url("https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=221.4.210.172")
+//                    .method("GET", null)
+//                    .addHeader("Cookie", "XSRF-TOKEN=863142da-e773-4e28-9dda-e256fcdb42e5")
+//                    .build();
+//            Response response = client.newCall(request).execute();
+//            return JSONObject.parseObject(response.body().string()).getString("data");
+
+//            HttpHeaders headers = new HttpHeaders();
+//            MediaType type = MediaType.TEXT_PLAIN;
+//            headers.setContentType(type);
+//            HttpEntity<String> formEntity = new HttpEntity<>(null,headers);
+//            ResponseEntity<JSONObject> responseEntity = restTemplate.exchange("http://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=" + ip,
+//                    HttpMethod.GET,formEntity, JSONObject.class);
+//            return responseEntity.getBody().getJSONObject("data").toJSONString();
+
+
+            restTemplate.getForObject("https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=221.4.210.172",String.class);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
+
+}

+ 1 - 1
src/main/resources/bootstrap.yml

@@ -9,4 +9,4 @@ server:
 
 logging:
   config: classpath:logback-spring.xml
-  path: /data/4dkankan/logs/4dkankan_tracking
+  path: /mnt/4Dkankan/v4/logs/4dkankan_tracking