Commit 8557f5d9 authored by ysongq's avatar ysongq

Merge remote-tracking branch 'origin/dm_dev' into dm_dev

# Conflicts:
#	jz-dm-apigateway/src/main/java/com/jz/dm/service/request/ApiQueryService.java
parents e8a13bd5 03996381
...@@ -140,10 +140,10 @@ CREATE TABLE `t_api_req_log` ( ...@@ -140,10 +140,10 @@ CREATE TABLE `t_api_req_log` (
`response_params`text COMMENT '返回参数', `response_params`text COMMENT '返回参数',
`request_url` varchar(64) DEFAULT NULL COMMENT '请求路径', `request_url` varchar(64) DEFAULT NULL COMMENT '请求路径',
`request_method` varchar(100) DEFAULT NULL COMMENT '请求方法', `request_method` varchar(100) DEFAULT NULL COMMENT '请求方法',
`request_type` varchar(20) DEFAULT NULL COMMENT '请求类型(方式)',
`encry_mode` varchar(20) DEFAULT NULL COMMENT '加密方式:MD5,RSA', `encry_mode` varchar(20) DEFAULT NULL COMMENT '加密方式:MD5,RSA',
`trans_mode` varchar(20) DEFAULT NULL COMMENT '传输方式:POST,GET', `trans_mode` varchar(20) DEFAULT NULL COMMENT '传输方式:POST,GET',
`request_token` varchar(64) DEFAULT NULL COMMENT '客户请求token', `request_token` varchar(64) DEFAULT NULL COMMENT '客户请求token',
`handler_status` varchar(20) NOT NULL DEFAULT '0' COMMENT '处理状态: 0未处理,1已处理'
`request_consuming` varchar(64) DEFAULT NULL COMMENT '请求总耗时', `request_consuming` varchar(64) DEFAULT NULL COMMENT '请求总耗时',
`request_start_time` datetime DEFAULT NULL COMMENT '请求开始时间', `request_start_time` datetime DEFAULT NULL COMMENT '请求开始时间',
`request_end_time` datetime DEFAULT NULL COMMENT '请求结束时间', `request_end_time` datetime DEFAULT NULL COMMENT '请求结束时间',
......
...@@ -136,6 +136,20 @@ ...@@ -136,6 +136,20 @@
<groupId>org.springframework.session</groupId> <groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId> <artifactId>spring-session-data-redis</artifactId>
</dependency> </dependency>
<!-- 代码自动生成依赖 begin -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- velocity -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- 代码自动生成依赖 end-->
</dependencies> </dependencies>
<build> <build>
......
...@@ -21,29 +21,37 @@ public enum OpenApiResultCode implements ResultCode { ...@@ -21,29 +21,37 @@ public enum OpenApiResultCode implements ResultCode {
/** 无效参数 */ /** 无效参数 */
ILLEGAL_ARGUMENT("ILLEGAL_ARGUMENT", "无效参数"), ILLEGAL_ARGUMENT("ILLEGAL_ARGUMENT", "无效参数"),
/** 请求数据异常 */
REQUEST_DATA_EXCEPTION("REQUEST_DATA_EXCEPTION", "请求数据异常"),
/** 名类型不支持 */ /** 名类型不支持 */
SIGN_TYPE_NOT_SUPPORT("SIGN_TYPE_NOT_SUPPORT", "签名类型不支持"), SIGN_TYPE_NOT_SUPPORT("SIGN_TYPE_NOT_SUPPORT", "签名类型不支持"),
/** 数据签名错误 */ /** 数据签名错误 */
DATA_SIGN_ERROR("DATA_SIGN_ERROR", "数据签名错误"), DATA_SIGN_ERROR("DATA_SIGN_ERROR", "数据签名错误"),
/** 公钥格式错误 */ /* *//** 公钥格式错误 *//*
PUBLIC_KEY_FORMAT_ERROR("PUBLIC_KEY_FORMAT_ERROR", "公钥格式错误"), PUBLIC_KEY_FORMAT_ERROR("PUBLIC_KEY_FORMAT_ERROR", "公钥格式错误"),
/** 私钥格式错误 */ *//** 私钥格式错误 *//*
PRIVATE_KEY_FORMAT_ERROR("PRIVATE_KEY_FORMAT_ERROR", "私钥格式错误"), PRIVATE_KEY_FORMAT_ERROR("PRIVATE_KEY_FORMAT_ERROR", "私钥格式错误"),*/
/** 响应数据格式错误 */ /** 响应数据格式错误 */
RESPONSE_DATA_FORMAT_ERROR("RESPONSE_DATA_FORMAT_ERROR", "响应数据格式错误"), RESPONSE_DATA_FORMAT_ERROR("RESPONSE_DATA_FORMAT_ERROR", "响应数据格式错误"),
/** 签名校验错误 */ /** 签名校验错误 */
SIGN_VERIFY_ERROR("SIGN_VERIFY_ERROR", "签名校验错误"), SIGN_VERIFY_ERROR("SIGN_VERIFY_ERROR", "签名校验错误"),
/*
*//** 不支持该信息摘要算法 *//*
NO_SUCH_MD_ALGORITHM("NO_SUCH_MD_ALGORITHM", "不支持该信息摘要算法"),*/
/** 不支持该信息摘要算法 */ /*
NO_SUCH_MD_ALGORITHM("NO_SUCH_MD_ALGORITHM", "不支持该信息摘要算法"), */
/** 信息摘要错误 *//*
/** 信息摘要错误 */
MESSAGE_DIGEST_ERROR("MESSAGE_DIGEST_ERROR", "信息摘要错误"), MESSAGE_DIGEST_ERROR("MESSAGE_DIGEST_ERROR", "信息摘要错误"),
*/
/** 数据加密错误 */ /** 数据加密错误 */
DATA_ENCRYPTION_ERROR("DATA_ENCRYPTION_ERROR", "数据加密错误"),; DATA_ENCRYPTION_ERROR("DATA_ENCRYPTION_ERROR", "数据加密错误"),;
......
package com.jz.dm.config;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.config
* @PROJECT_NAME: jz-dm-parent
* @NAME: GatewayConfiguration
* @DATE: 2021-2-5/10:58
* @DAY_NAME_SHORT: 周五
* @Description: 详情github地址:https://github.com/alibaba/Sentinel/blob/master/sentinel-demo/sentinel-demo-spring-cloud-gateway/src/main/java/com/alibaba/csp/sentinel/demo/spring/sc/gateway/GatewayConfiguration.java
**/
/*@Configuration
public class GatewayConfiguration {
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
@PostConstruct
public void doInit() {
initCustomizedApis();
initGatewayRules();
}
private void initCustomizedApis() {
Set<ApiDefinition> definitions = new HashSet<>();
ApiDefinition api1 = new ApiDefinition("some_customized_api")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/ahas"));
add(new ApiPathPredicateItem().setPattern("/product/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
ApiDefinition api2 = new ApiDefinition("another_customized_api")
.setPredicateItems(new HashSet<ApiPredicateItem>() {{
add(new ApiPathPredicateItem().setPattern("/**")
.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
}});
definitions.add(api1);
definitions.add(api2);
GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
private void initGatewayRules() {
Set<GatewayFlowRule> rules = new HashSet<>();
rules.add(new GatewayFlowRule("aliyun_route")
.setCount(10)
.setIntervalSec(1)
);
rules.add(new GatewayFlowRule("aliyun_route")
.setCount(2)
.setIntervalSec(2)
.setBurst(2)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)
)
);
rules.add(new GatewayFlowRule("httpbin_route")
.setCount(10)
.setIntervalSec(1)
.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)
.setMaxQueueingTimeoutMs(600)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER)
.setFieldName("X-Sentinel-Flag")
)
);
rules.add(new GatewayFlowRule("httpbin_route")
.setCount(1)
.setIntervalSec(1)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("pa")
)
);
rules.add(new GatewayFlowRule("httpbin_route")
.setCount(2)
.setIntervalSec(30)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("type")
.setPattern("warn")
.setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS)
)
);
rules.add(new GatewayFlowRule("some_customized_api")
.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
.setCount(5)
.setIntervalSec(1)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
.setFieldName("pn")
)
);
GatewayRuleManager.loadRules(rules);
}
}*/
...@@ -27,6 +27,13 @@ public class MybatisPlusConfig { ...@@ -27,6 +27,13 @@ public class MybatisPlusConfig {
paginationInterceptor.setLimit(500); paginationInterceptor.setLimit(500);
// 开启 count 的 join 优化,只针对部分 left join // 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor; return paginationInterceptor;
} }
/* @Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor plusInterceptor =new MybatisPlusInterceptor();
// plusInterceptor.addInnerInterceptor();
return plusInterceptor;
}*/
} }
...@@ -21,7 +21,7 @@ import java.util.List; ...@@ -21,7 +21,7 @@ import java.util.List;
* @NAME: SentinelConfig * @NAME: SentinelConfig
* @DATE: 2021-1-7/15:57 * @DATE: 2021-1-7/15:57
* @DAY_NAME_SHORT: 周四 * @DAY_NAME_SHORT: 周四
* @Description: 限流熔断初始化配置 * @Description: 限流熔断初始化配置(只初始化一次,重启系统后配置从新进行初始化)
**/ **/
@Configuration @Configuration
public class SentinelConfig { public class SentinelConfig {
......
...@@ -29,6 +29,8 @@ public class WebAppConfigurer extends WebMvcConfigurerAdapter { ...@@ -29,6 +29,8 @@ public class WebAppConfigurer extends WebMvcConfigurerAdapter {
"/**/*.js", //js静态资源 "/**/*.js", //js静态资源
"/**/*.css" //css静态资源 "/**/*.css" //css静态资源
); );
} }
@Bean @Bean
public AccessLimitInterceptor getAccessLimitInterceptor() { public AccessLimitInterceptor getAccessLimitInterceptor() {
......
...@@ -116,4 +116,14 @@ public class AuthController { ...@@ -116,4 +116,14 @@ public class AuthController {
public Mono<Result> resetSalt(@RequestBody @Validated SaltResetReq req) { public Mono<Result> resetSalt(@RequestBody @Validated SaltResetReq req) {
return Mono.fromSupplier(() -> authService.updateSaltInfo(req)); return Mono.fromSupplier(() -> authService.updateSaltInfo(req));
} }
/**
* @Description: 商城授权续费
* @Author: Mr.zhang
* @Date: 2020-12-26
*/
@ApiOperation("商城授权续费")
@PostMapping(value = "/auth-renew")
public Mono<Result> authRenew(@RequestBody @Validated AuthRenewUpdateReq req) {
return Mono.fromSupplier(() -> authService.updateAuthDate(req));
}
} }
...@@ -9,6 +9,7 @@ import com.jz.dm.models.enity.GatewayRequest; ...@@ -9,6 +9,7 @@ import com.jz.dm.models.enity.GatewayRequest;
import com.jz.dm.models.enity.GatewayResponse; import com.jz.dm.models.enity.GatewayResponse;
import com.jz.dm.models.enity.RequestContext; import com.jz.dm.models.enity.RequestContext;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
...@@ -35,17 +36,18 @@ public class GatewayController { ...@@ -35,17 +36,18 @@ public class GatewayController {
//@RequestMapping(value = "/gateway", consumes = "application/json") //@RequestMapping(value = "/gateway", consumes = "application/json")
@PostMapping(value="/gateway",consumes = "application/json") @PostMapping(value="/gateway",consumes = "application/json")
@ApiOperation(value = "Api请求接口")
public String gateway(@RequestBody String json, HttpServletRequest httpServletRequest, public String gateway(@RequestBody String json, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) { HttpServletResponse httpServletResponse) {
GatewayRequest gatewayRequest = JSON.parseObject(json, GatewayRequest.class); GatewayRequest gatewayRequest = JSON.parseObject(json, GatewayRequest.class);
return doGateway(gatewayRequest, httpServletRequest, httpServletResponse); return doGateway(gatewayRequest, httpServletRequest, httpServletResponse);
} }
@RequestMapping(value = "/gateway") /* @RequestMapping(value = "/gateway")
public String gateway(GatewayRequest gatewayRequest, HttpServletRequest httpServletRequest, public String gateway(GatewayRequest gatewayRequest, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) { HttpServletResponse httpServletResponse) {
return doGateway(gatewayRequest, httpServletRequest, httpServletResponse); return doGateway(gatewayRequest, httpServletRequest, httpServletResponse);
} }*/
private String doGateway(GatewayRequest gatewayRequest, HttpServletRequest httpServletRequest, private String doGateway(GatewayRequest gatewayRequest, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) { HttpServletResponse httpServletResponse) {
......
...@@ -38,9 +38,10 @@ public class ProducerController { ...@@ -38,9 +38,10 @@ public class ProducerController {
*/ */
@ApiOperation("获取文件夹列表") @ApiOperation("获取文件夹列表")
@GetMapping(value = "/getFileCatalog") @GetMapping(value = "/getFileCatalog")
public Mono<Result> getFileCatalog(@RequestParam(name = "projectId")Long projectId) { public Mono<Result> getFileCatalog(@RequestParam(name = "projectId") Long projectId) {
return Mono.fromSupplier(() -> producerService.getFileCatalog(projectId)); return Mono.fromSupplier(() -> producerService.getFileCatalog(projectId));
} }
/** /**
* @Description:创建文件夹 * @Description:创建文件夹
* @Author: Mr.zhang * @Author: Mr.zhang
...@@ -51,6 +52,7 @@ public class ProducerController { ...@@ -51,6 +52,7 @@ public class ProducerController {
public Mono<Result> createProjectFolder(@RequestBody @Valid CreateFolderReq req) { public Mono<Result> createProjectFolder(@RequestBody @Valid CreateFolderReq req) {
return Mono.fromSupplier(() -> producerService.createProjectFolder(req)); return Mono.fromSupplier(() -> producerService.createProjectFolder(req));
} }
/** /**
* @Description:服务发布列表 * @Description:服务发布列表
* @Author: Mr.zhang * @Author: Mr.zhang
...@@ -61,6 +63,7 @@ public class ProducerController { ...@@ -61,6 +63,7 @@ public class ProducerController {
public Mono<Result> getServiceIssueList(@RequestBody @Valid ServiceIssueReq req) { public Mono<Result> getServiceIssueList(@RequestBody @Valid ServiceIssueReq req) {
return Mono.fromSupplier(() -> producerService.getServiceIssueList(req)); return Mono.fromSupplier(() -> producerService.getServiceIssueList(req));
} }
/** /**
* @Description:服务发布/服务取消 * @Description:服务发布/服务取消
* @Author: Mr.zhang * @Author: Mr.zhang
...@@ -68,9 +71,9 @@ public class ProducerController { ...@@ -68,9 +71,9 @@ public class ProducerController {
*/ */
@ApiOperation("服务发布/服务取消") @ApiOperation("服务发布/服务取消")
@GetMapping(value = "/optionApiIssueStatus") @GetMapping(value = "/optionApiIssueStatus")
public Mono<Result> optionApiIssueStatus(@RequestParam(name = "id")Long id, public Mono<Result> optionApiIssueStatus(@RequestParam(name = "id") Long id,
@RequestParam(name="optStatus")Boolean optStatus) { @RequestParam(name = "optStatus") Boolean optStatus) {
return Mono.fromSupplier(() -> producerService.optionApiIssueStatus(id,optStatus)); return Mono.fromSupplier(() -> producerService.optionApiIssueStatus(id, optStatus));
} }
/** /**
...@@ -81,7 +84,7 @@ public class ProducerController { ...@@ -81,7 +84,7 @@ public class ProducerController {
@ApiOperation("Api制作(获取ApiId)") @ApiOperation("Api制作(获取ApiId)")
@PostMapping(value = "/getCustomApiId") @PostMapping(value = "/getCustomApiId")
public Mono<Result> getCustomApiId() { public Mono<Result> getCustomApiId() {
return Mono.fromSupplier(() ->producerService.getCustomApiId()); return Mono.fromSupplier(() -> producerService.getCustomApiId());
} }
/** /**
...@@ -94,6 +97,7 @@ public class ProducerController { ...@@ -94,6 +97,7 @@ public class ProducerController {
public Mono<Result> makeCustomApi(@RequestBody @Valid ApiInterfaceReq req) { public Mono<Result> makeCustomApi(@RequestBody @Valid ApiInterfaceReq req) {
return Mono.fromSupplier(() -> Result.of_success(producerService.saveInterfaceAPi(req))); return Mono.fromSupplier(() -> Result.of_success(producerService.saveInterfaceAPi(req)));
} }
/** /**
* @Description:Api制作(数据查询/标签查询) * @Description:Api制作(数据查询/标签查询)
* @Author: Mr.zhang * @Author: Mr.zhang
...@@ -117,7 +121,7 @@ public class ProducerController { ...@@ -117,7 +121,7 @@ public class ProducerController {
public Mono<Result> saveUpdateAPiInfo(@RequestBody @Valid ApiInterfaceReq req) { public Mono<Result> saveUpdateAPiInfo(@RequestBody @Valid ApiInterfaceReq req) {
return Mono.fromSupplier(() -> producerService.saveUpdateAPiInfo(req)); return Mono.fromSupplier(() -> producerService.saveUpdateAPiInfo(req));
}*/ }*/
/* *//** /* *//**
* @Description:Api制作(数据银行) * @Description:Api制作(数据银行)
* @Author: Mr.zhang * @Author: Mr.zhang
* @Date: 2020-12-26 * @Date: 2020-12-26
...@@ -152,8 +156,8 @@ public class ProducerController { ...@@ -152,8 +156,8 @@ public class ProducerController {
}*/ }*/
/* /*
*//** *//**
* @Description:获取数据源配置 * @Description:获取数据源配置
* @Author: Mr.zhang * @Author: Mr.zhang
* @Date: 2020-12-26 * @Date: 2020-12-26
......
...@@ -79,7 +79,7 @@ public class AuthFilter extends AbstractFilter { ...@@ -79,7 +79,7 @@ public class AuthFilter extends AbstractFilter {
try { try {
ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey()); ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey());
if (null == apiInterface) { if (null == apiInterface) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); throw new GatewayException(GatewayResultCode.REQUEST_INFO_UNEXIST);
} }
//下架状态$$ 检查是否有有效调用的api,如果有就放行,没有就置为无效 //下架状态$$ 检查是否有有效调用的api,如果有就放行,没有就置为无效
if (ApiStatusEnum.SOLDOUT.name().equals(apiInterface.getStatus())) { if (ApiStatusEnum.SOLDOUT.name().equals(apiInterface.getStatus())) {
......
...@@ -42,7 +42,7 @@ public class CheckArgsFilter extends AbstractFilter { ...@@ -42,7 +42,7 @@ public class CheckArgsFilter extends AbstractFilter {
request.setFormat(Format.JSON.name()); request.setFormat(Format.JSON.name());
} }
try { try {
request.setVersion("v1.0.0"); request.setVersion("1.0.0");
//格式,目前仅支持JSON //格式,目前仅支持JSON
Format.valueOf(request.getFormat()); Format.valueOf(request.getFormat());
//请求使用的编码格式,如UTF-8,GBK,GB2312等 //请求使用的编码格式,如UTF-8,GBK,GB2312等
......
package com.jz.dm.filter; package com.jz.dm.filter;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jz.dm.common.constant.Constants; import com.jz.dm.common.constant.Constants;
import com.jz.dm.common.constant.LoggingConstants; import com.jz.dm.common.constant.LoggingConstants;
...@@ -9,6 +10,7 @@ import com.jz.dm.gateway.DefaultOpenApiDispatcher; ...@@ -9,6 +10,7 @@ import com.jz.dm.gateway.DefaultOpenApiDispatcher;
import com.jz.dm.models.enity.DispatchContext; import com.jz.dm.models.enity.DispatchContext;
import com.jz.dm.models.enity.GatewayRequest; import com.jz.dm.models.enity.GatewayRequest;
import com.jz.dm.models.enity.GatewayResponse; import com.jz.dm.models.enity.GatewayResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -19,6 +21,7 @@ import org.springframework.stereotype.Component; ...@@ -19,6 +21,7 @@ import org.springframework.stereotype.Component;
* @author key * @author key
*/ */
@Component("invokeRouteFilter") @Component("invokeRouteFilter")
@Slf4j
public class InvokeRouteFilter extends AbstractFilter { public class InvokeRouteFilter extends AbstractFilter {
/** openapi dispatcher logger */ /** openapi dispatcher logger */
...@@ -41,14 +44,13 @@ public class InvokeRouteFilter extends AbstractFilter { ...@@ -41,14 +44,13 @@ public class InvokeRouteFilter extends AbstractFilter {
protected void internalDoFilter(GatewayRequest request, GatewayResponse response, protected void internalDoFilter(GatewayRequest request, GatewayResponse response,
FilterChain chain) { FilterChain chain) {
String openApiResponse = null; String openApiResponse = null;
//当前系统时间戳
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
try { try {
DispatchContext context = new DispatchContext(); DispatchContext context = new DispatchContext();
context.setApiKey(request.getApiKey());//apiKey context.setApiKey(request.getApiKey());
context.setOpenApiMethod(request.getMethod()); //方法 例如:tradd.add context.setOpenApiMethod(request.getMethod());
context.setOpenApiParams(request.getParams());//入参 context.setOpenApiParams(request.getParams());
context.setOpenApiVersion(request.getVersion()); //版本号 context.setOpenApiVersion(request.getVersion());
//扩展参数 //扩展参数
for (GatewayRequest.Attribute attribute : request.getExtAttributes().values()) { for (GatewayRequest.Attribute attribute : request.getExtAttributes().values()) {
if (attribute != null && attribute.isPass) { if (attribute != null && attribute.isPass) {
...@@ -56,10 +58,11 @@ public class InvokeRouteFilter extends AbstractFilter { ...@@ -56,10 +58,11 @@ public class InvokeRouteFilter extends AbstractFilter {
} }
} }
openApiResponse = defaultOpenApiDispatcher.doDispatch(context); openApiResponse = defaultOpenApiDispatcher.doDispatch(context);
JSONObject jsonObject = (JSONObject) JSON.parse(openApiResponse); JSONObject jsonObject = (JSONObject) JSON.parse(openApiResponse);
response.setAttribute(jsonObject); response.setAttribute(jsonObject);
} finally { }catch (JSONException ex){
log.error("json exchange exception =",ex.getMessage());
}finally {
long elapseTime = System.currentTimeMillis() - startTime; long elapseTime = System.currentTimeMillis() - startTime;
LogUtil.info(DISPATCHER_LOGGER, LogUtil.info(DISPATCHER_LOGGER,
"gateway do default dispatch,request=" + request + ",response=" + openApiResponse "gateway do default dispatch,request=" + request + ",response=" + openApiResponse
......
...@@ -5,7 +5,6 @@ import com.jz.common.utils.Md5; ...@@ -5,7 +5,6 @@ import com.jz.common.utils.Md5;
import com.jz.dm.common.constant.Constants; import com.jz.dm.common.constant.Constants;
import com.jz.dm.common.enums.GatewayResultCode; import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.exception.GatewayException; import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.exception.SignatureException;
import com.jz.dm.common.util.LogUtil; import com.jz.dm.common.util.LogUtil;
import com.jz.dm.common.util.MapUtil; import com.jz.dm.common.util.MapUtil;
import com.jz.dm.models.domian.ApiAuth; import com.jz.dm.models.domian.ApiAuth;
...@@ -52,7 +51,7 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -52,7 +51,7 @@ public class VerifySignFilter extends AbstractFilter {
//需要传入授权码 //需要传入授权码
ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(), authCode); ApiAuth apiAuthInfo = apiInterfaceService.getApiAuthInfo(request.getApiKey(), authCode);
if (null == apiAuthInfo) { if (null == apiAuthInfo) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST); throw new GatewayException(GatewayResultCode.REQUEST_NOT_AUTH);
} }
String sign = Md5.encrypt(signParams, apiAuthInfo.getSalt()); String sign = Md5.encrypt(signParams, apiAuthInfo.getSalt());
if (!request.getSign().equals(sign)) { if (!request.getSign().equals(sign)) {
...@@ -63,9 +62,8 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -63,9 +62,8 @@ public class VerifySignFilter extends AbstractFilter {
} }
} }
chain.doFilter(request, response); chain.doFilter(request, response);
} catch (SignatureException ex) { } catch (GatewayException ex) {
LogUtil.error(LOGGER, ex, LogUtil.error(LOGGER, ex,"sign response error. response=" + response.getResponse());
"sign response error. response=" + response.getResponse());
response.clearAttributes(); response.clearAttributes();
response.setCode(ex.getResultCode().getCode()); response.setCode(ex.getResultCode().getCode());
response.setMsg(ex.getResultCode().getMsg()); response.setMsg(ex.getResultCode().getMsg());
...@@ -73,11 +71,6 @@ public class VerifySignFilter extends AbstractFilter { ...@@ -73,11 +71,6 @@ public class VerifySignFilter extends AbstractFilter {
if (ex instanceof GatewayException) { if (ex instanceof GatewayException) {
throw (GatewayException) ex; throw (GatewayException) ex;
} }
LogUtil.error(LOGGER, ex,
"signatureFilter doFilter error. response=" + response.getResponse());
response.clearAttributes();
response.setCode(GatewayResultCode.UNKNOWN_EXCEPTION.getCode());
response.setMsg(GatewayResultCode.UNKNOWN_EXCEPTION.getMsg());
} }
} }
......
...@@ -45,7 +45,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher { ...@@ -45,7 +45,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
@Resource @Resource
public void setOpenApiServices(List<OpenApiService> openApiServiceList) { public void setOpenApiServices(List<OpenApiService> openApiServiceList) {
for (OpenApiService openApiService : openApiServiceList) { for (OpenApiService openApiService : openApiServiceList) {
String openApiServiceKey = getOpenApiServiceKey(openApiService.getOpenApiMethod(),openApiService.getOpenApiVersion()); String openApiServiceKey = getOpenApiServiceKey(openApiService.getOpenApiMethod(),
openApiService.getOpenApiVersion());
openApiServices.put(openApiServiceKey, openApiService); openApiServices.put(openApiServiceKey, openApiService);
} }
} }
...@@ -58,8 +59,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher { ...@@ -58,8 +59,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
@Override @Override
public String doDispatch(DispatchContext context) { public String doDispatch(DispatchContext context) {
OpenApiResponse response = new OpenApiResponse(); OpenApiResponse response = new OpenApiResponse();
String openApiServiceKey = getOpenApiServiceKey(context.getOpenApiMethod(), String openApiServiceKey = getOpenApiServiceKey(context.getOpenApiMethod(),
context.getOpenApiVersion()); context.getOpenApiVersion());
OpenApiService openApiService = openApiServices.get(openApiServiceKey); OpenApiService openApiService = openApiServices.get(openApiServiceKey);
if (openApiService == null) { if (openApiService == null) {
response.setCode(OpenApiResultCode.ILLEGAL_INTERFACE.getCode()); response.setCode(OpenApiResultCode.ILLEGAL_INTERFACE.getCode());
...@@ -84,8 +85,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher { ...@@ -84,8 +85,8 @@ public class DefaultOpenApiDispatcher implements OpenApiDispatcher {
return JSON.toJSONString(response.getAttributes()); return JSON.toJSONString(response.getAttributes());
} }
if (StringUtil.isEmpty(response.getCode())) { if (StringUtil.isEmpty(response.getCode())) {
response.setCode(OpenApiResultCode.UNKNOWN_EXCEPTION.getCode()); response.setCode(OpenApiResultCode.REQUEST_DATA_EXCEPTION.getCode());
response.setMsg(OpenApiResultCode.UNKNOWN_EXCEPTION.getMsg()); response.setMsg(OpenApiResultCode.REQUEST_DATA_EXCEPTION.getMsg());
} }
return JSON.toJSONString(response.getAttributes()); return JSON.toJSONString(response.getAttributes());
} }
......
...@@ -16,10 +16,10 @@ public interface OpenApiService { ...@@ -16,10 +16,10 @@ public interface OpenApiService {
* @return * @return
*/ */
public String getOpenApiMethod(); public String getOpenApiMethod();
/** /**
* 获取openapi接口版本 * 获取openapi接口版本
* *
* @return * @return
*/ */
public String getOpenApiVersion(); public String getOpenApiVersion();
......
package com.jz.dm.generate;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;
import java.util.Scanner;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.generate
* @PROJECT_NAME: jz-dm-parent
* @NAME: MybatisPlusGenerate
* @DATE: 2021-1-30/12:43
* @DAY_NAME_SHORT: 周六
* @Description:
**/
public class MybatisPlusGenerate {
public static void main(String[] args) {
//1. 全局配置
GlobalConfig config = new GlobalConfig();
// 是否支持AR模式
config.setActiveRecord(true)
// 作者
.setAuthor("ZC")
// 生成路径,最好使用绝对路径,window路径是不一样的
//TODO TODO TODO TODO
.setOutputDir("/Users/xdclass/Desktop/demo/src/main/java")
// 文件覆盖
.setFileOverride(true)
// 主键策略
.setIdType(IdType.AUTO)
.setDateType(DateType.ONLY_DATE)
// 设置生成的service接口的名字的首字母是否为I,默认Service是以I开头的
.setServiceName("%sService")
//实体类结尾名称
.setEntityName("%sDO")
//生成基本的resultMap
.setBaseResultMap(true)
//不使用AR模式
.setActiveRecord(false)
//生成基本的SQL片段
.setBaseColumnList(true);
//2. 数据源配置
DataSourceConfig dsConfig = new DataSourceConfig();
// 设置数据库类型
dsConfig.setDbType(DbType.MYSQL)
.setDriverName("com.mysql.cj.jdbc.Driver")
//TODO TODO TODO TODO
.setUrl("jdbc:mysql://127.0.0.1:3306/xd_shop?useSSL=false")
.setUsername("root")
.setPassword("xdclass.net");
//3. 策略配置globalConfiguration中
StrategyConfig stConfig = new StrategyConfig();
//全局大写命名
stConfig.setCapitalMode(true)
// 数据库表映射到实体的命名策略
.setNaming(NamingStrategy.underline_to_camel)
//使用lombok
.setEntityLombokModel(true)
//使用restcontroller注解
.setRestControllerStyle(true)
// 生成的表, 支持多表一起生成,以数组形式填写
//TODO TODO TODO TODO 两个方式,直接写,或者使用命令行输入
.setInclude("product", "banner", "address", "coupon", "product_order");
//.setInclude(scanner("表名,多个英文逗号分割").split(","));
//TODO TODO TODO TODO
//4. 包名策略配置(生成的项目包名)
PackageConfig pkConfig = new PackageConfig();
pkConfig.setParent("net.xdclass.shopmanager")
.setMapper("mapper")
.setService("service")
.setController("controller")
.setEntity("model")
.setXml("mapper");
//5. 整合配置
AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(config)
.setDataSource(dsConfig)
.setStrategy(stConfig)
.setPackageInfo(pkConfig);
//6. 执行操作
ag.execute();
System.out.println("======= 小滴课堂 Done 相关代码生成完毕 ========");
}
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
}
...@@ -3,6 +3,7 @@ package com.jz.dm.mapper; ...@@ -3,6 +3,7 @@ package com.jz.dm.mapper;
import com.jz.common.base.BaseMapper; import com.jz.common.base.BaseMapper;
import com.jz.dm.models.domian.ApiInterfaceFile; import com.jz.dm.models.domian.ApiInterfaceFile;
import com.jz.dm.models.dto.ApiInterfaceFileDto; import com.jz.dm.models.dto.ApiInterfaceFileDto;
import com.jz.dm.models.dto.DataFileDto;
import java.util.List; import java.util.List;
...@@ -22,4 +23,11 @@ public interface ApiInterfaceFileMapper extends BaseMapper<ApiInterfaceFile> { ...@@ -22,4 +23,11 @@ public interface ApiInterfaceFileMapper extends BaseMapper<ApiInterfaceFile> {
* @return * @return
*/ */
List<ApiInterfaceFileDto> getFileFolderLevel(Long projectId); List<ApiInterfaceFileDto> getFileFolderLevel(Long projectId);
/**
* 查询出所有项目文件列表
* @param projectId
* @return
*/
List<DataFileDto> listFileFolders(Long projectId);
} }
package com.jz.dm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jz.dm.models.domian.ApiSyncingDatasource;
/**api数据源配置信息表 mapper
* @author ybz
*
*/
public interface ApiSyncingDatasourceMapper extends BaseMapper<ApiSyncingDatasource> {
}
package com.jz.dm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jz.dm.models.domian.ApiSyncingDatasourceType;
/**api数据源类型信息表 mapper
* @author ybz
*
*/
public interface ApiSyncingDatasourceTypeMapper extends BaseMapper<ApiSyncingDatasourceType> {
}
...@@ -149,4 +149,15 @@ public class ApiInterface extends BaseObject implements Serializable { ...@@ -149,4 +149,15 @@ public class ApiInterface extends BaseObject implements Serializable {
@TableField(exist = false) @TableField(exist = false)
private String respCode; private String respCode;
/*---------------------------------日志查询--------------------------------*/
/**
* 日志id
* */
@TableField(exist = false)
private Long logId;
/**
* 是否测试
* */
@TableField(exist = false)
private Boolean isTest;
} }
...@@ -105,7 +105,4 @@ public class ApiInterfaceCustom extends BaseObject implements Serializable { ...@@ -105,7 +105,4 @@ public class ApiInterfaceCustom extends BaseObject implements Serializable {
@TableField("page_row") @TableField("page_row")
private Long pageRow; private Long pageRow;
} }
...@@ -9,6 +9,7 @@ import lombok.NoArgsConstructor; ...@@ -9,6 +9,7 @@ import lombok.NoArgsConstructor;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
/** /**
...@@ -58,4 +59,6 @@ public class ApiInterfaceFile extends BaseObject implements Serializable { ...@@ -58,4 +59,6 @@ public class ApiInterfaceFile extends BaseObject implements Serializable {
@TableField("status") @TableField("status")
private String status; private String status;
@TableField(exist = false)
private List<ApiInterfaceFile> children;//子集
} }
...@@ -88,13 +88,17 @@ public class ApiReqLog implements Serializable { ...@@ -88,13 +88,17 @@ public class ApiReqLog implements Serializable {
*/ */
@TableField("request_token") @TableField("request_token")
private String requestToken; private String requestToken;
/**
* 处理状态 默认为0 未处理
*/
@TableField("handler_status")
private String handlerStatus;
/** /**
* 请求总耗时 * 请求总耗时
*/ */
@ApiModelProperty("请求总耗时") @ApiModelProperty("请求总耗时")
@TableField("request_consuming") @TableField("request_consuming")
private Double requestConsuming; private String requestConsuming;
/** /**
* 请求开始时间 * 请求开始时间
*/ */
......
package com.jz.dm.models.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.dto
* @PROJECT_NAME: jz-dm-parent
* @NAME: DataFileDto
* @DATE: 2021-2-4/17:23
* @DAY_NAME_SHORT: 周四
* @Description:
**/
@Data
@ApiModel
public class DataFileDto implements Serializable {
@ApiModelProperty(value = "文件id")
private Long id;
@ApiModelProperty(value = "项目编号")
private Long projectId;
@ApiModelProperty(value = "父类id")
private Long parentId;
@ApiModelProperty(value = "文件名称")
private String name;
@ApiModelProperty(value = "子类集合")
private List<DataFileDto> children;
}
...@@ -87,14 +87,14 @@ public class GatewayRequest implements Serializable { ...@@ -87,14 +87,14 @@ public class GatewayRequest implements Serializable {
return extAttributes; return extAttributes;
} }
/** /**
* @see Object#toString() * @see Object#toString()
*/ */
@Override @Override
public String toString() { public String toString() {
return "GatewayRequest [appKey=" + apiKey + ", method=" + method + ", format=" + format return "GatewayRequest [appKey=" + apiKey + ", method=" + method + ", format=" + format
+ ", signType=" + signType + ", sign=" + sign + ", signType=" + signType + ", sign=" + sign
+ ", timestamp=" + timestamp + ", version=" + version + ", timestamp=" + timestamp
+ ", params=" + params + ", extAttributes=" + ", params=" + params + ", extAttributes="
+ extAttributes + "]"; + extAttributes + "]";
} }
......
package com.jz.dm.models.req.auth;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req.auth
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthRenewUpdateReq
* @DATE: 2021-2-5/16:54
* @DAY_NAME_SHORT: 周五
* @Description:
**/
@ApiModel
@Data
public class AuthRenewUpdateReq implements Serializable {
@ApiModelProperty(value = "授权码",required = true)
@NotNull(message = "授权码不能为空")
private String authCode;
@ApiModelProperty(value = "有效截止时间",required = true)
@NotNull(message = "有效截止时间不能空")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private String validEndTime;
}
...@@ -3,7 +3,6 @@ package com.jz.dm.models.resp; ...@@ -3,7 +3,6 @@ package com.jz.dm.models.resp;
import com.jz.common.bean.BasePageBean; import com.jz.common.bean.BasePageBean;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
......
package com.jz.dm.service; package com.jz.dm.service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jz.common.bean.PageInfoResponse;
import com.jz.common.utils.Result; import com.jz.common.utils.Result;
import com.jz.dm.models.domian.ApiReqLog; import com.jz.dm.models.domian.ApiReqLog;
import com.jz.dm.models.dto.DataBankNumberErrorDto; import com.jz.dm.models.dto.DataBankNumberErrorDto;
import com.jz.dm.models.req.DataBankNumberErrorReq; import com.jz.dm.models.req.DataBankNumberErrorReq;
import com.jz.dm.models.req.LogInfoDetailReq; import com.jz.dm.models.req.LogInfoDetailReq;
import com.jz.dm.models.req.LogInfoListReq; import com.jz.dm.models.req.LogInfoListReq;
import net.sf.json.JSONObject;
import java.util.List;
/** /**
* @author ZC * @author ZC
...@@ -45,9 +41,9 @@ public interface ApiLogService { ...@@ -45,9 +41,9 @@ public interface ApiLogService {
/** /**
* 根据id更新日志 * 根据id更新日志
* @param id * @param id
* @param jsonObject * @param response
*/ */
void updateLog(Long id, JSONObject jsonObject); void updateLog(Long id,String response);
/** /**
* 统计API调用数据 * 统计API调用数据
...@@ -63,4 +59,10 @@ public interface ApiLogService { ...@@ -63,4 +59,10 @@ public interface ApiLogService {
*/ */
Result<DataBankNumberErrorDto> queryNumberAndError(DataBankNumberErrorReq req); Result<DataBankNumberErrorDto> queryNumberAndError(DataBankNumberErrorReq req);
/**
* 根据apikey查询日志信息
* @param apiKey
* @return
*/
Long getReqLogging(String apiKey);
} }
...@@ -91,4 +91,11 @@ public interface AuthService { ...@@ -91,4 +91,11 @@ public interface AuthService {
* @return * @return
*/ */
Result getServiceAuthList(ServiceAuthReq req); Result getServiceAuthList(ServiceAuthReq req);
/**
* 更新授权时间
* @param req
* @return
*/
Result updateAuthDate(AuthRenewUpdateReq req);
} }
package com.jz.dm.service.impl; package com.jz.dm.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
...@@ -16,12 +15,12 @@ import com.jz.dm.models.req.LogInfoDetailReq; ...@@ -16,12 +15,12 @@ import com.jz.dm.models.req.LogInfoDetailReq;
import com.jz.dm.models.req.LogInfoListReq; import com.jz.dm.models.req.LogInfoListReq;
import com.jz.dm.service.ApiLogService; import com.jz.dm.service.ApiLogService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
...@@ -46,8 +45,10 @@ public class ApiLogServiceImpl implements ApiLogService { ...@@ -46,8 +45,10 @@ public class ApiLogServiceImpl implements ApiLogService {
// 加锁 // 加锁
@Autowired @Autowired
private ReentrantRedisLock lock; private ReentrantRedisLock lock;
/** /**
* 日志信息列表 * 日志信息列表
*
* @param req * @param req
* @return * @return
*/ */
...@@ -55,26 +56,26 @@ public class ApiLogServiceImpl implements ApiLogService { ...@@ -55,26 +56,26 @@ public class ApiLogServiceImpl implements ApiLogService {
public IPage<ApiReqLog> listApiLog(LogInfoListReq req) { public IPage<ApiReqLog> listApiLog(LogInfoListReq req) {
IPage<ApiReqLog> page = new Page<>(req.getPageNum(), req.getPageSize()); IPage<ApiReqLog> page = new Page<>(req.getPageNum(), req.getPageSize());
QueryWrapper<ApiReqLog> query = new QueryWrapper<>(); QueryWrapper<ApiReqLog> query = new QueryWrapper<>();
if (StringUtils.isNotBlank(req.getApiKey())){ if (StringUtils.isNotBlank(req.getApiKey())) {
query.eq("api_key",req.getApiKey()); query.eq("api_key", req.getApiKey());
} }
if (StringUtils.isNotBlank(req.getStatus())){ if (StringUtils.isNotBlank(req.getStatus())) {
query.eq("status",req.getStatus()); query.eq("status", req.getStatus());
} }
if (StringUtils.isNotBlank(req.getRequestToken())){ if (StringUtils.isNotBlank(req.getRequestToken())) {
query.eq("request_token",req.getRequestToken()); query.eq("request_token", req.getRequestToken());
} }
if (StringUtils.isNotBlank(req.getCreateDate())){ if (StringUtils.isNotBlank(req.getCreateDate())) {
String startTime = req.getCreateDate().substring(0, 10) + " 00:00:00"; String startTime = req.getCreateDate().substring(0, 10) + " 00:00:00";
String endTime = req.getCreateDate().substring(11, 21) + " 23:59:59"; String endTime = req.getCreateDate().substring(11, 21) + " 23:59:59";
query.between("create_date",startTime, endTime); query.between("create_date", startTime, endTime);
} }
query.eq("is_deleted",0); query.eq("is_deleted", 0);
query.orderByDesc("create_date"); query.orderByDesc("create_date");
IPage<ApiReqLog> apiReqLogIPage = apiReqLogMapper.selectPage(page, query); IPage<ApiReqLog> apiReqLogIPage = apiReqLogMapper.selectPage(page, query);
// 数据银行-历史查询返回参数 // 数据银行-历史查询返回参数
if (req.getHistoryQuery().equals("01") && req.getHistoryQuery() != null){ if (req.getHistoryQuery().equals("01") && req.getHistoryQuery() != null) {
for (ApiReqLog record : apiReqLogIPage.getRecords()) { for (ApiReqLog record : apiReqLogIPage.getRecords()) {
DataBankNumberErrorReq errorReq = new DataBankNumberErrorReq(); DataBankNumberErrorReq errorReq = new DataBankNumberErrorReq();
List<Map<String, String>> reqList = new ArrayList<>(); List<Map<String, String>> reqList = new ArrayList<>();
...@@ -97,6 +98,7 @@ public class ApiLogServiceImpl implements ApiLogService { ...@@ -97,6 +98,7 @@ public class ApiLogServiceImpl implements ApiLogService {
/** /**
* 查询日志详情 * 查询日志详情
*
* @param req * @param req
* @return * @return
*/ */
...@@ -107,27 +109,30 @@ public class ApiLogServiceImpl implements ApiLogService { ...@@ -107,27 +109,30 @@ public class ApiLogServiceImpl implements ApiLogService {
/** /**
* 保存日志 * 保存日志
*
* @param reqLog * @param reqLog
*/ */
@Override @Override
public void insetLogInfo(ApiReqLog reqLog) { public void insetLogInfo(ApiReqLog reqLog) {
apiReqLogMapper.insert(reqLog); apiReqLogMapper.insert(reqLog);
} }
/** /**
* api计量统计 * api计量统计
*
* @param date * @param date
* @return * @return
*/ */
@Override @Override
public Result countAPiCallStat(String date ) { public Result countAPiCallStat(String date) {
Date dateParam =null; Date dateParam = null;
try { try {
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM"); SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM");
dateParam = dateFormat.parse(date); dateParam = dateFormat.parse(date);
}catch (Exception ex){ } catch (Exception ex) {
log.error("时间转换异常:{}",ex.getMessage()); log.error("时间转换异常:{}", ex.getMessage());
} }
return Result.of_success(apiReqLogMapper.countApiCallMonthData(dateParam)); return Result.of_success(apiReqLogMapper.countApiCallMonthData(dateParam));
} }
/** /**
...@@ -150,43 +155,70 @@ public class ApiLogServiceImpl implements ApiLogService { ...@@ -150,43 +155,70 @@ public class ApiLogServiceImpl implements ApiLogService {
} }
/** /**
* 根据id更新日志 * 根据id更新日志
*
* @param id * @param id
* @param jsonObject * @param response
*/ */
@Override @Override
public void updateLog(Long id, JSONObject jsonObject) { public void updateLog(Long id,String response) {
com.alibaba.fastjson.JSONObject jsonObject = null;
if (StringUtils.isNotBlank(response)) {
jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response);
}
try { try {
/*ApiReqLog apiReqLog = apiReqLogMapper.maxId(id);*/ /*ApiReqLog apiReqLog = apiReqLogMapper.maxId(id);*/
ApiReqLog apiReqLog = apiReqLogMapper.selectById(id); ApiReqLog apiReqLog = apiReqLogMapper.selectById(id);
if (null != apiReqLog){ if (null != apiReqLog) {
UpdateWrapper<ApiReqLog> update = new UpdateWrapper<>(); UpdateWrapper<ApiReqLog> update = new UpdateWrapper<>();
if (200 == jsonObject.getInt("code")){ if (null != response){
update.set("status", GeneralStatusTypeEnum.SUCCEED); if (jsonObject.getString("return_code").equals("ESC00000") &&
}else { jsonObject.getString("return_message").equals("SUCCESS")) {
update.set("status", GeneralStatusTypeEnum.FAIL); update.set("status", GeneralStatusTypeEnum.SUCCEED);
} } else {
update.set("response_params",jsonObject.toString()); update.set("status", GeneralStatusTypeEnum.FAIL);
update.set("update_date",new Date()); }
update.set("request_end_time",new Date()); }else {
update.set("status", GeneralStatusTypeEnum.SUCCEED);
}
update.set("response_params", response);
update.set("update_date", new Date());
update.set("request_end_time", new Date());
update.set("handler_status", "1");
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(apiReqLog.getRequestStartTime()); calendar.setTime(apiReqLog.getRequestStartTime());
Long time = System.currentTimeMillis() - calendar.getTimeInMillis() / 1000; Long time = (System.currentTimeMillis() - calendar.getTimeInMillis());
double timeConsuming = time.doubleValue(); update.set("request_consuming", time.toString() + "ms");
update.set("request_consuming",timeConsuming); update.eq("id", id);
update.eq("id",id); if (apiReqLogMapper.update(null, update) == 0) {
if (apiReqLogMapper.update(null,update) == 0){
log.info("~~~~~~~~~~~更新日志信息失败~~~~~~~~"); log.info("~~~~~~~~~~~更新日志信息失败~~~~~~~~");
} }
}else { } else {
log.info("id为:{}",id+"--------------日志信息不存在"); log.info("id为:{}", id + "--------------日志信息不存在");
} }
}catch (Exception ex){ } catch (Exception ex) {
log.error("更新日志返回信息异常:{}",ex.getMessage()); log.error("更新日志返回信息异常:{}", ex.getMessage());
} }
} }
/**
* 根据apiKey查询日志
*
* @param apiKey
* @return
*/
@Override
public Long getReqLogging(String apiKey) {
QueryWrapper<ApiReqLog> queryWra = new QueryWrapper<>();
queryWra.select("id");
queryWra.eq("api_key", apiKey);
queryWra.eq("handler_status", 0);
queryWra.isNull("status");
List<ApiReqLog> apiReqLogs = apiReqLogMapper.selectList(queryWra);
if (!CollectionUtils.isEmpty(apiReqLogs)) {
return apiReqLogs.get(apiReqLogs.size() - 1).getId();
}
return null;
}
} }
...@@ -381,4 +381,28 @@ public class AuthServiceImpl implements AuthService { ...@@ -381,4 +381,28 @@ public class AuthServiceImpl implements AuthService {
} }
return null; return null;
} }
/**
* 更新授权续费信息
* @param req
* @return
*/
@Override
public Result updateAuthDate(AuthRenewUpdateReq req) {
QueryWrapper<ApiAuth> queryWra = new QueryWrapper<>();
queryWra.eq("auth_code",req.getAuthCode());
queryWra.eq("is_deleted",0);
queryWra.eq("status",GeneralStatusTypeEnum.VALID.name());
ApiAuth apiAuth = apiAuthMapper.selectOne(queryWra);
if (null == apiAuth){
return Result.of_error("授权信息不存在!");
}
UpdateWrapper<ApiAuth> updateWra = new UpdateWrapper<>();
updateWra.set("valid_end_time",req.getValidEndTime());
updateWra.eq("id",apiAuth.getId());
if (apiAuthMapper.update(null, updateWra) == 0){
return Result.of_error(ResultMsg.UPDATE_FAIL);
}
return Result.of_success(ResultMsg.UPDATE_SUCCESS);
}
} }
...@@ -16,12 +16,16 @@ import com.jz.dm.common.enums.GeneralStatusTypeEnum; ...@@ -16,12 +16,16 @@ import com.jz.dm.common.enums.GeneralStatusTypeEnum;
import com.jz.dm.common.enums.apiInterface.ApiInfoOutTypeEnum; import com.jz.dm.common.enums.apiInterface.ApiInfoOutTypeEnum;
import com.jz.dm.common.enums.apiInterface.ApiStatusEnum; import com.jz.dm.common.enums.apiInterface.ApiStatusEnum;
import com.jz.dm.common.util.RandomUtil; import com.jz.dm.common.util.RandomUtil;
import com.jz.dm.mapper.*; import com.jz.dm.mapper.ApiInterfaceCustomMapper;
import com.jz.dm.mapper.ApiInterfaceFileMapper;
import com.jz.dm.mapper.ApiInterfaceMapper;
import com.jz.dm.mapper.ApiOpenApiEsFieldsMapper;
import com.jz.dm.models.domian.ApiInterface; import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.models.domian.ApiInterfaceCustom; import com.jz.dm.models.domian.ApiInterfaceCustom;
import com.jz.dm.models.domian.ApiInterfaceFile; import com.jz.dm.models.domian.ApiInterfaceFile;
import com.jz.dm.models.domian.ApiOpenApiEsFields; import com.jz.dm.models.domian.ApiOpenApiEsFields;
import com.jz.dm.models.dto.ApiServiceApplyDto; import com.jz.dm.models.dto.ApiServiceApplyDto;
import com.jz.dm.models.dto.DataFileDto;
import com.jz.dm.models.req.folder.CreateFolderReq; import com.jz.dm.models.req.folder.CreateFolderReq;
import com.jz.dm.models.req.producer.ApiInterfaceReq; import com.jz.dm.models.req.producer.ApiInterfaceReq;
import com.jz.dm.models.req.producer.ServiceIssueReq; import com.jz.dm.models.req.producer.ServiceIssueReq;
...@@ -58,12 +62,8 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -58,12 +62,8 @@ public class ProducerServiceImpl implements ProducerService {
@Resource @Resource
private ApiInterfaceMapper apiInterfaceMapper; private ApiInterfaceMapper apiInterfaceMapper;
@Resource @Resource
private ApiSyncingDatasourceMapper apiSyncingDatasourceMapper;
@Resource
private ApiOpenApiEsFieldsMapper apiOpenApiEsFieldsMapper; private ApiOpenApiEsFieldsMapper apiOpenApiEsFieldsMapper;
@Resource @Resource
private ApiOpenApiEsTagconfigMapper apiOpenApiEsTagconfigMapper;
@Resource
private ApiInterfaceCustomMapper apiInterfaceCustomMapper; private ApiInterfaceCustomMapper apiInterfaceCustomMapper;
@Resource @Resource
private ApiInterfaceFileMapper apiInterfaceFileMapper; private ApiInterfaceFileMapper apiInterfaceFileMapper;
...@@ -99,9 +99,35 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -99,9 +99,35 @@ public class ProducerServiceImpl implements ProducerService {
*/ */
@Override @Override
public Result getFileCatalog(Long projectId) { public Result getFileCatalog(Long projectId) {
return Result.of_success(apiInterfaceFileMapper.getFileFolderLevel(projectId)); List<DataFileDto> interfaceFiles = apiInterfaceFileMapper.listFileFolders(projectId);
ArrayList<DataFileDto> children=null;
if (CollectionUtils.isNotEmpty(interfaceFiles)) {
// for (ApiInterfaceFile interfaceFile : interfaceFiles) {
//if (0 == interfaceFile.getParentId()){
children = recursiveTree(0L, interfaceFiles);
//}
// }
}
return Result.of_success(children);
//return Result.of_success(apiInterfaceFileMapper.getFileFolderLevel(projectId));
} }
private ArrayList<DataFileDto> recursiveTree(Long parentId, List<DataFileDto> allList) {
ArrayList<DataFileDto> files = new ArrayList<>();
for (DataFileDto interfaceFile : allList) {
if (Objects.equals(parentId,interfaceFile.getParentId())){
DataFileDto file = new DataFileDto();
file.setId(interfaceFile.getId());
file.setProjectId(interfaceFile.getProjectId());
file.setParentId(interfaceFile.getParentId());
file.setChildren(recursiveTree(interfaceFile.getId(),allList));
files.add(file);
}
}
/* files.forEach(System.out::println);*/
return files;
}
/** /**
* 创建文件夹 * 创建文件夹
...@@ -116,7 +142,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -116,7 +142,7 @@ public class ProducerServiceImpl implements ProducerService {
if (null == interfaceFile) { if (null == interfaceFile) {
return Result.of_error("父级文件夹不存在!"); return Result.of_error("父级文件夹不存在!");
} }
if (!req.getFileSource().equals(interfaceFile.getFileSource())){ if (!req.getFileSource().equals(interfaceFile.getFileSource())) {
return Result.of_error("父级文件类型错误!"); return Result.of_error("父级文件类型错误!");
} }
} }
...@@ -147,7 +173,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -147,7 +173,7 @@ public class ProducerServiceImpl implements ProducerService {
ApiInterfaceFile interfaceFile = new ApiInterfaceFile(); ApiInterfaceFile interfaceFile = new ApiInterfaceFile();
BeanUtils.copyProperties(req, interfaceFile); BeanUtils.copyProperties(req, interfaceFile);
interfaceFile.setStatus(GeneralStatusTypeEnum.VALID.name()); interfaceFile.setStatus(GeneralStatusTypeEnum.VALID.name());
if (null == interfaceFile.getParentId() || "".equals(interfaceFile.getParentId())){ if (null == interfaceFile.getParentId() || "".equals(interfaceFile.getParentId())) {
interfaceFile.setParentId(0L);//默认父类id为零 interfaceFile.setParentId(0L);//默认父类id为零
} }
if (apiInterfaceFileMapper.insert(interfaceFile) == 0) { if (apiInterfaceFileMapper.insert(interfaceFile) == 0) {
...@@ -211,7 +237,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -211,7 +237,7 @@ public class ProducerServiceImpl implements ProducerService {
updateWra.set("status", ApiStatusEnum.DRAFT.name()); updateWra.set("status", ApiStatusEnum.DRAFT.name());
updateWra.eq("id", id); updateWra.eq("id", id);
updateStatus = apiInterfaceMapper.update(null, updateWra); updateStatus = apiInterfaceMapper.update(null, updateWra);
} }
if (updateStatus > 0) { if (updateStatus > 0) {
return Result.of_success(ResultMsg.UPDATE_SUCCESS); return Result.of_success(ResultMsg.UPDATE_SUCCESS);
} }
...@@ -225,7 +251,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -225,7 +251,7 @@ public class ProducerServiceImpl implements ProducerService {
*/ */
@Override @Override
public Result getCustomApiId() { public Result getCustomApiId() {
return Result.of_success(ResultMsg.SUCCESS,getApiKey()); return Result.of_success(ResultMsg.SUCCESS, getApiKey());
} }
...@@ -239,7 +265,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -239,7 +265,7 @@ public class ProducerServiceImpl implements ProducerService {
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public Result addBigDataMakeApi(ApiInterfaceReq req) { public Result addBigDataMakeApi(ApiInterfaceReq req) {
ApiInterface anInterface = apiInterfaceService.getApiInfo(req.apiKey); ApiInterface anInterface = apiInterfaceService.getApiInfo(req.apiKey);
if (null != anInterface){ if (null != anInterface) {
return Result.of_error("API已存在!"); return Result.of_error("API已存在!");
} }
if ("10002".equals(req.getApiType())) {//数据查询 if ("10002".equals(req.getApiType())) {//数据查询
...@@ -254,7 +280,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -254,7 +280,7 @@ public class ProducerServiceImpl implements ProducerService {
return Result.of_error("限流次数不能小于或者等于零"); return Result.of_error("限流次数不能小于或者等于零");
} }
} }
if (null == req.getSignType() || 0 ==req.getSignType()) { if (null == req.getSignType() || 0 == req.getSignType()) {
return Result.of_error("加密类型错误!"); return Result.of_error("加密类型错误!");
} }
if (StringUtils.isNotBlank(req.getLimitType())) { if (StringUtils.isNotBlank(req.getLimitType())) {
...@@ -274,7 +300,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -274,7 +300,7 @@ public class ProducerServiceImpl implements ProducerService {
} }
} else { //保存 } else { //保存
ApiInterface apiInterface = saveApiBaseData(req); ApiInterface apiInterface = saveApiBaseData(req);
if (saveApiExtendData(req,apiInterface.getId())) { if (saveApiExtendData(req, apiInterface.getId())) {
throw ResponseException.of_error(ResultMsg.INSERT_FAIL); throw ResponseException.of_error(ResultMsg.INSERT_FAIL);
} }
} }
...@@ -289,7 +315,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -289,7 +315,7 @@ public class ProducerServiceImpl implements ProducerService {
* @param req * @param req
*/ */
private void saveTableField(ApiInterfaceReq req) { private void saveTableField(ApiInterfaceReq req) {
if (StringUtils.isBlank(req.getTableFields())){ if (StringUtils.isBlank(req.getTableFields())) {
return; return;
} }
QueryWrapper<ApiOpenApiEsFields> queryOpenApi = new QueryWrapper<>(); QueryWrapper<ApiOpenApiEsFields> queryOpenApi = new QueryWrapper<>();
...@@ -334,7 +360,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -334,7 +360,7 @@ public class ProducerServiceImpl implements ProducerService {
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public Result saveInterfaceAPi(ApiInterfaceReq req) { public Result saveInterfaceAPi(ApiInterfaceReq req) {
ApiInterface anInterface = apiInterfaceService.getApiInfo(req.apiKey); ApiInterface anInterface = apiInterfaceService.getApiInfo(req.apiKey);
if (null != anInterface){ if (null != anInterface) {
return Result.of_error("API已存在!"); return Result.of_error("API已存在!");
} }
if (StringUtils.isNotBlank(req.getLimitType())) { if (StringUtils.isNotBlank(req.getLimitType())) {
...@@ -350,15 +376,15 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -350,15 +376,15 @@ public class ProducerServiceImpl implements ProducerService {
return Result.of_error("限流次数不能小于或者等于零"); return Result.of_error("限流次数不能小于或者等于零");
} }
} }
if (StringUtils.isBlank(req.getTargetUrl())){ if (StringUtils.isBlank(req.getTargetUrl())) {
return Result.of_error("目标地址不存在!"); return Result.of_error("目标地址不存在!");
} }
//校验文件夹信息 //校验文件夹信息
if (checkFileifExist(req.getFileId(), req.getProjectId())) { if (checkFileifExist(req.getFileId(), req.getProjectId())) {
return Result.of_error("目标文件夹不存在!"); return Result.of_error("目标文件夹不存在!");
} }
ApiInterface apiInterface = saveApiBaseData(req); ApiInterface apiInterface = saveApiBaseData(req);
if (saveApiExtendData(req,apiInterface.getId())) { if (saveApiExtendData(req, apiInterface.getId())) {
throw ResponseException.of_error("保存Api扩展信息失败!"); throw ResponseException.of_error("保存Api扩展信息失败!");
} }
//缓存API信息 //缓存API信息
...@@ -368,11 +394,12 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -368,11 +394,12 @@ public class ProducerServiceImpl implements ProducerService {
/** /**
* 保存APi扩展信息 * 保存APi扩展信息
*
* @param id * @param id
* @param req * @param req
* @return * @return
*/ */
private boolean saveApiExtendData(ApiInterfaceReq req,Long id) { private boolean saveApiExtendData(ApiInterfaceReq req, Long id) {
ApiInterfaceCustom custom = new ApiInterfaceCustom(); ApiInterfaceCustom custom = new ApiInterfaceCustom();
BeanUtils.copyProperties(req, custom); BeanUtils.copyProperties(req, custom);
custom.setApiInterfaceId(id); custom.setApiInterfaceId(id);
...@@ -492,9 +519,9 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -492,9 +519,9 @@ public class ProducerServiceImpl implements ProducerService {
ApiInterfaceCustom interfaceCustom = null; ApiInterfaceCustom interfaceCustom = null;
if (null != apiInterface) { if (null != apiInterface) {
QueryWrapper<ApiInterfaceCustom> query = new QueryWrapper<>(); QueryWrapper<ApiInterfaceCustom> query = new QueryWrapper<>();
query.eq("api_interface_id",req.getId()); query.eq("api_interface_id", req.getId());
query.eq("api_key",apiInterface.getApiKey()); query.eq("api_key", apiInterface.getApiKey());
query.eq("is_deleted",0); query.eq("is_deleted", 0);
interfaceCustom = apiInterfaceCustomMapper.selectOne(query); interfaceCustom = apiInterfaceCustomMapper.selectOne(query);
} }
if (null == interfaceCustom) { if (null == interfaceCustom) {
...@@ -583,7 +610,7 @@ public class ProducerServiceImpl implements ProducerService { ...@@ -583,7 +610,7 @@ public class ProducerServiceImpl implements ProducerService {
object.put("targetUrl", req.getTargetUrl()); object.put("targetUrl", req.getTargetUrl());
object.put("outputType", ApiInfoOutTypeEnum.JSON.name()); object.put("outputType", ApiInfoOutTypeEnum.JSON.name());
object.put("reqType", req.getReqType()); object.put("reqType", req.getReqType());
object.put("apiType",req.getApiType()); object.put("apiType", req.getApiType());
String apiKey = TagConstants.OPEN_API_CACHE_KEY + req.getApiKey(); String apiKey = TagConstants.OPEN_API_CACHE_KEY + req.getApiKey();
String paramKey = redisUtils.get(apiKey); String paramKey = redisUtils.get(apiKey);
if (null != paramKey) { if (null != paramKey) {
......
package com.jz.dm.service.request; package com.jz.dm.service.request;
import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils; import com.jz.common.utils.HttpsUtils;
import com.jz.common.utils.RedisUtils; import com.jz.common.utils.RedisUtils;
...@@ -17,8 +18,8 @@ import com.jz.dm.models.domian.ApiAuth; ...@@ -17,8 +18,8 @@ import com.jz.dm.models.domian.ApiAuth;
import com.jz.dm.models.domian.ApiInterface; import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.models.domian.ApiInterfaceCustom; import com.jz.dm.models.domian.ApiInterfaceCustom;
import com.jz.dm.service.ApiInterfaceService; import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.service.ApiLogService;
import com.jz.dm.service.AuthService; import com.jz.dm.service.AuthService;
import com.jz.dm.web.annotation.AccessLimit;
import com.jz.dm.web.annotation.ApiLogAspect; import com.jz.dm.web.annotation.ApiLogAspect;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
...@@ -26,8 +27,6 @@ import org.apache.commons.lang3.StringUtils; ...@@ -26,8 +27,6 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -41,18 +40,18 @@ import java.util.Map; ...@@ -41,18 +40,18 @@ import java.util.Map;
* @DAY_NAME_SHORT: 周三 * @DAY_NAME_SHORT: 周三
* @Description: * @Description:
**/ **/
@Service("apiQueryService") @Service
@Slf4j @Slf4j
public class ApiQueryService extends ApiParamVerify implements OpenApiService { public class ApiQueryService extends ApiParamVerify implements OpenApiService {
@Override @Override
public String getOpenApiMethod() { public String getOpenApiMethod() {
return "query"; return "request";
} }
@Override @Override
public String getOpenApiVersion() { public String getOpenApiVersion() {
return "v1.0.0"; return "1.0.0";
} }
@Autowired @Autowired
...@@ -63,6 +62,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -63,6 +62,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
private RedisUtils redisUtils; private RedisUtils redisUtils;
@Autowired @Autowired
private HttpsUtils httpsUtils; private HttpsUtils httpsUtils;
@Autowired
private ApiLogService reqLogService;
/** /**
* 数据银行扣款链接 * 数据银行扣款链接
...@@ -78,9 +79,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -78,9 +79,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
*/ */
@Override @Override
@ApiLogAspect(description = "API请求日志") @ApiLogAspect(description = "API请求日志")
@AccessLimit(limit = 10000, sec = 1) //@AccessLimit(limit = 10000, sec = 1)
@SentinelResource(value = "api.gateway", fallback = "fallbackGateway") @SentinelResource(value = "api.gateway", fallback = "fallbackGateway",blockHandler = "exceptionHandler")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public void doService(OpenApiRequest request, OpenApiResponse response) { public void doService(OpenApiRequest request, OpenApiResponse response) {
boolean bResult = false; boolean bResult = false;
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams()); JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
...@@ -88,40 +88,48 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -88,40 +88,48 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
ApiAuth apiAuth = null; ApiAuth apiAuth = null;
JSONObject jsonParams = null; JSONObject jsonParams = null;
try { try {
Boolean isTest = parameter.getBoolean("isTest");
String reqParams = parameter.getString("reqParams"); String reqParams = parameter.getString("reqParams");
Map paramMap = null; Map paramMap = null;
if (StringUtils.isNotBlank(reqParams)) { if (StringUtils.isNotBlank(reqParams)) {
jsonParams = JSONObject.parseObject(reqParams); jsonParams = JSONObject.parseObject(reqParams);
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class); paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
jsonParams.put("is_test",false); jsonParams.put("is_test", isTest);
} apiInterface.setIsTest(isTest);
String authCode = parameter.getString("authCode");
if (StringUtils.isBlank(authCode)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
} }
verifyApiInterface(apiInterface); if (!isTest) {//是否是测试
apiAuth = authService.getAuthInfo(authCode); apiInterface.setLogId(reqLogService.getReqLogging(apiInterface.getApiKey()));
verifyAuth(apiAuth); String authCode = parameter.getString("authCode");
//取出缓存数据 if (StringUtils.isBlank(authCode)) {
// String redisReqParam = redisUtils.get(request.getApiKey()); throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
String redisReqParam = null; }
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在 verifyApiInterface(apiInterface);
//解析出API制作成功时的参数配置 apiAuth = authService.getAuthInfo(authCode);
JSONObject jsonObject = JSONObject.parseObject(redisReqParam); verifyAuth(apiAuth);
String targetUrl = jsonObject.getString("targetUrl"); //取出缓存数据
String outputType = jsonObject.getString("outputType"); //String redisReqParam = redisUtils.get(request.getApiKey());
String apiType = jsonObject.getString("apiType"); String redisReqParam = null;
bResult = rangRequestTarget(outputType, targetUrl, paramMap, if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
jsonParams, apiType, apiInterface, response); //解析出API制作成功时的参数配置
} else {//不存在查询数据库 JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
String apiType = jsonObject.getString("apiType");
bResult = rangRequestTarget(outputType, targetUrl, paramMap,
jsonParams, apiType, apiInterface, response);
} else {//不存在查询数据库
bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
//调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
//按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth);
}
} else {
bResult = rangRequestTarget(apiInterface.getOutputType(), bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response); apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
//调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
//按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth);
} }
} catch (Exception ex) { } catch (Exception ex) {
if (ex instanceof GatewayException) { if (ex instanceof GatewayException) {
...@@ -163,9 +171,9 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -163,9 +171,9 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if ("10002".equals(apiType) || "10009".equals(apiType)) {//数据表查询 if ("10002".equals(apiType) || "10009".equals(apiType)) {//数据表查询
return dataTableSelect(outputType, targetUrl, param, jsonParams, apiInterface, response); return dataTableSelect(outputType, targetUrl, param, jsonParams, apiInterface, response);
} else if ("10004".equals(apiType) || "10008".equals(apiType)) {//三方查询 } else if ("10004".equals(apiType) || "10008".equals(apiType)) {//三方查询
return thirdSelect(targetUrl, apiInterface,jsonParams, response); return thirdSelect(targetUrl, apiInterface, jsonParams, response);
} else if ("10005".equals(apiType) || "10007".equals(apiType)) {//数据包查询 } else if ("10005".equals(apiType) || "10007".equals(apiType)) {//数据包查询
return dataBagDownload(targetUrl,jsonParams,response); return dataBagDownload(targetUrl, jsonParams, response,apiInterface);
} else { } else {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR); throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
} }
...@@ -179,12 +187,12 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -179,12 +187,12 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param response * @param response
* @return * @return
*/ */
private boolean thirdSelect(String targetUrl, ApiInterface apiInterface,JSONObject jsonParams, OpenApiResponse response) { private boolean thirdSelect(String targetUrl, ApiInterface apiInterface, JSONObject jsonParams, OpenApiResponse response) {
if ("POST".equalsIgnoreCase(apiInterface.getReqType())) { if ("POST".equalsIgnoreCase(apiInterface.getReqType())) {
return callMethodResponse(httpsUtils.submitPost(targetUrl, jsonParams.toString()), response); return callMethodResponse(httpsUtils.submitPost(targetUrl, jsonParams.toString()), response,apiInterface);
} else { } else {
Map map = JSONObject.parseObject(jsonParams.toString(), Map.class); Map map = JSONObject.parseObject(jsonParams.toString(), Map.class);
return callMethodResponse(httpsUtils.doGet(targetUrl,map), response); return callMethodResponse(httpsUtils.doGet(targetUrl, map), response,apiInterface);
} }
} }
...@@ -194,16 +202,17 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -194,16 +202,17 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param param * @param param
* @return * @return
*/ */
private boolean dataBagDownload(String targetUrl,JSONObject param,OpenApiResponse response) { private boolean dataBagDownload(String targetUrl, JSONObject param, OpenApiResponse response,ApiInterface apiInterface) {
String fileLocation = param.getString("file_location"); String fileLocation = param.getString("file_location");
String datasourceId = param.getString("datasourceId"); String datasourceId = param.getString("datasourceId");
if (null == fileLocation || null == datasourceId) { if (null == fileLocation || null == datasourceId) {
throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST); throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST);
} }
JSONObject requestParams = new JSONObject(); JSONObject requestParams = new JSONObject();
requestParams.put("file_location",fileLocation); requestParams.put("file_location", fileLocation);
requestParams.put("datasourceId",datasourceId); requestParams.put("datasourceId", datasourceId);
HttpDownload.postDownload(targetUrl,requestParams); HttpDownload.postDownload(targetUrl, requestParams);
reqLogService.updateLog(apiInterface.getLogId(),null);
response.setCode(GatewayResultCode.SUCCESS.getCode()); response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg()); response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true; return true;
...@@ -223,7 +232,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -223,7 +232,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
JSONObject jsonParams, ApiInterface apiInterface, OpenApiResponse response) { JSONObject jsonParams, ApiInterface apiInterface, OpenApiResponse response) {
ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, apiInterface); ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, apiInterface);
if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求 if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求
return flowRequestMethod(targetUrl, param, response, apiCustomInfo); return flowRequestMethod(targetUrl, param, response, apiCustomInfo,apiInterface);
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求 } else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求
return jsonRequestMethod(targetUrl, param, jsonParams, response, apiCustomInfo); return jsonRequestMethod(targetUrl, param, jsonParams, response, apiCustomInfo);
} else { } else {
...@@ -270,31 +279,28 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -270,31 +279,28 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @return * @return
*/ */
private boolean flowRequestMethod(String targetUrl, Map<String, String> param, private boolean flowRequestMethod(String targetUrl, Map<String, String> param,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) { OpenApiResponse response, ApiInterfaceCustom apiCustomInfo,
ApiInterface apiInterface) {
String dataSize = param.get("dataSize"); String dataSize = param.get("dataSize");
Integer valueOf = -1; Integer valueOf = -1;
if (StringUtils.isNotBlank(dataSize)) { if (StringUtils.isNotBlank(dataSize)) {
valueOf = Integer.valueOf(dataSize); valueOf = Integer.valueOf(dataSize);
} }
String request$Filed =null;
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param); net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
/* try {
request$Filed = URLEncoder.encode(reqParams.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("流下载入参编码异常-------",e.getMessage());
}*/
JSONObject requestParams = new JSONObject(); JSONObject requestParams = new JSONObject();
requestParams.put("datasourceId",apiCustomInfo.getEsDataSource()); requestParams.put("datasourceId", apiCustomInfo.getEsDataSource());
requestParams.put("query_database",apiCustomInfo.getEsDataBase()); requestParams.put("query_database", apiCustomInfo.getEsDataBase());
requestParams.put("query_table",apiCustomInfo.getEsTable()); requestParams.put("query_table", apiCustomInfo.getEsTable());
requestParams.put("request_fileds",reqParams); requestParams.put("request_fileds", reqParams);
requestParams.put("response_fields",assembleResponseParams(apiCustomInfo.getResponseParam())); requestParams.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));
requestParams.put("data_size",valueOf); requestParams.put("data_size", valueOf);
HttpDownload.postDownload(targetUrl,requestParams); HttpDownload.postDownload(targetUrl, requestParams);
reqLogService.updateLog(apiInterface.getLogId(),null);
response.setCode(GatewayResultCode.SUCCESS.getCode()); response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg()); response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true; return true;
} }
/** /**
* 数据查询--json请求方式 * 数据查询--json请求方式
* *
...@@ -307,6 +313,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -307,6 +313,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
private boolean jsonRequestMethod(String targetUrl, Map<String, String> param, JSONObject jsonParams, private boolean jsonRequestMethod(String targetUrl, Map<String, String> param, JSONObject jsonParams,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) { OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
JSONObject params = new JSONObject(); JSONObject params = new JSONObject();
ApiInterface apiInterface = new ApiInterface();
apiInterface.setApiKey(apiCustomInfo.getApiKey());
try { try {
params.put("datasourceId", apiCustomInfo.getEsDataSource());//数据源id params.put("datasourceId", apiCustomInfo.getEsDataSource());//数据源id
params.put("query_database", apiCustomInfo.getEsDataBase());//数据源库名称 params.put("query_database", apiCustomInfo.getEsDataBase());//数据源库名称
...@@ -314,7 +322,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -314,7 +322,8 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param); net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
params.put("request_fileds", reqParams);//请求参数 params.put("request_fileds", reqParams);//请求参数
params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数 params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数
params.put("is_test",jsonParams.get("is_test"));//是否是测试 Boolean isTest = jsonParams.getBoolean("is_test");
params.put("is_test", isTest);//是否是测试
Integer pageNum = jsonParams.getInteger("page_num"); Integer pageNum = jsonParams.getInteger("page_num");
Integer pageSize = jsonParams.getInteger("page_size"); Integer pageSize = jsonParams.getInteger("page_size");
params.put("page_size", apiCustomInfo.getPageRow()); params.put("page_size", apiCustomInfo.getPageRow());
...@@ -325,12 +334,14 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -325,12 +334,14 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if (null != pageSize) { if (null != pageSize) {
params.put("page_size", pageSize); params.put("page_size", pageSize);
} }
apiInterface.setIsTest(isTest);
} catch (Exception ex) { } catch (Exception ex) {
log.error("数据转换异常:{}", ex.getMessage()); log.error("数据转换异常:{}", ex.getMessage());
ex.printStackTrace(); ex.printStackTrace();
} }
String respResult = httpsUtils.submitPost(targetUrl, params.toString()); String respResult = httpsUtils.submitPost(targetUrl, params.toString());
return callMethodResponse(respResult, response); return callMethodResponse(respResult, response,apiInterface);
} }
/** /**
...@@ -357,7 +368,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -357,7 +368,7 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
* @param result * @param result
* @param response * @param response
*/ */
private boolean callMethodResponse(String result, OpenApiResponse response) { private boolean callMethodResponse(String result, OpenApiResponse response,ApiInterface apiInterface) {
if (null == result) { if (null == result) {
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION); throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
} }
...@@ -365,7 +376,10 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -365,7 +376,10 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
if (null != resp && "ESC00000".equals(resp.getString("return_code"))) { if (null != resp && "ESC00000".equals(resp.getString("return_code"))) {
response.setCode(GatewayResultCode.SUCCESS.getCode()); response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg()); response.setMsg(GatewayResultCode.SUCCESS.getMsg());
response.setAttribute(resp); response.setAttribute("responseResult",resp);
if (null != apiInterface.getLogId()){
reqLogService.updateLog(apiInterface.getLogId(),result);
}
return true; return true;
} else { } else {
log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~"); log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~");
...@@ -406,6 +420,21 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService { ...@@ -406,6 +420,21 @@ public class ApiQueryService extends ApiParamVerify implements OpenApiService {
*/ */
public void fallbackGateway(OpenApiRequest request, OpenApiResponse response) { public void fallbackGateway(OpenApiRequest request, OpenApiResponse response) {
log.info("用户请求过于频繁触发限流接口:ApiKey为:{}" + request.getApiKey()); log.info("用户请求过于频繁触发限流接口:ApiKey为:{}" + request.getApiKey());
throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED); //throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED);
response.setCode(GatewayResultCode.RATE_LIMIT_EXCEEDED.getCode());
response.setMsg(GatewayResultCode.RATE_LIMIT_EXCEEDED.getMsg());
}
/**
* 熔断方法
* @param request
* @param response
*/
public void exceptionHandler(OpenApiRequest request, OpenApiResponse response, BlockException ex) {
log.info("用户请求过于频繁触发限流接口:ApiKey为:{}" + request.getApiKey());
//log.error("异常信息:{}",ex);
//throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED);
response.setCode(GatewayResultCode.RATE_LIMIT_EXCEEDED.getCode());
response.setMsg(GatewayResultCode.RATE_LIMIT_EXCEEDED.getMsg());
} }
} }
/*
package com.jz.dm.service.request; package com.jz.dm.service.request;
import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.jz.common.utils.HttpsUtils;
import com.jz.common.utils.RedisUtils; import com.jz.common.utils.RedisUtils;
import com.jz.dm.common.constant.LoggingConstants;
import com.jz.dm.common.enums.GatewayResultCode; import com.jz.dm.common.enums.GatewayResultCode;
import com.jz.dm.common.enums.apiInterface.ApiInfoOutTypeEnum; import com.jz.dm.common.enums.apiInterface.ApiInfoOutTypeEnum;
import com.jz.dm.common.enums.auth.AuthModeEnum;
import com.jz.dm.common.exception.GatewayException; import com.jz.dm.common.exception.GatewayException;
import com.jz.dm.common.util.OpenApiRequest; import com.jz.dm.common.util.OpenApiRequest;
import com.jz.dm.common.util.OpenApiResponse; import com.jz.dm.common.util.OpenApiResponse;
import com.jz.dm.common.util.stream.HttpDownload;
import com.jz.dm.gateway.OpenApiService; import com.jz.dm.gateway.OpenApiService;
import com.jz.dm.models.domian.ApiAuth;
import com.jz.dm.models.domian.ApiInterface; import com.jz.dm.models.domian.ApiInterface;
import com.jz.dm.models.domian.ApiInterfaceCustom;
import com.jz.dm.service.ApiInterfaceService; import com.jz.dm.service.ApiInterfaceService;
import com.jz.dm.web.annotation.AccessLimit; import com.jz.dm.service.AuthService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import java.util.List;
import java.util.Map; import java.util.Map;
*/
/** /**
* @author ZC * @author ZC
* @PACKAGE_NAME: com.jz.dm.service.request * @PACKAGE_NAME: com.jz.dm.service.request
...@@ -30,76 +40,407 @@ import java.util.Map; ...@@ -30,76 +40,407 @@ import java.util.Map;
* @DATE: 2021-1-25/10:13 * @DATE: 2021-1-25/10:13
* @DAY_NAME_SHORT: 周一 * @DAY_NAME_SHORT: 周一
* @Description: * @Description:
**/ **//*
@Service("apiQueryTestService")
@Service
@Slf4j @Slf4j
public class ApiQueryTestService extends ApiParamVerify implements OpenApiService { public class ApiQueryTestService extends ApiParamVerify implements OpenApiService {
@Override @Override
public String getOpenApiMethod() { public String getOpenApiMethod() {
return "test"; return "data.request";
} }
@Override @Override
public String getOpenApiVersion() { public String getOpenApiVersion() {
return "v1.0.0"; return "1.0.0";
} }
@Autowired @Autowired
private ApiQueryService apiQueryService;
@Resource
private ApiInterfaceService apiInterfaceService; private ApiInterfaceService apiInterfaceService;
@Autowired @Autowired
private AuthService authService;
@Autowired
private RedisUtils redisUtils; private RedisUtils redisUtils;
/** @Autowired
* API测试实现 private HttpsUtils httpsUtils;
*/
/**
* 数据银行扣款链接
*//*
@Value("${data.bank.balanceUrl}")
private String balanceUrl;
*/
/**
* API请求逻辑处理
*
* @param request * @param request
* @param response * @param response
*/ *//*
@Override @Override
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED) // @ApiLogAspect(description = "API请求日志")
@AccessLimit(sec = 1,limit = 1000) //@AccessLimit(limit = 10000, sec = 1)
@SentinelResource(value = "api.gatetest", fallback = "fallbackGateway") @SentinelResource(value = "api.gateway", fallback = "fallbackGateway")
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
public void doService(OpenApiRequest request, OpenApiResponse response) { public void doService(OpenApiRequest request, OpenApiResponse response) {
boolean bResult = false;
JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams()); JSONObject parameter = JSONObject.parseObject(request.getOpenApiParams());
ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey()); ApiInterface apiInterface = apiInterfaceService.getApiInfo(request.getApiKey());
verifyApiInterface(apiInterface); Boolean isTest = parameter.getBoolean("isTest");
String reqParams = parameter.getString("reqParams"); ApiAuth apiAuth = null;
JSONObject jsonParams = null; JSONObject jsonParams = null;
Map paramMap = null; try {
if (StringUtils.isNotBlank(reqParams)) { String reqParams = parameter.getString("reqParams");
jsonParams = JSONObject.parseObject(reqParams); Map paramMap = null;
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class); if (StringUtils.isNotBlank(reqParams)) {
jsonParams.put("is_test",true); jsonParams = JSONObject.parseObject(reqParams);
paramMap = JSONObject.parseObject(jsonParams.getString("request_fileds"), Map.class);
jsonParams.put("is_test",false);
}
if (!isTest){
String authCode = parameter.getString("authCode");
if (StringUtils.isBlank(authCode)) {
throw new GatewayException(GatewayResultCode.ILLEGAL_REQUEST);
}
verifyApiInterface(apiInterface);
apiAuth = authService.getAuthInfo(authCode);
verifyAuth(apiAuth);
//取出缓存数据
//String redisReqParam = redisUtils.get(request.getApiKey());
String redisReqParam = null;
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置
JSONObject jsonObject = JSONObject.parseObject(redisReqParam);
String targetUrl = jsonObject.getString("targetUrl");
String outputType = jsonObject.getString("outputType");
String apiType = jsonObject.getString("apiType");
bResult = rangRequestTarget(outputType, targetUrl, paramMap,
jsonParams, apiType, apiInterface, response);
} else {//不存在查询数据库
bResult = rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
//调用成功请求数据银行扣款
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
//按次调用时处理(处理为已调用)
authService.updateApiAuthStatus(apiAuth);
}
}else {
bResult=rangRequestTarget(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface.getApiType(), apiInterface, response);
}
} catch (Exception ex) {
if (ex instanceof GatewayException) {
//调用失败回调数据银行解冻
if (AuthModeEnum.POWER_CALL_MODE.name().equals(apiAuth.getAuthMode())) {
notifierMinusMoney(parameter, bResult);
}
throw (GatewayException) ex;
}
log.error("~~~~~~~~~~~~~~~请求api信息异常~~~~~~~~~~~~~");
log.error("异常信息:{}", ex.getMessage());
response.setCode(GatewayResultCode.ILLEGAL_REQUEST.getCode());
response.setMsg(GatewayResultCode.ILLEGAL_REQUEST.getMsg());
}
}
*/
/**
* 远程请求获取数据
*
* @param outputType
* @param targetUrl
* @param param
* @param apiType
* @param response
*//*
private boolean rangRequestTarget(String outputType, String targetUrl,
Map<String, String> param, JSONObject jsonParams, String apiType,
ApiInterface apiInterface, OpenApiResponse response) {
if (StringUtils.isBlank(outputType)) {
outputType = ApiInfoOutTypeEnum.JSON.name();
}
if (StringUtils.isBlank(targetUrl)) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
if (StringUtils.isBlank(apiType)) {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
if ("10002".equals(apiType) || "10009".equals(apiType)) {//数据表查询
return dataTableSelect(outputType, targetUrl, param, jsonParams, apiInterface, response);
} else if ("10004".equals(apiType) || "10008".equals(apiType)) {//三方查询
return thirdSelect(targetUrl, apiInterface,jsonParams, response);
} else if ("10005".equals(apiType) || "10007".equals(apiType)) {//数据包查询
return dataBagDownload(targetUrl,jsonParams,response);
} else {
throw new GatewayException(GatewayResultCode.API_TYPE_ERROR);
}
}
*/
/**
* 三方查询 $$ 数据银行+DMP
*
* @param targetUrl
* @param apiInterface
* @param response
* @return
*//*
private boolean thirdSelect(String targetUrl, ApiInterface apiInterface,JSONObject jsonParams, OpenApiResponse response) {
if ("POST".equalsIgnoreCase(apiInterface.getReqType())) {
return callMethodResponse(httpsUtils.submitPost(targetUrl, jsonParams.toString()), response);
} else {
Map map = JSONObject.parseObject(jsonParams.toString(), Map.class);
return callMethodResponse(httpsUtils.doGet(targetUrl,map), response);
}
}
*/
/**
* 数据包下载 $$ 数据银行+DMP
* @param targetUrl
* @param param
* @return
*//*
private boolean dataBagDownload(String targetUrl,JSONObject param,OpenApiResponse response) {
String fileLocation = param.getString("file_location");
String datasourceId = param.getString("datasourceId");
if (null == fileLocation || null == datasourceId) {
throw new GatewayException(GatewayResultCode.DATA_BIG_ADDR_UNEXIST);
}
JSONObject requestParams = new JSONObject();
requestParams.put("file_location",fileLocation);
requestParams.put("datasourceId",datasourceId);
HttpDownload.postDownload(targetUrl,requestParams);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
*/
/**
* 数据表查询
*
* @param outputType
* @param targetUrl
* @param param
* @param apiInterface
* @param response
* @return
*//*
protected boolean dataTableSelect(String outputType, String targetUrl, Map<String, String> param,
JSONObject jsonParams, ApiInterface apiInterface, OpenApiResponse response) {
ApiInterfaceCustom apiCustomInfo = checkParamLegal(param, apiInterface);
if (ApiInfoOutTypeEnum.FLOW.name().equals(outputType)) {//文件流形式请求
return flowRequestMethod(targetUrl, param, response, apiCustomInfo);
} else if (ApiInfoOutTypeEnum.JSON.name().equals(outputType)) { //json格式请求
return jsonRequestMethod(targetUrl, param, jsonParams, response, apiCustomInfo);
} else {
throw new GatewayException(GatewayResultCode.OUTPUT_TYPE_EXCEPTION);
}
}
*/
/**
* 校验参数合法性
*
* @param param
* @param apiInterface
* @return
*//*
private ApiInterfaceCustom checkParamLegal(Map<String, String> param, ApiInterface apiInterface) {
ApiInterfaceCustom apiCustomInfo = apiInterfaceService.getApiCustomInfo(apiInterface.getId());
boolean tag = false;
if (null != apiCustomInfo) {
List<Map> mapList = JSONObject.parseArray(apiCustomInfo.getRequestParam(), Map.class);
if (CollectionUtils.isNotEmpty(mapList)) {
for (Map map : mapList) {
if ((Boolean) map.get("required")) {
String name = (String) map.get("name");
String field = param.get(name);
if (null == field) {
tag = true;
}
}
}
if (tag) {
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY);
}
}
}
return apiCustomInfo;
}
*/
/**
* 数据查询---flow流请求方式
*
* @param targetUrl
* @param param
* @param response
* @return
*//*
private boolean flowRequestMethod(String targetUrl, Map<String, String> param,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
String dataSize = param.get("dataSize");
Integer valueOf = -1;
if (StringUtils.isNotBlank(dataSize)) {
valueOf = Integer.valueOf(dataSize);
}
String request$Filed =null;
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
*/
/* try {
request$Filed = URLEncoder.encode(reqParams.toString(), "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("流下载入参编码异常-------",e.getMessage());
}*//*
JSONObject requestParams = new JSONObject();
requestParams.put("datasourceId",apiCustomInfo.getEsDataSource());
requestParams.put("query_database",apiCustomInfo.getEsDataBase());
requestParams.put("query_table",apiCustomInfo.getEsTable());
requestParams.put("request_fileds",reqParams);
requestParams.put("response_fields",assembleResponseParams(apiCustomInfo.getResponseParam()));
requestParams.put("data_size",valueOf);
HttpDownload.postDownload(targetUrl,requestParams);
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
return true;
}
*/
/**
* 数据查询--json请求方式
*
* @param targetUrl
* @param param
* @param response
* @param apiCustomInfo
* @return
*//*
private boolean jsonRequestMethod(String targetUrl, Map<String, String> param, JSONObject jsonParams,
OpenApiResponse response, ApiInterfaceCustom apiCustomInfo) {
JSONObject params = new JSONObject();
try {
params.put("datasourceId", apiCustomInfo.getEsDataSource());//数据源id
params.put("query_database", apiCustomInfo.getEsDataBase());//数据源库名称
params.put("query_table", apiCustomInfo.getEsTable());//数据源库表
net.sf.json.JSONObject reqParams = net.sf.json.JSONObject.fromObject(param);
params.put("request_fileds", reqParams);//请求参数
params.put("response_fields", assembleResponseParams(apiCustomInfo.getResponseParam()));//响应参数
params.put("is_test",jsonParams.get("is_test"));//是否是测试
Integer pageNum = jsonParams.getInteger("page_num");
Integer pageSize = jsonParams.getInteger("page_size");
params.put("page_size", apiCustomInfo.getPageRow());
params.put("page_num", 1);
if (null != pageNum) {
params.put("page_num", pageNum);
}
if (null != pageSize) {
params.put("page_size", pageSize);
}
} catch (Exception ex) {
log.error("数据转换异常:{}", ex.getMessage());
ex.printStackTrace();
} }
//取出缓存数据 String respResult = httpsUtils.submitPost(targetUrl, params.toString());
// String redisReqParam = redisUtils.get(request.getApiKey()); return callMethodResponse(respResult, response);
String redisReqParam = null; }
if (StringUtils.isNotBlank(redisReqParam)) {//redis中存在
//解析出API制作成功时的参数配置 */
JSONObject jsonObject = JSONObject.parseObject(redisReqParam); /**
String targetUrl = jsonObject.getString("targetUrl"); * 组装响应参数
String outputType = jsonObject.getString("outputType"); *
if (StringUtils.isBlank(outputType)) { * @param respParams
outputType = ApiInfoOutTypeEnum.JSON.name(); * @return
*//*
private String assembleResponseParams(String respParams) {
StringBuilder builder = new StringBuilder();
List<Map> mapList = JSONObject.parseArray(respParams, Map.class);
if (CollectionUtils.isNotEmpty(mapList)) {
for (Map map : mapList) {
String name = (String) map.get("name");
builder.append(name).append(LoggingConstants.SEP);
} }
if (StringUtils.isBlank(targetUrl)) { }
throw new GatewayException(GatewayResultCode.REQUEST_PARAM_EMPTY); return builder.substring(0, builder.length() - 1);
}
*/
/**
* 调用方法处理结果
*
* @param result
* @param response
*//*
private boolean callMethodResponse(String result, OpenApiResponse response) {
if (null == result) {
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
JSONObject resp = JSONObject.parseObject(result);
if (null != resp && "ESC00000".equals(resp.getString("return_code"))) {
response.setCode(GatewayResultCode.SUCCESS.getCode());
response.setMsg(GatewayResultCode.SUCCESS.getMsg());
response.setAttribute(resp);
return true;
} else {
log.error("~~~~~~~~~~~~~~~~远程请求异常~~~~~~~~~~~~~~~~~");
throw new GatewayException(GatewayResultCode.DISTANCE_REQUEST_EXCEPTION);
}
}
*/
/**
* 通知扣款
*
* @param parameter
* @param bResult
*//*
private void notifierMinusMoney(JSONObject parameter, boolean bResult) {
Integer assetsId = parameter.getInteger("assetsId");
Integer userId = parameter.getInteger("userId");
String dataPrice = parameter.getString("dataPrice");
JSONObject jsonReq = new JSONObject();
jsonReq.put("assetsId", assetsId);
jsonReq.put("userId", userId);
jsonReq.put("dataPrice", dataPrice);
jsonReq.put("callStatus", bResult);//true 调用成功 扣款 false 调用失败,解冻金额
String responseResult = httpsUtils.submitPost(balanceUrl + "/mall/financeCustomerAssets/unfreezeMoney", jsonReq.toString());
JSONObject paramsResult = JSONObject.parseObject(responseResult);
if (null != paramsResult) {
if (200 != paramsResult.getInteger("code")) {
log.info("~~~~~~~~~~~~~~~调用数据银行扣款失败~~~~~~~~~~~~~");
throw new GatewayException(GatewayResultCode.CALL_AMOUNT_NOT_ENOUGH);
} }
apiQueryService.dataTableSelect(outputType, targetUrl, paramMap,
jsonParams, apiInterface, response);
} else {//不存在查询数据库
apiQueryService.dataTableSelect(apiInterface.getOutputType(),
apiInterface.getTargetUrl(), paramMap, jsonParams, apiInterface, response);
} }
} }
/**
*/
/**
* 限流返回方法 * 限流返回方法
* *
* @param request * @param request
* @param response * @param response
*/ *//*
public void fallbackGateway(OpenApiRequest request, OpenApiResponse response) { public void fallbackGateway(OpenApiRequest request, OpenApiResponse response) {
log.info("用户请求过于频繁触发限流接口:ApiKey为:{}" + request.getApiKey()); log.info("用户请求过于频繁触发限流接口:ApiKey为:{}" + request.getApiKey());
throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED); throw new GatewayException(GatewayResultCode.RATE_LIMIT_EXCEEDED);
} }
} }
*/
package com.jz.dm.web.aspect; package com.jz.dm.web.aspect;
import com.jz.common.utils.IpUtils; import com.jz.common.utils.IpUtils;
import com.jz.common.utils.JsonUtils; import com.jz.common.utils.JsonUtils;
import com.jz.common.utils.UrlUtil; import com.jz.common.utils.UrlUtil;
...@@ -17,10 +16,8 @@ import lombok.extern.slf4j.Slf4j; ...@@ -17,10 +16,8 @@ import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
...@@ -54,25 +51,26 @@ public class SystemLogAspect { ...@@ -54,25 +51,26 @@ public class SystemLogAspect {
@Resource @Resource
private ApiReqLogMapper apiReqLogMapper; private ApiReqLogMapper apiReqLogMapper;
/* //前置通知切入点 //前置通知切入点
@Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)") @Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
public void beforeAspect() { public void beforeAspect() {
}*/ }
//最终通知切入点 //最终通知切入点
@Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)") /* @Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
public void lastAspect() { public void lastAspect() {
} }*/
//环绕通知切入点 //环绕通知切入点
@Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)") /* @Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
public void aroundAspect() { public void aroundAspect() {
} }*/
@Around("aroundAspect()") /*@Around("aroundAspect()")
public void doAround(ProceedingJoinPoint joinPoint) { public void doAround(ProceedingJoinPoint joinPoint) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
String ip = IpUtils.getIpAddr(request); String ip = IpUtils.getIpAddr(request);
//获取用户请求方法的参数并序列化为JSON格式字符串 //获取用户请求方法的参数并序列化为JSON格式字符串
String params = ""; String params = "";
...@@ -103,11 +101,9 @@ public class SystemLogAspect { ...@@ -103,11 +101,9 @@ public class SystemLogAspect {
reqLog.setRequestIp(ip); reqLog.setRequestIp(ip);
reqLog.setApiKey(jsonObject.getString("apiKey")); reqLog.setApiKey(jsonObject.getString("apiKey"));
reqLog.setRequestParams(params); reqLog.setRequestParams(params);
reqLog.setResponseParams("");//响应参数
reqLog.setRequestUrl(url); reqLog.setRequestUrl(url);
reqLog.setRequestMethod(contextPath); reqLog.setRequestMethod(contextPath);
reqLog.setEncryMode(jsonObject.getString("signType")); //加密方式:MD5,RSA reqLog.setEncryMode(jsonObject.getString("signType")); //加密方式:MD5,RSA
//reqLog.setEncryMode("MD5"); //加密方式暂时写死MD5
reqLog.setTransMode(request.getMethod());//传输方式 GET POST reqLog.setTransMode(request.getMethod());//传输方式 GET POST
reqLog.setRequestToken(jsonParamsList.getString("authCode")); reqLog.setRequestToken(jsonParamsList.getString("authCode"));
reqLog.setRequestStartTime(new Date()); reqLog.setRequestStartTime(new Date());
...@@ -116,10 +112,12 @@ public class SystemLogAspect { ...@@ -116,10 +112,12 @@ public class SystemLogAspect {
if (null != reqLog) { if (null != reqLog) {
apiReqLogMapper.insert(reqLog); apiReqLogMapper.insert(reqLog);
} }
/* Object result = joinPoint.proceed(joinPoint.getArgs()); Object proceed = joinPoint.proceed();
System.out.println("proceed"+proceed);
Object result = joinPoint.proceed(joinPoint.getArgs());
jsonResult = JSONObject.fromObject(result); jsonResult = JSONObject.fromObject(result);
log.info("around响应结果为{}", jsonResult); log.info("around响应结果为{}", jsonResult);
apiLogService.updateLog(reqLog.getId(), jsonResult);*/ apiLogService.updateLog(reqLog.getId(), jsonResult);
} catch (GatewayException ex) { } catch (GatewayException ex) {
log.info("切面处理保存异常信息:{}", ex.getMessage()); log.info("切面处理保存异常信息:{}", ex.getMessage());
apiLogService.updateLog(reqLog.getId(), jsonResult); apiLogService.updateLog(reqLog.getId(), jsonResult);
...@@ -127,10 +125,9 @@ public class SystemLogAspect { ...@@ -127,10 +125,9 @@ public class SystemLogAspect {
throwable.printStackTrace(); throwable.printStackTrace();
} }
} }
} }*/
/*@Before("beforeAspect()") @Before("beforeAspect()")
public void doBefore(JoinPoint joinPoint) { public void doBefore(JoinPoint joinPoint) {
//日志信息收集切面
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = IpUtils.getIpAddr(request); String ip = IpUtils.getIpAddr(request);
//获取用户请求方法的参数并序列化为JSON格式字符串 //获取用户请求方法的参数并序列化为JSON格式字符串
...@@ -140,47 +137,55 @@ public class SystemLogAspect { ...@@ -140,47 +137,55 @@ public class SystemLogAspect {
params += JsonUtils.objectToJson(joinPoint.getArgs()[i]) + ";"; params += JsonUtils.objectToJson(joinPoint.getArgs()[i]) + ";";
} }
} }
if (StringUtils.isNotBlank(params)) {
params = params.substring(0, params.length() - 1).split(";")[0];
}
//获取请求路径 //获取请求路径
String url = UrlUtil.getServerUrl(request); String url = UrlUtil.getServerUrl(request);
// 访问项目名 // 访问项目名
String contextPath = request.getContextPath(); String contextPath = request.getContextPath();
//JSONObject jsonObject = JSONObject.parseObject(params); log.info("around请求参数为{}", params);
//String param = jsonObject.getString("params"); //动态修改其参数
//JSONObject object = JSONObject.parseObject(param); //注意,如果调用joinPoint.proceed()方法,则修改的参数值不会生效,必须调用joinPoint.proceed(Object[] args)
//String token = object.getString("token"); com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(params);
try { String paramsList = jsonObject.getString("openApiParams");
log.info("~~~~~~~~~~~~~~~~~~~~~~~前置通知记录请求信息~~~~~~~~~~~~~~~~"); com.alibaba.fastjson.JSONObject jsonParamsList = com.alibaba.fastjson.JSONObject.parseObject(paramsList);
ApiReqLog reqLog = new ApiReqLog(); ApiReqLog reqLog = new ApiReqLog();
reqLog.setRequestIp(ip); JSONObject jsonResult = null;
// reqLog.setApiKey(jsonObject.getString("apiKey")); Boolean isTest = jsonParamsList.getBoolean("isTest");
reqLog.setRequestParams(params); //不保存测试数据
reqLog.setResponseParams("");//响应参数 if (!isTest) {
reqLog.setRequestUrl(url); try {
reqLog.setRequestMethod(contextPath); reqLog.setRequestIp(ip);
reqLog.setEncryMode(SignType.MD5.name()); //加密方式:MD5,RSA reqLog.setApiKey(jsonObject.getString("apiKey"));
reqLog.setTransMode(request.getMethod());//传输方式 GET POST reqLog.setRequestParams(params);
// reqLog.setRequestToken(token); reqLog.setRequestUrl(url);
reqLog.setRequestTime(new Date()); reqLog.setEncryMode("MD5");
reqLog.setRemark(getServiceMethodDescription(joinPoint)); reqLog.setTransMode(request.getMethod());
System.out.println(reqLog); reqLog.setRequestToken(jsonParamsList.getString("authCode"));
if (null != reqLog) { reqLog.setRequestStartTime(new Date());
apiLogService.insetLogInfo(reqLog); reqLog.setRemark(getServiceMethodDescription(joinPoint));
log.info("请求参数:", reqLog);
if (null != reqLog) {
apiReqLogMapper.insert(reqLog);
}
} catch (GatewayException ex) {
log.error("切面处理保存异常信息:{}", ex.getMessage());
/* apiLogService.updateLog(reqLog.getId(), jsonResult);*/
} catch (Throwable throwable) {
throwable.printStackTrace();
} }
} catch (Exception e) {
log.error("~~~~~~~~~~~~~~~~~~~~~~~前置通知异常~~~~~~~~~~~~~~~~~~~~~~~");
log.error("异常信息{}", e.getMessage());
} }
}*/ }
/** /* *//**
* 返回异常通知,返回抛出异常的时候执行的通知,可以获得返回的异常 * 返回异常通知,返回抛出异常的时候执行的通知,可以获得返回的异常
* 可以访问到异常对象,且可以指定在出现特定异常的时候再执行通知代码 * 可以访问到异常对象,且可以指定在出现特定异常的时候再执行通知代码
*/ *//*
@AfterReturning(value = "lastAspect()", returning = "result") @AfterReturning(value = "lastAspect()", returning = "result")
public void afterReturn(JoinPoint joinPoint, Object result) { public void afterReturn(JoinPoint joinPoint, Object result) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest(); .getRequest();
log.info("~~~~~~~~~~~~~~~~~~~~~~最终通知执行~~~~~~~~~~~~~~~~~~~~~~~~");
String classType = joinPoint.getTarget().getClass().getName(); String classType = joinPoint.getTarget().getClass().getName();
try { try {
Class<?> clazz = Class.forName(classType); Class<?> clazz = Class.forName(classType);
...@@ -199,8 +204,8 @@ public class SystemLogAspect { ...@@ -199,8 +204,8 @@ public class SystemLogAspect {
JSONObject jsonObject = JSONObject.fromObject(args[1]); JSONObject jsonObject = JSONObject.fromObject(args[1]);
Map mapResult = (Map) jsonObject; Map mapResult = (Map) jsonObject;
//将返回的result参数取出 //将返回的result参数取出
/* Map<String, Object> res = (Map<String, Object>) mapResult.get("data"); Map<String, Object> res = (Map<String, Object>) mapResult.get("attributes");
Integer id = (Integer) res.get("id");*/ Integer id = (Integer) res.get("id");
JSONObject data = jsonObject.getJSONObject("attributes"); JSONObject data = jsonObject.getJSONObject("attributes");
log.info("最终通知得到结果:{}",jsonObject); log.info("最终通知得到结果:{}",jsonObject);
// apiLogService.updateLog(reqLog.getId(), jsonResult); // apiLogService.updateLog(reqLog.getId(), jsonResult);
...@@ -210,7 +215,7 @@ public class SystemLogAspect { ...@@ -210,7 +215,7 @@ public class SystemLogAspect {
log.error("异常信息{}", e.getMessage()); log.error("异常信息{}", e.getMessage());
} }
} }*/
/** /**
* 获取注解中对方法的描述信息 用于service层注解 * 获取注解中对方法的描述信息 用于service层注解
......
...@@ -25,18 +25,18 @@ spring: ...@@ -25,18 +25,18 @@ spring:
transport: transport:
# sentinel-dashboard服务地址 # sentinel-dashboard-1.6.0 的访问路径 , # sentinel-dashboard服务地址 # sentinel-dashboard-1.6.0 的访问路径 ,
#启动方式java -jar sentinel-dashboard-1.6.0.jar --server.port=9090 #启动方式java -jar sentinel-dashboard-1.6.0.jar --server.port=9090
dashboard: localhost:8080 dashboard: 127.0.0.1:8080
heartbeat-interval-ms: 500 heartbeat-interval-ms: 500
# 项目本地会启动一个服务的端口号,默认8719,用于与sentinel-dashboard通讯 # 项目本地会启动一个服务的端口号,默认8719,用于与sentinel-dashboard通讯
port: 8719 port: 8719
#服务器ip #服务器ip
clientIp: 192.168.1.114 # clientIp: 192.168.1.114
filter: # filter:
# 需要进行限流监控的接口,多个匹配用逗号隔开 # 需要进行限流监控的接口,多个匹配用逗号隔开
url-patterns: /* # url-patterns: /*
servlet: # servlet:
# 触发限流后重定向的页面 # 触发限流后重定向的页面
block-page: /sentinel/block # block-page: /sentinel/block
#取消Sentinel控制台懒加载 #取消Sentinel控制台懒加载
eager: true eager: true
......
...@@ -23,4 +23,13 @@ ...@@ -23,4 +23,13 @@
) src ) src
ORDER BY id ORDER BY id
</select> </select>
<select id="listFileFolders" resultType="com.jz.dm.models.dto.DataFileDto">
SELECT id AS id,
project_id AS projectId,
file_name AS fileName,
parent_id AS parentId
FROM t_api_interface_file
WHERE project_id =#{projectId}
AND `status`='VALID' AND is_deleted =0
</select>
</mapper> </mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jz.dm.mapper.ApiSyncingDatasourceMapper" >
<sql id="Base_Column_List">
id, datasource_type, datasource_name, datasource_desc, project_id,
jdbc_url, db_name, user_name, password, endpoint,
bucket, access_id, access_key, protocol, host,
port, default_fs, table_schema, data_status, remark,
create_date, create_user, update_date, update_user, is_deleted
</sql>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jz.dm.mapper.ApiSyncingDatasourceTypeMapper" >
<sql id="Base_Column_List">
id, datasource, datasource_catecode, datasource_catename, datasource_type,
img_url, data_status, is_enabled, datasource_catetype, driver_class_name,
is_enable_test, default_source_script, default_target_script, is_enable_source, is_enable_target,
remark, create_date, create_user, update_date, update_user,
is_deleted
</sql>
</mapper>
...@@ -24,12 +24,14 @@ public class UrlUtil { ...@@ -24,12 +24,14 @@ public class UrlUtil {
int port = request.getServerPort(); int port = request.getServerPort();
// 访问项目名 // 访问项目名
String contextPath = request.getContextPath(); String contextPath = request.getContextPath();
String url = "%s://%s%s%s"; //接口路径
String requestURI = request.getRequestURI();
String url = "%s://%s%s%s%s";
String portStr = ""; String portStr = "";
if (port != 80) { if (port != 80) {
portStr += ":" + port; portStr += ":" + port;
} }
return String.format(url, agreement, serverName, portStr, contextPath); return String.format(url, agreement, serverName, portStr, contextPath,requestURI);
} }
} }
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