refactor(接口定义): 资源池加载插件直接从MinIO获取

--story=1011108 --user=王孝刚 资源池加载插件直接从MinIO获取
https://www.tapd.cn/55049933/s/1331634
This commit is contained in:
wxg0103 2023-02-06 11:00:22 +08:00 committed by fit2-zhao
parent 745074fd1f
commit 6bea52fbc4
11 changed files with 116 additions and 13 deletions

View File

@ -26,6 +26,7 @@ import io.metersphere.engine.Engine;
import io.metersphere.jmeter.JMeterBase; import io.metersphere.jmeter.JMeterBase;
import io.metersphere.jmeter.LocalRunner; import io.metersphere.jmeter.LocalRunner;
import io.metersphere.service.ApiPoolDebugService; import io.metersphere.service.ApiPoolDebugService;
import io.metersphere.service.PluginService;
import io.metersphere.service.RemakeReportService; import io.metersphere.service.RemakeReportService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
@ -65,6 +66,8 @@ public class JMeterService {
private ExecThreadPoolExecutor execThreadPoolExecutor; private ExecThreadPoolExecutor execThreadPoolExecutor;
@Resource @Resource
private ApiPoolDebugService apiPoolDebugService; private ApiPoolDebugService apiPoolDebugService;
@Resource
private PluginService pluginService;
@PostConstruct @PostConstruct
private void init() { private void init() {

View File

@ -0,0 +1,23 @@
package io.metersphere.commons.config;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.config.MinioProperties;
import io.metersphere.enums.MinIOConfigEnum;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class MinioConfig {
public static Map<String, Object> getMinio() {
MinioProperties minioProperties = CommonBeanFactory.getBean(MinioProperties.class);
Map<String, Object> minioPros = new HashMap<>();
minioPros.put(MinIOConfigEnum.ENDPOINT, minioProperties.getEndpoint());
minioPros.put(MinIOConfigEnum.ACCESS_KEY, minioProperties.getAccessKey());
minioPros.put(MinIOConfigEnum.SECRET_KEY, minioProperties.getSecretKey());
minioPros.put(MinIOConfigEnum.BUCKET, minioProperties.getBucket());
return minioPros;
}
}

View File

@ -2,8 +2,10 @@ package io.metersphere.controller;
import io.metersphere.api.dto.BodyFileRequest; import io.metersphere.api.dto.BodyFileRequest;
import io.metersphere.api.jmeter.JMeterThreadUtils; import io.metersphere.api.jmeter.JMeterThreadUtils;
import io.metersphere.dto.PluginConfigDTO;
import io.metersphere.service.ApiJMeterFileService; import io.metersphere.service.ApiJMeterFileService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -11,7 +13,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource; import java.util.List;
import java.util.UUID; import java.util.UUID;
@RestController @RestController
@ -56,15 +58,21 @@ public class ApiJMeterFileController {
.body(bytes); .body(bytes);
} }
@GetMapping("download/plug/jar") @PostMapping("download/plugin/jar")
public ResponseEntity<byte[]> downloadPlugFiles() { public ResponseEntity<byte[]> downloadPluginFiles(@RequestBody List<String> request) {
byte[] bytes = apiJmeterFileService.downloadPlugJar(); byte[] bytes = apiJmeterFileService.downloadPluginJar(request);
return ResponseEntity.ok() return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream")) .contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + UUID.randomUUID().toString() + ".zip\"") .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + UUID.randomUUID().toString() + ".zip\"")
.body(bytes); .body(bytes);
} }
@GetMapping("download/plugin/jar/list")
public PluginConfigDTO downloadPlugLists() {
return apiJmeterFileService.downloadPluginJarList();
}
@PostMapping("download/files") @PostMapping("download/files")
public ResponseEntity<byte[]> downloadJmeterFiles(@RequestBody BodyFileRequest request) { public ResponseEntity<byte[]> downloadJmeterFiles(@RequestBody BodyFileRequest request) {
byte[] bytes = apiJmeterFileService.zipFilesToByteArray(request); byte[] bytes = apiJmeterFileService.zipFilesToByteArray(request);
@ -73,6 +81,7 @@ public class ApiJMeterFileController {
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + request.getReportId() + ".zip\"") .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + request.getReportId() + ".zip\"")
.body(bytes); .body(bytes);
} }
@GetMapping("get-script") @GetMapping("get-script")
public String getScript(@RequestParam("reportId") String reportId, @RequestParam("testId") String testId) { public String getScript(@RequestParam("reportId") String reportId, @RequestParam("testId") String testId) {
String key = StringUtils.join(reportId, "-", testId); String key = StringUtils.join(reportId, "-", testId);

View File

@ -9,13 +9,17 @@ import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiExecutionQueueDetailMapper; import io.metersphere.base.mapper.ApiExecutionQueueDetailMapper;
import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper; import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper;
import io.metersphere.commons.config.MinioConfig;
import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.PluginConfigDTO;
import io.metersphere.dto.PluginDTO;
import io.metersphere.environment.service.BaseEnvGroupProjectService; import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.service.FileMetadataService;
import io.metersphere.request.BodyFile; import io.metersphere.request.BodyFile;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
@ -23,8 +27,6 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -160,10 +162,19 @@ public class ApiJMeterFileService {
return listBytesToZip(files); return listBytesToZip(files);
} }
public byte[] downloadPlugJar() { public byte[] downloadPluginJar(List<String> jarPaths) {
Map<String, byte[]> files = new HashMap<>(); Map<String, byte[]> files = new HashMap<>();
// 获取JAR // 获取JAR
Map<String, byte[]> jarFiles = this.getPlugJar(); Map<String, byte[]> jarFiles = new LinkedHashMap<>();
jarPaths.forEach(item -> {
File file = new File(item);
if (file.exists() && !file.isDirectory()) {
byte[] fileByte = FileUtils.fileToByte(file);
if (ArrayUtils.isNotEmpty(fileByte)) {
jarFiles.put(file.getName(), fileByte);
}
}
});
if (MapUtils.isNotEmpty(jarFiles)) { if (MapUtils.isNotEmpty(jarFiles)) {
for (String k : jarFiles.keySet()) { for (String k : jarFiles.keySet()) {
byte[] v = jarFiles.get(k); byte[] v = jarFiles.get(k);
@ -173,6 +184,26 @@ public class ApiJMeterFileService {
return listBytesToZip(files); return listBytesToZip(files);
} }
public PluginConfigDTO downloadPluginJarList() {
PluginConfigDTO pluginConfigDTO = new PluginConfigDTO();
List<Plugin> plugins = pluginService.list();
if (CollectionUtils.isNotEmpty(plugins)) {
plugins = plugins.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(Plugin::getPluginId))), ArrayList::new));
List<PluginDTO> plugin = plugins.stream().map(
item -> {
PluginDTO pluginDTO = new PluginDTO();
pluginDTO.setPluginId(item.getPluginId());
pluginDTO.setSourcePath(item.getSourcePath());
return pluginDTO;
}
).collect(Collectors.toList());
pluginConfigDTO.setPluginDTOS(plugin);
}
pluginConfigDTO.setConfig(MinioConfig.getMinio());
return pluginConfigDTO;
}
private Map<String, byte[]> getJar(String projectId) { private Map<String, byte[]> getJar(String projectId) {
Map<String, byte[]> jarFiles = new LinkedHashMap<>(); Map<String, byte[]> jarFiles = new LinkedHashMap<>();
FileMetadataService jarConfigService = CommonBeanFactory.getBean(FileMetadataService.class); FileMetadataService jarConfigService = CommonBeanFactory.getBean(FileMetadataService.class);
@ -196,7 +227,7 @@ public class ApiJMeterFileService {
} }
} }
private Map<String, byte[]> getPlugJar() { public Map<String, byte[]> getPlugJar() {
Map<String, byte[]> jarFiles = new LinkedHashMap<>(); Map<String, byte[]> jarFiles = new LinkedHashMap<>();
List<Plugin> plugins = pluginService.list(); List<Plugin> plugins = pluginService.list();
if (CollectionUtils.isNotEmpty(plugins)) { if (CollectionUtils.isNotEmpty(plugins)) {

View File

@ -0,0 +1,13 @@
package io.metersphere.dto;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class PluginConfigDTO {
private List<PluginDTO> pluginDTOS;
private Map<String, Object> config;
}

View File

@ -0,0 +1,14 @@
package io.metersphere.dto;
import lombok.Data;
import java.io.Serializable;
@Data
public class PluginDTO implements Serializable {
private String pluginId;
private String sourcePath;
}

View File

@ -0,0 +1,9 @@
package io.metersphere.enums;
public class MinIOConfigEnum {
public static final String ENDPOINT = "endpoint";
public static final String ACCESS_KEY = "accessKey";
public static final String SECRET_KEY = "secretKey";
public static final String BUCKET = "bucket";
}

View File

@ -3,9 +3,10 @@ package io.metersphere.base.mapper;
import io.metersphere.base.domain.Plugin; import io.metersphere.base.domain.Plugin;
import io.metersphere.base.domain.PluginExample; import io.metersphere.base.domain.PluginExample;
import io.metersphere.base.domain.PluginWithBLOBs; import io.metersphere.base.domain.PluginWithBLOBs;
import java.util.List;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface PluginMapper { public interface PluginMapper {
long countByExample(PluginExample example); long countByExample(PluginExample example);

View File

@ -39,7 +39,7 @@ public class FilterChainUtils {
filterChainDefinitionMap.put("/api/jmeter/download/files", "anon"); filterChainDefinitionMap.put("/api/jmeter/download/files", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/jar", "anon"); filterChainDefinitionMap.put("/api/jmeter/download/jar", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/jar/**", "anon"); filterChainDefinitionMap.put("/api/jmeter/download/jar/**", "anon");
filterChainDefinitionMap.put("/api/jmeter/download/plug/jar", "anon"); filterChainDefinitionMap.put("/api/jmeter/download/plugin/**", "anon");
filterChainDefinitionMap.put("/api/jmeter/get-script", "anon"); filterChainDefinitionMap.put("/api/jmeter/get-script", "anon");
// for swagger // for swagger

View File

@ -14,6 +14,7 @@ import io.metersphere.dto.PluginResourceDTO;
import io.metersphere.plugin.core.api.UiScriptApi; import io.metersphere.plugin.core.api.UiScriptApi;
import io.metersphere.plugin.core.ui.PluginResource; import io.metersphere.plugin.core.ui.PluginResource;
import io.metersphere.utils.CommonUtil; import io.metersphere.utils.CommonUtil;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.reflect.ClassFinder; import org.apache.jorphan.reflect.ClassFinder;
@ -21,7 +22,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import jakarta.annotation.Resource;
import java.io.File; import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;

View File

@ -19,6 +19,7 @@ import io.metersphere.metadata.service.FileManagerService;
import io.metersphere.metadata.vo.FileRequest; import io.metersphere.metadata.vo.FileRequest;
import io.metersphere.request.PluginDTO; import io.metersphere.request.PluginDTO;
import io.metersphere.request.PluginRequest; import io.metersphere.request.PluginRequest;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -26,7 +27,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import jakarta.annotation.Resource;
import java.io.File; import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;