更新包管理

segment2.0
linrf 2 years ago
parent ab9ea38e0a
commit 6f889cc009

@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
/**
*
@ -37,6 +38,11 @@ public class SchCollectorConfigController implements SchCollectorConfigApi {
return iSchCollectorConfigService.findById(id);
}
@Override
public List<SchCollectorConfigVO> findAll() {
return iSchCollectorConfigService.findAll();
}
/**
*
*

@ -32,6 +32,9 @@ public interface SchCollectorConfigConvert {
@Mappings({})
SchCollectorConfigVO convertVO(SchCollectorConfig schCollectorConfig);
@Mappings({})
List<SchCollectorConfigVO> convertVO(List<SchCollectorConfig> schCollectorConfig);
@Mappings({})
PageResult<SchCollectorConfigVO> convertVO(PageResult<SchCollectorConfig> pageResult);

@ -7,6 +7,8 @@ import com.docus.server.dto.scheduling.management.schcollectorconfig.DeleteSchCo
import com.docus.server.dto.scheduling.management.schcollectorconfig.EditSchCollectorConfigDTO;
import com.docus.server.vo.scheduling.management.schcollectorconfig.SchCollectorConfigVO;
import java.util.List;
/**
*
*
@ -22,6 +24,8 @@ public interface ISchCollectorConfigService {
*/
SchCollectorConfigVO findById(String id);
List<SchCollectorConfigVO> findAll();
/**
*
*
@ -58,4 +62,6 @@ public interface ISchCollectorConfigService {
*
*/
void updateCollectorConfig();
}

