From f9e1a07e7c60e53a96b6f016e088e9686301f03e Mon Sep 17 00:00:00 2001 From: wxz Date: Sun, 23 Aug 2020 00:06:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E6=95=B0=E6=8D=AE=E6=BA=90-?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8F=82=E6=95=B0=E6=96=B9=E5=BC=8F=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E6=95=B0=E6=8D=AE=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet-commons-dynamic-datasource/pom.xml | 6 +++ .../datasource/aspect/DataSourceAspect.java | 48 +++++++------------ .../datasource/config/DynamicDataSource.java | 8 +++- .../datasource/enums/DataSourceEnum.java | 43 +++++++++++++++++ .../util/AbstractDataSourceNameFetcher.java | 13 +++++ .../HttpRequestDataSourceNameFetcher.java | 44 +++++++++++++++++ .../epmet/constant/DataSourceConstant.java | 1 + .../com/epmet/controller/DemoController.java | 17 +++++++ .../stats/impl/DimAgencyServiceImpl.java | 3 ++ .../src/main/resources/bootstrap.yml | 10 ++++ .../resources/mapper/stats/DimAgencyDao.xml | 1 + 11 files changed, 163 insertions(+), 31 deletions(-) create mode 100644 epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/enums/DataSourceEnum.java create mode 100644 epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/util/AbstractDataSourceNameFetcher.java create mode 100644 epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/util/HttpRequestDataSourceNameFetcher.java diff --git a/epmet-commons/epmet-commons-dynamic-datasource/pom.xml b/epmet-commons/epmet-commons-dynamic-datasource/pom.xml index 07f98a0b56..efb328a391 100644 --- a/epmet-commons/epmet-commons-dynamic-datasource/pom.xml +++ b/epmet-commons/epmet-commons-dynamic-datasource/pom.xml @@ -19,6 +19,12 @@ 2.0.0 provided + + + org.springframework.boot + spring-boot-starter-web + provided + diff --git a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/aspect/DataSourceAspect.java b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/aspect/DataSourceAspect.java index 45113847c6..080bda7565 100644 --- a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/aspect/DataSourceAspect.java +++ b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/aspect/DataSourceAspect.java @@ -9,8 +9,8 @@ package com.epmet.commons.dynamic.datasource.aspect; import com.epmet.commons.dynamic.datasource.annotation.DataSource; -import com.epmet.commons.dynamic.datasource.bean.DataSourceParam; import com.epmet.commons.dynamic.datasource.config.DynamicContextHolder; +import com.epmet.commons.dynamic.datasource.util.HttpRequestDataSourceNameFetcher; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; @@ -19,12 +19,12 @@ import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; -import java.lang.reflect.Parameter; /** * 多数据源,切面处理类 @@ -38,6 +38,9 @@ import java.lang.reflect.Parameter; public class DataSourceAspect { protected Logger logger = LoggerFactory.getLogger(getClass()); + @Autowired + private HttpRequestDataSourceNameFetcher httpRequestDataSourceNameFetcher; + @Pointcut("@annotation(com.epmet.commons.dynamic.datasource.annotation.DataSource) " + "|| @within(com.epmet.commons.dynamic.datasource.annotation.DataSource)") public void dataSourcePointCut() { @@ -55,12 +58,14 @@ public class DataSourceAspect { if(targetDataSource != null || methodDataSource != null){ String value; if(methodDataSource != null){ - value = getDatasourceName(methodDataSource, signature.getMethod().getParameters(), point.getArgs()); + value = getDatasourceName(methodDataSource); }else { - value = getDatasourceName(targetDataSource, signature.getMethod().getParameters(), point.getArgs()); + value = getDatasourceName(targetDataSource); } - DynamicContextHolder.push(value); + if (StringUtils.isNotBlank(value)) { + DynamicContextHolder.push(value); + } logger.debug("set datasource is {}", value); } @@ -77,33 +82,16 @@ public class DataSourceAspect { * @param dataSource * @return */ - public String getDatasourceName(DataSource dataSource, Parameter[] methodParameters, Object[] methodArgValues) { + public String getDatasourceName(DataSource dataSource) { + String dataSourceName = null; if (dataSource.datasourceNameFromArg()) { - // 1.从参数中动态获取数据源名称 - String datasourceNameFromParam = getDatasourceNameFromArg(methodParameters, methodArgValues); - if (StringUtils.isNotBlank(datasourceNameFromParam)) { - // 如果有DatasourceParam类型的参数并且设置了datasourceName值,那么返回这个值,否则使用硬编码的 - return datasourceNameFromParam; - } + // 1.优先从http header中动态获取数据源名称 + dataSourceName = httpRequestDataSourceNameFetcher.fetchDataSourceName(); } - // 2.硬编码指定数据源名称 - return dataSource.value(); - } - - /** - * 从参数中取数据源名称 - * @param parameters - * @param argsObject - * @return - */ - public String getDatasourceNameFromArg(Parameter[] parameters, Object[] argsObject) { - for (int i = 0; i < parameters.length; i++) { - if (parameters[i].getType() == DataSourceParam.class) { - DataSourceParam param = (DataSourceParam) argsObject[i]; - return param.getDatasourceName(); - } + // 2.硬编码指定默认的数据源名称 + if (StringUtils.isBlank(dataSourceName)) { + dataSourceName = dataSource.value(); } - - return null; + return dataSourceName; } } diff --git a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/config/DynamicDataSource.java b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/config/DynamicDataSource.java index 3fae7e003d..0a9d9adf91 100644 --- a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/config/DynamicDataSource.java +++ b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/config/DynamicDataSource.java @@ -8,6 +8,8 @@ package com.epmet.commons.dynamic.datasource.config; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** @@ -18,9 +20,13 @@ import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; */ public class DynamicDataSource extends AbstractRoutingDataSource { + private Logger logger = LoggerFactory.getLogger(getClass()); + @Override protected Object determineCurrentLookupKey() { - return DynamicContextHolder.peek(); + String datasourceName = DynamicContextHolder.peek(); + logger.info("使用的数据源名称为:{}", datasourceName); + return datasourceName; } } diff --git a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/enums/DataSourceEnum.java b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/enums/DataSourceEnum.java new file mode 100644 index 0000000000..23ed9ba634 --- /dev/null +++ b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/enums/DataSourceEnum.java @@ -0,0 +1,43 @@ +package com.epmet.commons.dynamic.datasource.enums; + +/** + * 服务-数据源flag-数据源名称对应关系 + */ +public enum DataSourceEnum { + + DATA_STATISTICAL_REAL("data-statistical-server", "real", "stats"), + DATA_STATISTICAL_FAKE("data-statistical-server", "fake", "statsDisplay"), + ; + + private String serviceName; + private String flag; + private String dataSourceName; + + DataSourceEnum(String serviceName, String flag, String dataSourceName) { + this.serviceName = serviceName; + this.flag = flag; + this.dataSourceName = dataSourceName; + } + + public static DataSourceEnum getEnum(String serviceName, String flag) { + DataSourceEnum[] values = DataSourceEnum.values(); + for (DataSourceEnum value : values) { + if (value.serviceName.equals(serviceName) && value.flag.equals(flag)) { + return value; + } + } + return null; + } + + public String getServiceName() { + return serviceName; + } + + public String getFlag() { + return flag; + } + + public String getDataSourceName() { + return dataSourceName; + } +} diff --git a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/util/AbstractDataSourceNameFetcher.java b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/util/AbstractDataSourceNameFetcher.java new file mode 100644 index 0000000000..5069bd5958 --- /dev/null +++ b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/util/AbstractDataSourceNameFetcher.java @@ -0,0 +1,13 @@ +package com.epmet.commons.dynamic.datasource.util; + +import com.epmet.commons.dynamic.datasource.enums.DataSourceEnum; + +public abstract class AbstractDataSourceNameFetcher { + + public abstract String fetchDataSourceName(); + + //protected String getDataSourceName(String dataType, String serviceName) { + // return DataSourceEnum.getEnum(serviceName, dataType) + //} + +} diff --git a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/util/HttpRequestDataSourceNameFetcher.java b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/util/HttpRequestDataSourceNameFetcher.java new file mode 100644 index 0000000000..df11b546bf --- /dev/null +++ b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/util/HttpRequestDataSourceNameFetcher.java @@ -0,0 +1,44 @@ +package com.epmet.commons.dynamic.datasource.util; + +import com.epmet.commons.dynamic.datasource.enums.DataSourceEnum; +import com.epmet.commons.tools.exception.RenException; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +/** + * Http请求中获取数据源名称 + */ +@Component +public class HttpRequestDataSourceNameFetcher extends AbstractDataSourceNameFetcher { + + protected Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired + private Environment environment; + + @Override + public String fetchDataSourceName() { + ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + javax.servlet.http.HttpServletRequest request = requestAttributes.getRequest(); + String dataType = request.getHeader("Data-Type"); + + logger.info("HttpRequestDataSourceNameFetcher获取到的DataType为:{}", dataType); + if (StringUtils.isBlank(dataType)) { + return null; + } + + String serviceName = environment.getProperty("spring.application.name"); + DataSourceEnum dataSourceEnum = DataSourceEnum.getEnum(serviceName, dataType); + if (dataSourceEnum == null) { + throw new RenException(String.format("根据前端传入的DataType[%s]无法找到对应的数据源。", dataType)); + } + logger.info("HttpRequestDataSourceNameFetcher根据DataType:[{}]获取到的DataSourceEnum为{}", dataType, dataSourceEnum.getDataSourceName()); + return dataSourceEnum.getDataSourceName(); + } +} diff --git a/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/constant/DataSourceConstant.java b/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/constant/DataSourceConstant.java index 920fc14e14..be529605de 100644 --- a/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/constant/DataSourceConstant.java +++ b/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/constant/DataSourceConstant.java @@ -4,6 +4,7 @@ public interface DataSourceConstant { String GOV_ORG = "govOrg"; String STATS = "stats"; + String STATS_DISPLAY = "statsDisplay"; String GOV_ISSUE = "govIssue"; String GOV_PROJECT = "govProject"; String GOV_VOICE = "govVoice"; diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java index b8f7c3e3d2..6ed40fc377 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/DemoController.java @@ -1,11 +1,15 @@ package com.epmet.controller; import com.epmet.commons.tools.exception.RenException; +import com.epmet.commons.tools.utils.Result; import com.epmet.dto.AgencySubTreeDto; +import com.epmet.entity.stats.DimAgencyEntity; import com.epmet.service.StatsDemoService; +import com.epmet.service.stats.DimAgencyService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -26,6 +30,9 @@ public class DemoController { @Autowired private ExecutorService executorService; + @Autowired + private DimAgencyService dimAgencyService; + @GetMapping("testAlarm") public void testAlarm() { //for (int i = 0; i < 20; i++) { @@ -91,4 +98,14 @@ public class DemoController { List result = demoService.getAllAgency(); return result; } + + /** + * 参数指定数据源 + * @return + */ + @PostMapping("paramDataSource") + public Result paramDataSource() { + List list = dimAgencyService.getAgencyListByCustomerId("ba7c0b5b21e882b263ee8456e2cfb63e"); + return new Result().ok(list); + } } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/stats/impl/DimAgencyServiceImpl.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/stats/impl/DimAgencyServiceImpl.java index d40987c1bc..10d4ba7a44 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/stats/impl/DimAgencyServiceImpl.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/stats/impl/DimAgencyServiceImpl.java @@ -19,12 +19,14 @@ package com.epmet.service.stats.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.epmet.commons.dynamic.datasource.annotation.DataSource; import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.constant.FieldConstant; +import com.epmet.constant.DataSourceConstant; import com.epmet.constant.DimAgencyConstant; import com.epmet.constant.RobotConstant; import com.epmet.constant.StatsSubject; @@ -208,6 +210,7 @@ public class DimAgencyServiceImpl extends BaseServiceImpl getAgencyListByCustomerId(String customerId) { if (StringUtils.isBlank(customerId)){ diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml b/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml index a475fa198f..7ece13bb19 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml +++ b/epmet-module/data-statistical/data-statistical-server/src/main/resources/bootstrap.yml @@ -155,6 +155,16 @@ dynamic: url: @datasource.druid.user.url@ username: @datasource.druid.user.username@ password: @datasource.druid.user.password@ + stats: + driver-class-name: com.mysql.cj.jdbc.Driver + url: @datasource.druid.stats.url@ + username: @datasource.druid.stats.username@ + password: @datasource.druid.stats.password@ + statsDisplay: + driver-class-name: com.mysql.cj.jdbc.Driver + url: @datasource.druid.statsdisplay.url@ + username: @datasource.druid.statsdisplay.username@ + password: @datasource.druid.statsdisplay.password@ thread: # 线程池配置 diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/stats/DimAgencyDao.xml b/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/stats/DimAgencyDao.xml index dedf82d8fe..3b7fb8b8ba 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/stats/DimAgencyDao.xml +++ b/epmet-module/data-statistical/data-statistical-server/src/main/resources/mapper/stats/DimAgencyDao.xml @@ -128,6 +128,7 @@ ID, CUSTOMER_ID, PID, + AGENCY_NAME, LEVEL FROM dim_agency