init
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package cn.datax.service.data.metadata.console;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.cloud.client.SpringCloudApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@EnableFeignClients(basePackages = {"cn.datax.service.system.api.feign", "cn.datax.service.data.metadata.api.feign"})
|
||||
@SpringBootApplication
|
||||
public class DataxConsoleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DataxConsoleApplication.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package cn.datax.service.data.metadata.console.concurrent;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
/**
|
||||
* 多线程模板类
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class CallableTemplate<V> implements Callable<V> {
|
||||
|
||||
/**
|
||||
* 前置处理,子类可以Override该方法
|
||||
*/
|
||||
public void beforeProcess() {
|
||||
log.info("before process....");
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理业务逻辑的方法,需要子类去Override
|
||||
* @return
|
||||
*/
|
||||
public abstract V process();
|
||||
|
||||
/**
|
||||
* 后置处理,子类可以Override该方法
|
||||
*/
|
||||
public void afterProcess() {
|
||||
log.info("after process....");
|
||||
}
|
||||
|
||||
@Override
|
||||
public V call() {
|
||||
beforeProcess();
|
||||
V result = process();
|
||||
afterProcess();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
package cn.datax.service.data.metadata.console.concurrent;
|
||||
|
||||
import cn.datax.service.data.metadata.api.vo.SqlConsoleVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
@Slf4j
|
||||
public class DateHander extends CallableTemplate<SqlConsoleVo> {
|
||||
|
||||
private CountDownLatch latch;
|
||||
|
||||
private Connection conn;
|
||||
|
||||
private String sql;
|
||||
|
||||
public DateHander(CountDownLatch latch, Connection conn, String sql) {
|
||||
this.latch = latch;
|
||||
this.conn = conn;
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlConsoleVo process() {
|
||||
log.info("执行sql:" + sql);
|
||||
long start = System.currentTimeMillis();
|
||||
Statement stmt = null;
|
||||
ResultSet rs = null;
|
||||
// 将查询数据存储到数据中
|
||||
List<Map<String, Object>> dataList = new ArrayList<>();
|
||||
// 存储列名的数组
|
||||
List<String> columnList = new LinkedList<>();
|
||||
// 新增、修改、删除受影响行数
|
||||
Integer updateCount = null;
|
||||
SqlConsoleVo sqlConsoleVo = new SqlConsoleVo();
|
||||
sqlConsoleVo.setSuccess(true);
|
||||
try {
|
||||
// 为了设置fetchSize,必须设置为false
|
||||
conn.setAutoCommit(false);
|
||||
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
stmt.setFetchSize(200);
|
||||
// 是否查询操作
|
||||
boolean execute = stmt.execute(sql);
|
||||
if (execute) {
|
||||
// 限制下最大数量
|
||||
stmt.setMaxRows(1000);
|
||||
rs = stmt.getResultSet();
|
||||
// 获取结果集的元数据信息
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
// 获取列字段的个数
|
||||
int colunmCount = rsmd.getColumnCount();
|
||||
for (int i = 1; i <= colunmCount; i++) {
|
||||
// 获取所有的字段名称
|
||||
columnList.add(rsmd.getColumnName(i));
|
||||
}
|
||||
while(rs.next()){
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (int i = 1; i <= colunmCount; i++) {
|
||||
// 获取列名
|
||||
String columnName = rsmd.getColumnName(i);
|
||||
Object val = null;
|
||||
switch (rsmd.getColumnType(i)) {
|
||||
case Types.ARRAY:
|
||||
val = rs.getArray(columnName);
|
||||
break;
|
||||
case Types.BIGINT:
|
||||
val = rs.getLong(columnName);
|
||||
break;
|
||||
case Types.BOOLEAN:
|
||||
case Types.BIT:
|
||||
val = rs.getBoolean(columnName);
|
||||
break;
|
||||
case Types.DOUBLE:
|
||||
val = rs.getDouble(columnName);
|
||||
break;
|
||||
case Types.FLOAT:
|
||||
case Types.REAL:
|
||||
val = rs.getFloat(columnName);
|
||||
break;
|
||||
case Types.INTEGER:
|
||||
val = rs.getInt(columnName);
|
||||
break;
|
||||
case Types.NVARCHAR:
|
||||
case Types.NCHAR:
|
||||
case Types.LONGNVARCHAR:
|
||||
val = rs.getNString(columnName);
|
||||
break;
|
||||
case Types.VARCHAR:
|
||||
case Types.CHAR:
|
||||
case Types.LONGVARCHAR:
|
||||
val = rs.getString(columnName);
|
||||
break;
|
||||
case Types.TINYINT:
|
||||
case Types.BINARY:
|
||||
case Types.VARBINARY:
|
||||
val = rs.getByte(columnName);
|
||||
break;
|
||||
case Types.SMALLINT:
|
||||
val = rs.getShort(columnName);
|
||||
break;
|
||||
case Types.DATE:
|
||||
val = rs.getDate(columnName);
|
||||
break;
|
||||
case Types.TIME:
|
||||
val = rs.getTime(columnName);
|
||||
break;
|
||||
case Types.TIMESTAMP:
|
||||
val = rs.getTimestamp(columnName);
|
||||
break;
|
||||
case Types.NUMERIC:
|
||||
case Types.DECIMAL:
|
||||
val = rs.getBigDecimal(columnName);
|
||||
break;
|
||||
case Types.BLOB:
|
||||
case Types.CLOB:
|
||||
case Types.LONGVARBINARY:
|
||||
case Types.DATALINK:
|
||||
case Types.REF:
|
||||
case Types.STRUCT:
|
||||
case Types.DISTINCT:
|
||||
case Types.JAVA_OBJECT:
|
||||
break;
|
||||
default:
|
||||
val = rs.getObject(columnName);
|
||||
break;
|
||||
}
|
||||
map.put(columnName, val);
|
||||
}
|
||||
dataList.add(map);
|
||||
}
|
||||
} else {
|
||||
// 执行新增、修改、删除受影响行数
|
||||
updateCount = stmt.getUpdateCount();
|
||||
}
|
||||
conn.commit();
|
||||
} catch (SQLException e) {
|
||||
sqlConsoleVo.setSuccess(false);
|
||||
if(conn != null){
|
||||
try {
|
||||
conn.rollback();
|
||||
} catch (SQLException e1) {
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
if(rs != null){
|
||||
rs.close();
|
||||
}
|
||||
if (stmt != null) {
|
||||
stmt.close();
|
||||
}
|
||||
if (conn != null) {
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
latch.countDown();
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("线程查询数据用时:" + (end - start) + "ms");
|
||||
sqlConsoleVo.setSql(sql);
|
||||
sqlConsoleVo.setCount(updateCount);
|
||||
sqlConsoleVo.setColumnList(columnList);
|
||||
sqlConsoleVo.setDataList(dataList);
|
||||
sqlConsoleVo.setTime(end - start);
|
||||
return sqlConsoleVo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package cn.datax.service.data.metadata.console.config;
|
||||
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
||||
import com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationAdvisor;
|
||||
import com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor;
|
||||
import com.baomidou.dynamic.datasource.processor.DsProcessor;
|
||||
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
|
||||
import com.baomidou.dynamic.datasource.provider.YmlDynamicDataSourceProvider;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceCreatorAutoConfiguration;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.druid.DruidDynamicDataSourceConfiguration;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Role;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 动态数据源核心自动配置类
|
||||
* @author AllDataDC
|
||||
* @date 2023/03/17
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
@EnableConfigurationProperties(DynamicDataSourceProperties.class)
|
||||
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
|
||||
@Import(value = {DruidDynamicDataSourceConfiguration.class, DynamicDataSourceCreatorAutoConfiguration.class})
|
||||
@ConditionalOnProperty(prefix = DynamicDataSourceProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class DynamicDSConfiguration {
|
||||
|
||||
private final DynamicDataSourceProperties properties;
|
||||
|
||||
//读取多数据源配置,注入到spring容器中
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DynamicDataSourceProvider dynamicDataSourceProvider() {
|
||||
Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
|
||||
return new YmlDynamicDataSourceProvider(datasourceMap);
|
||||
}
|
||||
|
||||
//注册自己的动态多数据源DataSource
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
|
||||
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
|
||||
dataSource.setPrimary(properties.getPrimary());
|
||||
dataSource.setStrict(properties.getStrict());
|
||||
dataSource.setStrategy(properties.getStrategy());
|
||||
dataSource.setProvider(dynamicDataSourceProvider);
|
||||
dataSource.setP6spy(properties.getP6spy());
|
||||
dataSource.setSeata(properties.getSeata());
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
//AOP切面,对DS注解过的方法进行增强,达到切换数据源的目的
|
||||
@Role(value = BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public DynamicDataSourceAnnotationAdvisor dynamicDatasourceAnnotationAdvisor(DsProcessor dsProcessor) {
|
||||
DynamicDataSourceAnnotationInterceptor interceptor = new DynamicDataSourceAnnotationInterceptor(properties.isAllowedPublicOnly(), dsProcessor);
|
||||
DynamicDataSourceAnnotationAdvisor advisor = new DynamicDataSourceAnnotationAdvisor(interceptor);
|
||||
advisor.setOrder(properties.getOrder());
|
||||
return advisor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.datax.service.data.metadata.console.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class StartedUpRunner implements ApplicationRunner {
|
||||
|
||||
private final ConfigurableApplicationContext context;
|
||||
private final Environment environment;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
if (context.isActive()) {
|
||||
String banner = "-----------------------------------------\n" +
|
||||
"服务启动成功,时间:" + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()) + "\n" +
|
||||
"服务名称:" + environment.getProperty("spring.application.name") + "\n" +
|
||||
"端口号:" + environment.getProperty("server.port") + "\n" +
|
||||
"-----------------------------------------";
|
||||
System.out.println(banner);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package cn.datax.service.data.metadata.console.config;
|
||||
|
||||
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
||||
import springfox.documentation.builders.*;
|
||||
import springfox.documentation.schema.ModelRef;
|
||||
import springfox.documentation.service.*;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "swagger", name = "enable", havingValue = "true")
|
||||
@EnableConfigurationProperties(SwaggerProperties.class)
|
||||
@EnableSwagger2
|
||||
@EnableKnife4j
|
||||
@Import(BeanValidatorPluginsConfiguration.class)
|
||||
public class SwaggerConfig {
|
||||
|
||||
@Autowired
|
||||
private SwaggerProperties swaggerProperties;
|
||||
|
||||
/**
|
||||
* 创建API应用
|
||||
* apiInfo() 增加API相关信息
|
||||
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
|
||||
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Docket createRestApi(){
|
||||
//版本类型是swagger2
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
//通过调用自定义方法apiInfo,获得文档的主要信息
|
||||
.apiInfo(apiInfo())
|
||||
//设置全局参数
|
||||
.globalOperationParameters(globalParamBuilder())
|
||||
//设置全局响应参数
|
||||
.globalResponseMessage(RequestMethod.GET,responseBuilder())
|
||||
.globalResponseMessage(RequestMethod.POST,responseBuilder())
|
||||
.globalResponseMessage(RequestMethod.PUT,responseBuilder())
|
||||
.globalResponseMessage(RequestMethod.DELETE,responseBuilder())
|
||||
.select()
|
||||
//扫描该包下面的API注解
|
||||
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
|
||||
.paths(PathSelectors.any())
|
||||
.build()
|
||||
//设置安全认证
|
||||
;
|
||||
}
|
||||
/**
|
||||
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
|
||||
* 访问地址:http://项目实际地址/swagger-ui.html
|
||||
* @return
|
||||
*/
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title(swaggerProperties.getTitle())
|
||||
.description(swaggerProperties.getDescription())
|
||||
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
|
||||
.version(swaggerProperties.getVersion())
|
||||
.contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全认证参数
|
||||
* @return
|
||||
*/
|
||||
private List<ApiKey> security() {
|
||||
List<ApiKey> apiKeys = new ArrayList<>();
|
||||
apiKeys.add(new ApiKey("Authorization", "Authorization", "header"));
|
||||
return apiKeys;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建全局参数列表
|
||||
* @return
|
||||
*/
|
||||
private List<Parameter> globalParamBuilder(){
|
||||
List<Parameter> pars = new ArrayList<>();
|
||||
pars.add(parameterBuilder("Authorization","令牌","string","header",false).build());
|
||||
return pars;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建参数
|
||||
* @return
|
||||
*/
|
||||
private ParameterBuilder parameterBuilder(String name, String desc, String type, String parameterType, boolean required) {
|
||||
ParameterBuilder tokenPar = new ParameterBuilder();
|
||||
tokenPar.name(name).description(desc).modelRef(new ModelRef(type)).parameterType(parameterType).required(required).build();
|
||||
return tokenPar;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建全局响应值
|
||||
* @return
|
||||
*/
|
||||
private List<ResponseMessage> responseBuilder() {
|
||||
List<ResponseMessage> responseMessageList = new ArrayList<>();
|
||||
responseMessageList.add(new ResponseMessageBuilder().code(200).message("响应成功").build());
|
||||
responseMessageList.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").build());
|
||||
return responseMessageList;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package cn.datax.service.data.metadata.console.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(ignoreUnknownFields = false, prefix = "swagger")
|
||||
public class SwaggerProperties {
|
||||
|
||||
private Boolean enable;
|
||||
private String title;
|
||||
private String description;
|
||||
private String version;
|
||||
private String termsOfServiceUrl;
|
||||
private String basePackage;
|
||||
private Contact contact;
|
||||
|
||||
public Boolean getEnable() {
|
||||
return enable;
|
||||
}
|
||||
|
||||
public void setEnable(Boolean enable) {
|
||||
this.enable = enable;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(String version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getTermsOfServiceUrl() {
|
||||
return termsOfServiceUrl;
|
||||
}
|
||||
|
||||
public void setTermsOfServiceUrl(String termsOfServiceUrl) {
|
||||
this.termsOfServiceUrl = termsOfServiceUrl;
|
||||
}
|
||||
|
||||
public String getBasePackage() {
|
||||
return basePackage;
|
||||
}
|
||||
|
||||
public void setBasePackage(String basePackage) {
|
||||
this.basePackage = basePackage;
|
||||
}
|
||||
|
||||
public Contact getContact() {
|
||||
return contact;
|
||||
}
|
||||
|
||||
public void setContact(Contact contact) {
|
||||
this.contact = contact;
|
||||
}
|
||||
|
||||
public static class Contact {
|
||||
private String name;
|
||||
private String url;
|
||||
private String email;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.datax.service.data.metadata.console.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity(debug = false)
|
||||
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.csrf().disable().authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package cn.datax.service.data.metadata.console.controller;
|
||||
|
||||
import cn.datax.common.base.BaseController;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/inner")
|
||||
public class InnerController extends BaseController {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cn.datax.service.data.metadata.console.controller;
|
||||
|
||||
import cn.datax.common.base.BaseController;
|
||||
import cn.datax.common.core.R;
|
||||
import cn.datax.common.validate.ValidationGroups;
|
||||
import cn.datax.service.data.metadata.api.dto.SqlConsoleDto;
|
||||
import cn.datax.service.data.metadata.api.vo.SqlConsoleVo;
|
||||
import cn.datax.service.data.metadata.console.service.SqlConsoleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/sql")
|
||||
public class SqlConsoleController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private SqlConsoleService sqlConsoleService;
|
||||
|
||||
@PostMapping("/run")
|
||||
public R sqlRun(@RequestBody @Validated SqlConsoleDto sqlConsoleDto){
|
||||
List<SqlConsoleVo> list = sqlConsoleService.sqlRun(sqlConsoleDto);
|
||||
return R.ok().setData(list);
|
||||
}
|
||||
|
||||
@PostMapping("/stop")
|
||||
public R sqlStop(@RequestBody @Validated({ValidationGroups.Other.class}) SqlConsoleDto sqlConsoleDto){
|
||||
sqlConsoleService.sqlStop(sqlConsoleDto);
|
||||
return R.ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.datax.service.data.metadata.console.service;
|
||||
|
||||
import cn.datax.service.data.metadata.api.dto.SqlConsoleDto;
|
||||
import cn.datax.service.data.metadata.api.vo.SqlConsoleVo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SqlConsoleService {
|
||||
|
||||
List<SqlConsoleVo> sqlRun(SqlConsoleDto sqlConsoleDto);
|
||||
|
||||
void sqlStop(SqlConsoleDto sqlConsoleDto);
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package cn.datax.service.data.metadata.console.service.impl;
|
||||
|
||||
import cn.datax.common.database.DataSourceFactory;
|
||||
import cn.datax.common.database.DbQuery;
|
||||
import cn.datax.common.database.constants.DbQueryProperty;
|
||||
import cn.datax.common.exception.DataException;
|
||||
import cn.datax.common.utils.ThrowableUtil;
|
||||
import cn.datax.service.data.metadata.api.dto.DbSchema;
|
||||
import cn.datax.service.data.metadata.api.dto.SqlConsoleDto;
|
||||
import cn.datax.service.data.metadata.api.entity.MetadataSourceEntity;
|
||||
import cn.datax.service.data.metadata.api.feign.MetadataSourceServiceFeign;
|
||||
import cn.datax.service.data.metadata.api.vo.SqlConsoleVo;
|
||||
import cn.datax.service.data.metadata.console.concurrent.CallableTemplate;
|
||||
import cn.datax.service.data.metadata.console.concurrent.DateHander;
|
||||
import cn.datax.service.data.metadata.console.service.SqlConsoleService;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.JSQLParserException;
|
||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import net.sf.jsqlparser.statement.Statement;
|
||||
import net.sf.jsqlparser.statement.Statements;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SqlConsoleServiceImpl implements SqlConsoleService {
|
||||
|
||||
@Autowired
|
||||
private DataSourceFactory dataSourceFactory;
|
||||
|
||||
@Autowired
|
||||
private MetadataSourceServiceFeign metadataSourceServiceFeign;
|
||||
|
||||
private static Map<String, List<Connection>> connectionMap = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public List<SqlConsoleVo> sqlRun(SqlConsoleDto sqlConsoleDto) {
|
||||
String sqlKey = sqlConsoleDto.getSqlKey();
|
||||
Statements stmts;
|
||||
try {
|
||||
stmts = CCJSqlParserUtil.parseStatements(sqlConsoleDto.getSqlText());
|
||||
} catch (JSQLParserException e) {
|
||||
log.error("全局异常信息ex={}, StackTrace={}", e.getMessage(), ThrowableUtil.getStackTrace(e));
|
||||
throw new DataException("SQL语法有问题,解析出错");
|
||||
}
|
||||
List<Statement> sqls = stmts.getStatements();
|
||||
if (CollUtil.isEmpty(sqls)) {
|
||||
throw new DataException("未解析到SQL语句");
|
||||
}
|
||||
MetadataSourceEntity dataSource = metadataSourceServiceFeign.getMetadataSourceById(sqlConsoleDto.getSourceId());
|
||||
if(dataSource == null){
|
||||
throw new DataException("SQL工作台查询数据源出错");
|
||||
}
|
||||
DbSchema dbSchema = dataSource.getDbSchema();
|
||||
DbQueryProperty dbQueryProperty = new DbQueryProperty(dataSource.getDbType(), dbSchema.getHost(),
|
||||
dbSchema.getUsername(), dbSchema.getPassword(), dbSchema.getPort(), dbSchema.getDbName(), dbSchema.getSid());
|
||||
DbQuery dbQuery = dataSourceFactory.createDbQuery(dbQueryProperty);
|
||||
// 定义计数器
|
||||
final CountDownLatch latch = new CountDownLatch(sqls.size());
|
||||
// 定义固定长度的线程池
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(sqls.size());
|
||||
// Callable用于产生结果
|
||||
List<CallableTemplate<SqlConsoleVo>> tasks = new ArrayList<>();
|
||||
List<Connection> conns = new ArrayList<>();
|
||||
for (int i = 0; i < sqls.size(); i++) {
|
||||
Connection conn = dbQuery.getConnection();
|
||||
conns.add(conn);
|
||||
DateHander dateHander = new DateHander(latch, conn, sqls.get(i).toString());
|
||||
tasks.add(dateHander);
|
||||
}
|
||||
connectionMap.put(sqlKey, conns);
|
||||
// Future用于获取结果
|
||||
List<SqlConsoleVo> result = new ArrayList<>();
|
||||
List<Future<SqlConsoleVo>> futures;
|
||||
try {
|
||||
futures = executorService.invokeAll(tasks);
|
||||
// 主线程阻塞,等待所有子线程执行完成
|
||||
latch.await();
|
||||
// 处理线程返回结果
|
||||
for (Future<SqlConsoleVo> future : futures) {
|
||||
result.add(future.get());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("全局异常信息ex={}, StackTrace={}", e.getMessage(), ThrowableUtil.getStackTrace(e));
|
||||
}
|
||||
// 关闭线程池
|
||||
executorService.shutdown();
|
||||
// 执行完清除
|
||||
connectionMap.remove(sqlKey);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sqlStop(SqlConsoleDto sqlConsoleDto) {
|
||||
String sqlKey = sqlConsoleDto.getSqlKey();
|
||||
List<Connection> conns = connectionMap.get(sqlKey);
|
||||
if (CollUtil.isNotEmpty(conns)) {
|
||||
for (int i = 0; i < conns.size(); i++) {
|
||||
Connection conn = conns.get(i);
|
||||
try {
|
||||
if (null != conn && !conn.isClosed()) {
|
||||
conn.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new DataException("SQL工作台停止出错");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
server:
|
||||
port: 8821
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: service-data-console
|
||||
profiles:
|
||||
active: dev
|
||||
cloud:
|
||||
config:
|
||||
label: master
|
||||
name: ${spring.application.name}
|
||||
profile: ${spring.profiles.active}
|
||||
discovery:
|
||||
enabled: true
|
||||
service-id: config
|
||||
|
||||
# 注册中心配置
|
||||
eureka:
|
||||
instance:
|
||||
lease-renewal-interval-in-seconds: 20
|
||||
prefer-ip-address: true
|
||||
ip-address: 192.168.1.169
|
||||
client:
|
||||
register-with-eureka: true
|
||||
fetch-registry: true
|
||||
instance-info-replication-interval-seconds: 30
|
||||
registry-fetch-interval-seconds: 3
|
||||
service-url:
|
||||
defaultZone: http://192.168.1.169:8610/eureka
|
||||
@@ -0,0 +1,78 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<springProperty scope="context" name="springAppName" source="spring.application.name"/>
|
||||
<property name="log.path" value="logs/service-data-console"/>
|
||||
<property name="log.maxHistory" value="15"/>
|
||||
<property name="log.totalSizeCap" value="500MB"/>
|
||||
<property name="log.maxFileSize" value="10MB"/>
|
||||
<property name="log.colorPattern"
|
||||
value="%magenta(%d{yyyy-MM-dd HH:mm:ss}) %highlight(%-5level) %boldCyan(${springAppName:-}) %yellow(%thread) %green(%logger) %msg%n"/>
|
||||
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5level ${springAppName:-} %thread %logger %msg%n"/>
|
||||
|
||||
<!--输出到控制台-->
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${log.colorPattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!--输出到文件-->
|
||||
<!-- RollingFileAppender:滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 -->
|
||||
<!-- 以下的大概意思是:1.先按日期存日志,日期变了,将前一天的日志文件名重命名为XXX%日期%索引,新的日志仍然是project_info.log -->
|
||||
<!-- 2.如果日期没有发生变化,但是当前日志的文件大小超过10MB时,对当前日志进行分割 重命名-->
|
||||
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<!--日志文件路径和名称-->
|
||||
<File>${log.path}/info/info.log</File>
|
||||
<!--是否追加到文件末尾,默认为true-->
|
||||
<append>true</append>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<!-- 日志文件的名字会根据fileNamePattern的值,每隔一段时间改变一次 -->
|
||||
<!-- 文件名:logs/project_info.2017-12-05.0.log -->
|
||||
<!-- 注意:SizeAndTimeBasedRollingPolicy中 %i和%d令牌都是强制性的,必须存在,要不会报错 -->
|
||||
<fileNamePattern>${log.path}/info/info.%d.%i.log</fileNamePattern>
|
||||
<!-- 每产生一个日志文件,该日志文件的保存期限为30天, ps:maxHistory的单位是根据fileNamePattern中的翻转策略自动推算出来的,例如上面选用了yyyy-MM-dd,则单位为天
|
||||
如果上面选用了yyyy-MM,则单位为月,另外上面的单位默认为yyyy-MM-dd-->
|
||||
<MaxHistory>${log.maxHistory}</MaxHistory>
|
||||
<!-- 每个日志文件到2mb的时候开始切分,最多保留30天,但最大到500MB,哪怕没到30天也要删除多余的日志 -->
|
||||
<totalSizeCap>${log.totalSizeCap}</totalSizeCap>
|
||||
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,测试时可改成5KB看效果 -->
|
||||
<maxFileSize>${log.maxFileSize}</maxFileSize>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>INFO</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<File>${log.path}/error/error.log</File>
|
||||
<append>true</append>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
<fileNamePattern>${log.path}/error/error.%d.%i.log</fileNamePattern>
|
||||
<MaxHistory>${log.maxHistory}</MaxHistory>
|
||||
<totalSizeCap>${log.totalSizeCap}</totalSizeCap>
|
||||
<maxFileSize>${log.maxFileSize}</maxFileSize>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>ERROR</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<root level="debug">
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="file_info"/>
|
||||
<appender-ref ref="file_error"/>
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,25 @@
|
||||
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
|
||||
Reference in New Issue
Block a user