Commit 4fef44be authored by 赵啸非's avatar 赵啸非

添加php健康度检测

parent fe629bcf
......@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
......@@ -36,114 +37,11 @@ public class CheckProjectStatusTaskImpl implements ITaskExcuteService {
@Autowired
private SetupProjectService setupProjectService;
@Value("${project.nacosUrl}")
private String nacosUrl;
@Override
public void excuteTask(ITask task) throws AppException {
log.info("检测项目运行状态任务");
List<SetupProjectEntity> setupProjectEntities = setupProjectService.find(new SetupProjectEntity());
for (SetupProjectEntity setupProjectEntity : setupProjectEntities) {
if (ProjectTypeEnum.后端.getValue() == setupProjectEntity.getProjectType()) {
//todo http://192.168.0.250:8848/nacos/v1/ns/catalog/instances?message=true&serviceName=bill-manager&clusterName=DEFAULT&groupName=DEFAULT_GROUP&pageSize=10&pageNo=1&namespaceId=smart-gov
String serverUrl = nacosUrl + "/nacos/v1/ns/catalog/instances?message=true&serviceName=" + setupProjectEntity.getProjectCode() + "&clusterName=DEFAULT&groupName=DEFAULT_GROUP&pageSize=10&pageNo=1&namespaceId=smart-gov";
String resp = "";
try {
resp = HttpUtil.get(serverUrl);
NacosResponse nacosResponse = JSON.parseObject(resp, new TypeReference<NacosResponse>() {
});
int count = nacosResponse.getCount();
if (count > 0) {
ListItem item = nacosResponse.getList().get(0);
boolean healthy = item.isHealthy();
if (healthy) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.运行中.getValue());
} else {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.停止.getValue());
}
setupProjectEntity.setUpdateTime(new Date());
setupProjectService.update(setupProjectEntity);
}
log.info("服务实例详细查询url:{},返回:{},naocsResponse:{}", serverUrl, resp, JSON.toJSONString(nacosResponse));
} catch (JSONException e) {
log.info("json反序列化异常:{},返回:{}", e.getMessage(), resp);
if (setupProjectEntity.getProjectStatus() > ProjectStatusEnum.未部署.getValue()) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.停止.getValue());
setupProjectEntity.setUpdateTime(new Date());
setupProjectService.update(setupProjectEntity);
}
}catch (Exception e){
log.error("发生异常:{}",e.getMessage());
}
} else if (ProjectTypeEnum.前端.getValue() == setupProjectEntity.getProjectType()) {
//todo 前端 判断路径文件是否存在 如果存在 则代表运行
String projectPath = setupProjectEntity.getProjectPath();
String projectCode = setupProjectEntity.getProjectCode();
String destPath = projectPath + File.separator + projectCode + File.separator + "dist" + File.separator + "index.html";
File uiFile = new File(destPath);
if (uiFile.exists()) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.运行中.getValue());
setupProjectEntity.setUpdateTime(new Date());
setupProjectService.update(setupProjectEntity);
} else {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.未部署.getValue());
setupProjectEntity.setUpdateTime(new Date());
setupProjectService.update(setupProjectEntity);
}
log.info("前端资源路径:{},是否存在:{}", destPath, uiFile.exists());
} else if (ProjectTypeEnum.PHP后端.getValue() == setupProjectEntity.getProjectType()) {
//todo 前端 判断路径文件是否存在 如果存在 则代表运行
if (ProductDisEnum.排号系统PHP后端.getValue().equals(setupProjectEntity.getProjectCode())) {
String serverUrl = "http://127.0.0.1:11078/zwfw_api/api/upload/dictionary";
String resp = "";
try {
resp = HttpUtil.get(serverUrl);
JSON.parseObject(resp);
setupProjectEntity.setProjectStatus(ProjectStatusEnum.运行中.getValue());
setupProjectEntity.setUpdateTime(new Date());
setupProjectService.update(setupProjectEntity);
} catch (JSONException e) {
log.info("json反序列化异常:{},返回:{}", e.getMessage(), resp);
if (setupProjectEntity.getProjectStatus() > ProjectStatusEnum.未部署.getValue()) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.停止.getValue());
setupProjectEntity.setUpdateTime(new Date());
setupProjectService.update(setupProjectEntity);
}
}catch (Exception e){
log.error("发生异常:{}",e.getMessage());
}
}
if (ProductDisEnum.窗口服务行为监察PHP后端.getValue().equals(setupProjectEntity.getProjectCode())) {
String serverUrl = "http://127.0.0.1:11030/admin/waited/verify";
String resp = "";
try {
resp = HttpUtil.get(serverUrl);
JSON.parseObject(resp);
setupProjectEntity.setProjectStatus(ProjectStatusEnum.运行中.getValue());
setupProjectEntity.setUpdateTime(new Date());
setupProjectService.update(setupProjectEntity);
} catch (JSONException e) {
log.info("json反序列化异常:{},返回:{}", e.getMessage(), resp);
if (setupProjectEntity.getProjectStatus() > ProjectStatusEnum.未部署.getValue()) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.停止.getValue());
setupProjectEntity.setUpdateTime(new Date());
setupProjectService.update(setupProjectEntity);
}
}catch (Exception e){
log.error("发生异常:{}",e.getMessage());
}
}
} else {
}
}
setupProjectService.updateProjectsStatus();
log.info("检测项目运行状态任务完毕!");
}
......
......@@ -27,4 +27,10 @@ public interface SetupProjectService extends ICRUDService<SetupProjectEntity,Lon
Rest<String> unProjectZip(MultipartFile file, Context context);
/**
* 更新项目状态
* @return
*/
Rest<String> updateProjectsStatus();
}
\ No newline at end of file
......@@ -3,7 +3,11 @@ package com.mortals.xhx.module.setup.service.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.DbUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.system.SystemUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
......@@ -13,6 +17,8 @@ import com.mortals.xhx.common.code.ProductDisEnum;
import com.mortals.xhx.common.code.ProjectStatusEnum;
import com.mortals.xhx.common.code.ProjectTypeEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.ListItem;
import com.mortals.xhx.common.pdu.NacosResponse;
import com.mortals.xhx.common.utils.ZipUtils;
import com.mortals.xhx.module.setup.dao.SetupProjectDao;
import com.mortals.xhx.module.setup.mode.DbSetupEntity;
......@@ -38,6 +44,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
......@@ -60,6 +67,9 @@ public class SetupProjectServiceImpl extends AbstractCRUDServiceImpl<SetupProjec
@Value("${project.publishPath:'/home/publish'}")
private String publishPath;
@Value("${project.nacosUrl}")
private String nacosUrl;
@Override
public Rest<String> distribute(SetupProjectEntity setupProjectEntity, Context context) {
......@@ -166,7 +176,7 @@ public class SetupProjectServiceImpl extends AbstractCRUDServiceImpl<SetupProjec
} else if (ProjectTypeEnum.PHP后端.getValue() == setupProjectEntity.getProjectType()) {
//TODO: 解压php逻辑
String sourceDirPath = this.publishPath + "/temp/project/" + ProductDisEnum.getByValue(setupProjectEntity.getProjectCode()).getDesc() + "/";
String sourcePath = sourceDirPath+ ProductDisEnum.getByValue(setupProjectEntity.getProjectCode()).getValue() + ".zip";
String sourcePath = sourceDirPath + ProductDisEnum.getByValue(setupProjectEntity.getProjectCode()).getValue() + ".zip";
File file = new File(sourcePath);
log.info("文件路径:{},存在:{}", sourcePath, file.exists());
......@@ -182,10 +192,10 @@ public class SetupProjectServiceImpl extends AbstractCRUDServiceImpl<SetupProjec
//获取zip的编码
try {
ZipUtil.unzip(inputStream, destDir, Charset.defaultCharset());
}catch (IllegalArgumentException e){
log.error("zip文件编码异常,尝试使用GBK编码,异常:{}",e.getMessage());
} catch (IllegalArgumentException e) {
log.error("zip文件编码异常,尝试使用GBK编码,异常:{}", e.getMessage());
inputStream = FileUtil.getInputStream(file);
ZipUtil.unzip(inputStream, destDir, Charset.forName("GBK"));
ZipUtil.unzip(inputStream, destDir, Charset.forName("GBK"));
}
String publicPath = setupProjectEntity.getProjectPath() + ProductDisEnum.getByValue(setupProjectEntity.getProjectCode()).getValue();
......@@ -204,8 +214,8 @@ public class SetupProjectServiceImpl extends AbstractCRUDServiceImpl<SetupProjec
if (YesNoEnum.YES.getValue() != dbRest.getCode()) {
log.info("数据库初始化成功!");
}
}else{
log.info("数据库文件不存在!{}",sqlPath.getPath());
} else {
log.info("数据库文件不存在!{}", sqlPath.getPath());
}
setupProjectEntity.setProjectStatus(ProjectStatusEnum.已部署.getValue());
this.update(setupProjectEntity);
......@@ -215,7 +225,7 @@ public class SetupProjectServiceImpl extends AbstractCRUDServiceImpl<SetupProjec
//todo 执行部署脚本
//后端服务,创建service 启动服务
try {
Runtime.getRuntime().exec("chmod -R 777 "+destDir);
Runtime.getRuntime().exec("chmod -R 777 " + destDir);
} catch (IOException e) {
log.error(e.getMessage());
}
......@@ -460,6 +470,111 @@ public class SetupProjectServiceImpl extends AbstractCRUDServiceImpl<SetupProjec
return Rest.ok();
}
@Override
public Rest<String> updateProjectsStatus() {
log.info("检测项目状态!");
List<SetupProjectEntity> setupProjectEntities = this.find(new SetupProjectEntity());
for (SetupProjectEntity setupProjectEntity : setupProjectEntities) {
if (ProjectTypeEnum.后端.getValue() == setupProjectEntity.getProjectType()) {
//todo http://192.168.0.250:8848/nacos/v1/ns/catalog/instances?message=true&serviceName=bill-manager&clusterName=DEFAULT&groupName=DEFAULT_GROUP&pageSize=10&pageNo=1&namespaceId=smart-gov
String serverUrl = nacosUrl + "/nacos/v1/ns/catalog/instances?message=true&serviceName=" + setupProjectEntity.getProjectCode() + "&clusterName=DEFAULT&groupName=DEFAULT_GROUP&pageSize=10&pageNo=1&namespaceId=smart-gov";
String resp = "";
try {
resp = HttpUtil.get(serverUrl);
NacosResponse nacosResponse = JSON.parseObject(resp, new TypeReference<NacosResponse>() {
});
int count = nacosResponse.getCount();
if (count > 0) {
ListItem item = nacosResponse.getList().get(0);
boolean healthy = item.isHealthy();
if (healthy) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.运行中.getValue());
} else {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.停止.getValue());
}
setupProjectEntity.setUpdateTime(new Date());
this.update(setupProjectEntity);
}
log.info("服务实例详细查询url:{},返回:{},naocsResponse:{}", serverUrl, resp, JSON.toJSONString(nacosResponse));
} catch (JSONException e) {
log.info("json反序列化异常:{},返回:{}", e.getMessage(), resp);
if (setupProjectEntity.getProjectStatus() > ProjectStatusEnum.未部署.getValue()) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.停止.getValue());
setupProjectEntity.setUpdateTime(new Date());
this.update(setupProjectEntity);
}
} catch (Exception e) {
log.error("发生异常:{}", e.getMessage());
}
} else if (ProjectTypeEnum.前端.getValue() == setupProjectEntity.getProjectType()) {
//todo 前端 判断路径文件是否存在 如果存在 则代表运行
String projectPath = setupProjectEntity.getProjectPath();
String projectCode = setupProjectEntity.getProjectCode();
String destPath = projectPath + File.separator + projectCode + File.separator + "dist" + File.separator + "index.html";
File uiFile = new File(destPath);
if (uiFile.exists()) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.运行中.getValue());
setupProjectEntity.setUpdateTime(new Date());
this.update(setupProjectEntity);
} else {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.未部署.getValue());
setupProjectEntity.setUpdateTime(new Date());
this.update(setupProjectEntity);
}
log.info("前端资源路径:{},是否存在:{}", destPath, uiFile.exists());
} else if (ProjectTypeEnum.PHP后端.getValue() == setupProjectEntity.getProjectType()) {
//todo 前端 判断路径文件是否存在 如果存在 则代表运行
if (ProductDisEnum.排号系统PHP后端.getValue().equals(setupProjectEntity.getProjectCode())) {
String serverUrl = "http://127.0.0.1:11078/zwfw_api/api/upload/dictionary";
String resp = "";
try {
resp = HttpUtil.get(serverUrl);
JSON.parseObject(resp);
setupProjectEntity.setProjectStatus(ProjectStatusEnum.运行中.getValue());
setupProjectEntity.setUpdateTime(new Date());
this.update(setupProjectEntity);
} catch (JSONException e) {
log.info("json反序列化异常:{},返回:{}", e.getMessage(), resp);
if (setupProjectEntity.getProjectStatus() > ProjectStatusEnum.未部署.getValue()) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.停止.getValue());
setupProjectEntity.setUpdateTime(new Date());
this.update(setupProjectEntity);
}
} catch (Exception e) {
log.error("发生异常:{}", e.getMessage());
}
}
if (ProductDisEnum.窗口服务行为监察PHP后端.getValue().equals(setupProjectEntity.getProjectCode())) {
String serverUrl = "http://127.0.0.1:11030/admin/waited/verify";
String resp = "";
try {
resp = HttpUtil.get(serverUrl);
JSON.parseObject(resp);
setupProjectEntity.setProjectStatus(ProjectStatusEnum.运行中.getValue());
setupProjectEntity.setUpdateTime(new Date());
this.update(setupProjectEntity);
} catch (JSONException e) {
log.info("json反序列化异常:{},返回:{}", e.getMessage(), resp);
if (setupProjectEntity.getProjectStatus() > ProjectStatusEnum.未部署.getValue()) {
setupProjectEntity.setProjectStatus(ProjectStatusEnum.停止.getValue());
setupProjectEntity.setUpdateTime(new Date());
this.update(setupProjectEntity);
}
} catch (Exception e) {
log.error("发生异常:{}", e.getMessage());
}
}
} else {
}
}
return Rest.ok();
}
public static void main(String[] args) {
System.out.println(FileUtil.getSuffix("test.tar.gz"));
......
......@@ -514,6 +514,9 @@ public class SetupProjectController extends BaseCRUDJsonBodyMappingController<Se
//初始化项目
SetupProjectEntity setupProjectEntity = new SetupProjectEntity();
this.service.distributeInit(setupProjectEntity, getContext());
this.service.updateProjectsStatus();
recordSysLog(request, busiDesc + " 【成功】");
} catch (Exception e) {
code = VALUE_RESULT_FAILURE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment