refactor: 缓存监控节点,避免 prometheus 查询频繁导致数据库链接超时
This commit is contained in:
parent
55f8c30c0c
commit
41c04b90b3
|
@ -0,0 +1,9 @@
|
|||
package io.metersphere.consul;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target({ElementType.METHOD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface CacheNode {
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package io.metersphere.consul;
|
||||
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import org.aspectj.lang.annotation.After;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class CacheNodeAspect {
|
||||
@Resource
|
||||
private ConsulService consulService;
|
||||
|
||||
/**
|
||||
* 定义切点 @Pointcut 在注解的位置切入代码
|
||||
*/
|
||||
@Pointcut("@annotation(io.metersphere.consul.CacheNode)")
|
||||
public void cacheNodes() {
|
||||
}
|
||||
|
||||
@After("cacheNodes()")
|
||||
public void after() {
|
||||
try {
|
||||
consulService.updateCache();
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -19,15 +19,24 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Service
|
||||
public class ConsulService {
|
||||
private final Map<String, List<String>> cache = new ConcurrentHashMap<>();
|
||||
@Resource
|
||||
private TestResourcePoolService testResourcePoolService;
|
||||
@Resource
|
||||
private PerformanceTestService performanceTestService;
|
||||
|
||||
public Map<String, List<String>> getActiveNodes() {
|
||||
if (cache.size() == 0) {
|
||||
updateCache();
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
public void updateCache() {
|
||||
Map<String, List<String>> result = new HashMap<>();
|
||||
|
||||
QueryResourcePoolRequest resourcePoolRequest = new QueryResourcePoolRequest();
|
||||
|
@ -65,6 +74,7 @@ public class ConsulService {
|
|||
result.put(node.getIp() + "-" + port, Collections.singletonList("metersphere"));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
cache.clear();
|
||||
cache.putAll(result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import io.metersphere.commons.constants.OperLogConstants;
|
|||
import io.metersphere.commons.constants.RoleConstants;
|
||||
import io.metersphere.commons.utils.PageUtils;
|
||||
import io.metersphere.commons.utils.Pager;
|
||||
import io.metersphere.consul.CacheNode;
|
||||
import io.metersphere.controller.request.resourcepool.QueryResourcePoolRequest;
|
||||
import io.metersphere.dto.TestResourcePoolDTO;
|
||||
import io.metersphere.dto.UpdatePoolDTO;
|
||||
|
@ -28,24 +29,28 @@ public class TestResourcePoolController {
|
|||
|
||||
@PostMapping("/add")
|
||||
@MsAuditLog(module = "system_test_resource", type = OperLogConstants.CREATE, content = "#msClass.getLogDetails(#testResourcePoolDTO.id)", msClass = TestResourcePoolService.class)
|
||||
@CacheNode // 把监控节点缓存起来
|
||||
public TestResourcePoolDTO addTestResourcePool(@RequestBody TestResourcePoolDTO testResourcePoolDTO) {
|
||||
return testResourcePoolService.addTestResourcePool(testResourcePoolDTO);
|
||||
}
|
||||
|
||||
@GetMapping("/delete/{testResourcePoolId}")
|
||||
@MsAuditLog(module = "system_test_resource", type = OperLogConstants.DELETE, beforeEvent = "#msClass.getLogDetails(#testResourcePoolId)", msClass = TestResourcePoolService.class)
|
||||
@MsAuditLog(module = "system_test_resource", type = OperLogConstants.DELETE, beforeEvent = "#msClass.getLogDetails(#testResourcePoolId)", msClass = TestResourcePoolService.class)
|
||||
@CacheNode // 把监控节点缓存起来
|
||||
public void deleteTestResourcePool(@PathVariable(value = "testResourcePoolId") String testResourcePoolId) {
|
||||
testResourcePoolService.deleteTestResourcePool(testResourcePoolId);
|
||||
}
|
||||
|
||||
@PostMapping("/update")
|
||||
@MsAuditLog(module = "system_test_resource", type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#testResourcePoolDTO.id)", content = "#msClass.getLogDetails(#testResourcePoolDTO.id)", msClass = TestResourcePoolService.class)
|
||||
@CacheNode // 把监控节点缓存起来
|
||||
public void updateTestResourcePool(@RequestBody TestResourcePoolDTO testResourcePoolDTO) {
|
||||
testResourcePoolService.updateTestResourcePool(testResourcePoolDTO);
|
||||
}
|
||||
|
||||
@GetMapping("/update/{poolId}/{status}")
|
||||
@MsAuditLog(module = "system_test_resource", type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#poolId)", content = "#msClass.getLogDetails(#poolId)", msClass = TestResourcePoolService.class)
|
||||
@CacheNode // 把监控节点缓存起来
|
||||
public void updateTestResourcePoolStatus(@PathVariable String poolId, @PathVariable String status) {
|
||||
testResourcePoolService.updateTestResourcePoolStatus(poolId, status);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import io.metersphere.commons.constants.PermissionConstants;
|
|||
import io.metersphere.commons.utils.PageUtils;
|
||||
import io.metersphere.commons.utils.Pager;
|
||||
import io.metersphere.commons.utils.SessionUtils;
|
||||
import io.metersphere.consul.CacheNode;
|
||||
import io.metersphere.controller.request.QueryScheduleRequest;
|
||||
import io.metersphere.controller.request.ScheduleRequest;
|
||||
import io.metersphere.dto.DashboardTestDTO;
|
||||
|
@ -74,6 +75,7 @@ public class PerformanceTestController {
|
|||
@PostMapping(value = "/save", consumes = {"multipart/form-data"})
|
||||
@MsAuditLog(module = "performance_test", type = OperLogConstants.CREATE, title = "#request.name", content = "#msClass.getLogDetails(#request.id)", msClass = PerformanceTestService.class)
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_PERFORMANCE_TEST_READ_CREATE)
|
||||
@CacheNode // 把监控节点缓存起来
|
||||
public String save(
|
||||
@RequestPart("request") SaveTestPlanRequest request,
|
||||
@RequestPart(value = "file", required = false) List<MultipartFile> files
|
||||
|
@ -92,6 +94,7 @@ public class PerformanceTestController {
|
|||
@PostMapping(value = "/edit", consumes = {"multipart/form-data"})
|
||||
@MsAuditLog(module = "performance_test", type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#request.id)", title = "#request.name", content = "#msClass.getLogDetails(#request.id)", msClass = PerformanceTestService.class)
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_PERFORMANCE_TEST_READ_EDIT)
|
||||
@CacheNode // 把监控节点缓存起来
|
||||
public String edit(
|
||||
@RequestPart("request") EditTestPlanRequest request,
|
||||
@RequestPart(value = "file", required = false) List<MultipartFile> files
|
||||
|
@ -141,6 +144,7 @@ public class PerformanceTestController {
|
|||
@PostMapping("/delete")
|
||||
@MsAuditLog(module = "performance_test", type = OperLogConstants.DELETE, beforeEvent = "#msClass.getLogDetails(#request.id)", msClass = PerformanceTestService.class)
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_PERFORMANCE_TEST_READ_DELETE)
|
||||
@CacheNode // 把监控节点缓存起来
|
||||
public void delete(@RequestBody DeleteTestPlanRequest request) {
|
||||
checkPermissionService.checkPerformanceTestOwner(request.getId());
|
||||
performanceTestService.delete(request);
|
||||
|
@ -191,6 +195,7 @@ public class PerformanceTestController {
|
|||
@PostMapping(value = "/copy")
|
||||
@MsAuditLog(module = "performance_test", type = OperLogConstants.COPY, content = "#msClass.getLogDetails(#request.id)", msClass = PerformanceTestService.class)
|
||||
@RequiresPermissions(PermissionConstants.PROJECT_PERFORMANCE_TEST_READ_COPY)
|
||||
@CacheNode // 把监控节点缓存起来
|
||||
public void copy(@RequestBody SaveTestPlanRequest request) {
|
||||
performanceTestService.copy(request);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue