|
|
@@ -11,7 +11,6 @@ import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
|
|
|
import org.springframework.cloud.gateway.route.RouteDefinition;
|
|
|
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
|
|
|
import org.springframework.cloud.gateway.support.NotFoundException;
|
|
|
-import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
@@ -26,22 +25,15 @@ import java.util.Map;
|
|
|
public class JdbcRouteDefinitionRepository implements RouteDefinitionRepository {
|
|
|
|
|
|
@Autowired
|
|
|
- @Lazy
|
|
|
private GatewayRoutesService gatewayRoutesService;
|
|
|
|
|
|
@Override
|
|
|
public Flux<RouteDefinition> getRouteDefinitions() {
|
|
|
try {
|
|
|
- log.info("开始从数据库加载路由...");
|
|
|
QueryWrapper<GatewayRoutes> queryWrapper = new QueryWrapper<>();
|
|
|
queryWrapper.eq("status", "0");
|
|
|
List<GatewayRoutes> gatewayRoutesList = gatewayRoutesService.list(queryWrapper);
|
|
|
- log.info("从数据库加载到 {} 条路由", gatewayRoutesList.size());
|
|
|
-
|
|
|
- for (GatewayRoutes routes : gatewayRoutesList) {
|
|
|
- log.info("路由详情:ID={}, URI={}, Predicates={}, Filters={}",
|
|
|
- routes.getId(), routes.getUri(), routes.getPredicates(), routes.getFilters());
|
|
|
- }
|
|
|
+ log.info("Loading {} routes from database", gatewayRoutesList.size());
|
|
|
|
|
|
Map<String, RouteDefinition> routes = new LinkedHashMap<>();
|
|
|
for (GatewayRoutes gatewayRoutes : gatewayRoutesList) {
|
|
|
@@ -79,31 +71,16 @@ public class JdbcRouteDefinitionRepository implements RouteDefinitionRepository
|
|
|
public Mono<Void> save(Mono<RouteDefinition> route) {
|
|
|
return route.flatMap(r -> {
|
|
|
try {
|
|
|
- log.info("尝试保存路由定义: ID={}, URI={}, Predicates={}, Filters={}",
|
|
|
- r.getId(), r.getUri(), r.getPredicates(), r.getFilters());
|
|
|
- GatewayRoutes gatewayRoutes = new GatewayRoutes();
|
|
|
- gatewayRoutes.setId(r.getId());
|
|
|
- gatewayRoutes.setUri(r.getUri().toString());
|
|
|
- gatewayRoutes.setPredicates(JsonUtils.objectToJson(r.getPredicates()));
|
|
|
- gatewayRoutes.setFilters(JsonUtils.objectToJson(r.getFilters()));
|
|
|
- gatewayRoutes.setStatus("0"); // 默认设置为启用状态
|
|
|
-
|
|
|
-// // 检查路由是否已存在
|
|
|
-// if (gatewayRoutesService.getById(r.getId()) != null) {
|
|
|
-// // 路由已存在,执行更新操作
|
|
|
-// log.info("路由定义已存在,执行更新操作: ID={}", r.getId());
|
|
|
-// gatewayRoutesService.updateById(gatewayRoutes);
|
|
|
-// } else {
|
|
|
-// // 路由不存在,执行插入操作
|
|
|
-// log.info("路由定义不存在,执行插入操作: ID={}", r.getId());
|
|
|
-// gatewayRoutesService.save(gatewayRoutes);
|
|
|
-// }
|
|
|
-
|
|
|
- log.info("路由定义保存成功: ID={}", r.getId());
|
|
|
+// GatewayRoutes gatewayRoutes = new GatewayRoutes();
|
|
|
+// gatewayRoutes.setId(r.getId());
|
|
|
+// gatewayRoutes.setUri(r.getUri().toString());
|
|
|
+// gatewayRoutes.setPredicates(JsonUtils.objectToJson(r.getPredicates()));
|
|
|
+// gatewayRoutes.setFilters(JsonUtils.objectToJson(r.getFilters()));
|
|
|
+// gatewayRoutesService.save(gatewayRoutes);
|
|
|
return Mono.empty();
|
|
|
} catch (Exception e) {
|
|
|
- log.error("RouteDefinition save error: " + r.getId(), e);
|
|
|
- return Mono.defer(() -> Mono.error(e));
|
|
|
+ log.error("RouteDefinition save error: " + r.getId());
|
|
|
+ return Mono.defer(() -> Mono.error(new NotFoundException("RouteDefinition save error: " + r.getId())));
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@@ -112,7 +89,7 @@ public class JdbcRouteDefinitionRepository implements RouteDefinitionRepository
|
|
|
public Mono<Void> delete(Mono<String> routeId) {
|
|
|
return routeId.flatMap(id -> {
|
|
|
try {
|
|
|
- gatewayRoutesService.removeById(id);
|
|
|
+// gatewayRoutesService.removeById(id);
|
|
|
return Mono.empty();
|
|
|
} catch (Exception e) {
|
|
|
log.error("删除路由失败", e);
|
|
|
@@ -120,4 +97,4 @@ public class JdbcRouteDefinitionRepository implements RouteDefinitionRepository
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
-}
|
|
|
+}
|