fix(项目管理): 优化项目管理TCP端口设置

优化项目管理TCP端口设置:启用时后台查询可用端口
This commit is contained in:
song-tianyang 2021-08-19 17:40:38 +08:00 committed by 刘瑞斌
parent e9f312a1a0
commit fae8ff5f8c
9 changed files with 135 additions and 6 deletions

View File

@ -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();
}
}

View File

@ -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();

View File

@ -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();

View File

@ -30,4 +30,6 @@ public interface ExtProjectMapper {
Map<String, Project> queryNameByIds(@Param("ids") List<String> ids);
Organization getOrganizationByProjectId(@Param("projectId")String projectId);
List<Integer> selectTcpPorts();
}

View File

@ -158,4 +158,11 @@
JOIN project ON workspace.id = workspace_id
WHERE project.id = #{projectId, jdbcType=VARCHAR}
</select>
<select id="selectTcpPorts" resultType="java.lang.Integer">
SELECT mock_tcp_port
FROM project
WHERE mock_tcp_port is not null and mock_tcp_port != 0
</select>
</mapper>

View File

@ -136,4 +136,9 @@ public class ProjectController {
public Collection<String> getOwnerProjectIds() {
return checkPermissionService.getUserRelatedProjectIds();
}
@GetMapping("/genTcpMockPort/{id}")
public String genTcpMockPort(@PathVariable String id){
return projectService.genTcpMockPort(id);
}
}

View File

@ -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<Project> projectList = projectMapper.selectByExample(example);
List<Integer> 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<Integer> portInRange = new ArrayList<>();
List<Integer> 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);
}
}

View File

@ -97,7 +97,7 @@
<el-form-item :label-width="labelWidth" label="TCP Mock Port">
<el-input-number v-model="form.mockTcpPort" :controls="false" style="width: 30%;margin-right: 30px"></el-input-number>
<el-switch v-model="form.isMockTcpOpen"></el-switch>
<el-switch v-model="form.isMockTcpOpen" @change="chengeMockTcpSwitch"></el-switch>
</el-form-item>
<el-form-item :label-width="labelWidth" :label="$t('commons.description')" prop="description">
@ -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);
}
},
};
</script>

View File

@ -108,6 +108,10 @@
<template-select :data="form" scene="ISSUE" prop="issueTemplateId" ref="issueTemplate"/>
</el-form-item>
<el-form-item :label-width="labelWidth" label="TCP Mock Port">
<el-input-number v-model="form.mockTcpPort" :controls="false" style="width: 30%;margin-right: 30px"></el-input-number>
<el-switch v-model="form.isMockTcpOpen" @change="chengeMockTcpSwitch"></el-switch>
</el-form-item>
<el-form-item :label-width="labelWidth" :label="$t('commons.description')" prop="description">
<el-input :autosize="{ minRows: 2, maxRows: 4}" type="textarea" v-model="form.description"></el-input>
@ -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);