diff --git a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysGatewayRouteServiceImpl.java b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysGatewayRouteServiceImpl.java index 611aae96..88aa7c4d 100644 --- a/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysGatewayRouteServiceImpl.java +++ b/jeecg-boot/jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/service/impl/SysGatewayRouteServiceImpl.java @@ -17,9 +17,7 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * @Description: gateway路由管理 @@ -37,7 +35,7 @@ public class SysGatewayRouteServiceImpl extends ServiceImpl ls = this.list(new LambdaQueryWrapper().eq(SysGatewayRoute::getStatus, 1)); + List ls = this.list(new LambdaQueryWrapper()); redisTemplate.opsForValue().set(key, JSON.toJSONString(ls)); } diff --git a/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/DynamicRouteLoader.java b/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/DynamicRouteLoader.java index d3d3420d..df952aa7 100644 --- a/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/DynamicRouteLoader.java +++ b/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/DynamicRouteLoader.java @@ -128,7 +128,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware { * @return */ private void loadRoutesByRedis() { - List routes = Lists.newArrayList(); + List routes = Lists.newArrayList(); configService = createConfigService(); if (configService == null) { log.warn("initConfigService fail"); @@ -143,9 +143,14 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware { e.printStackTrace(); } } - for (RouteDefinition definition : routes) { + for (MyRouteDefinition definition : routes) { log.info("update route : {}", definition.toString()); - dynamicRouteService.add(definition); + Integer status=definition.getStatus(); + if(status.equals(0)){ + dynamicRouteService.delete(definition.getId()); + }else{ + dynamicRouteService.add(definition); + } } this.publisher.publishEvent(new RefreshRoutesEvent(this)); } @@ -161,12 +166,13 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware { * @return */ - public static List getRoutesByJson(JSONArray array) throws URISyntaxException { - List ls = new ArrayList<>(); + public static List getRoutesByJson(JSONArray array) throws URISyntaxException { + List ls = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { JSONObject obj = array.getJSONObject(i); - RouteDefinition route = new RouteDefinition(); + MyRouteDefinition route = new MyRouteDefinition(); route.setId(obj.getString("routerId")); + route.setStatus(obj.getInteger("status")); Object uri = obj.get("uri"); if (uri == null) { route.setUri(new URI("lb://" + obj.getString("name"))); diff --git a/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/DynamicRouteService.java b/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/DynamicRouteService.java index a9432769..cda91e30 100644 --- a/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/DynamicRouteService.java +++ b/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/DynamicRouteService.java @@ -47,14 +47,13 @@ public class DynamicRouteService implements ApplicationEventPublisherAware { * @param id * @return */ - public synchronized Mono> delete(String id) { - return this.repository.delete(Mono.just(id)).then(Mono.defer(() -> { - return Mono.just(ResponseEntity.ok().build()); - })).onErrorResume((t) -> { - return t instanceof NotFoundException; - }, (t) -> { - return Mono.just(ResponseEntity.notFound().build()); - }); + public synchronized void delete(String id) { + try { + repository.delete(Mono.just(id)).subscribe(); + this.publisher.publishEvent(new RefreshRoutesEvent(this)); + }catch (Exception e){ + e.printStackTrace(); + } } /** diff --git a/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/MyRouteDefinition.java b/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/MyRouteDefinition.java new file mode 100644 index 00000000..474f272c --- /dev/null +++ b/jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/MyRouteDefinition.java @@ -0,0 +1,22 @@ +package org.jeecg.loader; + +import org.springframework.cloud.gateway.route.RouteDefinition; + +/** + * 自定义RouteDefinition + * @author zyf + */ +public class MyRouteDefinition extends RouteDefinition { + /** + * 路由状态 + */ + private Integer status; + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } +}