|
|
@@ -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;
|
|
|
+ }
|
|
|
+}
|