Bladeren bron

代码提交

jingyuanchao 1 jaar geleden
bovenliggende
commit
d23c8748f3

+ 42 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/controller/LogDownloadAggregatorController.java

@@ -0,0 +1,42 @@
+package com.xunmei.system.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cloud.client.ServiceInstance;
+import org.springframework.cloud.client.discovery.DiscoveryClient;
+import org.springframework.core.io.Resource;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/logDownload")
+public class LogDownloadAggregatorController {
+
+    @Autowired
+    private DiscoveryClient discoveryClient;
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @GetMapping("/{serviceName}/{fileName}")
+    public ResponseEntity<Resource> downloadLogFile(@PathVariable String serviceName, @PathVariable String fileName) {
+        // 获取指定服务的所有实例
+        List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
+        if (instances.isEmpty()) {
+            return ResponseEntity.notFound().build();
+        }
+
+        // 选择第一个实例进行日志下载
+        ServiceInstance instance = instances.get(0);
+        String instanceUrl = instance.getUri().toString();
+
+        // 调用实例的日志下载接口
+        ResponseEntity<Resource> response = restTemplate.getForEntity(instanceUrl + "/logs/" + fileName, Resource.class);
+        return response;
+    }
+}

+ 33 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/controller/LogDownloadController.java

@@ -0,0 +1,33 @@
+package com.xunmei.system.controller;
+
+import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.io.File;
+
+@RestController
+@RequestMapping("/logs")
+public class LogDownloadController {
+
+    private static final String LOG_DIR = "/Users/jingyuanchao/IdeaProjects/soc/logs/";
+
+    @GetMapping("/{fileName}")
+    public ResponseEntity<Resource> downloadLogFile(@PathVariable String fileName) {
+        File file = new File(LOG_DIR + "soc-system" + File.separator + fileName);
+        if (!file.exists()) {
+            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
+        }
+
+        Resource resource = new FileSystemResource(file);
+        return ResponseEntity.ok()
+                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"")
+                .body(resource);
+    }
+}