diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8d693e8..03a0229 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,118 +4,83 @@ - + - - - - - - - @@ -294,6 +250,19 @@ + + + - + + - @@ -341,7 +310,8 @@ - + + - @@ -388,7 +366,8 @@ - diff --git a/etl-common/src/main/java/com/etl/common/exception/LimitException.java b/etl-common/src/main/java/com/etl/common/exception/LimitException.java new file mode 100644 index 0000000..6dc5d92 --- /dev/null +++ b/etl-common/src/main/java/com/etl/common/exception/LimitException.java @@ -0,0 +1,28 @@ +package com.etl.common.exception; + +public class LimitException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + // 默认的构造函数 + public LimitException() { + super(); + } + + // 带错误信息的构造函数 + public LimitException(String message) { + super(message); + } + + // 带错误信息和原因的构造函数 + public LimitException(String message, Throwable cause) { + super(message, cause); + } + + // 带原因的构造函数 + public LimitException(Throwable cause) { + super(cause); + } + + +} diff --git a/etl-common/target/classes/com/etl/common/exception/LimitException.class b/etl-common/target/classes/com/etl/common/exception/LimitException.class new file mode 100644 index 0000000..19d1459 Binary files /dev/null and b/etl-common/target/classes/com/etl/common/exception/LimitException.class differ diff --git a/etl-database/etl-database-common/pom.xml b/etl-database/etl-database-common/pom.xml index 2d1e3c4..a4d63d7 100644 --- a/etl-database/etl-database-common/pom.xml +++ b/etl-database/etl-database-common/pom.xml @@ -54,6 +54,12 @@ 3.5.4.1 compile + + io.swagger + swagger-annotations + 1.6.6 + compile + diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/annoation/Limit.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/annoation/Limit.java new file mode 100644 index 0000000..e91bb2d --- /dev/null +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/annoation/Limit.java @@ -0,0 +1,26 @@ +package com.etl.database.common.annoation; + +import java.lang.annotation.*; +import java.util.concurrent.TimeUnit; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +@Documented +public @interface Limit { + + // 资源key + String key() default ""; + + // 最多访问次数 + double permitsPerSecond(); + + // 时间 + long timeout(); + + // 时间类型 + TimeUnit timeunit() default TimeUnit.MILLISECONDS; + + // 提示信息 + String msg() default "系统繁忙,请稍后再试"; + +} diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSourceType.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSourceType.java index 17aafba..92dcd45 100644 --- a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSourceType.java +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSourceType.java @@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; import javax.validation.constraints.NotBlank; @@ -15,11 +17,14 @@ import javax.validation.constraints.NotBlank; @ApiModel(description = "数据源类型") @TableName("t_data_source_type") @Data +@NoArgsConstructor +@AllArgsConstructor public class DataSourceType{ @TableId(value = "id",type = IdType.AUTO) @ApiModelProperty(value = "数据源类型id") private Integer id; + @NotBlank(message = "数据源类型名称不能为空") @ApiModelProperty(value = "数据源类型名称") private String dataSourceTypeName; diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSource.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSources.java similarity index 84% rename from etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSource.java rename to etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSources.java index f374d0f..737c589 100644 --- a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSource.java +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DataSources.java @@ -1,9 +1,10 @@ package com.etl.database.common.entity; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import com.etl.database.common.database.BaseMate; +import com.etl.database.common.entity.database.BaseMate; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -16,12 +17,11 @@ import javax.validation.constraints.*; @ApiModel("数据源配置") @Data @TableName("t_data_source_etl") -public class DataSource extends BaseMate { +public class DataSources extends BaseMate { @TableId(value = "id",type = IdType.AUTO) @ApiModelProperty(value = "数据源id") private Integer id; - @NotBlank(message = "数据源描述不能为空") @ApiModelProperty(value = "数据源描述") private String databaseDescription; @@ -33,11 +33,9 @@ public class DataSource extends BaseMate { @ApiModelProperty(value = "端口") private String port; - @NotBlank(message = "数据库名称不能为空") @ApiModelProperty(value = "数据库名称") private String databaseName; - @ApiModelProperty(value = "状态 0-停用 1-启用") @Min(value = 0, message = "状态值不能小于0") @Max(value = 1, message = "状态值不能大于1") @@ -50,7 +48,10 @@ public class DataSource extends BaseMate { @ApiModelProperty(value = "额外配置") private String extraEvlop; - @NotNull(message = "数据库类型id不能为空") @ApiModelProperty(value ="数据库类型id") private Integer databaseType; + + @TableField(exist = false) + @ApiModelProperty(value = "数据库类型") + private String dataSourceTypeName; } diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DatabaseRedis.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DatabaseRedis.java new file mode 100644 index 0000000..a44b06e --- /dev/null +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/DatabaseRedis.java @@ -0,0 +1,22 @@ +package com.etl.database.common.entity; + +import io.swagger.annotations.ApiModel; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@ApiModel(value = "redis数据库配置") +public class DatabaseRedis { + + @NotBlank(message = "redis连接地址不能为空") + private String url; + + @NotNull(message = "redis端口号不能为空") + private Integer port; +} diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/constants/DataSourceConstants.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/constants/DataSourceConstants.java new file mode 100644 index 0000000..7a380b4 --- /dev/null +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/constants/DataSourceConstants.java @@ -0,0 +1,7 @@ +package com.etl.database.common.entity.constants; + +public class DataSourceConstants { + public static final String OK = "ok"; + public static final String MYSQL_USER = "user"; + public static final String MYSQL_PASSWORD = "password"; +} diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/database/BaseMate.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/database/BaseMate.java similarity index 91% rename from etl-database/etl-database-common/src/main/java/com/etl/database/common/database/BaseMate.java rename to etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/database/BaseMate.java index 224acca..7de3d18 100644 --- a/etl-database/etl-database-common/src/main/java/com/etl/database/common/database/BaseMate.java +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/database/BaseMate.java @@ -1,4 +1,4 @@ -package com.etl.database.common.database; +package com.etl.database.common.entity.database; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/req/DataSourceReq.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/req/DataSourceReq.java new file mode 100644 index 0000000..a8f6734 --- /dev/null +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/req/DataSourceReq.java @@ -0,0 +1,21 @@ +package com.etl.database.common.entity.req; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +@ApiModel("查询数据源") +public class DataSourceReq { + @ApiModelProperty("页码") + private Integer pageNum=1; + + @ApiModelProperty("每页条数") + private Integer pageSize=3; + + @ApiModelProperty("数据源描述") + private String dataSourceDescribe; + + @ApiModelProperty("数据源类型") + private Integer dataSourceType; +} diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/req/DruidReq.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/req/DruidReq.java new file mode 100644 index 0000000..4cbedab --- /dev/null +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/req/DruidReq.java @@ -0,0 +1,39 @@ +package com.etl.database.common.entity.req; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import javax.validation.constraints.NotBlank; + +@ApiModel("druid连接池实体类") +@Data +@AllArgsConstructor +@NoArgsConstructor +@ToString +public class DruidReq { + @NotBlank(message = "数据库连接地址不能为空") + @ApiModelProperty(value = "地址") + private String url; + + @NotBlank(message = "用户名不能为空") + @ApiModelProperty("用户名") + private String username; + + @NotBlank(message = "密码不能为空") + @ApiModelProperty("密码") + private String password; + + @NotBlank(message = "驱动类名不能为空") + @ApiModelProperty("驱动类名") + private String driverClassName; + + @ApiModelProperty(value = "数据库名称") + private String databaseName; + + @ApiModelProperty(value = "数据库表名") + private String tableName; +} diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/resp/ColumnInfo.java b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/resp/ColumnInfo.java new file mode 100644 index 0000000..38ba0f0 --- /dev/null +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/common/entity/resp/ColumnInfo.java @@ -0,0 +1,32 @@ +package com.etl.database.common.entity.resp; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@ApiModel(value = "字段信息") +public class ColumnInfo { + @ApiModelProperty(value = "字段名") + private String columnName; + + @ApiModelProperty(value = "字段类型") + private String dataType; + + @ApiModelProperty(value = "字段长度") + private Integer characterMaximumLength; // 注意这里可能是null + + @ApiModelProperty(value = "字段键类型") + private String columnKey; // 主键、唯一键等 + + @ApiModelProperty(value = "是否为空") + private String isNullable; // YES 或 NO + + @ApiModelProperty(value = "字段注释") + private String columnComment; // 列注释 + +} diff --git a/etl-database/etl-database-common/src/main/java/com/etl/database/ennum/LimitType.java b/etl-database/etl-database-common/src/main/java/com/etl/database/ennum/LimitType.java new file mode 100644 index 0000000..381462a --- /dev/null +++ b/etl-database/etl-database-common/src/main/java/com/etl/database/ennum/LimitType.java @@ -0,0 +1,33 @@ +package com.etl.database.ennum; + +public enum LimitType { + IP, + /** + * 直接拒绝超过限流阈值的请求 + */ + REJECT, + + /** + * 对超过限流阈值的请求进行排队,等待资源可用 + */ + QUEUE, + + /** + * 允许超过限流阈值的请求,但可能需要更长时间处理 + * (比如降级服务到较慢的后端) + */ + DEGRADE, + + /** + * 自定义处理方式 + */ + CUSTOM, + + // 这里可以添加更多的处理方式... + + /** + * 默认的处理方式 + */ + DEFAULT, + CUSTOMER, +} diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/annoation/Limit.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/annoation/Limit.class new file mode 100644 index 0000000..9c8726c Binary files /dev/null and b/etl-database/etl-database-common/target/classes/com/etl/database/common/annoation/Limit.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSource.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSource.class deleted file mode 100644 index c00b6d5..0000000 Binary files a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSource.class and /dev/null differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSourceType.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSourceType.class index df090d2..cb527eb 100644 Binary files a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSourceType.class and b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSourceType.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSources.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSources.class new file mode 100644 index 0000000..da8ea5d Binary files /dev/null and b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DataSources.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DatabaseRedis.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DatabaseRedis.class new file mode 100644 index 0000000..e79ae9b Binary files /dev/null and b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/DatabaseRedis.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/constants/DataSourceConstants.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/constants/DataSourceConstants.class new file mode 100644 index 0000000..fec5903 Binary files /dev/null and b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/constants/DataSourceConstants.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/database/BaseMate.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/database/BaseMate.class similarity index 60% rename from etl-database/etl-database-common/target/classes/com/etl/database/common/database/BaseMate.class rename to etl-database/etl-database-common/target/classes/com/etl/database/common/entity/database/BaseMate.class index bf96bc3..005750d 100644 Binary files a/etl-database/etl-database-common/target/classes/com/etl/database/common/database/BaseMate.class and b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/database/BaseMate.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/req/DataSourceReq.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/req/DataSourceReq.class new file mode 100644 index 0000000..8717a95 Binary files /dev/null and b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/req/DataSourceReq.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/req/DruidReq.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/req/DruidReq.class new file mode 100644 index 0000000..9e01ab6 Binary files /dev/null and b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/req/DruidReq.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/resp/ColumnInfo.class b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/resp/ColumnInfo.class new file mode 100644 index 0000000..f7e18c8 Binary files /dev/null and b/etl-database/etl-database-common/target/classes/com/etl/database/common/entity/resp/ColumnInfo.class differ diff --git a/etl-database/etl-database-common/target/classes/com/etl/database/ennum/LimitType.class b/etl-database/etl-database-common/target/classes/com/etl/database/ennum/LimitType.class new file mode 100644 index 0000000..11ee75a Binary files /dev/null and b/etl-database/etl-database-common/target/classes/com/etl/database/ennum/LimitType.class differ diff --git a/etl-database/etl-database-common/target/etl-database-common-1.0-SNAPSHOT.jar b/etl-database/etl-database-common/target/etl-database-common-1.0-SNAPSHOT.jar index 3f78b5b..b17f3a0 100644 Binary files a/etl-database/etl-database-common/target/etl-database-common-1.0-SNAPSHOT.jar and b/etl-database/etl-database-common/target/etl-database-common-1.0-SNAPSHOT.jar differ diff --git a/etl-database/etl-database-common/target/maven-archiver/pom.properties b/etl-database/etl-database-common/target/maven-archiver/pom.properties index d85b229..2e9fac8 100644 --- a/etl-database/etl-database-common/target/maven-archiver/pom.properties +++ b/etl-database/etl-database-common/target/maven-archiver/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven -#Tue Jun 25 19:44:07 CST 2024 +#Wed Jun 26 17:13:09 CST 2024 version=1.0-SNAPSHOT groupId=com.bwie artifactId=etl-database-common diff --git a/etl-database/etl-database-common/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/etl-database/etl-database-common/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 0000000..e73a2b6 --- /dev/null +++ b/etl-database/etl-database-common/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,10 @@ +com\etl\database\common\entity\req\DruidReq.class +com\etl\database\common\entity\resp\ColumnInfo.class +com\etl\database\common\entity\req\DataSourceReq.class +com\etl\database\common\annoation\Limit.class +com\etl\database\common\entity\DataSources.class +com\etl\database\common\entity\DataSourceType.class +com\etl\database\common\entity\constants\DataSourceConstants.class +com\etl\database\common\entity\DatabaseRedis.class +com\etl\database\common\entity\database\BaseMate.class +com\etl\database\ennum\LimitType.class diff --git a/etl-database/etl-database-common/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/etl-database/etl-database-common/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst index e69de29..21c4ea7 100644 --- a/etl-database/etl-database-common/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ b/etl-database/etl-database-common/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,10 @@ +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\annoation\Limit.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\entity\DataSources.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\entity\resp\ColumnInfo.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\ennum\LimitType.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\entity\DatabaseRedis.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\entity\DataSourceType.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\entity\req\DruidReq.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\entity\req\DataSourceReq.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\entity\constants\DataSourceConstants.java +D:\workspace\ETL\etl-database\etl-database-common\src\main\java\com\etl\database\common\entity\database\BaseMate.java diff --git a/etl-database/etl-database-server/pom.xml b/etl-database/etl-database-server/pom.xml index 35a0314..da2272f 100644 --- a/etl-database/etl-database-server/pom.xml +++ b/etl-database/etl-database-server/pom.xml @@ -16,14 +16,13 @@ - com.bwie - etl-common - 1.0-SNAPSHOT + io.springfox + springfox-swagger2 + 3.0.0 - com.bwie - etl-database-common - 1.0-SNAPSHOT + com.baomidou + mybatis-plus-boot-starter com.github.yulichang @@ -31,8 +30,46 @@ 1.3.8 - com.baomidou - mybatis-plus-boot-starter + com.bwie + etl-database-common + 1.0-SNAPSHOT + + + com.bwie + etl-common + 1.0-SNAPSHOT + + + org.springframework.cloud + spring-cloud-starter-bootstrap + 3.1.3 + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework + spring-jdbc + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + redis.clients + jedis + 3.7.0 + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + com.google.guava + guava + 30.1-jre org.springframework.boot @@ -41,11 +78,7 @@ com.mysql mysql-connector-j - - - org.springframework.cloud - spring-cloud-starter-bootstrap - 3.1.7 + runtime org.springframework.boot @@ -57,6 +90,15 @@ spring-boot-starter-test test + + com.github.yulichang + mybatis-plus-join-boot-starter + 1.3.8 + + + org.projectlombok + lombok + diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/aop/LimitAspect.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/aop/LimitAspect.java new file mode 100644 index 0000000..53a1186 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/aop/LimitAspect.java @@ -0,0 +1,53 @@ +package com.etl.database.server.aop; + + + +import com.etl.database.common.annoation.Limit; +import com.etl.database.server.exception.LimitException; +import com.google.common.collect.Maps; +import com.google.common.util.concurrent.RateLimiter; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.springframework.stereotype.Component; +import java.lang.reflect.Method; +import org.aspectj.lang.reflect.MethodSignature; +import java.util.Map; +import org.aspectj.lang.annotation.*; +@Slf4j +@Aspect +@Component +public class LimitAspect { + + private final Map limitMap = Maps.newConcurrentMap(); + + @Around("@annotation(com.etl.database.common.annoation.Limit)") + public Object around(ProceedingJoinPoint pjp) throws Throwable { + MethodSignature signature = (MethodSignature)pjp.getSignature(); + + Method method = signature.getMethod(); + //拿limit的注解 + Limit limit = method.getAnnotation(Limit.class); + if (limit != null) { + //key作用:不同的接口,不同的流量控制 + String key=limit.key(); + RateLimiter rateLimiter; + //验证缓存是否有命中key + if (!limitMap.containsKey(key)) { + // 创建令牌桶 + rateLimiter = RateLimiter.create(limit.permitsPerSecond()); + limitMap.put(key, rateLimiter); + log.info("新建了令牌桶={},容量={}",key,limit.permitsPerSecond()); + } + rateLimiter = limitMap.get(key); + // 拿令牌 + boolean acquire = rateLimiter.tryAcquire(limit.timeout(), limit.timeunit()); + // 拿不到命令,直接返回异常提示 + if (!acquire) { + log.debug("令牌桶={},获取令牌失败",key); + throw new LimitException(limit.msg()); + } + } + return pjp.proceed(); + } + +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/config/RedisConfig.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/config/RedisConfig.java new file mode 100644 index 0000000..4bae3b9 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/config/RedisConfig.java @@ -0,0 +1,34 @@ +package com.etl.database.server.config; + + +import com.etl.database.common.entity.DatabaseRedis; +import org.springframework.context.annotation.Configuration; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; + +@Configuration +public class RedisConfig { + + private static JedisPool jedisPool; + + public static synchronized JedisPool getJedisPool(DatabaseRedis databaseRedis) { + if (jedisPool == null) { + JedisPoolConfig poolConfig = new JedisPoolConfig(); + // 设置连接池参数,如最大连接数、最大空闲连接数等 + poolConfig.setMaxTotal(128); + poolConfig.setMaxIdle(128); + poolConfig.setMinIdle(16); + poolConfig.setTestOnBorrow(true); + poolConfig.setTestOnReturn(true); + // 其他配置... + + String host = databaseRedis.getUrl(); // Redis服务器地址 + int port = databaseRedis.getPort(); // Redis服务器端口 + jedisPool = new JedisPool(poolConfig, host, port); + } + return jedisPool; + } + + + +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSouceController.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSourceController.java similarity index 54% rename from etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSouceController.java rename to etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSourceController.java index 3515281..fae4c4c 100644 --- a/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSouceController.java +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSourceController.java @@ -1,34 +1,31 @@ package com.etl.database.server.controller; import com.etl.common.result.Result; -import com.etl.database.common.entity.DataSource; +import com.etl.database.common.entity.req.DataSourceReq; import com.etl.database.server.service.DataSouceService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; 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.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; -import java.util.List; /** * 数据源控制层 */ @RestController @RequestMapping(value ="/datasource") -@Api(tags = "数据源-API") -public class DataSouceController { +@Api(tags = "数据源-datasource") +public class DataSourceController { @Autowired private DataSouceService dataSouceService; - @ApiOperation(value = "测试连接mysql") - @RequestMapping(value = "/testConnectMysql",method = RequestMethod.POST) - public Result testConnectMysql(@RequestBody @Valid DataSource dataSource){ - List list = dataSouceService.list(); - return null; + @PostMapping("/findDataSource") + @ApiOperation(value = "查询数据源") + public Result findDateSource(@RequestBody @Valid DataSourceReq dataSourceReq) { + return dataSouceService.findDateSource(dataSourceReq); } } diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSourceTypeController.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSourceTypeController.java new file mode 100644 index 0000000..6f09167 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/DataSourceTypeController.java @@ -0,0 +1,26 @@ +package com.etl.database.server.controller; + +import com.etl.common.result.Result; +import com.etl.database.server.service.DataSourceTypeService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 数据源类型控制层 + */ +@RestController +@Api(tags = "数据源类型-API") +@RequestMapping("/datasourcetype") +public class DataSourceTypeController { + @Autowired + private DataSourceTypeService dataSourceTypeService; + @GetMapping("/findDataSourceType") + @ApiOperation(value = "查询数据源类型") + public Result findDataSourceType() { + return dataSourceTypeService.findDataSourceType(); + } +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/MysqlController.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/MysqlController.java new file mode 100644 index 0000000..8051023 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/MysqlController.java @@ -0,0 +1,61 @@ +package com.etl.database.server.controller; + +import com.etl.common.result.Result; +import com.etl.database.common.entity.constants.DataSourceConstants; +import com.etl.database.common.entity.req.DruidReq; +import com.etl.database.common.entity.resp.ColumnInfo; +import com.etl.database.server.service.MysqlService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; +import java.util.Map; + +/** + * mysql控制层 + */ +@RestController +@RequestMapping(value ="/mysql") +@Api(tags = "数据源-mysql") +public class MysqlController { + @Autowired + private MysqlService dataSouceService; + + @ApiOperation(value = "测试连接mysql") + @RequestMapping(value = "/testConnectMysql",method = RequestMethod.POST) + public Result testConnectMysql(@RequestBody @Valid DruidReq druidReq){ + String result = dataSouceService.testConnectMysql(druidReq); + if (result.equals(DataSourceConstants.OK)){ + return Result.success("连接成功"); + }else { + return Result.error("连接失败"); + } + } + + @PostMapping("/findDatabaseTable") + @ApiOperation(value = "查询数据库表") + public Result> findDatabaseTable(@Valid @RequestBody DruidReq druidReq){ + return Result.success(dataSouceService.findDatabaseTable(druidReq)); + } + + @PostMapping("/findDatabaseTableName") + @ApiOperation("查询指定数据库的所有表名") + public Result> findDatabaseTableName(@Valid @RequestBody DruidReq druidReq){ + return Result.success(dataSouceService.findDatabaseTables(druidReq)); + } + + @PostMapping("/findDatabaseTableField") + @ApiOperation("查询指定数据库的指定表的所有字段") + public Result> findDatabaseTableField(@Valid @RequestBody DruidReq druidReq){ + return Result.success(dataSouceService.findDatabaseTableField(druidReq)); + } + + @PostMapping("/findDatabaseTableFieldPrice") + @ApiOperation("复制数据源数据") + public Result> > findDatabaseTableFieldPrice(@Valid @RequestBody DruidReq druidReq){ + return Result.success(dataSouceService.findDatabaseTableFieldPrice(druidReq)); + } +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/RedisController.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/RedisController.java new file mode 100644 index 0000000..f3626d5 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/controller/RedisController.java @@ -0,0 +1,34 @@ +package com.etl.database.server.controller; + +import com.etl.common.result.Result; +import com.etl.database.common.annoation.Limit; +import com.etl.database.common.entity.DatabaseRedis; +import com.etl.database.server.service.RedisService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +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 javax.validation.Valid; +import java.util.Map; + +/** + * redis控制层 + */ +@RestController +@RequestMapping(value ="/redis") +@Api(tags = "数据源-redis") +public class RedisController { + @Autowired + private RedisService redisService; + @PostMapping("/testDatabaseRedis") + @ApiOperation(value = "测试redis连接") + @Limit(key = "testDatabaseRedis", permitsPerSecond = 1, timeout = 500, msg = "当前排队人数较多,请稍后再试!") + public Result> testDatabaseRedis(@Valid @RequestBody DatabaseRedis databaseRedis) { + Map map = redisService.testDatabaseRedis(databaseRedis); + return Result.success(map); + } +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/exception/LimitException.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/exception/LimitException.java new file mode 100644 index 0000000..33b93d4 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/exception/LimitException.java @@ -0,0 +1,28 @@ +package com.etl.database.server.exception; + +public class LimitException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + // 默认的构造函数 + public LimitException() { + super(); + } + + // 带错误信息的构造函数 + public LimitException(String message) { + super(message); + } + + // 带错误信息和原因的构造函数 + public LimitException(String message, Throwable cause) { + super(message, cause); + } + + // 带原因的构造函数 + public LimitException(Throwable cause) { + super(cause); + } + + +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/DataSourceMapper.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/DataSourceMapper.java index d0172d7..c17524f 100644 --- a/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/DataSourceMapper.java +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/DataSourceMapper.java @@ -1,9 +1,10 @@ package com.etl.database.server.mapper; -import com.etl.database.common.entity.DataSource; +import com.etl.database.common.entity.DataSources; + import com.github.yulichang.base.MPJBaseMapper; import org.springframework.stereotype.Repository; @Repository -public interface DataSourceMapper extends MPJBaseMapper { +public interface DataSourceMapper extends MPJBaseMapper { } diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/DataSourceTypeMapper.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/DataSourceTypeMapper.java new file mode 100644 index 0000000..829dcf8 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/DataSourceTypeMapper.java @@ -0,0 +1,7 @@ +package com.etl.database.server.mapper; + +import com.etl.database.common.entity.DataSourceType; +import com.github.yulichang.base.MPJBaseMapper; + +public interface DataSourceTypeMapper extends MPJBaseMapper { +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/MysqlMapper.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/MysqlMapper.java new file mode 100644 index 0000000..457eae6 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/MysqlMapper.java @@ -0,0 +1,9 @@ +package com.etl.database.server.mapper; + +import com.etl.database.common.entity.DataSources; +import com.github.yulichang.base.MPJBaseMapper; +import org.springframework.stereotype.Repository; + +@Repository +public interface MysqlMapper extends MPJBaseMapper { +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/RedisMapper.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/RedisMapper.java new file mode 100644 index 0000000..5cbcb63 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/mapper/RedisMapper.java @@ -0,0 +1,10 @@ +package com.etl.database.server.mapper; + +import com.etl.database.common.entity.DataSources; +import com.etl.database.common.entity.DatabaseRedis; +import com.github.yulichang.base.MPJBaseMapper; +import org.springframework.stereotype.Repository; + +@Repository +public interface RedisMapper extends MPJBaseMapper { +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/DataSouceService.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/DataSouceService.java index e21702f..c231e59 100644 --- a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/DataSouceService.java +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/DataSouceService.java @@ -1,7 +1,10 @@ package com.etl.database.server.service; -import com.etl.database.common.entity.DataSource; +import com.etl.common.result.Result; +import com.etl.database.common.entity.DataSources; +import com.etl.database.common.entity.req.DataSourceReq; import com.github.yulichang.base.MPJBaseService; -public interface DataSouceService extends MPJBaseService { +public interface DataSouceService extends MPJBaseService { + Result findDateSource(DataSourceReq dataSourceReq); } diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/DataSourceTypeService.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/DataSourceTypeService.java new file mode 100644 index 0000000..4595bb7 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/DataSourceTypeService.java @@ -0,0 +1,10 @@ +package com.etl.database.server.service; + +import com.etl.common.result.Result; +import com.etl.database.common.entity.DataSourceType; +import com.github.yulichang.base.MPJBaseService; + +public interface DataSourceTypeService extends MPJBaseService { + Result findDataSourceType(); + +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/MysqlService.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/MysqlService.java new file mode 100644 index 0000000..2c4731e --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/MysqlService.java @@ -0,0 +1,21 @@ +package com.etl.database.server.service; + +import com.etl.database.common.entity.DataSources; +import com.etl.database.common.entity.req.DruidReq; +import com.etl.database.common.entity.resp.ColumnInfo; +import com.github.yulichang.base.MPJBaseService; + +import java.util.List; +import java.util.Map; + +public interface MysqlService extends MPJBaseService { + String testConnectMysql(DruidReq druidReq); + + List findDatabaseTable(DruidReq druidReq); + + List findDatabaseTables(DruidReq druidReq); + + List findDatabaseTableField(DruidReq druidReq); + + List> findDatabaseTableFieldPrice(DruidReq druidReq); +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/RedisService.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/RedisService.java new file mode 100644 index 0000000..4473f9c --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/RedisService.java @@ -0,0 +1,11 @@ +package com.etl.database.server.service; + +import com.etl.database.common.entity.DataSources; +import com.etl.database.common.entity.DatabaseRedis; +import com.github.yulichang.base.MPJBaseService; + +import java.util.Map; + +public interface RedisService extends MPJBaseService { + Map testDatabaseRedis(DatabaseRedis databaseRedis); +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/DataSouceServiceImpl.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/DataSouceServiceImpl.java index 7e58e29..64645ac 100644 --- a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/DataSouceServiceImpl.java +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/DataSouceServiceImpl.java @@ -1,16 +1,43 @@ package com.etl.database.server.service.impl; -import com.etl.database.common.entity.DataSource; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.etl.common.result.Result; +import com.etl.database.common.entity.DataSourceType; +import com.etl.database.common.entity.DataSources; + +import com.etl.database.common.entity.req.DataSourceReq; import com.etl.database.server.mapper.DataSourceMapper; import com.etl.database.server.service.DataSouceService; import com.github.yulichang.base.MPJBaseServiceImpl; +import com.github.yulichang.wrapper.MPJLambdaWrapper; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -/** - * 数据源业务层 - */ +import java.util.List; + + @Service @Slf4j -public class DataSouceServiceImpl extends MPJBaseServiceImpl implements DataSouceService { +public class DataSouceServiceImpl extends MPJBaseServiceImpl implements DataSouceService { + @Autowired + private DataSourceMapper dataSourceMapper; + @Override + public Result findDateSource(DataSourceReq dataSourceReq) { + MPJLambdaWrapper dataSourceMPJLambdaWrapper = new MPJLambdaWrapper(); + dataSourceMPJLambdaWrapper.selectAll(DataSources.class) + .select(DataSourceType::getDataSourceTypeName) + .leftJoin(DataSourceType.class, DataSourceType::getId, DataSources::getDatabaseType); + if (dataSourceReq.getDataSourceDescribe()!=null && !dataSourceReq.getDataSourceDescribe().isEmpty()){ + dataSourceMPJLambdaWrapper.like(DataSources::getDatabaseDescription,dataSourceReq.getDataSourceDescribe()); + } + if (dataSourceReq.getDataSourceType()!=null){ + dataSourceMPJLambdaWrapper.eq(DataSources::getDatabaseType,dataSourceReq.getDataSourceType()); + } + Page page = new Page<>(dataSourceReq.getPageNum(),dataSourceReq.getPageSize()); + IPage dataSourceReqIPage = dataSourceMapper.selectJoinPage(page, DataSources.class, dataSourceMPJLambdaWrapper); + List records = dataSourceReqIPage.getRecords(); + return Result.success(records); + } } diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/DataSourceTypeServiceImpl.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/DataSourceTypeServiceImpl.java new file mode 100644 index 0000000..13424dd --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/DataSourceTypeServiceImpl.java @@ -0,0 +1,27 @@ +package com.etl.database.server.service.impl; + +import com.etl.common.result.Result; +import com.etl.database.common.entity.DataSourceType; +import com.etl.database.server.mapper.DataSourceTypeMapper; +import com.etl.database.server.service.DataSourceTypeService; +import com.github.yulichang.base.MPJBaseServiceImpl; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +@Slf4j +public class DataSourceTypeServiceImpl extends MPJBaseServiceImpl implements DataSourceTypeService { + @Autowired + private DataSourceTypeMapper dataSourceTypeBaseMapper; + @Override + public Result findDataSourceType() { + MPJLambdaWrapper dataSourceTypeMPJLambdaWrapper = new MPJLambdaWrapper() + .selectAll(DataSourceType.class); + List dataSourceTypes = dataSourceTypeBaseMapper.selectJoinList(DataSourceType.class, dataSourceTypeMPJLambdaWrapper); + return Result.success(dataSourceTypes); + } +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/MysqlServiceImpl.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/MysqlServiceImpl.java new file mode 100644 index 0000000..31fc94c --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/MysqlServiceImpl.java @@ -0,0 +1,223 @@ +package com.etl.database.server.service.impl; + + +import com.etl.common.util.StringUtils; +import com.etl.database.common.entity.DataSources; +import com.etl.database.common.entity.constants.DataSourceConstants; +import com.etl.database.common.entity.req.DruidReq; +import com.etl.database.common.entity.resp.ColumnInfo; +import com.etl.database.server.mapper.MysqlMapper; +import com.etl.database.server.service.MysqlService; +import com.github.yulichang.base.MPJBaseServiceImpl; +import lombok.extern.slf4j.Slf4j; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.stereotype.Service; + +import javax.sql.DataSource; +import java.sql.*; +import java.util.*; + + +/** + * 数据源业务层 + */ +@Service +@Slf4j +public class MysqlServiceImpl extends MPJBaseServiceImpl implements MysqlService { + /** + * 测试连接mysql + * + * @param druidReq + * @return + */ + @Override + public String testConnectMysql(DruidReq druidReq) { + try { + // 创建数据源 + DataSource dataSource = createDataSource(druidReq); + // 使用JdbcTemplate执行SQL语句 + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + jdbcTemplate.execute("SELECT 1"); + // 如果连接成功,返回"ok" + return "ok"; + } catch (Exception e) { + // 如果发生异常,打印异常信息并返回"error" + e.printStackTrace(); + return "error"; + } + } + + @Override + public List findDatabaseTable(DruidReq druidReq) { + List tableNames = new ArrayList<>(); + try (Connection connection = getConnection(druidReq)) { + try (Statement statement = connection.createStatement()) { + // 查询所有数据库名 + try (ResultSet resultSet = statement.executeQuery("SHOW DATABASES;")) { + while (resultSet.next()) { + tableNames.add(resultSet.getString(1)); + } + } + } + } catch (ClassNotFoundException | SQLException e) { + e.printStackTrace(); + } + return tableNames; + } + + @Override + public List findDatabaseTables(DruidReq druidReq) { + if (StringUtils.isNull(druidReq.getDatabaseName())) { + throw new RuntimeException("请选择数据库"); + } + List tableNames = new ArrayList<>(); + try (Connection connection = getConnection(druidReq)) { + try (Statement statement = connection.createStatement()) { + // 查询指定数据库的所有表名 + try (ResultSet resultSet = statement.executeQuery( + "SHOW TABLES FROM " + druidReq.getDatabaseName() + ";")) { + while (resultSet.next()) { + tableNames.add(resultSet.getString(1)); + } + } + } + } catch (ClassNotFoundException | SQLException e) { + e.printStackTrace(); + } + return tableNames; + } + + @Override + public List findDatabaseTableField(DruidReq druidReq) { + if (StringUtils.isNull(druidReq.getDatabaseName())) { + throw new RuntimeException("请选择数据库"); + } + if (StringUtils.isNull(druidReq.getTableName())) { + throw new RuntimeException("请选择表"); + } + List columnInfos = new ArrayList<>(); + Connection connection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + + try { + // 加载并注册JDBC驱动 + Class.forName(druidReq.getDriverClassName()); + + // 建立数据库连接 + connection = DriverManager.getConnection(druidReq.getUrl(), druidReq.getUsername(), druidReq.getPassword()); + + // 创建PreparedStatement以避免SQL注入 + String sql = "SELECT " + + "COLUMN_NAME, " + + "DATA_TYPE, " + + "CHARACTER_MAXIMUM_LENGTH, " + + "COLUMN_KEY, " + + "IS_NULLABLE, " + + "COLUMN_COMMENT " + + "FROM INFORMATION_SCHEMA.COLUMNS " + + "WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?"; + + preparedStatement = connection.prepareStatement(sql); + preparedStatement.setString(1, druidReq.getDatabaseName()); + preparedStatement.setString(2, druidReq.getTableName()); + + // 执行查询 + resultSet = preparedStatement.executeQuery(); + + // 遍历结果集 + while (resultSet.next()) { + ColumnInfo columnInfo = new ColumnInfo(); + columnInfo.setColumnName(resultSet.getString("COLUMN_NAME")); + columnInfo.setDataType(resultSet.getString("DATA_TYPE")); + columnInfo.setCharacterMaximumLength(resultSet.getInt("CHARACTER_MAXIMUM_LENGTH")); // 注意可能为null + columnInfo.setColumnKey(resultSet.getString("COLUMN_KEY")); + columnInfo.setIsNullable(resultSet.getString("IS_NULLABLE")); + columnInfo.setColumnComment(resultSet.getString("COLUMN_COMMENT")); + + columnInfos.add(columnInfo); + } + + } catch (ClassNotFoundException | SQLException e) { + // 处理异常 + e.printStackTrace(); + } finally { + // 关闭资源 + try { + if (resultSet != null) resultSet.close(); + if (preparedStatement != null) preparedStatement.close(); + if (connection != null) connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + return columnInfos; + } + + @Override + public List> findDatabaseTableFieldPrice(DruidReq druidReq) { + List> results = new ArrayList<>(); + Connection connection = null; + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + + try { + connection = getConnection(druidReq); + // 这里我们仅作为示例直接构建SQL查询语句,实际中可能需要更复杂的逻辑来构建SQL + String sql = "SELECT * FROM " + druidReq.getDatabaseName() + "." + druidReq.getTableName(); + preparedStatement = connection.prepareStatement(sql); + resultSet = preparedStatement.executeQuery(); + + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + + while (resultSet.next()) { + Map rowData = new HashMap<>(); + for (int i = 1; i <= columnCount; i++) { + String columnName = metaData.getColumnName(i); + Object columnValue = resultSet.getObject(i); + rowData.put(columnName, columnValue); + } + results.add(rowData); + } + } catch (SQLException e) { + // 处理异常 + e.printStackTrace(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } finally { + // 关闭资源 + try { + if (resultSet != null) resultSet.close(); + if (preparedStatement != null) preparedStatement.close(); + if (connection != null) connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return results; + } + + private DataSource createDataSource(DruidReq druidReq) { + DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); + driverManagerDataSource.setDriverClassName(druidReq.getDriverClassName()); + driverManagerDataSource.setUrl(druidReq.getUrl()); + driverManagerDataSource.setUsername(druidReq.getUsername()); + driverManagerDataSource.setPassword(druidReq.getPassword()); + return driverManagerDataSource; + } + + private static Connection getConnection(DruidReq config) throws ClassNotFoundException, SQLException { + // 加载MySQL驱动 + Class.forName(config.getDriverClassName()); + // 设置数据库连接信息 + String url = config.getUrl(); + Properties properties = new Properties(); + properties.setProperty(DataSourceConstants.MYSQL_USER, config.getUsername()); + properties.setProperty(DataSourceConstants.MYSQL_PASSWORD, config.getPassword()); + // 获取数据库连接 + return DriverManager.getConnection(url, properties); + } +} diff --git a/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/RedisServiceImpl.java b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/RedisServiceImpl.java new file mode 100644 index 0000000..5719181 --- /dev/null +++ b/etl-database/etl-database-server/src/main/java/com/etl/database/server/service/impl/RedisServiceImpl.java @@ -0,0 +1,69 @@ +package com.etl.database.server.service.impl; + + +import com.etl.database.common.entity.DatabaseRedis; +import com.etl.database.server.config.RedisConfig; +import com.etl.database.server.mapper.RedisMapper; +import com.etl.database.server.service.RedisService; +import com.github.yulichang.base.MPJBaseServiceImpl; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.ScanParams; +import redis.clients.jedis.ScanResult; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +@Slf4j +public class RedisServiceImpl extends MPJBaseServiceImpl implements RedisService { + @Override + public Map testDatabaseRedis(DatabaseRedis databaseRedis) { + // 创建一个空的HashMap来存储Redis中的所有键值对 + Map allData = new HashMap<>(); + + // 从Redis配置中获取Jedis连接池 + JedisPool jedisPool = RedisConfig.getJedisPool(databaseRedis); + + // 从连接池中获取一个Jedis资源 + Jedis resource = jedisPool.getResource(); + + // 创建一个ScanParams对象,设置每次迭代返回的键的数量(这里设置为100) + ScanParams scanParams = new ScanParams().count(100); + + // 初始化游标为0,表示开始迭代 + String cursor = "0"; + + // 使用do-while循环来迭代Redis中的所有键 + do { + // 使用scan方法和ScanParams对象来获取一批键和新的游标 + ScanResult scanResult = resource.scan(cursor, scanParams); + + // 更新游标以便下一次迭代 + cursor = scanResult.getCursor(); + + // 获取当前迭代返回的键列表 + List keys = scanResult.getResult(); + + // 遍历键列表,并获取每个键对应的值 + for (String key : keys) { + // 假设所有键对应的值都是字符串类型 + String value = resource.get(key); + + // 如果值不为null,则将其添加到Map中 + if (value != null) { + // 将键值对添加到Map中 + allData.put(key, value); + } + } + + // 当游标为"0"时,表示迭代完成 + } while (!cursor.equals("0")); + + // 返回包含所有键值对的Map + return allData; + } +} diff --git a/etl-database/etl-database-server/src/main/resources/mapper/MysqlMapper.xml b/etl-database/etl-database-server/src/main/resources/mapper/MysqlMapper.xml new file mode 100644 index 0000000..ca64baf --- /dev/null +++ b/etl-database/etl-database-server/src/main/resources/mapper/MysqlMapper.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/etl-database/etl-database-server/src/main/resources/mapper/RedisMapper.xml b/etl-database/etl-database-server/src/main/resources/mapper/RedisMapper.xml new file mode 100644 index 0000000..e318fc5 --- /dev/null +++ b/etl-database/etl-database-server/src/main/resources/mapper/RedisMapper.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/aop/LimitAspect.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/aop/LimitAspect.class new file mode 100644 index 0000000..ca37b53 Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/aop/LimitAspect.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/config/RedisConfig.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/config/RedisConfig.class new file mode 100644 index 0000000..26bccf2 Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/config/RedisConfig.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/DataSourceController.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/DataSourceController.class new file mode 100644 index 0000000..1567bcb Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/DataSourceController.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/MysqlController.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/MysqlController.class new file mode 100644 index 0000000..828679b Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/MysqlController.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/RedisController.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/RedisController.class new file mode 100644 index 0000000..cf3179c Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/controller/RedisController.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/exception/LimitException.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/exception/LimitException.class new file mode 100644 index 0000000..9a75d40 Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/exception/LimitException.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/DataSourceMapper.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/DataSourceMapper.class index b467879..7ba05d6 100644 Binary files a/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/DataSourceMapper.class and b/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/DataSourceMapper.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/DataSourceTypeMapper.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/DataSourceTypeMapper.class new file mode 100644 index 0000000..1ba7b49 Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/DataSourceTypeMapper.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/MysqlMapper.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/MysqlMapper.class new file mode 100644 index 0000000..f1778db Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/MysqlMapper.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/RedisMapper.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/RedisMapper.class new file mode 100644 index 0000000..305444e Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/mapper/RedisMapper.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/service/DataSouceService.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/DataSouceService.class index 92fb1eb..4248cbc 100644 Binary files a/etl-database/etl-database-server/target/classes/com/etl/database/server/service/DataSouceService.class and b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/DataSouceService.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/service/MysqlService.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/MysqlService.class new file mode 100644 index 0000000..8288a0e Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/MysqlService.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/service/RedisService.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/RedisService.class new file mode 100644 index 0000000..8fd82de Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/RedisService.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/DataSouceServiceImpl.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/DataSouceServiceImpl.class index 49920ae..e257a93 100644 Binary files a/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/DataSouceServiceImpl.class and b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/DataSouceServiceImpl.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/MysqlServiceImpl.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/MysqlServiceImpl.class new file mode 100644 index 0000000..56216d8 Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/MysqlServiceImpl.class differ diff --git a/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/RedisServiceImpl.class b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/RedisServiceImpl.class new file mode 100644 index 0000000..7a0425a Binary files /dev/null and b/etl-database/etl-database-server/target/classes/com/etl/database/server/service/impl/RedisServiceImpl.class differ diff --git a/etl-database/etl-database-server/target/classes/mapper/MysqlMapper.xml b/etl-database/etl-database-server/target/classes/mapper/MysqlMapper.xml new file mode 100644 index 0000000..ca64baf --- /dev/null +++ b/etl-database/etl-database-server/target/classes/mapper/MysqlMapper.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/etl-database/etl-database-server/target/classes/mapper/RedisMapper.xml b/etl-database/etl-database-server/target/classes/mapper/RedisMapper.xml new file mode 100644 index 0000000..e318fc5 --- /dev/null +++ b/etl-database/etl-database-server/target/classes/mapper/RedisMapper.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/etl-database/etl-database-server/target/etl-database-server-1.0-SNAPSHOT.jar b/etl-database/etl-database-server/target/etl-database-server-1.0-SNAPSHOT.jar index a259ffe..dc72eb2 100644 Binary files a/etl-database/etl-database-server/target/etl-database-server-1.0-SNAPSHOT.jar and b/etl-database/etl-database-server/target/etl-database-server-1.0-SNAPSHOT.jar differ diff --git a/etl-database/etl-database-server/target/maven-archiver/pom.properties b/etl-database/etl-database-server/target/maven-archiver/pom.properties index f52d52e..586ea8e 100644 --- a/etl-database/etl-database-server/target/maven-archiver/pom.properties +++ b/etl-database/etl-database-server/target/maven-archiver/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven -#Tue Jun 25 19:44:08 CST 2024 +#Wed Jun 26 17:13:10 CST 2024 version=1.0-SNAPSHOT groupId=com.bwie artifactId=etl-database-server diff --git a/etl-database/etl-database-server/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/etl-database/etl-database-server/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst index 1d47c76..e69de29 100644 --- a/etl-database/etl-database-server/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ b/etl-database/etl-database-server/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -1 +0,0 @@ -com\etl\database\server\EtlDatabaseServerApplication.class diff --git a/etl-database/etl-database-server/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/etl-database/etl-database-server/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst index 082654b..f75bd28 100644 --- a/etl-database/etl-database-server/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ b/etl-database/etl-database-server/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -1 +1,17 @@ +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\config\RedisConfig.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\aop\LimitAspect.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\service\RedisService.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\service\impl\DataSouceServiceImpl.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\controller\DataSourceController.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\exception\GlobalExceptionHandler.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\mapper\DataSourceMapper.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\service\DataSouceService.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\mapper\MysqlMapper.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\mapper\RedisMapper.java D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\EtlDatabaseServerApplication.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\service\impl\MysqlServiceImpl.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\service\impl\RedisServiceImpl.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\controller\RedisController.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\exception\LimitException.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\controller\MysqlController.java +D:\workspace\ETL\etl-database\etl-database-server\src\main\java\com\etl\database\server\service\MysqlService.java diff --git a/etl-gateway/src/main/java/com/etl/gateway/filters/AuthFilter.java b/etl-gateway/src/main/java/com/etl/gateway/filters/AuthFilter.java index 52441d0..19df148 100644 --- a/etl-gateway/src/main/java/com/etl/gateway/filters/AuthFilter.java +++ b/etl-gateway/src/main/java/com/etl/gateway/filters/AuthFilter.java @@ -1,95 +1,95 @@ package com.etl.gateway.filters;//package com.health.cloud.gateway.filters; -// -// -//import com.health.cloud.common.constants.TokenConstants; -//import com.health.cloud.common.util.JwtUtils; -//import com.health.cloud.common.util.StringUtils; -//import com.health.cloud.gateway.config.IgnoreWhiteConfig; -//import com.health.cloud.gateway.utils.GatewayUtils; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.cloud.gateway.filter.GatewayFilterChain; -//import org.springframework.cloud.gateway.filter.GlobalFilter; -//import org.springframework.core.Ordered; -//import org.springframework.data.redis.core.RedisTemplate; -//import org.springframework.http.server.reactive.ServerHttpRequest; -//import org.springframework.stereotype.Component; -//import org.springframework.web.server.ServerWebExchange; -//import reactor.core.publisher.Mono; -// -//import java.util.List; -//import java.util.concurrent.TimeUnit; -// -///** -// * @ClassName: -// * @Description: 过滤请求,验证 token -// * @Author: dongyan Ma -// * @Date: 2024/2/28 -// */ -//@Component -//public class AuthFilter implements GlobalFilter, Ordered { -// -// @Autowired -// private IgnoreWhiteConfig ignoreWhitesConfig; -// -// @Autowired -// private RedisTemplate redisTemplate; -// -// /** -// * 过滤请求方法 -// * @param exchange 请求的上下文, 通过这个对象可以获取请求对象以及响应对象 -// * @param chain 过滤器链 使用他进行 过滤 或者 放行 -// * @return -// */ -// @Override -// public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { -// // 判断这次请求是否需要验证 token 白名单【直接放行的请求】 -// // 获取系统白名单 -// List whites = ignoreWhitesConfig.getWhites(); -// // 获取当前请求的 URI -// ServerHttpRequest request = exchange.getRequest(); -// String path = request.getURI().getPath(); -// boolean matches = StringUtils.matches(path, whites); -// if (matches) { -// // 白名单请求 + + +import com.etl.common.constants.TokenConstants; +import com.etl.common.util.JwtUtils; +import com.etl.common.util.StringUtils; +import com.etl.gateway.config.IgnoreWhiteConfig; +import com.etl.gateway.utils.GatewayUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.stereotype.Component; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * @ClassName: + * @Description: 过滤请求,验证 token + * @Author: dongyan Ma + * @Date: 2024/2/28 + */ +@Component +public class AuthFilter implements GlobalFilter, Ordered { + + @Autowired + private IgnoreWhiteConfig ignoreWhitesConfig; + + @Autowired + private RedisTemplate redisTemplate; + + /** + * 过滤请求方法 + * @param exchange 请求的上下文, 通过这个对象可以获取请求对象以及响应对象 + * @param chain 过滤器链 使用他进行 过滤 或者 放行 + * @return + */ + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + // 判断这次请求是否需要验证 token 白名单【直接放行的请求】 + // 获取系统白名单 + List whites = ignoreWhitesConfig.getWhites(); + // 获取当前请求的 URI + ServerHttpRequest request = exchange.getRequest(); + String path = request.getURI().getPath(); + boolean matches = StringUtils.matches(path, whites); + if (matches) { + // 白名单请求 + return chain.filter(exchange); + } +// boolean anyMatch = whites.stream().anyMatch(white -> white.equals(path)); +// if (anyMatch) { + // 白名单请求 // return chain.filter(exchange); // } -//// boolean anyMatch = whites.stream().anyMatch(white -> white.equals(path)); -//// if (anyMatch) { -// // 白名单请求 -//// return chain.filter(exchange); -//// } -// // 验证 token -// String token = request.getHeaders().getFirst(TokenConstants.TOKEN); -// // 非空验证 -// if (StringUtils.isBlank(token)) { -// // 空的 -// return GatewayUtils.errorResponse(exchange, "token不能为空!"); -// } -// // 是否合法 -// try { -// JwtUtils.parseToken(token); -// } catch (Exception e) { -// return GatewayUtils.errorResponse(exchange, "token不合法!"); -// } -// // 是否有效 -// String userKey = JwtUtils.getUserKey(token); -// if (!redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey)) { -// return GatewayUtils.errorResponse(exchange, "token过期!"); -// } else { -// // 重新设置 redis中的用户有效时间 -// redisTemplate.expire(TokenConstants.LOGIN_TOKEN_KEY + userKey, 30, TimeUnit.MINUTES); -// } -// // 放行 -// return chain.filter(exchange); -// } -// -// /** -// * 规定过滤器执行的顺序 -// * @return 方法的返回值越小 执行顺序越高 -// */ -// @Override -// public int getOrder() { -// return 0; -// } -// -//} + // 验证 token + String token = request.getHeaders().getFirst(TokenConstants.TOKEN); + // 非空验证 + if (StringUtils.isBlank(token)) { + // 空的 + return GatewayUtils.errorResponse(exchange, "token不能为空!"); + } + // 是否合法 + try { + JwtUtils.parseToken(token); + } catch (Exception e) { + return GatewayUtils.errorResponse(exchange, "token不合法!"); + } + // 是否有效 + String userKey = JwtUtils.getUserKey(token); + if (!redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey)) { + return GatewayUtils.errorResponse(exchange, "token过期!"); + } else { + // 重新设置 redis中的用户有效时间 + redisTemplate.expire(TokenConstants.LOGIN_TOKEN_KEY + userKey, 30, TimeUnit.MINUTES); + } + // 放行 + return chain.filter(exchange); + } + + /** + * 规定过滤器执行的顺序 + * @return 方法的返回值越小 执行顺序越高 + */ + @Override + public int getOrder() { + return 0; + } + +} diff --git a/etl-gateway/src/main/resources/application-routers.yml b/etl-gateway/src/main/resources/application-routers.yml deleted file mode 100644 index 79bd339..0000000 --- a/etl-gateway/src/main/resources/application-routers.yml +++ /dev/null @@ -1,10 +0,0 @@ -spring: - cloud: - gateway: - routes: - - id: route_auth # 认证微服务路由规则 - uri: lb://cloud-auth-server # 负载均衡,将请求转发到注册中心注册的 auth 服务进行认证 - predicates: # 断言 - - Path=/api/auth/** # 如果前端请求路径包含 api/auth,则应用这条路由规则 - filters: #过滤器 - - RewritePath=/api/(?.*),/$\{segment} # 将跳转路径中包含的api替换成空 diff --git a/etl-gateway/src/main/resources/bootstrap.yml b/etl-gateway/src/main/resources/bootstrap.yml index e60b097..d13bfdd 100644 --- a/etl-gateway/src/main/resources/bootstrap.yml +++ b/etl-gateway/src/main/resources/bootstrap.yml @@ -1,5 +1,5 @@ server: - port: 8080 + port: 18080 spring: application: name: engine-gateway