数据源正常展示,permission控制层待测试

lzm
廖忠美 2024-06-26 20:22:50 +08:00
parent 6a8a0af1cc
commit fab9f96f28
76 changed files with 1226 additions and 285 deletions

File diff suppressed because one or more lines are too long

View File

@ -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);
}
}

View File

@ -54,6 +54,12 @@
<version>3.5.4.1</version> <version>3.5.4.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.6</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>

View File

@ -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 "系统繁忙,请稍后再试";
}

View File

@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
@ -15,11 +17,14 @@ import javax.validation.constraints.NotBlank;
@ApiModel(description = "数据源类型") @ApiModel(description = "数据源类型")
@TableName("t_data_source_type") @TableName("t_data_source_type")
@Data @Data
@NoArgsConstructor
@AllArgsConstructor
public class DataSourceType{ public class DataSourceType{
@TableId(value = "id",type = IdType.AUTO) @TableId(value = "id",type = IdType.AUTO)
@ApiModelProperty(value = "数据源类型id") @ApiModelProperty(value = "数据源类型id")
private Integer id; private Integer id;
@NotBlank(message = "数据源类型名称不能为空") @NotBlank(message = "数据源类型名称不能为空")
@ApiModelProperty(value = "数据源类型名称") @ApiModelProperty(value = "数据源类型名称")
private String dataSourceTypeName; private String dataSourceTypeName;

View File

@ -1,9 +1,10 @@
package com.etl.database.common.entity; package com.etl.database.common.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; 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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@ -16,12 +17,11 @@ import javax.validation.constraints.*;
@ApiModel("数据源配置") @ApiModel("数据源配置")
@Data @Data
@TableName("t_data_source_etl") @TableName("t_data_source_etl")
public class DataSource extends BaseMate { public class DataSources extends BaseMate {
@TableId(value = "id",type = IdType.AUTO) @TableId(value = "id",type = IdType.AUTO)
@ApiModelProperty(value = "数据源id") @ApiModelProperty(value = "数据源id")
private Integer id; private Integer id;
@NotBlank(message = "数据源描述不能为空")
@ApiModelProperty(value = "数据源描述") @ApiModelProperty(value = "数据源描述")
private String databaseDescription; private String databaseDescription;
@ -33,11 +33,9 @@ public class DataSource extends BaseMate {
@ApiModelProperty(value = "端口") @ApiModelProperty(value = "端口")
private String port; private String port;
@NotBlank(message = "数据库名称不能为空")
@ApiModelProperty(value = "数据库名称") @ApiModelProperty(value = "数据库名称")
private String databaseName; private String databaseName;
@ApiModelProperty(value = "状态 0-停用 1-启用") @ApiModelProperty(value = "状态 0-停用 1-启用")
@Min(value = 0, message = "状态值不能小于0") @Min(value = 0, message = "状态值不能小于0")
@Max(value = 1, message = "状态值不能大于1") @Max(value = 1, message = "状态值不能大于1")
@ -50,7 +48,10 @@ public class DataSource extends BaseMate {
@ApiModelProperty(value = "额外配置") @ApiModelProperty(value = "额外配置")
private String extraEvlop; private String extraEvlop;
@NotNull(message = "数据库类型id不能为空")
@ApiModelProperty(value ="数据库类型id") @ApiModelProperty(value ="数据库类型id")
private Integer databaseType; private Integer databaseType;
@TableField(exist = false)
@ApiModelProperty(value = "数据库类型")
private String dataSourceTypeName;
} }

View File

@ -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;
}

View File

@ -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";
}

View File

@ -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.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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; // 列注释
}

View File

@ -0,0 +1,33 @@
package com.etl.database.ennum;
public enum LimitType {
IP,
/**
*
*/
REJECT,
/**
*
*/
QUEUE,
/**
*
*
*/
DEGRADE,
/**
*
*/
CUSTOM,
// 这里可以添加更多的处理方式...
/**
*
*/
DEFAULT,
CUSTOMER,
}

View File

@ -1,5 +1,5 @@
#Generated by Maven #Generated by Maven
#Tue Jun 25 19:44:07 CST 2024 #Wed Jun 26 17:13:09 CST 2024
version=1.0-SNAPSHOT version=1.0-SNAPSHOT
groupId=com.bwie groupId=com.bwie
artifactId=etl-database-common artifactId=etl-database-common

View File

@ -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

View File

@ -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

View File

@ -16,14 +16,13 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.bwie</groupId> <groupId>io.springfox</groupId>
<artifactId>etl-common</artifactId> <artifactId>springfox-swagger2</artifactId>
<version>1.0-SNAPSHOT</version> <version>3.0.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.bwie</groupId> <groupId>com.baomidou</groupId>
<artifactId>etl-database-common</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.github.yulichang</groupId> <groupId>com.github.yulichang</groupId>
@ -31,8 +30,46 @@
<version>1.3.8</version> <version>1.3.8</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.bwie</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>etl-database-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.bwie</groupId>
<artifactId>etl-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -41,11 +78,7 @@
<dependency> <dependency>
<groupId>com.mysql</groupId> <groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId> <artifactId>mysql-connector-j</artifactId>
</dependency> <scope>runtime</scope>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.7</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -57,6 +90,15 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.yulichang</groupId>
<artifactId>mybatis-plus-join-boot-starter</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>

View File

@ -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<String, RateLimiter> 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();
}
}

View File

@ -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;
}
}

View File

@ -1,34 +1,31 @@
package com.etl.database.server.controller; package com.etl.database.server.controller;
import com.etl.common.result.Result; 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 com.etl.database.server.service.DataSouceService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; 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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List;
/** /**
* *
*/ */
@RestController @RestController
@RequestMapping(value ="/datasource") @RequestMapping(value ="/datasource")
@Api(tags = "数据源-API") @Api(tags = "数据源-datasource")
public class DataSouceController { public class DataSourceController {
@Autowired @Autowired
private DataSouceService dataSouceService; private DataSouceService dataSouceService;
@ApiOperation(value = "测试连接mysql") @PostMapping("/findDataSource")
@RequestMapping(value = "/testConnectMysql",method = RequestMethod.POST) @ApiOperation(value = "查询数据源")
public Result testConnectMysql(@RequestBody @Valid DataSource dataSource){ public Result findDateSource(@RequestBody @Valid DataSourceReq dataSourceReq) {
List<DataSource> list = dataSouceService.list(); return dataSouceService.findDateSource(dataSourceReq);
return null;
} }
} }

View File

@ -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();
}
}

View File

@ -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<List<String>> findDatabaseTable(@Valid @RequestBody DruidReq druidReq){
return Result.success(dataSouceService.findDatabaseTable(druidReq));
}
@PostMapping("/findDatabaseTableName")
@ApiOperation("查询指定数据库的所有表名")
public Result<List<String>> findDatabaseTableName(@Valid @RequestBody DruidReq druidReq){
return Result.success(dataSouceService.findDatabaseTables(druidReq));
}
@PostMapping("/findDatabaseTableField")
@ApiOperation("查询指定数据库的指定表的所有字段")
public Result<List<ColumnInfo>> findDatabaseTableField(@Valid @RequestBody DruidReq druidReq){
return Result.success(dataSouceService.findDatabaseTableField(druidReq));
}
@PostMapping("/findDatabaseTableFieldPrice")
@ApiOperation("复制数据源数据")
public Result<List<Map<String, Object>> > findDatabaseTableFieldPrice(@Valid @RequestBody DruidReq druidReq){
return Result.success(dataSouceService.findDatabaseTableFieldPrice(druidReq));
}
}

View File

@ -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<Map<String,String>> testDatabaseRedis(@Valid @RequestBody DatabaseRedis databaseRedis) {
Map<String,String> map = redisService.testDatabaseRedis(databaseRedis);
return Result.success(map);
}
}

View File

@ -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);
}
}

View File

@ -1,9 +1,10 @@
package com.etl.database.server.mapper; 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 com.github.yulichang.base.MPJBaseMapper;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
public interface DataSourceMapper extends MPJBaseMapper<DataSource> { public interface DataSourceMapper extends MPJBaseMapper<DataSources> {
} }

View File

@ -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<DataSourceType> {
}

View File

@ -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<DataSources> {
}

View File

@ -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<DatabaseRedis> {
}

View File

@ -1,7 +1,10 @@
package com.etl.database.server.service; 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; import com.github.yulichang.base.MPJBaseService;
public interface DataSouceService extends MPJBaseService<DataSource> { public interface DataSouceService extends MPJBaseService<DataSources> {
Result findDateSource(DataSourceReq dataSourceReq);
} }

View File

@ -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<DataSourceType> {
Result findDataSourceType();
}

View File

@ -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<DataSources> {
String testConnectMysql(DruidReq druidReq);
List<String> findDatabaseTable(DruidReq druidReq);
List<String> findDatabaseTables(DruidReq druidReq);
List<ColumnInfo> findDatabaseTableField(DruidReq druidReq);
List<Map<String, Object>> findDatabaseTableFieldPrice(DruidReq druidReq);
}

View File

@ -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<DatabaseRedis> {
Map<String, String> testDatabaseRedis(DatabaseRedis databaseRedis);
}

