|
|
|
@@ -9,8 +9,12 @@ import com.ruoyi.platform.mapper.ServiceDao; |
|
|
|
import com.ruoyi.platform.service.ServiceService; |
|
|
|
import com.ruoyi.platform.utils.HttpUtils; |
|
|
|
import com.ruoyi.platform.utils.JacksonUtil; |
|
|
|
import com.ruoyi.platform.vo.serviceVos.ServiceCodeConfigVo; |
|
|
|
import com.ruoyi.platform.vo.serviceVos.ServiceModelVo; |
|
|
|
import com.ruoyi.platform.vo.serviceVos.ServiceVersionVo; |
|
|
|
import com.ruoyi.system.api.model.LoginUser; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.data.domain.Page; |
|
|
|
import org.springframework.data.domain.PageImpl; |
|
|
|
@@ -18,6 +22,7 @@ import org.springframework.data.domain.PageRequest; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
@@ -43,7 +48,7 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Page<ServiceVersion> queryByPageServiceVersion(ServiceVersion serviceVersion, int page, int size) { |
|
|
|
public Page<ServiceVersionVo> queryByPageServiceVersion(ServiceVersion serviceVersion, int page, int size) { |
|
|
|
PageRequest pageRequest; |
|
|
|
if (StringUtils.isNotEmpty(serviceVersion.getRunState())) { |
|
|
|
pageRequest = PageRequest.of(page, Integer.MAX_VALUE); |
|
|
|
@@ -51,13 +56,22 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
|
|
|
|
List<String> deploymentNames = serviceVersions.stream().map(ServiceVersion::getDeploymentName).collect(Collectors.toList()); |
|
|
|
Map<String, String> runStates = getRunState(deploymentNames); |
|
|
|
updateRunState(runStates); |
|
|
|
if (runStates != null) { |
|
|
|
updateRunState(runStates, serviceVersions); |
|
|
|
} |
|
|
|
|
|
|
|
List<ServiceVersion> result = serviceVersions.stream().filter(serviceVersion1 -> { |
|
|
|
String runState = runStates.get(serviceVersion1.getDeploymentName()); |
|
|
|
return serviceVersion1.getRunState().equals(runState); |
|
|
|
List<ServiceVersion> serviceVersionList = serviceVersions.stream().filter(serviceVersion1 -> { |
|
|
|
String runState; |
|
|
|
if (runStates != null) { |
|
|
|
runState = runStates.get(serviceVersion1.getDeploymentName()); |
|
|
|
} else { |
|
|
|
runState = serviceVersion1.getRunState(); |
|
|
|
} |
|
|
|
return serviceVersion.getRunState().equals(runState); |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
|
|
List<ServiceVersionVo> result = getServiceVersionVoList(serviceVersionList); |
|
|
|
|
|
|
|
PageRequest.of(page, size); |
|
|
|
return new PageImpl<>(result, pageRequest, result.size()); |
|
|
|
} else { |
|
|
|
@@ -67,33 +81,22 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
|
|
|
|
List<String> deploymentNames = serviceVersions.stream().map(ServiceVersion::getDeploymentName).collect(Collectors.toList()); |
|
|
|
Map<String, String> runStates = getRunState(deploymentNames); |
|
|
|
updateRunState(runStates); |
|
|
|
|
|
|
|
return new PageImpl<>(serviceVersions, pageRequest, total); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Map<String, String> getRunState(List<String> deploymentNames) { |
|
|
|
HashMap<String, Object> paramMap = new HashMap<>(); |
|
|
|
paramMap.put("deployment_names", deploymentNames); |
|
|
|
String req = HttpUtils.sendPost(argoUrl + modelService + "/getStatus", JSON.toJSONString(paramMap)); |
|
|
|
if (StringUtils.isNotEmpty(req)) { |
|
|
|
Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req); |
|
|
|
Map<String, Map<String, String>> data = (Map<String, Map<String, String>>) reqMap.get("data"); |
|
|
|
return data.get("status"); |
|
|
|
} else { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
if (runStates != null) { |
|
|
|
updateRunState(runStates, serviceVersions); |
|
|
|
} |
|
|
|
List<ServiceVersionVo> result = getServiceVersionVoList(serviceVersions); |
|
|
|
|
|
|
|
void updateRunState(Map<String, String> runStates) { |
|
|
|
for (Map.Entry<String, String> entry : runStates.entrySet()) { |
|
|
|
serviceDao.updateRunState(entry.getKey(), entry.getValue()); |
|
|
|
return new PageImpl<>(result, pageRequest, total); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public com.ruoyi.platform.domain.Service addService(com.ruoyi.platform.domain.Service service) { |
|
|
|
com.ruoyi.platform.domain.Service serviceByName = serviceDao.getServiceByName(service.getServiceName()); |
|
|
|
if (serviceByName != null) { |
|
|
|
throw new RuntimeException("服务名称已存在,无法新增"); |
|
|
|
} |
|
|
|
|
|
|
|
LoginUser loginUser = SecurityUtils.getLoginUser(); |
|
|
|
service.setCreateBy(loginUser.getUsername()); |
|
|
|
service.setUpdateBy(loginUser.getUsername()); |
|
|
|
@@ -102,11 +105,18 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ServiceVersion addServiceVersion(ServiceVersion serviceVersion) { |
|
|
|
LoginUser loginUser = SecurityUtils.getLoginUser(); |
|
|
|
serviceVersion.setCreateBy(loginUser.getUsername()); |
|
|
|
serviceVersion.setUpdateBy(loginUser.getUsername()); |
|
|
|
public ServiceVersion addServiceVersion(ServiceVersionVo serviceVersionVo) { |
|
|
|
ServiceVersion svByVersion = serviceDao.getSvByVersion(serviceVersionVo.getVersion(), serviceVersionVo.getServiceId()); |
|
|
|
if (svByVersion != null) { |
|
|
|
throw new RuntimeException("服务版本已存在,无法新增"); |
|
|
|
} |
|
|
|
|
|
|
|
ServiceVersion serviceVersion = getServiceVersion(serviceVersionVo); |
|
|
|
// LoginUser loginUser = SecurityUtils.getLoginUser(); |
|
|
|
// serviceVersion.setCreateBy(loginUser.getUsername()); |
|
|
|
// serviceVersion.setUpdateBy(loginUser.getUsername()); |
|
|
|
serviceDao.insertServiceVersion(serviceVersion); |
|
|
|
runServiceVersion(serviceVersion.getId()); |
|
|
|
return serviceVersion; |
|
|
|
} |
|
|
|
|
|
|
|
@@ -119,47 +129,45 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ServiceVersion editServiceVersion(ServiceVersion serviceVersion) { |
|
|
|
public String editServiceVersion(ServiceVersionVo serviceVersionVo) { |
|
|
|
ServiceVersion serviceVersion = getServiceVersion(serviceVersionVo); |
|
|
|
|
|
|
|
LoginUser loginUser = SecurityUtils.getLoginUser(); |
|
|
|
serviceVersion.setUpdateBy(loginUser.getUsername()); |
|
|
|
serviceDao.updateServiceVersion(serviceVersion); |
|
|
|
return serviceDao.getServiceVersionById(serviceVersion.getId()); |
|
|
|
return "修改成功"; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public com.ruoyi.platform.domain.Service getService(Long id) { |
|
|
|
com.ruoyi.platform.domain.Service service = serviceDao.getServiceById(id); |
|
|
|
|
|
|
|
ServiceVersion serviceVersion = new ServiceVersion(); |
|
|
|
serviceVersion.setServiceId(id); |
|
|
|
service.setVersionCount(serviceDao.countServiceVersion(serviceVersion)); |
|
|
|
return serviceDao.getServiceById(id); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ServiceVersion getServiceVersion(Long id) { |
|
|
|
return serviceDao.getServiceVersionById(id); |
|
|
|
public ServiceVersionVo getServiceVersion(Long id) { |
|
|
|
ServiceVersion serviceVersion = serviceDao.getServiceVersionById(id); |
|
|
|
return getServiceVersionVo(serviceVersion); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String deleteService(Long id) { |
|
|
|
com.ruoyi.platform.domain.Service service = serviceDao.getServiceById(id); |
|
|
|
if (service == null) { |
|
|
|
return "服务不存在"; |
|
|
|
throw new RuntimeException("服务不存在"); |
|
|
|
} |
|
|
|
|
|
|
|
LoginUser loginUser = SecurityUtils.getLoginUser(); |
|
|
|
String username = loginUser.getUsername(); |
|
|
|
String createBy = service.getCreateBy(); |
|
|
|
if (!(StringUtils.equals(username, "admin") || StringUtils.equals(username, createBy))) { |
|
|
|
return "无权限删除该服务"; |
|
|
|
throw new RuntimeException("无权限删除该服务"); |
|
|
|
} |
|
|
|
|
|
|
|
ServiceVersion serviceVersion = new ServiceVersion(); |
|
|
|
serviceVersion.setServiceId(id); |
|
|
|
long versionCount = serviceDao.countServiceVersion(serviceVersion); |
|
|
|
if (versionCount != 0) { |
|
|
|
return "该服务下还有版本,不能删除"; |
|
|
|
throw new RuntimeException("该服务下还有版本,不能删除"); |
|
|
|
} |
|
|
|
|
|
|
|
service.setState(Constant.State_invalid); |
|
|
|
@@ -176,6 +184,7 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
@Override |
|
|
|
public String runServiceVersion(Long id) { |
|
|
|
ServiceVersion serviceVersion = serviceDao.getServiceVersionById(id); |
|
|
|
ServiceVersionVo serviceVersionVo = getServiceVersionVo(serviceVersion); |
|
|
|
com.ruoyi.platform.domain.Service service = serviceDao.getServiceById(serviceVersion.getServiceId()); |
|
|
|
HashMap<String, Object> paramMap = new HashMap<>(); |
|
|
|
paramMap.put("service_name", service.getServiceName()); |
|
|
|
@@ -193,11 +202,11 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req); |
|
|
|
if ((Integer) reqMap.get("code") == 200) { |
|
|
|
Map<String, String> data = (Map<String, String>) reqMap.get("data"); |
|
|
|
serviceVersion.setUrl(data.get("url")); |
|
|
|
serviceVersion.setDeploymentName(data.get("deployment_name")); |
|
|
|
serviceVersion.setSvcName(data.get("svc_name")); |
|
|
|
serviceVersion.setRunState(Constant.Init); |
|
|
|
editServiceVersion(serviceVersion); |
|
|
|
serviceVersionVo.setUrl(data.get("url")); |
|
|
|
serviceVersionVo.setDeploymentName(data.get("deployment_name")); |
|
|
|
serviceVersionVo.setSvcName(data.get("svc_name")); |
|
|
|
serviceVersionVo.setRunState(Constant.Init); |
|
|
|
editServiceVersion(serviceVersionVo); |
|
|
|
return "启动成功"; |
|
|
|
} else { |
|
|
|
throw new RuntimeException("启动失败"); |
|
|
|
@@ -210,12 +219,13 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
@Override |
|
|
|
public String stopServiceVersion(Long id) { |
|
|
|
ServiceVersion serviceVersion = serviceDao.getServiceVersionById(id); |
|
|
|
ServiceVersionVo serviceVersionVo = getServiceVersionVo(serviceVersion); |
|
|
|
HashMap<String, Object> paramMap = new HashMap<>(); |
|
|
|
paramMap.put("deployment_name", serviceVersion.getDeploymentName()); |
|
|
|
String req = HttpUtils.sendPost(argoUrl + modelService + "/stop", JSON.toJSONString(paramMap)); |
|
|
|
if (StringUtils.isNotEmpty(req)) { |
|
|
|
serviceVersion.setRunState(Constant.Stopped); |
|
|
|
editServiceVersion(serviceVersion); |
|
|
|
serviceVersionVo.setRunState(Constant.Stopped); |
|
|
|
editServiceVersion(serviceVersionVo); |
|
|
|
return "停止成功"; |
|
|
|
} else { |
|
|
|
throw new RuntimeException("停止失败"); |
|
|
|
@@ -223,7 +233,7 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ServiceVersion updateServiceVersion(ServiceVersion serviceVersion) { |
|
|
|
public String updateServiceVersion(ServiceVersion serviceVersion) { |
|
|
|
HashMap<String, Object> paramMap = new HashMap<>(); |
|
|
|
paramMap.put("deployment_name", serviceVersion.getDeploymentName()); |
|
|
|
HashMap<String, Object> updateMap = new HashMap<>(); |
|
|
|
@@ -232,7 +242,8 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
paramMap.put("update_model", JSON.toJSONString(updateMap)); |
|
|
|
String req = HttpUtils.sendPost(argoUrl + modelService + "/update", JSON.toJSONString(paramMap)); |
|
|
|
if (StringUtils.isNotEmpty(req)) { |
|
|
|
return editServiceVersion(serviceVersion); |
|
|
|
ServiceVersionVo serviceVersionVo = getServiceVersionVo(serviceVersion); |
|
|
|
return editServiceVersion(serviceVersionVo); |
|
|
|
} else { |
|
|
|
throw new RuntimeException("更新失败"); |
|
|
|
} |
|
|
|
@@ -275,4 +286,58 @@ public class ServiceServiceImpl implements ServiceService { |
|
|
|
return serviceDao.getServiceVersionList(id); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ServiceVersion getServiceVersion(ServiceVersionVo serviceVersionVo) { |
|
|
|
ServiceVersion serviceVersion = new ServiceVersion(); |
|
|
|
BeanUtils.copyProperties(serviceVersionVo, serviceVersion); |
|
|
|
serviceVersion.setModel(JSON.toJSONString(serviceVersionVo.getModel())); |
|
|
|
serviceVersion.setCodeConfig(JSON.toJSONString(serviceVersionVo.getCodeConfig())); |
|
|
|
|
|
|
|
return serviceVersion; |
|
|
|
} |
|
|
|
|
|
|
|
List<ServiceVersionVo> getServiceVersionVoList(List<ServiceVersion> serviceVersionList) { |
|
|
|
List<ServiceVersionVo> result = new ArrayList<>(); |
|
|
|
|
|
|
|
for (ServiceVersion sv : serviceVersionList) { |
|
|
|
ServiceVersionVo serviceVersionVo = getServiceVersionVo(sv); |
|
|
|
result.add(serviceVersionVo); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
ServiceVersionVo getServiceVersionVo(ServiceVersion sv) { |
|
|
|
ServiceVersionVo serviceVersionVo = new ServiceVersionVo(); |
|
|
|
BeanUtils.copyProperties(sv, serviceVersionVo); |
|
|
|
ServiceModelVo serviceModelVo = JSON.parseObject(sv.getModel(), ServiceModelVo.class); |
|
|
|
serviceVersionVo.setModel(serviceModelVo); |
|
|
|
|
|
|
|
ServiceCodeConfigVo serviceCodeConfigVo = JSON.parseObject(sv.getCodeConfig(), ServiceCodeConfigVo.class); |
|
|
|
serviceVersionVo.setCodeConfig(serviceCodeConfigVo); |
|
|
|
return serviceVersionVo; |
|
|
|
} |
|
|
|
|
|
|
|
Map<String, String> getRunState(List<String> deploymentNames) { |
|
|
|
HashMap<String, Object> paramMap = new HashMap<>(); |
|
|
|
paramMap.put("deployment_names", deploymentNames); |
|
|
|
String req = HttpUtils.sendPost(argoUrl + modelService + "/getStatus", JSON.toJSONString(paramMap)); |
|
|
|
if (StringUtils.isNotEmpty(req)) { |
|
|
|
Map<String, Object> reqMap = JacksonUtil.parseJSONStr2Map(req); |
|
|
|
Map<String, Map<String, String>> data = (Map<String, Map<String, String>>) reqMap.get("data"); |
|
|
|
if (data != null) { |
|
|
|
return data.get("status"); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} else { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void updateRunState(Map<String, String> runStates, List<ServiceVersion> serviceVersionList) { |
|
|
|
for (ServiceVersion sv : serviceVersionList) { |
|
|
|
String runState = runStates.get(sv.getDeploymentName()); |
|
|
|
sv.setRunState(runState); |
|
|
|
serviceDao.updateRunState(sv.getDeploymentName(), runState); |
|
|
|
} |
|
|
|
} |
|
|
|
} |