提交 6feb12f1 authored 作者: Matrix's avatar Matrix

删掉了不必要的文件,修复了BUG

上级 4ac2a6b6
流水线 #206 已失败 于阶段
package com.zjty.fp;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
package com.zjty.fp.api.misc.entity;
/**
* ImportAds.
*
* @author Matrix <xhyrzldf@gmail.com>
* @since 2020/7/29 at 6:31 下午
*/
public class ImportAds {
public static final String IMPORT_URL = "/Users/matrix/code/test/";
}
...@@ -9,12 +9,18 @@ package com.zjty.fp.api.misc.entity; ...@@ -9,12 +9,18 @@ package com.zjty.fp.api.misc.entity;
public class PsspCount { public class PsspCount {
/** /**
* 代表下一个文件应当用的后缀号 * 代表下一个报警文件应当用的后缀号
*/ */
public static int count = -1; public static int alert_count = -1;
/**
* 代表下一个网站文件应当用的后缀号
*/
public static int website_count = -1;
public static final String COUNT_PATH = "files/pssp/count"; public static final String COUNT_PATH = "files/pssp/count";
public static final String COUNT_ADDRESS_ALERT = "files/pssp/count/alert.txt";
public static final String COUNT_ADDRESS = "files/pssp/count/count.txt"; public static final String COUNT_ADDRESS_WEBSITE = "files/pssp/count/website.txt";
} }
...@@ -36,43 +36,6 @@ public class FileCreator { ...@@ -36,43 +36,6 @@ public class FileCreator {
return new File(pathname + filename); return new File(pathname + filename);
} }
public File createFileAndZip(String subName, String catalog, String data) {
String zipData = DeflaterUtils.zipString(data);
String pathname = "files/" + subName + "/" + catalog + "/";
String filename = getPsspFileName(subName, catalog);
Path path = Paths.get(pathname + filename);
boolean make = createFilePath(new File(pathname));
if (make) {
try {
Files.write(path, zipData.getBytes());
} catch (IOException e) {
log.info("生成文件时出现异常:" + e);
}
}
//生成完毕后需要更新一下count的值
count += 1;
//写入到count文件中去
Path countPath = Paths.get(COUNT_ADDRESS);
//先删除原有的count文件,再写入现有的
try {
Files.deleteIfExists(countPath);
} catch (IOException e) {
log.warn("删除原count文件失败!原因:{}", e.toString());
}
boolean countMake = createFilePath(new File(COUNT_PATH));
if (countMake) {
try {
Files.write(Paths.get(COUNT_ADDRESS), String.valueOf(count).getBytes());
log.info("[pssp]磁盘count值更新成功:count = {}", count);
} catch (IOException e) {
log.info("[pssp]生成文件时出现异常:" + e);
}
}
return new File(pathname + filename);
}
public File createFileNoTime(String subName, String catalog, String data) { public File createFileNoTime(String subName, String catalog, String data) {
String pathname = "files/" + subName + "/" + catalog + "/"; String pathname = "files/" + subName + "/" + catalog + "/";
...@@ -183,35 +146,6 @@ public class FileCreator { ...@@ -183,35 +146,6 @@ public class FileCreator {
return subName + "-" + catalog + ".txt"; return subName + "-" + catalog + ".txt";
} }
private String getPsspFileName(String subName, String catalog) {
//count 先从内存里取读 内存里读不到从磁盘读
int count = PsspCount.count;
//如果count为初始值-1,则代表还没有从文件中读取之前的记录
if (count == -1) {
//读取文件获得文件编号后缀值
Path path = Paths.get(COUNT_ADDRESS);
List<String> data = Lists.newArrayList();
try {
data = Files.readAllLines(path);
} catch (IOException e) {
log.info("读取文件时出现异常:" + e);
}
count = data.get(0) == null ? 0 : Integer.parseInt(data.get(0));
}
if (count <= 0) count = 0;
String psspFileName = subName + "-" + catalog + count + ".txt";
log.info("成功生成文件名:{}", psspFileName);
return psspFileName;
}
/** /**
* 生成指定的文件夹路径 * 生成指定的文件夹路径
......
package com.zjty.fp.api.misc.utils; package com.zjty.fp.api.misc.utils;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.zjty.fp.api.misc.entity.ImportAds;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -14,6 +15,8 @@ import java.text.SimpleDateFormat; ...@@ -14,6 +15,8 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import static com.zjty.fp.api.misc.entity.PsspCount.*;
/** /**
* <p>Description : vompin * <p>Description : vompin
* <p>Date : 2018/9/29 15:44 * <p>Date : 2018/9/29 15:44
...@@ -28,7 +31,7 @@ public class FileReader { ...@@ -28,7 +31,7 @@ public class FileReader {
public List<String> readFileData(String subName, String catalog) { public List<String> readFileData(String subName, String catalog) {
List<String> data = Lists.newArrayList(); List<String> data = Lists.newArrayList();
String localFilePath = "file/" + subName + "/" + catalog + "/"; String localFilePath = ImportAds.IMPORT_URL + "files/" + subName + "/" + catalog + "/";
String fileName = getFileNameByDate(subName, catalog); String fileName = getFileNameByDate(subName, catalog);
File file = new File(localFilePath + fileName); File file = new File(localFilePath + fileName);
Path path = Paths.get(localFilePath + fileName); Path path = Paths.get(localFilePath + fileName);
...@@ -40,6 +43,126 @@ public class FileReader { ...@@ -40,6 +43,126 @@ public class FileReader {
return data; return data;
} }
public String readFileAndUnzip(String countAddress, String subName, String catalog) {
String data = "";
String localFilePath = ImportAds.IMPORT_URL + "files/" + subName + "/" + catalog + "/";
String fileName = getPsspFileName(countAddress, subName, catalog);
File file = new File(localFilePath + fileName);
Path path = Paths.get(localFilePath + fileName);
try {
data = new String(Files.readAllBytes(path));
log.info("文件读取成功,长度为 : {}", data.length());
//读取完之后unzip一下
String unzippedString = DeflaterUtils.unzipString(data);
//更新count
updateCount(countAddress);
return unzippedString;
} catch (IOException e) {
log.info("[pssp] 读取文件时出现异常,准备核对Count值采取策略:" + e);
//读取全局的count值,如果全局的count值大于当前count值,则继续增加count值,否则count值不变
int globalCount = readCount(subName, countAddress);
if (countAddress.equals(COUNT_ADDRESS_ALERT)) {
if (globalCount >= alert_count) {
log.info("[pssp] 全局alertCount值为{},当前alertCount值为{},采取策略继续增加当前Count值", globalCount, alert_count);
alert_count++;
} else {
log.info("[pssp] 全局alertCount值为{},当前alertCount值为{},采取策略当前Count值保持不变", globalCount, alert_count);
}
}
if (countAddress.equals(COUNT_ADDRESS_WEBSITE)) {
if (globalCount >= website_count) {
log.info("全局websiteCount值为{},当前websiteCount值为{},采取策略继续增加当前Count值", globalCount, website_count);
website_count++;
} else {
log.info("全局websiteCount值为{},当前websiteCount值为{},采取策略当前Count值保持不变", globalCount, website_count);
}
}
return "";
}
}
/**
* 读取全局Count值
*
* @return count值
*/
private int readCount(String subName, String countAddress) {
String count = "";
String localFilePath = ImportAds.IMPORT_URL + "files/" + subName + "/count/";
String fileName = "";
if (countAddress.equals(COUNT_ADDRESS_ALERT)) {
fileName = "alert.txt";
} else if (countAddress.equals(COUNT_ADDRESS_WEBSITE)) {
fileName = "website.txt";
}
String countPath = localFilePath + fileName;
Path path = Paths.get(countPath);
try {
count = new String(Files.readAllBytes(path));
} catch (IOException e) {
log.info("读取全局Count文件时出现异常:" + e);
return 0;
}
return Integer.parseInt(count);
}
public void deleteFile(String countAddress, String subName, String catalog) {
String localFilePath = "files/" + subName + "/" + catalog + "/";
String fileName = getPsspFileName(countAddress, subName, catalog);
Path path = Paths.get(localFilePath + fileName);
try {
log.info("[pssp] 即将删除 {} 文件", fileName);
Files.deleteIfExists(path);
log.info("[pssp] 删除 {} 文件成功", fileName);
} catch (IOException e) {
log.warn("[pssp] 删除失败, 原因 : {}", e.toString());
}
}
private String getPsspFileName(String countAddress, String subName, String catalog) {
//count 先从内存里取读 内存里读不到从磁盘读
int count = -1;
if (countAddress.equals(COUNT_ADDRESS_ALERT)) {
count = alert_count;
} else if (countAddress.equals(COUNT_ADDRESS_WEBSITE)) {
count = website_count;
}
//如果count为初始值-1,则代表还没有从文件中读取之前的记录
if (count == -1) {
//读取文件获得文件编号后缀值
Path path = Paths.get(countAddress);
List<String> data = Lists.newArrayList();
try {
data = Files.readAllLines(path);
} catch (IOException e) {
log.info("[pssp]读取文件时出现异常:" + e);
}
count = data.get(0) == null ? 0 : Integer.parseInt(data.get(0));
}
if (count <= 0) {
count = 0;
}
String psspFileName = subName + "-" + catalog + count + ".txt";
log.info("[pssp]成功生成文件名:{}", psspFileName);
return psspFileName;
}
/** /**
* 根据日期生成文件名 * 根据日期生成文件名
* 以天为单位 * 以天为单位
...@@ -50,6 +173,42 @@ public class FileReader { ...@@ -50,6 +173,42 @@ public class FileReader {
return subName + "-" + catalog + "-" + sdf.format(new Date()) + ".txt"; return subName + "-" + catalog + "-" + sdf.format(new Date()) + ".txt";
} }
/**
* 更新Count值,具体为Count+1
*/
private void updateCount(String countAddress) {
if (countAddress.equals(COUNT_ADDRESS_ALERT)) {
alert_count += 1;
} else if (countAddress.equals(COUNT_ADDRESS_WEBSITE)) {
website_count += 1;
}
//写入到count文件中去
Path countPath = Paths.get(countAddress);
//先删除原有的count文件,再写入现有的
try {
Files.deleteIfExists(countPath);
} catch (IOException e) {
log.warn("删除原count文件失败!原因:{}", e.toString());
}
boolean countMake = createFilePath(new File(COUNT_PATH));
if (countMake) {
try {
if (countAddress.equals(COUNT_ADDRESS_ALERT)) {
Files.write(Paths.get(countAddress), String.valueOf(alert_count).getBytes());
log.info("[pssp]磁盘count_alert 值更新成功:count = {}", alert_count);
} else if (countAddress.equals(COUNT_ADDRESS_WEBSITE)) {
Files.write(Paths.get(countAddress), String.valueOf(website_count).getBytes());
log.info("[pssp]磁盘count_website 值更新成功:count = {}", website_count);
}
} catch (IOException e) {
log.info("[pssp]生成文件时出现异常:" + e);
}
}
}
/** /**
* 生成指定的文件夹路径 * 生成指定的文件夹路径
* 若文件夹不存在则创建 * 若文件夹不存在则创建
......
package com.zjty.fp.api.pssp.base.beans; package com.zjty.fp.api.pssp.base.beans;
import com.google.common.collect.Lists;
import com.zjty.fp.api.pssp.base.constant.DictConst; import com.zjty.fp.api.pssp.base.constant.DictConst;
import com.zjty.fp.api.pssp.subject.entity.location.Region; import com.zjty.fp.api.pssp.subject.entity.location.Region;
import com.zjty.fp.api.pssp.subject.entity.location.Website; import com.zjty.fp.api.pssp.subject.entity.location.Website;
...@@ -12,8 +13,16 @@ import org.springframework.context.annotation.Bean; ...@@ -12,8 +13,16 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map; import java.util.Map;
import static com.zjty.fp.api.misc.entity.PsspCount.*;
/** /**
* PsspInitialRunner 初始化Runner程序 * PsspInitialRunner 初始化Runner程序
* *
...@@ -24,33 +33,63 @@ import java.util.Map; ...@@ -24,33 +33,63 @@ import java.util.Map;
@Configuration @Configuration
public class PsspInitialRunner { public class PsspInitialRunner {
@Autowired
private WebsiteService websiteService;
@Autowired
private RegionService regionService;
@Order(100) @Order(100)
@Bean @Bean
public CommandLineRunner initializeDictMap() { public CommandLineRunner initializeDictMap() {
return args -> { return args -> {
log.info("[pssp] 正在尝试初始化 pssp 网站Map字段表"); initCount("COUNT_ALERT", COUNT_ADDRESS_ALERT, alert_count);
initCount("COUNT_WEBSITE", COUNT_ADDRESS_WEBSITE, website_count);
};
}
// List<Website> -> key : id , value : Website private void initCount(String countFileName, String CountAddress, int countVar) {
Map<Long, Website> websiteMap = websiteService.findDictMap(); log.info("[pssp] 正在尝试初始化 {} 文件", countFileName);
DictConst.WEBSITE_MAP = websiteMap;
log.info("[pssp] 初始化 pssp 网站字典Map完毕,字典表数据大小为{}", websiteMap.size()); boolean countExists = Files.exists(Paths.get(CountAddress));
log.info("[pssp] 正在尝试初始化 pssp 地区Map字段表"); if (countExists) {
log.info("[pssp] 磁盘已存在 {} 文件,跳过初始化流程,将磁盘的 {} 读取入内存中", countFileName, countFileName);
//读取文件获得文件编号后缀值
Path path = Paths.get(CountAddress);
List<String> data = Lists.newArrayList();
// List<Website> -> key : code , value : Region try {
Map<String, Region> regionMap = regionService.findDictMap(); data = Files.readAllLines(path);
DictConst.REGION_MAP = regionMap; } catch (IOException e) {
log.info("[pssp] 读取文件时出现异常:" + e);
}
countVar = data.get(0) == null ? 0 : Integer.parseInt(data.get(0));
log.info("[pssp] 初始化 pssp 地区字典Map完毕,字典表数据大小为{}", regionMap.size()); if (CountAddress.equals(COUNT_ADDRESS_ALERT)) {
}; alert_count = countVar;
} else if (CountAddress.equals(COUNT_ADDRESS_WEBSITE)) {
website_count = countVar;
}
} else {
log.info("[pssp] 磁盘不存在 {} 文件,初始化 {} 文件", countFileName, countFileName);
boolean countMake = createFilePath(new File(COUNT_PATH));
countVar = 0;
if (countMake) {
try {
Files.write(Paths.get(CountAddress), String.valueOf(countVar).getBytes());
log.info("[pssp]磁盘 {} 值更新成功:count = {}", countFileName, countVar);
} catch (IOException e) {
log.info("[pssp]生成文件时出现异常:" + e);
}
}
log.info("[pssp]初始化 {} 文件完毕!", countFileName);
if (CountAddress.equals(COUNT_ADDRESS_ALERT)) {
alert_count = countVar;
} else if (CountAddress.equals(COUNT_ADDRESS_WEBSITE)) {
website_count = countVar;
}
}
}
private boolean createFilePath(File file) {
return file.exists() || file.mkdirs();
} }
......
...@@ -31,15 +31,6 @@ public class PsspTestController { ...@@ -31,15 +31,6 @@ public class PsspTestController {
@Autowired @Autowired
CollectDataTask psspTask; CollectDataTask psspTask;
/**
* 抓取所有报警数据(当前月)
*/
@GetMapping("/alerts/all")
public ResponseEntity<String> fetchAllAlerts() {
alertService.fetchAllData();
return ResponseEntity.ok("alerts fetch job is running success");
}
/** /**
* 抓取更新的数据 * 抓取更新的数据
*/ */
...@@ -49,16 +40,6 @@ public class PsspTestController { ...@@ -49,16 +40,6 @@ public class PsspTestController {
return ResponseEntity.ok("update alerts data success"); return ResponseEntity.ok("update alerts data success");
} }
/**
* 抓取所有网页数据
*/
@GetMapping("/websites/all")
public ResponseEntity<String> fetchAllWebsites() {
//抓取指定月份的数据
websiteService.fetchAllData();
return ResponseEntity.ok("fetch websites success");
}
/** /**
* 抓取更新的数据 * 抓取更新的数据
*/ */
...@@ -68,15 +49,6 @@ public class PsspTestController { ...@@ -68,15 +49,6 @@ public class PsspTestController {
return ResponseEntity.ok("update websites data success "); return ResponseEntity.ok("update websites data success ");
} }
/**
* 抓取所有区域数据
*/
@GetMapping("/regions/all")
public ResponseEntity<String> fetchAllRegions() {
regionService.fetchAllData();
return ResponseEntity.ok("fetch regions success");
}
/** /**
* 抓取更新的数据 * 抓取更新的数据
*/ */
......
package com.zjty.fp.api.pssp.subject.repository.remote;
import com.zjty.fp.api.pssp.subject.entity.remote.RemoteAlert;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.Date;
import java.util.List;
/**
* @author matrix
*/
@Repository
public interface RemoteAlertRepository extends JpaRepository<RemoteAlert, Long>,
JpaSpecificationExecutor<RemoteAlert> {
/**
* 抓取指定时间之后的源数据
*
* @param time 指定的时间
* @return 源报警数据集合
*/
@Query("select o from RemoteAlert o where o.tmFetch > ?1")
List<RemoteAlert> findDataFromTime(Date time);
/**
* 抓取指定id之后的源数据
*
* @param id 源数据的主键id
* @return 源报警数据集合
*/
@Query("select o from RemoteAlert o where o.id > ?1")
List<RemoteAlert> findDataFromId(Long id);
}
package com.zjty.fp.api.pssp.subject.repository.remote;
import com.zjty.fp.api.pssp.subject.entity.remote.RemoteRegion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* RemoteRegionRepository.
*
* @author Matrix <xhyrzldf@gmail.com>
* @since 2019-04-08 at 16:06
*/
public interface RemoteRegionRepository extends JpaRepository<RemoteRegion,String>, JpaSpecificationExecutor<RemoteRegion> {
}
package com.zjty.fp.api.pssp.subject.repository.remote;
import com.zjty.fp.api.pssp.subject.entity.remote.RemoteWebsite;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* fusion-platform.
*
* @author : Matrix [xhyrzldf@gmail.com]
* 18-9-6 .
*/
@Repository
public interface RemoteWebsiteRepository extends JpaRepository<RemoteWebsite, Long>, JpaSpecificationExecutor<RemoteWebsite> {
/**
* 抓取指定id之后的源数据
*
* @param id 源网站数据主键id
* @return 源网站数据集合
*/
@Query("select o from RemoteWebsite o where o.id > ?1")
List<RemoteWebsite> findDataFromId(Long id);
}
...@@ -13,11 +13,6 @@ import java.util.List; ...@@ -13,11 +13,6 @@ import java.util.List;
@Service @Service
public interface PsspService<T> { public interface PsspService<T> {
/**
* 抓取所有历史数据
*/
void fetchAllData();
/** /**
* 抓取子平台更新的数据 * 抓取子平台更新的数据
*/ */
......
package com.zjty.fp.api.pssp.subject.service.impl; package com.zjty.fp.api.pssp.subject.service.impl;
import com.zjty.fp.api.misc.utils.FileReader;
import com.zjty.fp.api.misc.utils.JacksonUtil;
import com.zjty.fp.api.pssp.subject.entity.location.Alert; import com.zjty.fp.api.pssp.subject.entity.location.Alert;
import com.zjty.fp.api.pssp.subject.entity.remote.RemoteAlert;
import com.zjty.fp.api.pssp.subject.repository.location.AlertRepository; import com.zjty.fp.api.pssp.subject.repository.location.AlertRepository;
import com.zjty.fp.api.pssp.subject.repository.remote.RemoteAlertRepository;
import com.zjty.fp.api.pssp.subject.service.AlertService; import com.zjty.fp.api.pssp.subject.service.AlertService;
import com.zjty.fp.api.pssp.task.CollectDataTask;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.*; import java.util.Arrays;
import java.util.concurrent.CompletableFuture; import java.util.Date;
import java.util.List;
import java.util.Objects;
import static com.zjty.fp.api.misc.entity.PsspCount.COUNT_ADDRESS_ALERT;
import static com.zjty.fp.api.misc.utils.DateTimeUtil.LocalToDate; import static com.zjty.fp.api.misc.utils.DateTimeUtil.LocalToDate;
import static com.zjty.fp.api.misc.utils.DateTimeUtil.parseToDate; import static com.zjty.fp.api.misc.utils.DateTimeUtil.parseToDate;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
/** /**
* 告警服务实现类 * 告警服务实现类
...@@ -38,130 +38,47 @@ import static java.util.stream.Collectors.toList; ...@@ -38,130 +38,47 @@ import static java.util.stream.Collectors.toList;
@Transactional(rollbackOn = Exception.class) @Transactional(rollbackOn = Exception.class)
public class AlertServiceImpl implements AlertService { public class AlertServiceImpl implements AlertService {
private static final String ALERT_NAME = "alert";
@Autowired @Autowired
private AlertRepository alertRepo; private AlertRepository alertRepo;
@Autowired @Autowired
private RemoteAlertRepository remoteAlertRepo; private FileReader fileReader;
private long supposeId = 0;
/**
* 抓取所有源数据然后保存入库,历史任务进行的时候,更新任务暂停
* 注意 : 这是一个相当耗时的操作
*/
@Override
public void fetchAllData() {
//遍历一个时间集合,用来抓取历史数据,获取当前月,添加今年一月至当前月的数据
List<LocalDate> monthTables = new ArrayList<>();
//WZ项目 添加2017年12月 201801 202007 三个月的数据
monthTables.add(LocalDate.of(2017, 12, 1));
monthTables.add(LocalDate.of(2018, 1, 1));
// monthTables.add(LocalDate.of(2020, 7, 1));
// int nowYear = LocalDate.now().getYear();
// int nowMonth = LocalDate.now().getMonthValue();
// log.info("[pssp] [历史任务] 确认历史任务的采集月份范围");
// for (int i = 1; i < nowMonth; i++) {
// //check month
// try {
// remoteAlertRepo.count();
// monthTables.add(LocalDate.of(nowYear, i, 1));
// } catch (Exception e) {
// log.warn("[pssp] [历史任务] 检测源目标数据库没有 {}-{}月表数据", nowYear, i);
// }
// }
log.info("[pssp] [历史任务] 正在进行历史任务采集任务... 要采集的月份时间为{}", monthTables
.stream()
.map(LocalDate::toString)
.collect(joining(",")));
//该任务是一个异步任务
CompletableFuture.runAsync(() -> {
for (LocalDate date : monthTables) {
int handledDataCount = 0;
try {
//先将其他数据同步计划任务关闭
CollectDataTask.trigger = false;
log.info("[pssp] [历史任务] 采集 {} 时间的历史数据 ", date.toString());
//设置远程表的动态月份
CollectDataTask.setDynamicTableMonth(date);
log.info("[pssp] 准备抓取源报警历史数据");
//历史数据这里的操作分块操作,每10W条历史数据进行一次分块
long dataCount = remoteAlertRepo.count();
//如果数据量大于10w 分多次操作
int size = 50000;
log.info("[pssp] 报警数据历史同步任务数据量较大,数据量为 {},进行分块处理", dataCount);
long jobCount = (dataCount - 1) / size + 1;
for (int page = 0; page < jobCount; page++) {
// 采集原始数据
List<RemoteAlert> content = remoteAlertRepo.findAll(new PageRequest(page, size)).getContent();
List<Alert> partAlert = content
.parallelStream()
.map(RemoteAlert::toDo)
.collect(toList());
log.info("[pssp] 第{}/{}部分的历史数据[采集]完成,该部分处理了{}条数据", page + 1, jobCount, partAlert.size());
// 写入数据库
List<Alert> savedAlertList = alertRepo.save(partAlert);
log.info("[pssp] 第{}/{}部分的历史数据[写入DB]完成,该部分处理了{}条数据", page + 1, jobCount, savedAlertList.size());
}
log.info("[pssp] {} 月源报警历史数据抓取任务完成,size = {}", date, handledDataCount);
} catch (Exception e) {
log.error("[pssp] [历史任务] 采集发生异常,目前采集月份为 {},采集数量为 {}, 异常栈 : {}", date, handledDataCount, e);
} finally {
//历史任务完成之后重新设置回当前月份
log.info("[pssp] [历史任务] {} 月采集任务结束,将月份重新设置回{}时间", date, LocalDate.now());
CollectDataTask.setDynamicTableMonth(LocalDate.now());
//全部完成后再开启计划任务
CollectDataTask.trigger = true;
}
}
});
}
/** /**
* 获取所有更新的数据,步骤如下 * 获取所有更新的报警数据,步骤如下
* <li>1.获取当前月份本地最新的一条报警数据id</li> * <li>1.到指定文件夹中读取还没处理过的文件(依据count值判断),并更新Count值</li>
* <li>2.向源数据库抓取在步骤1id之后的数据</li> * <li>2.将数据转化为Alert类写入数据库</li>
* <li>3.数据入库</li>
* <li>4.更新缓存相关数据</>
*/ */
@Override @Override
public void fetchUpdatedData() { public void fetchUpdatedData() {
//读取文件
String jsonString = fileReader.readFileAndUnzip(COUNT_ADDRESS_ALERT, "pssp", ALERT_NAME);
if (StringUtils.isEmpty(jsonString)) {
log.info("[pssp] 本次更新采集没有要更新的数据");
return;
}
//从拦截器中获得当前应当处理的月份时间,并进行查询获得当前月份本地数据库报警数据的最新id List<Alert> updatedData = null;
String tableMonth = CollectDataTask.getDynamicTableMonth(); try {
Alert lastedAlert = alertRepo.findSpecifiedMonthLastedData(tableMonth); Alert[] alerts = JacksonUtil.readValue(jsonString, Alert[].class);
Long lastedDataId = Objects.isNull(lastedAlert) ? 0L : lastedAlert.getId(); updatedData = Arrays.asList(alerts);
} catch (Exception e) {
log.info("[pssp] 执行更新数据任务,本地数据库 {} 月最新数据的id为 {} ,向源数据库采集在这id之后的数据....", tableMonth, lastedDataId); log.info("[pssp] 本次更新转换数据时发生异常 : {}", e.toString());
log.debug("[pssp] [id_check],supposeId = {},actuallyId = {}", supposeId, lastedDataId); e.printStackTrace();
}
List<Alert> updatedData = remoteAlertRepo.findDataFromId(lastedDataId).stream()
.map(RemoteAlert::toDo)
.collect(toList());
//写入数据
if (CollectionUtils.isEmpty(updatedData)) { if (CollectionUtils.isEmpty(updatedData)) {
log.info("[pssp] 本次更新采集没有要更新的数据"); log.info("[pssp] 本次更新采集没有要更新的数据");
} else { } else {
log.info("[pssp] 采集完成,本次采集了 {} 条数据,数据写入本机数据库与Es数据库", updatedData.size()); log.info("[pssp] 采集完成,本次采集了 {} 条数据,数据写入本机数据库与Es数据库", updatedData.size());
// List<Alert> savedUpdatedData = alertRepo.save(updatedData);
supposeId = updatedData.stream() log.info("[pssp] 数据库写入数据完成,本次写入 {} 条数据", updatedData.size());
.map(Alert::getId) //完毕之后删除对应的数据
.max(Comparator.naturalOrder()) fileReader.deleteFile(COUNT_ADDRESS_ALERT, "pssp", ALERT_NAME);
.orElse(0L);
//1.更新写入本地数据库
List<Alert> savedUpdatedData = alertRepo.save(updatedData);
} }
} }
/** /**
...@@ -176,9 +93,9 @@ public class AlertServiceImpl implements AlertService { ...@@ -176,9 +93,9 @@ public class AlertServiceImpl implements AlertService {
public Page<Alert> findAllDataBetweenTime(LocalDate startTime, LocalDate endTime, Pageable pageable) { public Page<Alert> findAllDataBetweenTime(LocalDate startTime, LocalDate endTime, Pageable pageable) {
LocalDateTime maxEndTime = endTime.atTime(LocalTime.MAX); LocalDateTime maxEndTime = endTime.atTime(LocalTime.MAX);
return alertRepo.findBetweenTime( return alertRepo.findBetweenTime(
LocalToDate(startTime), LocalToDate(startTime),
LocalToDate(maxEndTime), LocalToDate(maxEndTime),
pageable); pageable);
} }
/** /**
...@@ -244,7 +161,7 @@ public class AlertServiceImpl implements AlertService { ...@@ -244,7 +161,7 @@ public class AlertServiceImpl implements AlertService {
@Override @Override
public Page<Alert> findAllAfterId(Long primaryId,Pageable pageable) { public Page<Alert> findAllAfterId(Long primaryId, Pageable pageable) {
//check id //check id
Alert alert = alertRepo.findOne(primaryId); Alert alert = alertRepo.findOne(primaryId);
if (Objects.isNull(alert)) { if (Objects.isNull(alert)) {
......
package com.zjty.fp.api.pssp.subject.service.impl; package com.zjty.fp.api.pssp.subject.service.impl;
import com.zjty.fp.api.pssp.base.constant.DictConst;
import com.zjty.fp.api.pssp.subject.entity.location.Region; import com.zjty.fp.api.pssp.subject.entity.location.Region;
import com.zjty.fp.api.pssp.subject.entity.remote.RemoteRegion;
import com.zjty.fp.api.pssp.subject.repository.location.RegionRepository; import com.zjty.fp.api.pssp.subject.repository.location.RegionRepository;
import com.zjty.fp.api.pssp.subject.repository.remote.RemoteRegionRepository;
import com.zjty.fp.api.pssp.subject.service.RegionService; import com.zjty.fp.api.pssp.subject.service.RegionService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -13,14 +10,9 @@ import org.springframework.stereotype.Service; ...@@ -13,14 +10,9 @@ import org.springframework.stereotype.Service;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
/** /**
* RegionServiceImpl. 区域接口实现类 * RegionServiceImpl. 区域接口实现类
* *
...@@ -35,71 +27,10 @@ public class RegionServiceImpl implements RegionService { ...@@ -35,71 +27,10 @@ public class RegionServiceImpl implements RegionService {
@Autowired @Autowired
private RegionRepository regionRepository; private RegionRepository regionRepository;
@Autowired
private RemoteRegionRepository remoteRegionRepository;
@Override
public void fetchAllData() {
log.info("[pssp]准备抓取全部源地区数据");
List<Region> regionList = remoteRegionRepository.findAll()
.stream()
.map(RemoteRegion::toDo)
.collect(toList());
log.info("[pssp]源地区数据抓取完成,size = {},准备写入融合平台数据库", regionList.size());
// 异步
CompletableFuture.runAsync(() -> {
//更新本地DB
log.info("[pssp]正在异步写入地区数据到本地数据库");
regionRepository.save(regionList);
log.info("[pssp]异步地区数据写入完成");
//更新字典map
log.info("[pssp] [定时任务] 开始更新字典Map任务,更新前的字典Map大小为,regionMap : {}", DictConst.REGION_MAP.size());
DictConst.REGION_MAP = findDictMap();
log.info("[pssp] [定时任务] 完成更新字典Map任务,更新后的字典Map大小为,regionMap : {}", DictConst.REGION_MAP.size());
});
}
@Override @Override
public void fetchUpdatedData() { public void fetchUpdatedData() {
log.info("[pssp]正在执行地区数据的更新"); log.info("[pssp] 正在执行地区数据的更新");
log.info("[pssp] 本次更新没有更新区域数据");
//批量对比 首先获取远端DB所有区域,对比数量,数量一致任务结束,数量不一致获取自己没有的进行采集
List<RemoteRegion> remoteRegionList = remoteRegionRepository.findAll();
List<Region> localRegionList = regionRepository.findAll();
int remoteSize = remoteRegionList.size();
int localSize = localRegionList.size();
if (remoteSize != localSize) {
log.info("[pssp] 本次地区更新采集没有要更新的数据,本地区域size : {},远端区域size : {}", localSize, remoteSize);
} else {
CompletableFuture.runAsync(() -> {
//映射为Map , key : code ,value : region object ,如果远程的区域对象code 在此get不到则,证明需要插入
Map<String, Region> localRegionMap = localRegionList.stream().collect(toMap(Region::getCode, Function.identity()));
List<Region> regionList = remoteRegionList.stream()
.filter(remoteRegion -> nonNull(localRegionMap.get(remoteRegion.getCode())))
.map(RemoteRegion::toDo)
.collect(toList());
log.info("[pssp] 发现需要同步更新的地区对象,对象内容为 : {}", regionList);
log.info("[pssp]正在异步写入地区更新数据到本地数据库");
regionRepository.save(regionList);
log.info("[pssp]异步地区更新数据写入完成");
//更新字典map
log.info("[pssp] [定时任务] 开始更新字典Map任务,更新前的字典Map大小为,regionMap : {}", DictConst.REGION_MAP.size());
DictConst.REGION_MAP = findDictMap();
log.info("[pssp] [定时任务] 完成更新字典Map任务,更新后的字典Map大小为,regionMap : {}", DictConst.REGION_MAP.size());
});
}
} }
@Override @Override
......
package com.zjty.fp.api.pssp.subject.service.impl; package com.zjty.fp.api.pssp.subject.service.impl;
import com.zjty.fp.api.pssp.base.constant.DictConst; import com.zjty.fp.api.misc.utils.FileReader;
import com.zjty.fp.api.misc.utils.JacksonUtil;
import com.zjty.fp.api.pssp.subject.entity.location.Website; import com.zjty.fp.api.pssp.subject.entity.location.Website;
import com.zjty.fp.api.pssp.subject.entity.remote.RemoteWebsite;
import com.zjty.fp.api.pssp.subject.repository.location.WebsiteRepository; import com.zjty.fp.api.pssp.subject.repository.location.WebsiteRepository;
import com.zjty.fp.api.pssp.subject.repository.remote.RemoteWebsiteRepository;
import com.zjty.fp.api.pssp.subject.service.WebsiteService; import com.zjty.fp.api.pssp.subject.service.WebsiteService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -12,13 +11,14 @@ import org.springframework.stereotype.Service; ...@@ -12,13 +11,14 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList; import static com.zjty.fp.api.misc.entity.PsspCount.COUNT_ADDRESS_ALERT;
import static com.zjty.fp.api.misc.entity.PsspCount.COUNT_ADDRESS_WEBSITE;
/** /**
* fusion-platform. * fusion-platform.
...@@ -31,64 +31,43 @@ import static java.util.stream.Collectors.toList; ...@@ -31,64 +31,43 @@ import static java.util.stream.Collectors.toList;
@Transactional(rollbackOn = Exception.class) @Transactional(rollbackOn = Exception.class)
public class WebsiteServiceImpl implements WebsiteService { public class WebsiteServiceImpl implements WebsiteService {
private static final String WEBSITE_NAME = "website";
@Autowired @Autowired
private WebsiteRepository websiteRepository; private WebsiteRepository websiteRepository;
@Autowired @Autowired
private RemoteWebsiteRepository remoteWebsiteRepository; private FileReader fileReader;
@Override
public void fetchAllData() {
log.info("[pssp]准备抓取全部源网站数据");
List<Website> websiteList = remoteWebsiteRepository.findAll().stream()
.map(RemoteWebsite::toDo)
.collect(Collectors.toList());
log.info("[pssp]源网站数据抓取完成,size = {},准备写入融合平台数据库", websiteList.size());
// 异步写入数据库
CompletableFuture.runAsync(() -> {
log.info("[pssp]正在异步写入网站数据到本地数据库");
websiteRepository.save(websiteList);
log.info("[pssp]异步网站数据写入完成");
//更新字典map
log.info("[pssp] [定时任务] 开始更新字典Map任务,更新前的字典Map大小为,websiteMap : {}", DictConst.WEBSITE_MAP.size());
DictConst.WEBSITE_MAP = findDictMap();
log.info("[pssp] [定时任务] 完成更新字典Map任务,更新后的字典Map大小为,websiteMap : {}", DictConst.WEBSITE_MAP.size());
});
}
/**
* 获取所有更新的网站数据,步骤如下
* <li>1.到指定文件夹中读取还没处理过的文件(依据count值判断)</li>
* <li>2.将数据转化为Alert类写入数据库</li>
* <li>3.更新Count值</li>
*/
@Override @Override
public void fetchUpdatedData() { public void fetchUpdatedData() {
log.info("[pssp]正在执行网站数据的增量更新"); //读取文件
Website lastedWebsite = websiteRepository.findFirstByOrderByIdDesc(); String jsonString = fileReader.readFileAndUnzip(COUNT_ADDRESS_WEBSITE, "pssp", WEBSITE_NAME);
if (lastedWebsite == null) { List<Website> updatedData = null;
log.info("[pssp] 当前DB中没有任何网站,请先执行网站历史采集任务"); try {
return; Website[] alerts = JacksonUtil.readValue(jsonString, Website[].class);
updatedData = Arrays.asList(alerts);
} catch (Exception e) {
log.info("[pssp] 本次更新转换数据时发生异常 : {}",e.toString());
} }
Long lastedDataId = lastedWebsite.getId(); //写入数据
log.info("[pssp]当前本机网站表数据的最新id为 {} ,向源数据库采集在这id之后的数据....", lastedDataId);
List<Website> updatedData = remoteWebsiteRepository.findDataFromId(lastedDataId).stream()
.map(RemoteWebsite::toDo)
.collect(toList());
if (CollectionUtils.isEmpty(updatedData)) { if (CollectionUtils.isEmpty(updatedData)) {
log.info("[pssp] 本次网站更新采集没有要更新的数据"); log.info("[pssp] 本次网站更新采集没有要更新的数据");
} else { } else {
log.info("[pssp] 网站数据采集完成,本次采集了 {}条数据,数据写入本机数据库", updatedData.size()); log.info("[pssp] 网站数据采集完成,本次采集了 {}条数据,数据写入本机数据库", updatedData.size());
// 异步写入数据库 websiteRepository.save(updatedData);
CompletableFuture.runAsync(() -> { log.info("[pssp]异步网站更新数据写入完成,本次写入 {} 条数据", updatedData.size());
log.info("[pssp]正在异步写入网站更新数据到本地数据库"); //完毕之后删除对应的数据
websiteRepository.save(updatedData); fileReader.deleteFile(COUNT_ADDRESS_WEBSITE, "pssp", WEBSITE_NAME);
log.info("[pssp]异步网站更新数据写入完成");
//更新字典map
log.info("[pssp] [定时任务] 开始更新字典Map任务,更新前的字典Map大小为,websiteMap : {}", DictConst.WEBSITE_MAP.size());
DictConst.WEBSITE_MAP = findDictMap();
log.info("[pssp] [定时任务] 完成更新字典Map任务,更新后的字典Map大小为,websiteMap : {}", DictConst.WEBSITE_MAP.size());
});
} }
} }
@Override @Override
......
...@@ -5,13 +5,13 @@ import com.zjty.fp.api.misc.utils.DateTimeUtil; ...@@ -5,13 +5,13 @@ import com.zjty.fp.api.misc.utils.DateTimeUtil;
import com.zjty.fp.api.pssp.base.aspect.EnablePsspSchProtect; import com.zjty.fp.api.pssp.base.aspect.EnablePsspSchProtect;
import com.zjty.fp.api.pssp.base.aspect.PsspScheduleTrigger; import com.zjty.fp.api.pssp.base.aspect.PsspScheduleTrigger;
import com.zjty.fp.api.pssp.subject.repository.location.AlertRepository; import com.zjty.fp.api.pssp.subject.repository.location.AlertRepository;
import com.zjty.fp.api.pssp.subject.repository.remote.RemoteAlertRepository;
import com.zjty.fp.api.pssp.subject.service.AlertService; import com.zjty.fp.api.pssp.subject.service.AlertService;
import com.zjty.fp.api.pssp.subject.service.RegionService; import com.zjty.fp.api.pssp.subject.service.RegionService;
import com.zjty.fp.api.pssp.subject.service.WebsiteService; import com.zjty.fp.api.pssp.subject.service.WebsiteService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
...@@ -36,11 +36,6 @@ public class CollectDataTask { ...@@ -36,11 +36,6 @@ public class CollectDataTask {
* 用于控制计划任务的开关,一般来讲开关默认开启,当进行历史数据同步的时候,开关关闭 * 用于控制计划任务的开关,一般来讲开关默认开启,当进行历史数据同步的时候,开关关闭
*/ */
public static boolean trigger = true; public static boolean trigger = true;
private static String dynamicTableMonth;
static {
dynamicTableMonth = DateTimeUtil.formatDateTimetoString(new Date(), "yyyyMM");
}
@Autowired @Autowired
private AlertService alertService; private AlertService alertService;
...@@ -56,48 +51,21 @@ public class CollectDataTask { ...@@ -56,48 +51,21 @@ public class CollectDataTask {
@Autowired @Autowired
private AlertRepository localPsspRep; private AlertRepository localPsspRep;
@Autowired
private RemoteAlertRepository remotePsspRep;
public static String getDynamicTableMonth() {
return dynamicTableMonth;
}
public static void setDynamicTableMonth(LocalDate time) {
dynamicTableMonth = time.format(DateTimeFormatter.ofPattern("yyyyMM"));
}
public static void setDynamicTableMonth(Date time) {
dynamicTableMonth = DateTimeUtil.formatDateTimetoString(time, "yyyyMM");
}
/**
* This method is for debug
*/
// @EnablePsspSchProtect
// @Scheduled(cron = "0 0/1 * * * ?")
public void checkPsspDataCount() {
long simcCount = remotePsspRep.count();
long apiCount = localPsspRep.count();
log.debug("[pssp] [count_check] simc={} api={}",
simcCount, apiCount);
}
/** /**
* 抓取更新的网站的数据,当前为1h/次 * 抓取更新的网站的数据,当前为1h/次
*/ */
@EnablePsspSchProtect // @EnablePsspSchProtect
// @Scheduled(cron = "0 0 0/1 * * ?") @Scheduled(cron = "0 0 0/1 * * ?")
public void collectWebsiteData() { public void collectWebsiteData() {
log.info("[pssp] [定时任务]抓取更新的网站数据"); log.info("[pssp] [定时任务]抓取更新的网站数据");
websiteService.fetchUpdatedData(); websiteService.fetchUpdatedData();
} }
/** /**
* 抓取更新的地区的数据,当前为1h/次 * 抓取更新的地区的数据,当前关闭
*/ */
@EnablePsspSchProtect // @EnablePsspSchProtect
// @Scheduled(cron = "0 0 0/1 * * ?") // @Scheduled(cron = "0 0 0/1 * * ?")
public void collectRegionData() { public void collectRegionData() {
log.info("[pssp] [定时任务]抓取更新的区域数据"); log.info("[pssp] [定时任务]抓取更新的区域数据");
...@@ -105,33 +73,13 @@ public class CollectDataTask { ...@@ -105,33 +73,13 @@ public class CollectDataTask {
} }
/** /**
* 抓取更新的报警数据,当前为1min/次 * 抓取更新的报警数据,当前为1min15s/次
*/ */
@EnablePsspSchProtect // @EnablePsspSchProtect
// @Scheduled(cron = "30 0/1 * * * ?") @Scheduled(cron = "0 0/1 * * * ?")
public void collectAlertData() { public void collectAlertData() {
log.info("[pssp] [定时任务] 抓取更新的报警数据"); log.info("[pssp] [定时任务] 抓取更新的报警数据");
alertService.fetchUpdatedData(); alertService.fetchUpdatedData();
} }
/**
* 在每个月月初的第10分钟,执行一次上个月的数据丢失补偿任务,大体逻辑如下
* <li>1.关闭其他计划任务</li>
* <li>2.将当前动态处理的表时间更改为上个月时间</li>
* <li>3.执行数据更新任务</li>
* <li>4.更正时间,开启其他计划任务></li>
*/
// @Scheduled(cron = "0 10 0 1 * ?")
@EnablePsspSchProtect
public void replenishMysqlDataMonthly() {
log.info("[pssp] [定时任务] 正在执行月末数据补偿任务,暂停其他计划任务,要补偿的月份数据是 {}", LocalDate.now().minusMonths(1L).toString());
setDynamicTableMonth(LocalDate.now().minusMonths(1L));
alertService.fetchUpdatedData();
log.info("[pssp] [定时任务] 月末数据补偿任务执行完毕,更正时间,开启其他计划任务");
setDynamicTableMonth(LocalDate.now());
}
} }
...@@ -30,29 +30,7 @@ public class DataSourceConfig { ...@@ -30,29 +30,7 @@ public class DataSourceConfig {
@Autowired @Autowired
private Environment env; private Environment env;
/**
* fp-simc数据源,用于pssp平台采集数据
*/
@Bean(name = "remoteDataSource")
@Qualifier("remoteDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.remote")
public DataSource primaryDataSource() {
return DruidDataSourceBuilder.create().build();
}
/**
* fp-simc数据源,用于vomp平台采集数据
*/
@Bean(name = "remoteDataSource2")
@Qualifier("remoteDataSource2")
@ConfigurationProperties(prefix = "spring.datasource.remote2")
public DataSource vompDataSource() {
return DruidDataSourceBuilder.create().build();
}
/** /**
* fp-api数据源,用于正常提供api服务 * fp-api数据源,用于正常提供api服务
*/ */
......
package com.zjty.fp.api.union.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
/**
* <p>Description : fusion-platform
* <p>Date : 2019/1/3 14:33
* <p>@author : C
*/
@SuppressWarnings("SpringAutowiredFieldsWarningInspection")
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryRemote2",
transactionManagerRef = "transactionManagerRemote2",
basePackages = {"com.zjty.fp.api.vomp.subject.repository.remote"}
)
@EnableConfigurationProperties(JpaProperties.class)
public class Remote2Config {
@Autowired
@Qualifier("remoteDataSource2")
private DataSource remoteDataSource2;
@Autowired
private JpaProperties jpaProperties;
@Bean(name = "entityManageRemote2")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryRemote(builder).getObject().createEntityManager();
}
@Bean(name = "entityManagerFactoryRemote2")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryRemote(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(remoteDataSource2)
.properties(getVendorProperties(remoteDataSource2))
.packages("com.zjty.fp.api.vomp.subject.entity.remotedo")
.persistenceUnit("remotePersistenceUnit2")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Bean(name = "transactionManagerRemote2")
public PlatformTransactionManager transactionManagerRemote(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryRemote(builder).getObject());
}
}
package com.zjty.fp.api.union.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactoryRemote",
transactionManagerRef = "transactionManagerRemote",
basePackages = {"com.zjty.fp.api.pssp.subject.repository.remote"}
)
@EnableConfigurationProperties(JpaProperties.class)
public class RemoteConfig {
@Autowired
@Qualifier("remoteDataSource")
private DataSource remoteDataSource;
@Autowired
private JpaProperties jpaProperties;
@Primary
@Bean(name = "entityManageRemote")
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryRemote(builder).getObject().createEntityManager();
}
@Primary
@Bean(name = "entityManagerFactoryRemote")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryRemote(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(remoteDataSource)
.properties(getVendorProperties(remoteDataSource))
.packages("com.zjty.fp.api.pssp.subject.entity.remote")
.persistenceUnit("remotePersistenceUnit")
.build();
}
private Map<String, String> getVendorProperties(DataSource dataSource) {
return jpaProperties.getHibernateProperties(dataSource);
}
@Primary
@Bean(name = "transactionManagerRemote")
public PlatformTransactionManager transactionManagerRemote(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(entityManagerFactoryRemote(builder).getObject());
}
}
package com.zjty.fp.api.union.config;
import com.zjty.fp.api.pssp.task.CollectDataTask;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.EmptyInterceptor;
/**
* Sql 拦截器 用于替换alert表名
*
* @author : Matrix [xhyrzldf@gmail.com]
* 18-9-6 .
*/
@Slf4j
@SuppressWarnings("FieldCanBeLocal")
public class SqlInterceptor extends EmptyInterceptor {
private String originalTableName = "remote.alert";
@Override
public String onPrepareStatement(String sql) {
String prepareStatement = super.onPrepareStatement(sql);
if (prepareStatement.contains(originalTableName)) {
String tableMonth = CollectDataTask.getDynamicTableMonth();
// replace指定月份 if trigger false 替换当前月
String realTableName = "t_doc_" + tableMonth;
log.info("[pssp]正在进行sql拦截 拦截的表为 {},替换后的表名为{}", "remote.alert", realTableName);
prepareStatement = prepareStatement.replaceAll(originalTableName, realTableName);
}
return prepareStatement;
}
}
# suppress inspection "SpringBootApplicationProperties" for whole file # suppress inspection "SpringBootApplicationProperties" for whole file
spring.application.name=fp-api-wz spring.application.name=fp-api-wz
# 数据库one相关配置 21.18.29.98:3306/db_secret_alert.
spring.datasource.remote.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.remote.url=jdbc:mysql://localhost:3306/fp_simc?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
spring.datasource.remote.username=fp
#spring.datasource.remote.password=efield-tech
spring.datasource.remote.password=fp123456
## StatFilter
spring.datasource.remote.filter.stat.db-type=mysql
spring.datasource.remote.initial-size=4
spring.datasource.remote.min-idle=4
spring.datasource.remote.max-active=20
## 配置获取连接等待超时的时间 ms
spring.datasource.remote.max-wait=60000
## 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.remote.pool-prepared-statements=true
spring.datasource.remote.max-open-prepared-statements=20
## 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.remote.time-between-eviction-runs-millis=60000
## 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.remote.min-evictable-idle-time-millis=300000
## 配置查询语句验证(用于查询是否还在连接的语句)
spring.datasource.remote.validation-query=SELECT 1
spring.datasource.remote.test-on-borrow=true
spring.datasource.remote.test-on-return=false
spring.datasource.remote.test-while-idle=true
# 数据库two相关配置 # 数据库two相关配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.location.driver-class-name=com.mysql.jdbc.Driver spring.datasource.location.driver-class-name=com.mysql.jdbc.Driver
...@@ -52,16 +26,6 @@ spring.datasource.location.validation-query=SELECT 1 ...@@ -52,16 +26,6 @@ spring.datasource.location.validation-query=SELECT 1
spring.datasource.location.test-on-borrow=true spring.datasource.location.test-on-borrow=true
spring.datasource.location.test-on-return=false spring.datasource.location.test-on-return=false
spring.datasource.location.test-while-idle=true spring.datasource.location.test-while-idle=true
# 数据库three相关配置
spring.datasource.remote2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.remote2.url=jdbc:mysql://localhost:3306/fp_simc?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
spring.datasource.remote2.username=fp
spring.datasource.remote2.password=fp123456
#spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
#spring.datasource.url=jdbc:sqlserver://21.28.120.2:1433;DatabaseName=PowerMonJava;
#spring.datasource.username=ms
#spring.datasource.password=ms123456
# ftp # ftp
ftp.server.ip=192.168.1.159 ftp.server.ip=192.168.1.159
ftp.server.port=2121 ftp.server.port=2121
......
# suppress inspection "SpringBootApplicationProperties" for whole file # suppress inspection "SpringBootApplicationProperties" for whole file
spring.application.name=fp-api-wz spring.application.name=fp-api-wz
# 数据库one相关配置 21.18.29.98:3306/db_secret_alert.
spring.datasource.remote.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.remote.url=jdbc:mysql://localhost:3306/fp_simc?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
spring.datasource.remote.username=fp
#spring.datasource.remote.password=efield-tech
spring.datasource.remote.password=fp123456
## StatFilter
spring.datasource.remote.filter.stat.db-type=mysql
spring.datasource.remote.initial-size=4
spring.datasource.remote.min-idle=4
spring.datasource.remote.max-active=20
## 配置获取连接等待超时的时间 ms
spring.datasource.remote.max-wait=60000
## 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.remote.pool-prepared-statements=true
spring.datasource.remote.max-open-prepared-statements=20
## 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.remote.time-between-eviction-runs-millis=60000
## 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.remote.min-evictable-idle-time-millis=300000
## 配置查询语句验证(用于查询是否还在连接的语句)
spring.datasource.remote.validation-query=SELECT 1
spring.datasource.remote.test-on-borrow=true
spring.datasource.remote.test-on-return=false
spring.datasource.remote.test-while-idle=true
# 数据库two相关配置 # 数据库two相关配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.location.driver-class-name=com.mysql.jdbc.Driver spring.datasource.location.driver-class-name=com.mysql.jdbc.Driver
...@@ -53,11 +27,6 @@ spring.datasource.location.test-on-borrow=true ...@@ -53,11 +27,6 @@ spring.datasource.location.test-on-borrow=true
spring.datasource.location.test-on-return=false spring.datasource.location.test-on-return=false
spring.datasource.location.test-while-idle=true spring.datasource.location.test-while-idle=true
# 数据库three相关配置
spring.datasource.remote2.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.remote2.url=jdbc:mysql://localhost:3306/fp_simc?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&allowPublicKeyRetrieval=true
spring.datasource.remote2.username=fp
spring.datasource.remote2.password=fp123456
# ftp # ftp
ftp.server.ip=192.168.1.159 ftp.server.ip=192.168.1.159
ftp.server.port=2121 ftp.server.port=2121
......
...@@ -9,9 +9,6 @@ spring.jpa.show-sql=false ...@@ -9,9 +9,6 @@ spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.open-in-view=true spring.jpa.open-in-view=true
#interceptor
spring.jpa.properties.hibernate.ejb.interceptor=com.zjty.fp.api.union.config.SqlInterceptor
# jackson # jackson
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8 spring.jackson.time-zone=GMT+8
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论