View File

@ -1,16 +1,43 @@
package com.etl.database.server.service.impl; 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.mapper.DataSourceMapper;
import com.etl.database.server.service.DataSouceService; import com.etl.database.server.service.DataSouceService;
import com.github.yulichang.base.MPJBaseServiceImpl; import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** import java.util.List;
*
*/
@Service @Service
@Slf4j @Slf4j
public class DataSouceServiceImpl extends MPJBaseServiceImpl<DataSourceMapper, DataSource> implements DataSouceService { public class DataSouceServiceImpl extends MPJBaseServiceImpl<DataSourceMapper, DataSources> implements DataSouceService {
@Autowired
private DataSourceMapper dataSourceMapper;
@Override
public Result findDateSource(DataSourceReq dataSourceReq) {
MPJLambdaWrapper<DataSources> dataSourceMPJLambdaWrapper = new MPJLambdaWrapper<DataSources>();
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<DataSources> page = new Page<>(dataSourceReq.getPageNum(),dataSourceReq.getPageSize());
IPage<DataSources> dataSourceReqIPage = dataSourceMapper.selectJoinPage(page, DataSources.class, dataSourceMPJLambdaWrapper);
List<DataSources> records = dataSourceReqIPage.getRecords();
return Result.success(records);
}
} }

View File

@ -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<DataSourceTypeMapper, DataSourceType> implements DataSourceTypeService {
@Autowired
private DataSourceTypeMapper dataSourceTypeBaseMapper;
@Override
public Result findDataSourceType() {
MPJLambdaWrapper<DataSourceType> dataSourceTypeMPJLambdaWrapper = new MPJLambdaWrapper<DataSourceType>()
.selectAll(DataSourceType.class);
List<DataSourceType> dataSourceTypes = dataSourceTypeBaseMapper.selectJoinList(DataSourceType.class, dataSourceTypeMPJLambdaWrapper);
return Result.success(dataSourceTypes);
}
}

View File