@ -60,6 +60,11 @@ public class SchCollectorConfigServiceImpl implements ISchCollectorConfigService
return SchCollectorConfigConvert.INSTANCE.convertVO(iSchCollectorConfigDao.findById(id));
}
@Override
public List<SchCollectorConfigVO> findAll() {
return SchCollectorConfigConvert.INSTANCE.convertVO(iSchCollectorConfigDao.findAll());
}
/**
*
*

@ -1,26 +1,131 @@
package com.docus.server.common;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import com.docus.core.util.Func;
import com.docus.core.util.ListUtils;
import com.docus.core.util.StopWatch;
import com.docus.core.util.json.JSON;
import com.docus.server.api.scheduling.management.SchCollectorApi;
import com.docus.server.api.scheduling.management.SchCollectorConfigApi;
import com.docus.server.common.download.downLoader.HttpDownloader;
import com.docus.server.vo.scheduling.management.schcollector.LoadSchCollectorVO;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* load collector package and config
*/
@Component
@Slf4j
public class LoadPackageCommandLineRunner implements CommandLineRunner {
@Resource
private SchCollectorApi schCollectorApi;
@Resource
private SchCollectorConfigApi schCollectorConfigApi;
@Value("${docus.collector-package-download-savePath:H://packages/}")
private String saveCollectorPackagePath;
@Value("${docus.collector-package-download-url}")
private String collectorPackageDownloadUrl;
private final static Lock DOWNLOAD_LOCK = new ReentrantLock();
@Resource
private StringRedisTemplate template;
@Resource
private Environment env;
@Override
public void run(String... args) throws Exception {
loadPackages();
loadConfig();
}
private void loadConfig() {
schCollectorConfigApi.findAll();
}
private void loadPackages() throws Exception {
//先从redis获取key若没有则全部更新若有局部更新
String key = "docus:collectorsPackagesDownload:" + env.getProperty("server.port");
String value = template.opsForValue().get(key);
List<LoadSchCollectorVO> loadSchCollectorVOList = schCollectorApi.findAll();
System.out.println(loadSchCollectorVOList);
if (Func.isBlank(value)) {
updateAll(loadSchCollectorVOList);
} else {
updatePart(loadSchCollectorVOList, value);
}
//根据服务器端口区分,每次更新了哪些采集器版本
template.opsForValue().set(key, JSON.toJSON(loadSchCollectorVOList));
}
private void updatePart(List<LoadSchCollectorVO> loadSchCollectorVOList, String packageValue) throws Exception {
List<LoadSchCollectorVO> redisLoadSchCollectors = JSON.fromJSONWithGeneric(packageValue, new TypeReference<List<LoadSchCollectorVO>>() {
});
Map<Long, LoadSchCollectorVO> collectorVoMap = ListUtils.toMap(redisLoadSchCollectors, LoadSchCollectorVO::getCollectorId);
StopWatch watch = new StopWatch();
for (LoadSchCollectorVO loadSchCollectorVO : loadSchCollectorVOList) {
if (collectorVoMap.containsKey(loadSchCollectorVO.getCollectorId())) {
LoadSchCollectorVO redisLoadSchCollectorVO = collectorVoMap.get(loadSchCollectorVO.getCollectorId());
if (!redisLoadSchCollectorVO.getCollectorVersionId().equals(loadSchCollectorVO.getCollectorVersionId())) {
actionDownLoad(loadSchCollectorVO);
}
}
}
log.info("终端局部下载采集器更新包耗时:{}ms", watch.elapsedTime());
}
private void updateAll(List<LoadSchCollectorVO> loadSchCollectorVOList) throws Exception {
StopWatch watch = new StopWatch();
for (LoadSchCollectorVO loadSchCollectorVO : loadSchCollectorVOList) {
actionDownLoad(loadSchCollectorVO);
}
log.info("终端全量下载采集器更新包耗时:{}ms", watch.elapsedTime());
}
private void actionDownLoad(LoadSchCollectorVO loadSchCollectorVO) throws Exception {
DOWNLOAD_LOCK.lock();
try {
//下载更新包
HttpDownloader httpDownloader = new HttpDownloader(null);
String url = collectorPackageDownloadUrl + "/sch/file/download?filePath=" + loadSchCollectorVO.getFilePath();
String fileName = "collector.zip";
String savePath = saveCollectorPackagePath + loadSchCollectorVO.getCollectorId(); //部署路径
String procName = loadSchCollectorVO.getProcessName(); //进程名称
httpDownloader.downLoadFromUrl(url, fileName, savePath);
//解压更新压缩包
ZipUtil.unzip(savePath + "\\" + fileName, savePath + "\\collector");
//删除更新压缩包
FileUtil.del(savePath + "\\" + fileName);
} finally {
DOWNLOAD_LOCK.unlock();
}
}
}

@ -239,9 +239,10 @@ public class HttpDownloader extends Thread {
//文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
saveDir.mkdirs();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {

@ -1,12 +1,9 @@
package com.docus.server.common.utils;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@Component
public class StartUpExeUtils {
public static void main(String[] args) throws IOException {

@ -24,7 +24,7 @@ public class FileController {
HttpDownloader httpDownloader = new HttpDownloader(null);
String url = "http://192.168.16.110:9113/sch/file/download?filePath=collector_packages/20230718/91d930e6-0490-44e5-9756-caee3251d645/navicat.zip";
String fileName = "collector.zip";
String savePath = "H:\\test"; //部署路径
String savePath = "H:\\docus\\1"; //部署路径
String procName = "navicat.exe"; //进程名称
httpDownloader.downLoadFromUrl(url, fileName, savePath);

@ -17,6 +17,8 @@ import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* API
@ -39,6 +41,13 @@ public interface SchCollectorConfigApi {
@GetMapping("/find/{id}")
SchCollectorConfigVO findById(@PathVariable(value = "id") String id);
/**
*
*
* @return SchCollectorConfigVO
*/
List<SchCollectorConfigVO> findAll();
/**
*
*
@ -78,4 +87,5 @@ public interface SchCollectorConfigApi {
@ApiOperation("批量删除")
@DeleteMapping("/delete")
int delete(@RequestBody DeleteSchCollectorConfigDTO deleteSchCollectorConfigDTO);
}

Loading…
Cancel
Save