Commit 3251e865 by yuwei

2.0.0项目初始化

parent 9fba3e63
...@@ -55,7 +55,7 @@ hystrix: ...@@ -55,7 +55,7 @@ hystrix:
strategy: SEMAPHORE strategy: SEMAPHORE
thread: thread:
timeoutInMilliseconds: 5000 #断路器超时时间,默认1000ms timeoutInMilliseconds: 5000 #断路器超时时间,默认1000ms
dataApiHystrix: #api调用方法的超时时间 60s dataApiCallHystrix: #api调用方法的超时时间 60s
fallback: fallback:
enabled: true enabled: true
execution: execution:
......
...@@ -84,8 +84,9 @@ spring: ...@@ -84,8 +84,9 @@ spring:
args: args:
name: dataMarketHystrix name: dataMarketHystrix
fallbackUri: forward:/fallback fallbackUri: forward:/fallback
- id: datax-service-data-market # 数据API调用
uri: lb://datax-service-data-market - id: datax-service-data-api-call
uri: lb://datax-service-data-api-call
predicates: predicates:
- Path=/data/api/** - Path=/data/api/**
filters: filters:
...@@ -93,5 +94,5 @@ spring: ...@@ -93,5 +94,5 @@ spring:
- StripPrefix=2 - StripPrefix=2
- name: Hystrix - name: Hystrix
args: args:
name: dataApiHystrix name: dataApiCallHystrix
fallbackUri: forward:/fallback fallbackUri: forward:/fallback
\ No newline at end of file
# 数据源配置
spring:
redis:
database: 1
host: 192.168.234.101
port: 6379
password: 1234@abcd # 密码(默认为空)
timeout: 6000ms # 连接超时时长(毫秒)
lettuce:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
datasource:
dynamic:
type: com.zaxxer.hikari.HikariDataSource
hikari:
auto-commit: false
connection-timeout: 30000
idle-timeout: 25000
login-timeout: 5
max-lifetime: 30000
read-only: false
validation-timeout: 3000
maximum-pool-size: 15
minimum-idle: 5
pool-name: DataxHikariCP
connection-test-query: SELECT 1 FROM DUAL
data-source-properties:
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
useLocalSessionState: true
rewriteBatchedStatements: true
cacheResultSetMetadata: true
cacheServerConfiguration: true
elideSetAutoCommits: true
maintainTimeStats: false
primary: mysql
datasource:
mysql:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.234.100:3306/data_cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 1234@abcd
mybatis-plus:
mapper-locations: classpath*:mapper/*Mapper.xml
type-aliases-package: cn.datax.service.data.market.api.entity
global-config:
db-config:
id-type: ASSIGN_ID
banner: false
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# spring security 配置
security:
oauth2:
client:
access-token-uri: http://localhost:8612/auth/oauth/token
user-authorization-uri: http://localhost:8612/auth/oauth/authorize
client-id: datax
client-secret: 123456
scope: all
resource:
loadBalanced: true
token-info-uri: http://localhost:8612/auth/oauth/check_token
\ No newline at end of file
...@@ -31,12 +31,15 @@ public class SwaggerProvider implements SwaggerResourcesProvider { ...@@ -31,12 +31,15 @@ public class SwaggerProvider implements SwaggerResourcesProvider {
routeLocator.getRoutes().subscribe(route -> routes.add(route.getId())); routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
//结合配置的route-路径(Path),和route过滤,只获取有效的route节点 //结合配置的route-路径(Path),和route过滤,只获取有效的route节点
gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())) gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId()))
.forEach(routeDefinition -> routeDefinition.getPredicates().stream() .forEach(routeDefinition -> {
.filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName())) routeDefinition.getPredicates().stream()
.filter(predicateDefinition -> !"datax-auth".equalsIgnoreCase(routeDefinition.getId())) .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
.forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(), .filter(predicateDefinition -> !"datax-auth".equalsIgnoreCase(routeDefinition.getId()) ||
predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0") !"datax-service-data-api-call".equalsIgnoreCase(routeDefinition.getId()))
.replace("/**", API_URI))))); .forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(),
predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
.replace("/**", API_URI))));
});
return resources; return resources;
} }
......
...@@ -10,8 +10,8 @@ public class DataSourceServiceFeignFallbackFactory implements FallbackFactory<Da ...@@ -10,8 +10,8 @@ public class DataSourceServiceFeignFallbackFactory implements FallbackFactory<Da
@Override @Override
public DataSourceServiceFeign create(Throwable throwable) { public DataSourceServiceFeign create(Throwable throwable) {
DataSourceServiceFeignFallbackImpl userServiceFeignFallback = new DataSourceServiceFeignFallbackImpl(); DataSourceServiceFeignFallbackImpl dataSourceServiceFeignFallback = new DataSourceServiceFeignFallbackImpl();
userServiceFeignFallback.setCause(throwable); dataSourceServiceFeignFallback.setCause(throwable);
return userServiceFeignFallback; return dataSourceServiceFeignFallback;
} }
} }
...@@ -71,6 +71,11 @@ ...@@ -71,6 +71,11 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.datax</groupId> <groupId>cn.datax</groupId>
<artifactId>system-service-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>data-factory-service-api</artifactId> <artifactId>data-factory-service-api</artifactId>
<version>2.0.0</version> <version>2.0.0</version>
</dependency> </dependency>
......
...@@ -12,7 +12,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients; ...@@ -12,7 +12,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDataMybatis @EnableDataMybatis
@EnableDataRedis @EnableDataRedis
@EnableDataLog @EnableDataLog
@EnableFeignClients(basePackages = {"cn.datax.service.data.factory.api.feign"}) @EnableFeignClients(basePackages = {"cn.datax.service.system.api.feign", "cn.datax.service.data.factory.api.feign"})
@SpringCloudApplication @SpringCloudApplication
public class DataFactoryApplication { public class DataFactoryApplication {
......
package cn.datax.service.data.factory.mapstruct;
import cn.datax.service.data.factory.api.dto.DataSetDto;
import cn.datax.service.data.factory.api.entity.DataSetEntity;
import cn.datax.service.data.factory.api.vo.DataSetVo;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-04-06T12:17:20+0800",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_231 (Oracle Corporation)"
)
@Component
public class DataSetMapperImpl implements DataSetMapper {
@Override
public DataSetDto toDTO(DataSetEntity arg0) {
if ( arg0 == null ) {
return null;
}
DataSetDto dataSetDto = new DataSetDto();
dataSetDto.setId( arg0.getId() );
dataSetDto.setSourceId( arg0.getSourceId() );
dataSetDto.setSetName( arg0.getSetName() );
dataSetDto.setRemark( arg0.getRemark() );
dataSetDto.setSetSql( arg0.getSetSql() );
return dataSetDto;
}
@Override
public List<DataSetDto> toDTO(List<DataSetEntity> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataSetDto> list = new ArrayList<DataSetDto>( arg0.size() );
for ( DataSetEntity dataSetEntity : arg0 ) {
list.add( toDTO( dataSetEntity ) );
}
return list;
}
@Override
public DataSetVo toVO(DataSetEntity arg0) {
if ( arg0 == null ) {
return null;
}
DataSetVo dataSetVo = new DataSetVo();
dataSetVo.setId( arg0.getId() );
dataSetVo.setStatus( arg0.getStatus() );
dataSetVo.setCreateTime( arg0.getCreateTime() );
dataSetVo.setSourceId( arg0.getSourceId() );
dataSetVo.setSetName( arg0.getSetName() );
dataSetVo.setRemark( arg0.getRemark() );
dataSetVo.setSetSql( arg0.getSetSql() );
return dataSetVo;
}
@Override
public List<DataSetVo> toVO(List<DataSetEntity> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataSetVo> list = new ArrayList<DataSetVo>( arg0.size() );
for ( DataSetEntity dataSetEntity : arg0 ) {
list.add( toVO( dataSetEntity ) );
}
return list;
}
@Override
public DataSetEntity toEntity(DataSetDto arg0) {
if ( arg0 == null ) {
return null;
}
DataSetEntity dataSetEntity = new DataSetEntity();
dataSetEntity.setId( arg0.getId() );
dataSetEntity.setSourceId( arg0.getSourceId() );
dataSetEntity.setSetName( arg0.getSetName() );
dataSetEntity.setRemark( arg0.getRemark() );
dataSetEntity.setSetSql( arg0.getSetSql() );
return dataSetEntity;
}
@Override
public List<DataSetEntity> toEntity(List<DataSetDto> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataSetEntity> list = new ArrayList<DataSetEntity>( arg0.size() );
for ( DataSetDto dataSetDto : arg0 ) {
list.add( toEntity( dataSetDto ) );
}
return list;
}
}
package cn.datax.service.data.factory.mapstruct;
import cn.datax.service.data.factory.api.dto.DataSourceDto;
import cn.datax.service.data.factory.api.entity.DataSourceEntity;
import cn.datax.service.data.factory.api.vo.DataSourceVo;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-04-06T12:17:20+0800",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_231 (Oracle Corporation)"
)
@Component
public class DataSourceMapperImpl implements DataSourceMapper {
@Override
public DataSourceDto toDTO(DataSourceEntity arg0) {
if ( arg0 == null ) {
return null;
}
DataSourceDto dataSourceDto = new DataSourceDto();
dataSourceDto.setId( arg0.getId() );
dataSourceDto.setDbType( arg0.getDbType() );
dataSourceDto.setSourceName( arg0.getSourceName() );
dataSourceDto.setRemark( arg0.getRemark() );
dataSourceDto.setDbSchema( arg0.getDbSchema() );
return dataSourceDto;
}
@Override
public List<DataSourceDto> toDTO(List<DataSourceEntity> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataSourceDto> list = new ArrayList<DataSourceDto>( arg0.size() );
for ( DataSourceEntity dataSourceEntity : arg0 ) {
list.add( toDTO( dataSourceEntity ) );
}
return list;
}
@Override
public DataSourceVo toVO(DataSourceEntity arg0) {
if ( arg0 == null ) {
return null;
}
DataSourceVo dataSourceVo = new DataSourceVo();
dataSourceVo.setId( arg0.getId() );
dataSourceVo.setStatus( arg0.getStatus() );
dataSourceVo.setCreateTime( arg0.getCreateTime() );
dataSourceVo.setDbType( arg0.getDbType() );
dataSourceVo.setSourceName( arg0.getSourceName() );
dataSourceVo.setRemark( arg0.getRemark() );
dataSourceVo.setDbSchema( arg0.getDbSchema() );
return dataSourceVo;
}
@Override
public List<DataSourceVo> toVO(List<DataSourceEntity> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataSourceVo> list = new ArrayList<DataSourceVo>( arg0.size() );
for ( DataSourceEntity dataSourceEntity : arg0 ) {
list.add( toVO( dataSourceEntity ) );
}
return list;
}
@Override
public DataSourceEntity toEntity(DataSourceDto arg0) {
if ( arg0 == null ) {
return null;
}
DataSourceEntity dataSourceEntity = new DataSourceEntity();
dataSourceEntity.setId( arg0.getId() );
dataSourceEntity.setDbType( arg0.getDbType() );
dataSourceEntity.setSourceName( arg0.getSourceName() );
dataSourceEntity.setRemark( arg0.getRemark() );
dataSourceEntity.setDbSchema( arg0.getDbSchema() );
return dataSourceEntity;
}
@Override
public List<DataSourceEntity> toEntity(List<DataSourceDto> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataSourceEntity> list = new ArrayList<DataSourceEntity>( arg0.size() );
for ( DataSourceDto dataSourceDto : arg0 ) {
list.add( toEntity( dataSourceDto ) );
}
return list;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>data-market-service-parent</artifactId>
<groupId>cn.datax</groupId>
<version>2.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>2.0.0</version>
<description>数据市场API调用</description>
<artifactId>data-market-service-api-call</artifactId>
<dependencies>
<!--web 模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--配置中心客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>datax-common-mybatis</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>datax-common-redis</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>datax-common-security</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>datax-common-database</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>datax-common-log</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>system-service-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>data-factory-service-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId>
<artifactId>data-market-service-api</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package cn.datax.service.data.market.api.call;
import cn.datax.common.log.annotation.EnableDataLog;
import cn.datax.common.mybatis.annotation.EnableDataMybatis;
import cn.datax.common.redis.annotation.EnableDataRedis;
import cn.datax.common.security.annotation.EnableDataServerProtect;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDataServerProtect
@EnableDataMybatis
@EnableDataRedis
@EnableDataLog
@EnableFeignClients(basePackages = {"cn.datax.service.system.api.feign", "cn.datax.service.data.factory.api.feign", "cn.datax.service.data.market.api.feign"})
@SpringCloudApplication
public class DataApiCallApplication {
public static void main(String[] args) {
SpringApplication.run(DataApiCallApplication.class);
}
}
package cn.datax.service.data.market.config; package cn.datax.service.data.market.api.call.config;
import cn.datax.common.core.DataConstant; import cn.datax.common.core.DataConstant;
import cn.datax.common.core.R; import cn.datax.common.core.R;
...@@ -6,12 +6,14 @@ import cn.datax.common.utils.HttpUtil; ...@@ -6,12 +6,14 @@ import cn.datax.common.utils.HttpUtil;
import cn.datax.common.utils.IPUtil; import cn.datax.common.utils.IPUtil;
import cn.datax.common.utils.MD5Util; import cn.datax.common.utils.MD5Util;
import cn.datax.common.utils.ResponseUtil; import cn.datax.common.utils.ResponseUtil;
import cn.datax.service.data.market.api.call.utils.ThreadUtil;
import cn.datax.service.data.market.api.dto.ApiLogDto; import cn.datax.service.data.market.api.dto.ApiLogDto;
import cn.datax.service.data.market.api.entity.DataApiEntity; import cn.datax.service.data.market.api.entity.DataApiEntity;
import cn.datax.service.data.market.service.DataApiService; import cn.datax.service.data.market.api.feign.DataApiServiceFeign;
import cn.datax.service.data.market.utils.ThreadUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
...@@ -34,7 +36,7 @@ public class ApiInterceptor implements HandlerInterceptor { ...@@ -34,7 +36,7 @@ public class ApiInterceptor implements HandlerInterceptor {
private RedisTemplate<String, Object> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
@Autowired @Autowired
private DataApiService dataApiService; private DataApiServiceFeign dataApiServiceFeign;
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
...@@ -53,7 +55,13 @@ public class ApiInterceptor implements HandlerInterceptor { ...@@ -53,7 +55,13 @@ public class ApiInterceptor implements HandlerInterceptor {
MD5Util mt = MD5Util.getInstance(); MD5Util mt = MD5Util.getInstance();
String apiId = mt.decode(apiKey); String apiId = mt.decode(apiKey);
String userId = mt.decode(secretKey); String userId = mt.decode(secretKey);
DataApiEntity dataApiEntity = dataApiService.getById(apiId); R apiResult = dataApiServiceFeign.getDataApiById(apiId);
if (apiResult == null || !apiResult.isSuccess() || ObjectUtil.isEmpty(apiResult.getData())) {
ResponseUtil.makeResponse(response, MediaType.APPLICATION_JSON_VALUE,
HttpServletResponse.SC_FORBIDDEN, R.error("查询API{" + apiId + "}出错"));
return false;
}
DataApiEntity dataApiEntity = JSON.parseObject(JSON.toJSONString(apiResult.getData()), DataApiEntity.class);
if (dataApiEntity == null) { if (dataApiEntity == null) {
ResponseUtil.makeResponse(response, MediaType.APPLICATION_JSON_VALUE, ResponseUtil.makeResponse(response, MediaType.APPLICATION_JSON_VALUE,
HttpServletResponse.SC_FORBIDDEN, R.error("API不存在")); HttpServletResponse.SC_FORBIDDEN, R.error("API不存在"));
......
package cn.datax.service.data.market.api.call.config;
import cn.datax.common.security.handler.DataAccessDeniedHandler;
import cn.datax.common.security.handler.DataAuthExceptionEntryPoint;
import cn.datax.common.security.utils.DataRedisTokenServices;
import cn.datax.common.security.utils.RedisTokenStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class DataResourceServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private DataAccessDeniedHandler accessDeniedHandler;
@Autowired
private DataAuthExceptionEntryPoint exceptionEntryPoint;
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public TokenStore redisTokenStore() {
return new RedisTokenStore(redisConnectionFactory);
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) {
DataRedisTokenServices dataTokenServices = new DataRedisTokenServices();
dataTokenServices.setTokenStore(redisTokenStore());
resources
.tokenStore(redisTokenStore())
.tokenServices(dataTokenServices)
.authenticationEntryPoint(exceptionEntryPoint)
.accessDeniedHandler(accessDeniedHandler);
}
@Override
public void configure(HttpSecurity http) throws Exception {
//允许使用iframe 嵌套,避免swagger-ui 不被加载的问题
http.headers().frameOptions().disable();
http.authorizeRequests()
.antMatchers(
"/actuator/**",
"/v2/api-docs/**",
"/swagger-ui.html",
"/doc.html",
"/swagger-resources/**",
"/webjars/**",
// API调用
"/v1/**"
).permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}
package cn.datax.service.data.market.config; package cn.datax.service.data.market.api.call.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
......
package cn.datax.service.data.market.controller; package cn.datax.service.data.market.api.call.controller;
import cn.datax.common.base.BaseController; import cn.datax.common.base.BaseController;
import cn.datax.common.core.R; import cn.datax.common.core.R;
import cn.datax.common.database.core.PageResult; import cn.datax.common.database.core.PageResult;
import cn.datax.service.data.market.service.ApiService; import cn.datax.service.data.market.api.call.service.ApiCallService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
...@@ -12,14 +12,15 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -12,14 +12,15 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.Map; import java.util.Map;
@RestController @RestController
public class ApiController extends BaseController { @RequestMapping("/v1")
public class ApiCallController extends BaseController {
@Autowired @Autowired
private ApiService apiService; private ApiCallService apiCallService;
@RequestMapping(value = "/v1/**", method = {RequestMethod.GET, RequestMethod.POST}) @RequestMapping(value = "/**", method = {RequestMethod.GET, RequestMethod.POST})
public R apiV1(){ public R apiV1(){
PageResult<Map<String, Object>> pageResult = apiService.v1(); PageResult<Map<String, Object>> pageResult = apiCallService.v1();
return R.ok().setData(pageResult); return R.ok().setData(pageResult);
} }
} }
package cn.datax.service.data.market.dao; package cn.datax.service.data.market.api.call.dao;
import cn.datax.common.base.BaseDao; import cn.datax.common.base.BaseDao;
import cn.datax.service.data.market.api.entity.ApiLogEntity; import cn.datax.service.data.market.api.entity.ApiLogEntity;
......
package cn.datax.service.data.market.mapstruct; package cn.datax.service.data.market.api.call.mapstruct;
import cn.datax.common.mapstruct.EntityMapper; import cn.datax.common.mapstruct.EntityMapper;
import cn.datax.service.data.market.api.dto.ApiLogDto; import cn.datax.service.data.market.api.dto.ApiLogDto;
......
package cn.datax.service.data.market.service; package cn.datax.service.data.market.api.call.service;
import cn.datax.common.database.core.PageResult; import cn.datax.common.database.core.PageResult;
import java.util.Map; import java.util.Map;
public interface ApiService { public interface ApiCallService {
PageResult<Map<String, Object>> v1(); PageResult<Map<String, Object>> v1();
} }
package cn.datax.service.data.market.service; package cn.datax.service.data.market.api.call.service;
import cn.datax.common.base.BaseService; import cn.datax.common.base.BaseService;
import cn.datax.service.data.market.api.dto.ApiLogDto; import cn.datax.service.data.market.api.dto.ApiLogDto;
......
package cn.datax.service.data.market.service.impl; package cn.datax.service.data.market.api.call.service.impl;
import cn.datax.common.core.R; import cn.datax.common.core.R;
import cn.datax.common.database.DataSourceFactory; import cn.datax.common.database.DataSourceFactory;
...@@ -11,11 +11,11 @@ import cn.datax.common.utils.ThrowableUtil; ...@@ -11,11 +11,11 @@ import cn.datax.common.utils.ThrowableUtil;
import cn.datax.service.data.factory.api.dto.DbSchema; import cn.datax.service.data.factory.api.dto.DbSchema;
import cn.datax.service.data.factory.api.entity.DataSourceEntity; import cn.datax.service.data.factory.api.entity.DataSourceEntity;
import cn.datax.service.data.factory.api.feign.DataSourceServiceFeign; import cn.datax.service.data.factory.api.feign.DataSourceServiceFeign;
import cn.datax.service.data.market.api.call.service.ApiCallService;
import cn.datax.service.data.market.api.call.utils.SqlBuilderUtil;
import cn.datax.service.data.market.api.call.utils.ThreadUtil;
import cn.datax.service.data.market.api.entity.DataApiEntity; import cn.datax.service.data.market.api.entity.DataApiEntity;
import cn.datax.service.data.market.dao.DataApiDao; import cn.datax.service.data.market.api.feign.DataApiServiceFeign;
import cn.datax.service.data.market.service.ApiService;
import cn.datax.service.data.market.utils.SqlBuilderUtil;
import cn.datax.service.data.market.utils.ThreadUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -27,7 +27,7 @@ import java.util.Optional; ...@@ -27,7 +27,7 @@ import java.util.Optional;
@Slf4j @Slf4j
@Service @Service
public class ApiServiceImpl implements ApiService { public class ApiCallServiceImpl implements ApiCallService {
@Autowired @Autowired
private DataSourceFactory dataSourceFactory; private DataSourceFactory dataSourceFactory;
...@@ -36,21 +36,26 @@ public class ApiServiceImpl implements ApiService { ...@@ -36,21 +36,26 @@ public class ApiServiceImpl implements ApiService {
private DataSourceServiceFeign dataSourceServiceFeign; private DataSourceServiceFeign dataSourceServiceFeign;
@Autowired @Autowired
private DataApiDao dataApiDao; private DataApiServiceFeign dataApiServiceFeign;
@Override @Override
public PageResult<Map<String, Object>> v1() { public PageResult<Map<String, Object>> v1() {
// 缓存取数据 // 缓存取数据
// redisTemplate.opsForValue().get("myCache::userName") // redisTemplate.opsForValue().get("myCache::userName")
R apiResult = dataApiServiceFeign.getDataApiById(ThreadUtil.getInstance().get().getApiId());
DataApiEntity dataApiEntity = dataApiDao.selectById(ThreadUtil.getInstance().get().getApiId()); if(apiResult == null || !apiResult.isSuccess() || ObjectUtil.isEmpty(apiResult.getData())){
R result = dataSourceServiceFeign.getDataSourceById(dataApiEntity.getExecuteConfig().getSourceId()); ThreadUtil.getInstance().get().setStatus(0);
if(result == null || !result.isSuccess() || ObjectUtil.isEmpty(result.getData())){ ThreadUtil.getInstance().get().setMsg("API调用查询数据API{" + ThreadUtil.getInstance().get().getApiId() + "}出错");
throw new DataException("API调用查询数据API出错");
}
DataApiEntity dataApiEntity = JSON.parseObject(JSON.toJSONString(apiResult.getData()), DataApiEntity.class);
R sourceResult = dataSourceServiceFeign.getDataSourceById(dataApiEntity.getExecuteConfig().getSourceId());
if(sourceResult == null || !sourceResult.isSuccess() || ObjectUtil.isEmpty(sourceResult.getData())){
ThreadUtil.getInstance().get().setStatus(0); ThreadUtil.getInstance().get().setStatus(0);
ThreadUtil.getInstance().get().setMsg("API调用查询数据源{"+dataApiEntity.getExecuteConfig().getSourceId()+"}出错"); ThreadUtil.getInstance().get().setMsg("API调用查询数据源{"+dataApiEntity.getExecuteConfig().getSourceId()+"}出错");
throw new DataException("API调用查询数据源出错"); throw new DataException("API调用查询数据源出错");
} }
DataSourceEntity dataSource = JSON.parseObject(JSON.toJSONString(result.getData()), DataSourceEntity.class); DataSourceEntity dataSource = JSON.parseObject(JSON.toJSONString(sourceResult.getData()), DataSourceEntity.class);
DbSchema dbSchema = dataSource.getDbSchema(); DbSchema dbSchema = dataSource.getDbSchema();
DbQueryProperty dbQueryProperty = new DbQueryProperty(dataSource.getDbType(), dbSchema.getHost(), DbQueryProperty dbQueryProperty = new DbQueryProperty(dataSource.getDbType(), dbSchema.getHost(),
dbSchema.getUsername(), dbSchema.getPassword(), dbSchema.getPort(), dbSchema.getDbName()); dbSchema.getUsername(), dbSchema.getPassword(), dbSchema.getPort(), dbSchema.getDbName());
...@@ -77,6 +82,7 @@ public class ApiServiceImpl implements ApiService { ...@@ -77,6 +82,7 @@ public class ApiServiceImpl implements ApiService {
try { try {
PageResult<Map<String, Object>> pageResult = dbQuery.queryByPage(sqlFilterResult.getSql(), args, offset, pageSize); PageResult<Map<String, Object>> pageResult = dbQuery.queryByPage(sqlFilterResult.getSql(), args, offset, pageSize);
pageResult.setPageNum(pageNum).setPageSize(pageSize); pageResult.setPageNum(pageNum).setPageSize(pageSize);
ThreadUtil.getInstance().get().setCallerSize(pageResult.getData().size());
return pageResult; return pageResult;
} catch (Exception e) { } catch (Exception e) {
log.error("全局异常信息ex={}, StackTrace={}", e.getMessage(), ThrowableUtil.getStackTrace(e)); log.error("全局异常信息ex={}, StackTrace={}", e.getMessage(), ThrowableUtil.getStackTrace(e));
......
package cn.datax.service.data.market.service.impl; package cn.datax.service.data.market.api.call.service.impl;
import cn.datax.common.base.BaseServiceImpl; import cn.datax.common.base.BaseServiceImpl;
import cn.datax.service.data.market.api.call.dao.ApiLogDao;
import cn.datax.service.data.market.api.call.mapstruct.ApiLogMapper;
import cn.datax.service.data.market.api.call.service.ApiLogService;
import cn.datax.service.data.market.api.dto.ApiLogDto; import cn.datax.service.data.market.api.dto.ApiLogDto;
import cn.datax.service.data.market.api.entity.ApiLogEntity; import cn.datax.service.data.market.api.entity.ApiLogEntity;
import cn.datax.service.data.market.dao.ApiLogDao;
import cn.datax.service.data.market.mapstruct.ApiLogMapper;
import cn.datax.service.data.market.service.ApiLogService;
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.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
......
package cn.datax.service.data.market.api.call.utils;
import cn.datax.common.exception.DataException;
import org.springframework.util.Assert;
import java.util.*;
/**
* 带参数sql处理工具类
*/
public class NamedParameterUtil {
private NamedParameterUtil() {}
/**
* 定义特殊字符(增加最后的自定义的'}')
*/
private static final char[] PARAMETER_SEPARATORS =
new char[] {'"', '\'', ':', '&', ',', ';', '(', ')', '|', '=', '+', '-', '*', '%', '/', '\\', '<', '>', '^', '}'};
/**
* 对带参数sql的统计式封装,便于后续肢解拼装
* @param originalSql
* @return
*/
public static ParsedSql parseSqlStatement(String originalSql) {
Assert.notNull(originalSql, "SQL must not be null");
ParsedSql parsedSql = new ParsedSql(originalSql);
Set<String> namedParameters = new HashSet();
char[] sqlchars = originalSql.toCharArray();
int namedParamCount = 0;
int unNamedParamCount = 0;
int totalParamCount = 0;
int i = 0;
while (i < sqlchars.length) {
char statement = sqlchars[i];
if (statement == ':') {
int j = i + 1;
while (j < sqlchars.length && !isSeparatorsChar(sqlchars[j])) {
j++;
}
if (j - i > 1) {
String paramName = originalSql.substring(i + 1, j);
if (!namedParameters.contains(paramName)) {
namedParameters.add(paramName);
namedParamCount++;
}
parsedSql.addParamNames(paramName, i, j);
totalParamCount++;
}
i = j - 1;
} else if (statement == '?') {
unNamedParamCount++;
totalParamCount++;
}
i++;
}
parsedSql.setNamedParamCount(namedParamCount);
parsedSql.setUnnamedParamCount(unNamedParamCount);
parsedSql.setTotalParamCount(totalParamCount);
return parsedSql;
}
/**
* 获得不带参数的sql,即替换参数为?
* @param parsedSql
* @param params
* @return
*/
public static String substituteNamedParams(ParsedSql parsedSql, Map<String, Object> params){
String original = parsedSql.getOriginalSql();
StringBuffer actual = new StringBuffer("");
int lastIndex = 0;
List<String> paramNames = parsedSql.getParamNames();
for (int i = 0; i < paramNames.size(); i++) {
int[] indexs = parsedSql.getParamIndexs(i);
int startIndex = indexs[0];
int endIndex = indexs[1];
String paramName = paramNames.get(i);
actual.append(original.substring(lastIndex, startIndex));
if (params != null && params.containsKey(paramName)) {
actual.append("?");
} else{
actual.append("?");
}
lastIndex = endIndex;
}
actual.append(original.subSequence(lastIndex, original.length()));
return actual.toString();
}
/**
* 获得sql所需参数K,V
* @param parsedSql
* @param params
* @return
*/
public static LinkedHashMap<String, Object> buildValueArray(ParsedSql parsedSql, Map<String, Object> params){
List<String> paramNames = parsedSql.getParamNames();
LinkedHashMap<String, Object> acceptedFilters = new LinkedHashMap<>(parsedSql.getTotalParamCount());
if (parsedSql.getNamedParamCount() > 0 && parsedSql.getUnnamedParamCount() > 0) {
throw new DataException("parameter方式与?方式不能混合!");
}
for (int i = 0; i < paramNames.size(); i++) {
String keyName = paramNames.get(i);
if (params.containsKey(keyName)) {
acceptedFilters.put(keyName, params.get(keyName));
}
}
return acceptedFilters;
}
private static boolean isSeparatorsChar(char statement){
if (Character.isWhitespace(statement)) {
return true;
}
for (int i = 0; i < PARAMETER_SEPARATORS.length; i++) {
if (statement == PARAMETER_SEPARATORS[i]) {
return true;
}
}
return false;
}
}
package cn.datax.service.data.market.api.call.utils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* 此类封装NamedParameterSql
*/
public class ParsedSql implements Serializable {
private static final long serialVersionUID=1L;
private String originalSql;
//参数名
private List<String> paramNames = new ArrayList<>();
//参数在sql中对应的位置
private List<int[]> paramIndexs = new ArrayList<>();
//统计参数个数(不包含重复)
private int namedParamCount;
//统计sql中?的个数
private int unnamedParamCount;
private int totalParamCount;
public ParsedSql(String originalSql){
this.originalSql = originalSql;
}
public List<String> getParamNames() {
return paramNames;
}
public void addParamNames(String paramName,int startIndex,int endIndex) {
paramNames.add(paramName);
paramIndexs.add(new int[]{startIndex,endIndex});
}
public int[] getParamIndexs(int position) {
return paramIndexs.get(position);
}
public String getOriginalSql() {
return originalSql;
}
public int getNamedParamCount() {
return namedParamCount;
}
public void setNamedParamCount(int namedParamCount) {
this.namedParamCount = namedParamCount;
}
public int getUnnamedParamCount() {
return unnamedParamCount;
}
public void setUnnamedParamCount(int unnamedParamCount) {
this.unnamedParamCount = unnamedParamCount;
}
public int getTotalParamCount() {
return totalParamCount;
}
public void setTotalParamCount(int totalParamCount) {
this.totalParamCount = totalParamCount;
}
@Override
public String toString() {
return "ParsedSql{" +
"originalSql='" + originalSql + '\'' +
", paramNames=" + paramNames +
", paramIndexs=" + paramIndexs +
", namedParamCount=" + namedParamCount +
", unnamedParamCount=" + unnamedParamCount +
", totalParamCount=" + totalParamCount +
'}';
}
}
package cn.datax.service.data.market.api.call.utils;
import cn.datax.service.data.market.api.dto.ReqParam;
import cn.datax.service.data.market.api.enums.WhereType;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 用于动态构造sql语句
* ${ segment... } 为一个条件代码块
*
* String sql = "select * from user where 1=1
* ${ and username = :username }
* ${ and password = :password }
* ${ and age = :age }"
*
* Map filters = new HashMap();
* filters.put("username", "yuwei");
* filters.put("age", "12");
* filters.put("id", "123");
*
* SqlFilterResult result = SqlBuilderUtil.applyFilters(sql, filters);
*
* result.getSql()结果
* select * from user where 1=1 and username=:username and age=:age
*
* result.getAcceptedFilters()结果
* {username=yuwei}
* {age=12}
*/
@Slf4j
public class SqlBuilderUtil {
private SqlBuilderUtil() {}
private static volatile SqlBuilderUtil instance;
public static SqlBuilderUtil getInstance() {
if(instance == null) {
synchronized (SqlBuilderUtil.class) {
if(instance == null) {
instance = new SqlBuilderUtil();
}
}
}
return instance;
}
/**
* 空格
*/
private final String SPACE = " ";
/**
* 冒号占位符
*/
private final String COLON = ":";
/**
* 问号占位符
*/
private final String MARK = "?";
/**
* where关键字
*/
private final String WHERE_SQL = "WHERE";
/**
* AND连接符
*/
private final String WHERE_AND = "AND";
/**
* where 1=1条件
*/
private final String WHERE_INIT = WHERE_SQL + " 1 = 1";
/**
* 条件代码块标记开始
*/
public String MARK_KEY_START = "${";
/**
* 条件代码块标记结束
*/
public String MARK_KEY_END = "}";
/**
* 拼接命名参数sql
* @param sql
* @param params
* @return
*/
public String buildHql(String sql, List<ReqParam> params){
Assert.notNull(sql, "SQL must not be null");
return buildHql(new StringBuffer(sql), params);
}
private String buildHql(StringBuffer sql, List<ReqParam> params){
if(CollUtil.isEmpty(params)){
return sql.toString();
}
sql.append(SPACE).append(WHERE_INIT);
for (int i = 0; i < params.size(); i++) {
ReqParam reqParam = params.get(i);
sql.append(SPACE).append(MARK_KEY_START).append(WHERE_AND).append(SPACE).append(reqParam.getParamName()).append(SPACE).append(WhereType.getWhereType(reqParam.getWhereType())).append(SPACE).append(COLON).append(reqParam.getParamName()).append(MARK_KEY_END);
}
return sql.toString();
}
/**
* 根据入参动态构造sql语句
* @param sql
* @param filters
* @return
*/
public SqlFilterResult applyFilters(String sql, Map<String, Object> filters){
Assert.notNull(sql, "SQL must not be null");
return applyFilters(new StringBuffer(sql), filters);
}
private SqlFilterResult applyFilters(StringBuffer sql, Map<String, Object> filters){
LinkedHashMap<String, Object> acceptedFilters = new LinkedHashMap<>();
for (int i = 0, end = 0, start = sql.indexOf(MARK_KEY_START); ((start = sql.indexOf(MARK_KEY_START, end)) >= 0); i++) {
end = sql.indexOf(MARK_KEY_END, start);
// 封装该条件代码块中的NamedParameterSql
ParsedSql parsedSql = getSegmentParsedSql(sql, start, end);
if (CollUtil.isEmpty(parsedSql.getParamNames())){
throw new IllegalArgumentException("Not key found in segment=" + sql.substring(start, end + MARK_KEY_END.length()));
}
// 判断输入参数filters中是否存在查询参数
if (isAcceptedKeys(filters, parsedSql.getParamNames())) {
// 动态构造可执行的sql语句,去掉条件代码块两边的${ }标记符
if (log.isDebugEnabled()) {
log.debug("The filter namedParameters=" + parsedSql.getParamNames() + " is accepted on segment=" + sql.substring(start, end + MARK_KEY_END.length()));
}
// 下面方法2选1可以获取条件代码块
// String segment = sql.substring(start + MARK_KEY_START.length(), end);
// String segment = parsedSql.getOriginalSql();
// 转换命名参数:为?
String segment = NamedParameterUtil.substituteNamedParams(parsedSql, filters);
// 获取传参中包含命名参数的数据
LinkedHashMap<String, Object> linkAcceptedFilters = NamedParameterUtil.buildValueArray(parsedSql, filters);
acceptedFilters.putAll(linkAcceptedFilters);
sql.replace(start, end + MARK_KEY_END.length(), segment);
end = start + segment.length();
} else {
// 抛弃该条件代码块
if (log.isDebugEnabled()) {
log.debug("The filter namedParameters=" + parsedSql.getParamNames() + " is removed from the query on segment=" + sql.substring(start, end + MARK_KEY_END.length()));
}
sql.replace(start, end + MARK_KEY_END.length(), "");
end = start;
}
}
return new SqlFilterResult(sql.toString(), acceptedFilters);
}
/**
* 验证入参,并过滤值为空的入参
*/
private boolean isAcceptedKeys(Map<String, Object> filters, List<String> keys) {
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
Object value = getProperty(filters, key);
if (!isValuePopulated(value, true)) {
return false;
}
}
return true;
}
/**
* 封装该条件代码块中的NamedParameterSql
*/
private ParsedSql getSegmentParsedSql(StringBuffer sql, int start, int end) {
String segment = sql.substring(start + MARK_KEY_START.length(), end);
ParsedSql parsedSql = NamedParameterUtil.parseSqlStatement(segment);
return parsedSql;
}
/**
* 获取参数值
* @param filters
* @param key
* @return
*/
private Object getProperty(Map<String, Object> filters, String key) {
if (MapUtil.isEmpty(filters))
return null;
return filters.get(key);
}
/**
* 验证参数值是否空
* @param value
* @param isRemoveEmpty
* @return
*/
private boolean isValuePopulated(Object value, boolean isRemoveEmpty) {
if (value == null) {
return false;
}
if (isRemoveEmpty) {
return ObjectUtil.isNotEmpty(value);
} else {
return true;
}
}
public class SqlFilterResult implements Serializable {
private static final long serialVersionUID=1L;
private String sql;
private Map<String, Object> acceptedFilters;
public SqlFilterResult(String sql, Map<String, Object> acceptedFilters) {
this.setSql(sql);
this.setAcceptedFilters(acceptedFilters);
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public Map<String, Object> getAcceptedFilters() {
return acceptedFilters;
}
public void setAcceptedFilters(Map<String, Object> acceptedFilters) {
this.acceptedFilters = acceptedFilters;
}
@Override
public String toString() {
return "SqlFilterResult{" +
"sql='" + sql + '\'' +
", acceptedFilters=" + acceptedFilters +
'}';
}
}
}
package cn.datax.service.data.market.utils; package cn.datax.service.data.market.api.call.utils;
import cn.datax.service.data.market.api.dto.ApiLogDto; import cn.datax.service.data.market.api.dto.ApiLogDto;
......
server:
port: 8815
spring:
application:
name: datax-service-data-api-call
profiles:
active: dev
cloud:
config:
fail-fast: true
name: ${spring.application.name}
profile: ${spring.profiles.active}
discovery:
enabled: true
service-id: datax-config
# 注册中心配置
eureka:
instance:
lease-renewal-interval-in-seconds: 20
client:
register-with-eureka: true
fetch-registry: true
instance-info-replication-interval-seconds: 30
registry-fetch-interval-seconds: 3
service-url:
defaultZone: http://localhost:8610/eureka
\ 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="cn.datax.service.data.market.api.call.dao.ApiLogDao">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="cn.datax.service.data.market.api.entity.ApiLogEntity">
<result column="id" property="id" />
<result column="status" property="status" />
<result column="api_id" property="apiId" />
<result column="api_name" property="apiName" />
<result column="caller_id" property="callerId" />
<result column="caller_ip" property="callerIp" />
<result column="caller_url" property="callerUrl" />
<result column="caller_params" property="callerParams" />
<result column="caller_date" property="callerDate" />
<result column="caller_size" property="callerSize" />
<result column="time" property="time" />
<result column="msg" property="msg" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id,
status,
api_id, api_name, caller_id, caller_ip, caller_url, caller_params, caller_date, callerSize, time, msg
</sql>
</mapper>
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,batch,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2
# 开启过滤
filter=true
# 配置不打印的内容
exclude=select 1
\ No newline at end of file
package cn.datax.service.data.market.api.feign;
import cn.datax.common.core.R;
import cn.datax.service.data.market.api.feign.factory.DataApiServiceFeignFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(contextId = "dataApiServiceFeign", value = "datax-service-data-market", fallbackFactory = DataApiServiceFeignFallbackFactory.class)
public interface DataApiServiceFeign {
@GetMapping("/inner/dataApi/{id}")
R getDataApiById(@PathVariable("id") String id);
}
package cn.datax.service.data.market.api.feign.factory;
import cn.datax.service.data.market.api.feign.DataApiServiceFeign;
import cn.datax.service.data.market.api.feign.fallback.DataApiServiceFeignFallbackImpl;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class DataApiServiceFeignFallbackFactory implements FallbackFactory<DataApiServiceFeign> {
@Override
public DataApiServiceFeign create(Throwable throwable) {
DataApiServiceFeignFallbackImpl dataApiServiceFeignFallback = new DataApiServiceFeignFallbackImpl();
dataApiServiceFeignFallback.setCause(throwable);
return dataApiServiceFeignFallback;
}
}
package cn.datax.service.data.market.api.feign.fallback;
import cn.datax.common.core.R;
import cn.datax.service.data.market.api.feign.DataApiServiceFeign;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class DataApiServiceFeignFallbackImpl implements DataApiServiceFeign {
@Setter
private Throwable cause;
@Override
public R getDataApiById(String id) {
log.error("feign 调用{}出错", id, cause);
return null;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.datax.service.data.market.api.feign.factory.DataApiServiceFeignFallbackFactory,\
cn.datax.service.data.market.api.feign.fallback.DataApiServiceFeignFallbackImpl
...@@ -71,12 +71,12 @@ ...@@ -71,12 +71,12 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.datax</groupId> <groupId>cn.datax</groupId>
<artifactId>data-market-service-api</artifactId> <artifactId>system-service-api</artifactId>
<version>2.0.0</version> <version>2.0.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.datax</groupId> <groupId>cn.datax</groupId>
<artifactId>data-factory-service-api</artifactId> <artifactId>data-market-service-api</artifactId>
<version>2.0.0</version> <version>2.0.0</version>
</dependency> </dependency>
</dependencies> </dependencies>
......
...@@ -12,7 +12,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients; ...@@ -12,7 +12,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDataMybatis @EnableDataMybatis
@EnableDataRedis @EnableDataRedis
@EnableDataLog @EnableDataLog
@EnableFeignClients(basePackages = {"cn.datax.service.data.factory.api.feign"}) @EnableFeignClients(basePackages = {"cn.datax.service.system.api.feign"})
@SpringCloudApplication @SpringCloudApplication
public class DataMarketApplication { public class DataMarketApplication {
......
...@@ -59,7 +59,6 @@ public class DataResourceServerConfig extends ResourceServerConfigurerAdapter { ...@@ -59,7 +59,6 @@ public class DataResourceServerConfig extends ResourceServerConfigurerAdapter {
"/swagger-resources/**", "/swagger-resources/**",
"/webjars/**", "/webjars/**",
// feign 内部调用不用授权 // feign 内部调用不用授权
"/v1/**",
"/inner/**" "/inner/**"
).permitAll() ).permitAll()
.anyRequest().authenticated() .anyRequest().authenticated()
......
...@@ -73,7 +73,7 @@ public class DataApiController extends BaseController { ...@@ -73,7 +73,7 @@ public class DataApiController extends BaseController {
@GetMapping("/page") @GetMapping("/page")
public R getRolePage(DataApiQuery dataApiQuery) { public R getRolePage(DataApiQuery dataApiQuery) {
QueryWrapper<DataApiEntity> queryWrapper = new QueryWrapper<>(); QueryWrapper<DataApiEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id", "status", "createTime", "apiName", "apiVersion", "apiUrl", "reqMethod", "resType", "remark"); queryWrapper.select("id", "status", "create_time", "api_name", "api_version", "api_url", "req_method", "res_type", "remark");
IPage<DataApiEntity> page = dataApiService.page(new Page<>(dataApiQuery.getPageNum(), dataApiQuery.getPageSize()), queryWrapper); IPage<DataApiEntity> page = dataApiService.page(new Page<>(dataApiQuery.getPageNum(), dataApiQuery.getPageSize()), queryWrapper);
List<DataApiVo> collect = page.getRecords().stream().map(dataApiMapper::toVO).collect(Collectors.toList()); List<DataApiVo> collect = page.getRecords().stream().map(dataApiMapper::toVO).collect(Collectors.toList());
JsonPage<DataApiVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect); JsonPage<DataApiVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
......
package cn.datax.service.data.market.controller; package cn.datax.service.data.market.controller;
import cn.datax.common.base.BaseController; import cn.datax.common.base.BaseController;
import cn.datax.common.core.R;
import cn.datax.common.security.annotation.DataInner;
import cn.datax.service.data.market.api.entity.DataApiEntity;
import cn.datax.service.data.market.mapstruct.DataApiMapper;
import cn.datax.service.data.market.service.DataApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/inner") @RequestMapping("/inner")
public class InnerController extends BaseController { public class InnerController extends BaseController {
@Autowired
private DataApiService dataApiService;
@Autowired
private DataApiMapper dataApiMapper;
/**
* 通过ID查询信息
*
* @param id
* @return
*/
@DataInner
@GetMapping("/dataApi/{id}")
public R getDataApiById(@PathVariable String id) {
DataApiEntity dataApiEntity = dataApiService.getById(id);
return R.ok().setData(dataApiMapper.toVO(dataApiEntity));
}
} }
...@@ -4,8 +4,8 @@ import cn.datax.common.exception.DataException; ...@@ -4,8 +4,8 @@ import cn.datax.common.exception.DataException;
import cn.datax.common.utils.ThrowableUtil; import cn.datax.common.utils.ThrowableUtil;
import cn.datax.service.data.market.api.dto.*; import cn.datax.service.data.market.api.dto.*;
import cn.datax.service.data.market.api.entity.DataApiEntity; import cn.datax.service.data.market.api.entity.DataApiEntity;
import cn.datax.service.data.market.api.enums.ConfigType;
import cn.datax.service.data.market.api.vo.SqlParseVo; import cn.datax.service.data.market.api.vo.SqlParseVo;
import cn.datax.service.data.market.enums.ConfigType;
import cn.datax.service.data.market.service.DataApiService; import cn.datax.service.data.market.service.DataApiService;
import cn.datax.service.data.market.mapstruct.DataApiMapper; import cn.datax.service.data.market.mapstruct.DataApiMapper;
import cn.datax.service.data.market.dao.DataApiDao; import cn.datax.service.data.market.dao.DataApiDao;
......
package cn.datax.service.data.market.utils; package cn.datax.service.data.market.utils;
import cn.datax.common.exception.DataException; import cn.datax.common.exception.DataException;
import com.alibaba.fastjson.JSON;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import java.util.*; import java.util.*;
...@@ -12,9 +11,6 @@ import java.util.*; ...@@ -12,9 +11,6 @@ import java.util.*;
public class NamedParameterUtil { public class NamedParameterUtil {
public static void main(String[] args) { public static void main(String[] args) {
String json = "{\"id\": \"1214835832967581698\", \"age\": \"12\"}";
Map<String, Object> params = JSON.parseObject(json);
System.out.println(params);
// String sql = "select * from user where 1 = 1 ${ and id = :id } ${and name = :name}"; // String sql = "select * from user where 1 = 1 ${ and id = :id } ${and name = :name}";
// int start = sql.indexOf("${"); // int start = sql.indexOf("${");
// int end = sql.indexOf("}", start); // int end = sql.indexOf("}", start);
...@@ -40,7 +36,7 @@ public class NamedParameterUtil { ...@@ -40,7 +36,7 @@ public class NamedParameterUtil {
private NamedParameterUtil() {} private NamedParameterUtil() {}
/** /**
* 定义特殊字符(增加最后的定义的'}') * 定义特殊字符(增加最后的定义的'}')
*/ */
private static final char[] PARAMETER_SEPARATORS = private static final char[] PARAMETER_SEPARATORS =
new char[] {'"', '\'', ':', '&', ',', ';', '(', ')', '|', '=', '+', '-', '*', '%', '/', '\\', '<', '>', '^', '}'}; new char[] {'"', '\'', ':', '&', ',', ';', '(', ')', '|', '=', '+', '-', '*', '%', '/', '\\', '<', '>', '^', '}'};
......
package cn.datax.service.data.market.utils; package cn.datax.service.data.market.utils;
import cn.datax.service.data.market.api.dto.ReqParam; import cn.datax.service.data.market.api.dto.ReqParam;
import cn.datax.service.data.market.enums.WhereType; import cn.datax.service.data.market.api.enums.WhereType;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
......
package cn.datax.service.data.market.mapstruct;
import cn.datax.service.data.market.api.dto.DataApiDto;
import cn.datax.service.data.market.api.dto.ReqParam;
import cn.datax.service.data.market.api.dto.ResParam;
import cn.datax.service.data.market.api.entity.DataApiEntity;
import cn.datax.service.data.market.api.vo.DataApiVo;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2020-04-06T12:17:46+0800",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_231 (Oracle Corporation)"
)
@Component
public class DataApiMapperImpl implements DataApiMapper {
@Override
public DataApiDto toDTO(DataApiEntity arg0) {
if ( arg0 == null ) {
return null;
}
DataApiDto dataApiDto = new DataApiDto();
dataApiDto.setId( arg0.getId() );
dataApiDto.setApiName( arg0.getApiName() );
dataApiDto.setApiVersion( arg0.getApiVersion() );
dataApiDto.setApiUrl( arg0.getApiUrl() );
dataApiDto.setRemark( arg0.getRemark() );
dataApiDto.setReqMethod( arg0.getReqMethod() );
dataApiDto.setResType( arg0.getResType() );
dataApiDto.setDeny( arg0.getDeny() );
dataApiDto.setRateLimit( arg0.getRateLimit() );
dataApiDto.setTimes( arg0.getTimes() );
dataApiDto.setSeconds( arg0.getSeconds() );
dataApiDto.setExecuteConfig( arg0.getExecuteConfig() );
List<ReqParam> list = arg0.getReqParams();
if ( list != null ) {
dataApiDto.setReqParams( new ArrayList<ReqParam>( list ) );
}
List<ResParam> list1 = arg0.getResParams();
if ( list1 != null ) {
dataApiDto.setResParams( new ArrayList<ResParam>( list1 ) );
}
return dataApiDto;
}
@Override
public List<DataApiDto> toDTO(List<DataApiEntity> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataApiDto> list = new ArrayList<DataApiDto>( arg0.size() );
for ( DataApiEntity dataApiEntity : arg0 ) {
list.add( toDTO( dataApiEntity ) );
}
return list;
}
@Override
public DataApiVo toVO(DataApiEntity arg0) {
if ( arg0 == null ) {
return null;
}
DataApiVo dataApiVo = new DataApiVo();
dataApiVo.setId( arg0.getId() );
dataApiVo.setStatus( arg0.getStatus() );
dataApiVo.setCreateTime( arg0.getCreateTime() );
dataApiVo.setApiName( arg0.getApiName() );
dataApiVo.setApiVersion( arg0.getApiVersion() );
dataApiVo.setApiUrl( arg0.getApiUrl() );
dataApiVo.setRemark( arg0.getRemark() );
dataApiVo.setReqMethod( arg0.getReqMethod() );
dataApiVo.setDeny( arg0.getDeny() );
dataApiVo.setResType( arg0.getResType() );
dataApiVo.setRateLimit( arg0.getRateLimit() );
dataApiVo.setTimes( arg0.getTimes() );
dataApiVo.setSeconds( arg0.getSeconds() );
dataApiVo.setExecuteConfig( arg0.getExecuteConfig() );
List<ReqParam> list = arg0.getReqParams();
if ( list != null ) {
dataApiVo.setReqParams( new ArrayList<ReqParam>( list ) );
}
List<ResParam> list1 = arg0.getResParams();
if ( list1 != null ) {
dataApiVo.setResParams( new ArrayList<ResParam>( list1 ) );
}
return dataApiVo;
}
@Override
public List<DataApiVo> toVO(List<DataApiEntity> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataApiVo> list = new ArrayList<DataApiVo>( arg0.size() );
for ( DataApiEntity dataApiEntity : arg0 ) {
list.add( toVO( dataApiEntity ) );
}
return list;
}
@Override
public DataApiEntity toEntity(DataApiDto arg0) {
if ( arg0 == null ) {
return null;
}
DataApiEntity dataApiEntity = new DataApiEntity();
dataApiEntity.setId( arg0.getId() );
dataApiEntity.setApiName( arg0.getApiName() );
dataApiEntity.setApiVersion( arg0.getApiVersion() );
dataApiEntity.setApiUrl( arg0.getApiUrl() );
dataApiEntity.setRemark( arg0.getRemark() );
dataApiEntity.setReqMethod( arg0.getReqMethod() );
dataApiEntity.setResType( arg0.getResType() );
dataApiEntity.setDeny( arg0.getDeny() );
dataApiEntity.setRateLimit( arg0.getRateLimit() );
dataApiEntity.setTimes( arg0.getTimes() );
dataApiEntity.setSeconds( arg0.getSeconds() );
dataApiEntity.setExecuteConfig( arg0.getExecuteConfig() );
List<ReqParam> list = arg0.getReqParams();
if ( list != null ) {
dataApiEntity.setReqParams( new ArrayList<ReqParam>( list ) );
}
List<ResParam> list1 = arg0.getResParams();
if ( list1 != null ) {
dataApiEntity.setResParams( new ArrayList<ResParam>( list1 ) );
}
return dataApiEntity;
}
@Override
public List<DataApiEntity> toEntity(List<DataApiDto> arg0) {
if ( arg0 == null ) {
return null;
}
List<DataApiEntity> list = new ArrayList<DataApiEntity>( arg0.size() );
for ( DataApiDto dataApiDto : arg0 ) {
list.add( toEntity( dataApiDto ) );
}
return list;
}
}
...@@ -16,5 +16,6 @@ ...@@ -16,5 +16,6 @@
<modules> <modules>
<module>data-market-service</module> <module>data-market-service</module>
<module>data-market-service-api</module> <module>data-market-service-api</module>
<module>data-market-service-api-call</module>
</modules> </modules>
</project> </project>
\ No newline at end of file
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