From fae8ff5f8c11e869edac3a84731db83e9d7d8f45 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 19 Aug 2021 17:40:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86TCP?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化项目管理TCP端口设置:启用时后台查询可用端口 --- .../api/controller/MockApiController.java | 6 ++ .../service/ApiTestEnvironmentService.java | 2 +- .../java/io/metersphere/api/tcp/TCPPool.java | 2 +- .../base/mapper/ext/ExtProjectMapper.java | 2 + .../base/mapper/ext/ExtProjectMapper.xml | 7 ++ .../controller/ProjectController.java | 5 + .../metersphere/service/ProjectService.java | 92 ++++++++++++++++++- .../settings/project/ProjectList.vue | 13 ++- .../settings/workspace/MsProject.vue | 12 +++ 9 files changed, 135 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/controller/MockApiController.java b/backend/src/main/java/io/metersphere/api/controller/MockApiController.java index de11fa5b2a..453a42b928 100644 --- a/backend/src/main/java/io/metersphere/api/controller/MockApiController.java +++ b/backend/src/main/java/io/metersphere/api/controller/MockApiController.java @@ -2,6 +2,7 @@ package io.metersphere.api.controller; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.MockConfigService; +import io.metersphere.api.tcp.TCPPool; import io.metersphere.base.domain.Project; import io.metersphere.controller.handler.annotation.NoResultHolder; import io.metersphere.service.ProjectService; @@ -79,4 +80,9 @@ public class MockApiController { Project project = projectService.findBySystemId(projectSystemId); mockConfigService.checkReturnWithMockExpectByUrlParam("HEAD", project, request, response); } + + @GetMapping("/getTcpMockPortStatus/") + public String genTcpMockPort(){ + return TCPPool.getTcpStatus(); + } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestEnvironmentService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestEnvironmentService.java index 02b2fd61ad..1218cf291e 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestEnvironmentService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestEnvironmentService.java @@ -342,7 +342,7 @@ public class ApiTestEnvironmentService { tcpConfigObj.put("closeConnection", false); if(project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0){ tcpConfigObj.put("server", tcpSocket); - tcpConfigObj.put("port", 12138); + tcpConfigObj.put("port", project.getMockTcpPort().intValue()); } JSONObject object = new JSONObject(); diff --git a/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java b/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java index 1e9b83ddae..03882869e9 100644 --- a/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java +++ b/backend/src/main/java/io/metersphere/api/tcp/TCPPool.java @@ -63,7 +63,7 @@ public class TCPPool { stringBuffer.append("Server is null;"); }else { stringBuffer.append("Port is "+port + ";"); - stringBuffer.append("Server is open: "+ tcpServer.isSocketOpen()); + stringBuffer.append("Server is open: "+ tcpServer.isSocketOpen()+";"); } } return stringBuffer.toString(); diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java index aa7ef79255..edcd176623 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.java @@ -30,4 +30,6 @@ public interface ExtProjectMapper { Map queryNameByIds(@Param("ids") List ids); Organization getOrganizationByProjectId(@Param("projectId")String projectId); + + List selectTcpPorts(); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml index 03eb5ca727..3f210a4c5c 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtProjectMapper.xml @@ -158,4 +158,11 @@ JOIN project ON workspace.id = workspace_id WHERE project.id = #{projectId, jdbcType=VARCHAR} + + + diff --git a/backend/src/main/java/io/metersphere/controller/ProjectController.java b/backend/src/main/java/io/metersphere/controller/ProjectController.java index c72fe3cdf7..7ce693e2ba 100644 --- a/backend/src/main/java/io/metersphere/controller/ProjectController.java +++ b/backend/src/main/java/io/metersphere/controller/ProjectController.java @@ -136,4 +136,9 @@ public class ProjectController { public Collection getOwnerProjectIds() { return checkPermissionService.getUserRelatedProjectIds(); } + + @GetMapping("/genTcpMockPort/{id}") + public String genTcpMockPort(@PathVariable String id){ + return projectService.genTcpMockPort(id); + } } diff --git a/backend/src/main/java/io/metersphere/service/ProjectService.java b/backend/src/main/java/io/metersphere/service/ProjectService.java index f85105bd9d..08587ef17a 100644 --- a/backend/src/main/java/io/metersphere/service/ProjectService.java +++ b/backend/src/main/java/io/metersphere/service/ProjectService.java @@ -302,6 +302,34 @@ public class ProjectService { } } + private boolean isMockTcpPortIsInRange(int port){ + boolean inRange = false; + if(StringUtils.isNotEmpty(this.tcpMockPorts)){ + try { + if(this.tcpMockPorts.contains("-")){ + String [] tcpMockPortArr = this.tcpMockPorts.split("-"); + int num1 = Integer.parseInt(tcpMockPortArr[0]); + int num2 = Integer.parseInt(tcpMockPortArr[1]); + + int startNum = num1 > num2 ? num2 : num1; + int endNum = num1 < num2 ? num2 : num1; + + if(port < startNum || port > endNum){ + inRange = false; + }else { + inRange = true; + } + }else { + int tcpPortConfigNum = Integer.parseInt(this.tcpMockPorts); + if(port == tcpPortConfigNum){ + inRange = true; + } + } + }catch (Exception e){ + } + } + return inRange; + } private void checkMockTcpPort(int port) { if(StringUtils.isNotEmpty(this.tcpMockPorts)){ try { @@ -338,8 +366,9 @@ public class ProjectService { private void checkProjectTcpPort(Project project) { //判断端口是否重复 if (project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0) { + String projectId = StringUtils.isEmpty(project.getId())?"":project.getId(); ProjectExample example = new ProjectExample(); - example.createCriteria().andMockTcpPortEqualTo(project.getMockTcpPort()); + example.createCriteria().andMockTcpPortEqualTo(project.getMockTcpPort()).andIdNotEqualTo(projectId); long countResult = projectMapper.countByExample(example); if (countResult > 0) { MSException.throwException("TCP Port is not unique!"); @@ -609,12 +638,71 @@ public class ProjectService { example.createCriteria().andIsMockTcpOpenEqualTo(statusBoolean).andMockTcpPortNotEqualTo(portInteger); List projectList = projectMapper.selectByExample(example); + List opendPortList = new ArrayList<>(); for (Project p : projectList) { - this.openMockTcp(p); + boolean isPortInRange = this.isMockTcpPortIsInRange(p.getMockTcpPort()); + if(isPortInRange && !opendPortList.contains(p.getMockTcpPort())){ + opendPortList.add(p.getMockTcpPort()); + this.openMockTcp(p); + }else { + if(opendPortList.contains(p.getMockTcpPort())){ + p.setMockTcpPort(0); + } + p.setIsMockTcpOpen(false); + projectMapper.updateByPrimaryKeySelective(p); + } } } public Organization getOrganizationByProjectId(String projectId) { return extProjectMapper.getOrganizationByProjectId(projectId); } + + public String genTcpMockPort(String id) { + int returnPort = 0; + Project project = projectMapper.selectByPrimaryKey(id); + if(project != null && project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0 ){ + if(this.isMockTcpPortIsInRange(project.getMockTcpPort().intValue())){ + returnPort = project.getMockTcpPort(); + } + }else { + if(StringUtils.isNotEmpty(this.tcpMockPorts)){ + List portInRange = new ArrayList<>(); + List tcpPortInDataBase = extProjectMapper.selectTcpPorts(); + for (Integer port :tcpPortInDataBase) { + if(this.isMockTcpPortIsInRange(port)){ + portInRange.add(port); + } + } + + try { + if(this.tcpMockPorts.contains("-")){ + String [] tcpMockPortArr = this.tcpMockPorts.split("-"); + int num1 = Integer.parseInt(tcpMockPortArr[0]); + int num2 = Integer.parseInt(tcpMockPortArr[1]); + + int startNum = num1 > num2 ? num2 : num1; + int endNum = num1 < num2 ? num2 : num1; + + for (int i = startNum; i <= endNum ; i++) { + if(!portInRange.contains(i)){ + returnPort = i; + break; + } + } + }else { + int tcpPortConfigNum = Integer.parseInt(this.tcpMockPorts); + if(!portInRange.contains(tcpPortConfigNum)){ + returnPort = tcpPortConfigNum; + } + } + }catch (Exception e){ + } + } + } + if(returnPort == 0 ){ + MSException.throwException("无可用TCP端口"); + } + return String.valueOf(returnPort); + } } diff --git a/frontend/src/business/components/settings/project/ProjectList.vue b/frontend/src/business/components/settings/project/ProjectList.vue index 8248baf404..62434b3fa5 100644 --- a/frontend/src/business/components/settings/project/ProjectList.vue +++ b/frontend/src/business/components/settings/project/ProjectList.vue @@ -97,7 +97,7 @@ - + @@ -185,6 +185,7 @@ import MsResourceFiles from "@/business/components/performance/test/components/R import TemplateSelect from "@/business/components/settings/workspace/template/TemplateSelect"; import {PROJECT_CONFIGS} from "@/business/components/common/components/search/search-components"; import MsInstructionsIcon from "@/business/components/common/components/MsInstructionsIcon"; +import {parseEnvironment} from "@/business/components/api/test/model/EnvironmentModel"; export default { name: "MsProject", @@ -394,13 +395,21 @@ export default { openEnvironmentConfig(project) { this.$refs.environmentConfig.open(project.id); }, + chengeMockTcpSwitch(value){ + if(value && this.form.mockTcpPort === 0){ + this.result = this.$get('/project/genTcpMockPort/' + this.form.id, res => { + let port = res.data; + this.form.mockTcpPort = port; + }) + } + } }, created() { document.addEventListener('keydown', this.handleEvent); }, beforeDestroy() { document.removeEventListener('keydown', this.handleEvent); - } + }, }; diff --git a/frontend/src/business/components/settings/workspace/MsProject.vue b/frontend/src/business/components/settings/workspace/MsProject.vue index 046bf4f909..6b8128c358 100644 --- a/frontend/src/business/components/settings/workspace/MsProject.vue +++ b/frontend/src/business/components/settings/workspace/MsProject.vue @@ -108,6 +108,10 @@ + + + + @@ -662,6 +666,14 @@ export default { return (user.email.indexOf(queryString.toLowerCase()) === 0 || user.id.indexOf(queryString.toLowerCase()) === 0); }; }, + chengeMockTcpSwitch(value){ + if(value && this.form.mockTcpPort === 0){ + this.result = this.$get('/project/genTcpMockPort/' + this.form.id, res => { + let port = res.data; + this.form.mockTcpPort = port; + }) + } + }, }, created() { document.addEventListener('keydown', this.handleEvent);