@ -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<MysqlMapper, DataSources> 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<String> findDatabaseTable(DruidReq druidReq) {
List<String> 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<String> findDatabaseTables(DruidReq druidReq) {
if (StringUtils.isNull(druidReq.getDatabaseName())) {
throw new RuntimeException("请选择数据库");
}
List<String> 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<ColumnInfo> findDatabaseTableField(DruidReq druidReq) {
if (StringUtils.isNull(druidReq.getDatabaseName())) {
throw new RuntimeException("请选择数据库");
}
if (StringUtils.isNull(druidReq.getTableName())) {
throw new RuntimeException("请选择表");
}
List<ColumnInfo> 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<Map<String, Object>> findDatabaseTableFieldPrice(DruidReq druidReq) {
List<Map<String, Object>> 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<String, Object> 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);
}
}

View File

@ -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<RedisMapper, DatabaseRedis> implements RedisService {
@Override
public Map<String, String> testDatabaseRedis(DatabaseRedis databaseRedis) {
// 创建一个空的HashMap来存储Redis中的所有键值对
Map<String, String> 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<String> scanResult = resource.scan(cursor, scanParams);
// 更新游标以便下一次迭代
cursor = scanResult.getCursor();
// 获取当前迭代返回的键列表
List<String> 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;
}
}

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.etl.database.server.mapper.MysqlMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.etl.database.server.mapper.RedisMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.etl.database.server.mapper.MysqlMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.etl.database.server.mapper.RedisMapper">
</mapper>

View File

@ -1,5 +1,5 @@
#Generated by Maven #Generated by Maven
#Tue Jun 25 19:44:08 CST 2024 #Wed Jun 26 17:13:10 CST 2024
version=1.0-SNAPSHOT version=1.0-SNAPSHOT
groupId=com.bwie groupId=com.bwie
artifactId=etl-database-server artifactId=etl-database-server

View File

@ -1 +0,0 @@
com\etl\database\server\EtlDatabaseServerApplication.class

View File

@ -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\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

View File

@ -1,95 +1,95 @@
package com.etl.gateway.filters;//package com.health.cloud.gateway.filters; package com.etl.gateway.filters;//package com.health.cloud.gateway.filters;
//
//
//import com.health.cloud.common.constants.TokenConstants; import com.etl.common.constants.TokenConstants;
//import com.health.cloud.common.util.JwtUtils; import com.etl.common.util.JwtUtils;
//import com.health.cloud.common.util.StringUtils; import com.etl.common.util.StringUtils;
//import com.health.cloud.gateway.config.IgnoreWhiteConfig; import com.etl.gateway.config.IgnoreWhiteConfig;
//import com.health.cloud.gateway.utils.GatewayUtils; import com.etl.gateway.utils.GatewayUtils;
//import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered; import org.springframework.core.Ordered;
//import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
//
//import java.util.List; import java.util.List;
//import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
//
///** /**
// * @ClassName: * @ClassName:
// * @Description: 过滤请求,验证 token * @Description: token
// * @Author: dongyan Ma * @Author: dongyan Ma
// * @Date: 2024/2/28 * @Date: 2024/2/28
// */ */
//@Component @Component
//public class AuthFilter implements GlobalFilter, Ordered { public class AuthFilter implements GlobalFilter, Ordered {
//
// @Autowired @Autowired
// private IgnoreWhiteConfig ignoreWhitesConfig; private IgnoreWhiteConfig ignoreWhitesConfig;
//
// @Autowired @Autowired
// private RedisTemplate<String,String> redisTemplate; private RedisTemplate<String,String> redisTemplate;
//
// /** /**
// * 过滤请求方法 *
// * @param exchange 请求的上下文, 通过这个对象可以获取请求对象以及响应对象 * @param exchange
// * @param chain 过滤器链 使用他进行 过滤 或者 放行 * @param chain 使
// * @return * @return
// */ */
// @Override @Override
// public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
// // 判断这次请求是否需要验证 token 白名单【直接放行的请求】 // 判断这次请求是否需要验证 token 白名单【直接放行的请求】
// // 获取系统白名单 // 获取系统白名单
// List<String> whites = ignoreWhitesConfig.getWhites(); List<String> whites = ignoreWhitesConfig.getWhites();
// // 获取当前请求的 URI // 获取当前请求的 URI
// ServerHttpRequest request = exchange.getRequest(); ServerHttpRequest request = exchange.getRequest();
// String path = request.getURI().getPath(); String path = request.getURI().getPath();
// boolean matches = StringUtils.matches(path, whites); boolean matches = StringUtils.matches(path, whites);
// if (matches) { if (matches) {
// // 白名单请求 // 白名单请求
return chain.filter(exchange);
}
// boolean anyMatch = whites.stream().anyMatch(white -> white.equals(path));
// if (anyMatch) {
// 白名单请求
// return chain.filter(exchange); // return chain.filter(exchange);
// } // }
//// boolean anyMatch = whites.stream().anyMatch(white -> white.equals(path)); // 验证 token
//// if (anyMatch) { String token = request.getHeaders().getFirst(TokenConstants.TOKEN);
// // 白名单请求 // 非空验证
//// return chain.filter(exchange); if (StringUtils.isBlank(token)) {
//// } // 空的
// // 验证 token return GatewayUtils.errorResponse(exchange, "token不能为空");
// String token = request.getHeaders().getFirst(TokenConstants.TOKEN); }
// // 非空验证 // 是否合法
// if (StringUtils.isBlank(token)) { try {
// // 空的 JwtUtils.parseToken(token);
// return GatewayUtils.errorResponse(exchange, "token不能为空"); } catch (Exception e) {
// } return GatewayUtils.errorResponse(exchange, "token不合法");
// // 是否合法 }
// try { // 是否有效
// JwtUtils.parseToken(token); String userKey = JwtUtils.getUserKey(token);
// } catch (Exception e) { if (!redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey)) {
// return GatewayUtils.errorResponse(exchange, "token不合法"); return GatewayUtils.errorResponse(exchange, "token过期");
// } } else {
// // 是否有效 // 重新设置 redis中的用户有效时间
// String userKey = JwtUtils.getUserKey(token); redisTemplate.expire(TokenConstants.LOGIN_TOKEN_KEY + userKey, 30, TimeUnit.MINUTES);
// if (!redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey)) { }
// return GatewayUtils.errorResponse(exchange, "token过期"); // 放行
// } else { return chain.filter(exchange);
// // 重新设置 redis中的用户有效时间 }
// redisTemplate.expire(TokenConstants.LOGIN_TOKEN_KEY + userKey, 30, TimeUnit.MINUTES);
// } /**
// // 放行 *
// return chain.filter(exchange); * @return
// } */
// @Override
// /** public int getOrder() {
// * 规定过滤器执行的顺序 return 0;
// * @return 方法的返回值越小 执行顺序越高 }
// */
// @Override }
// public int getOrder() {
// return 0;
// }
//
//}

View File

@ -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>.*),/$\{segment} # 将跳转路径中包含的api替换成空

View File

@ -1,5 +1,5 @@
server: server:
port: 8080 port: 18080
spring: spring:
application: application:
name: engine-gateway name: engine-gateway