Commit aa5f19000e1e2cdbbea16ebb99d476329294fb26

Authored by zhangdaiscott
1 parent b88f1150

路由网关禁用Demo配置后,系统仍可以通过网关路由到Demo服务。issues/I49457

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; @@ -17,9 +17,7 @@ import org.springframework.data.redis.core.RedisTemplate;
17 import org.springframework.stereotype.Service; 17 import org.springframework.stereotype.Service;
18 import org.springframework.transaction.annotation.Transactional; 18 import org.springframework.transaction.annotation.Transactional;
19 19
20 -import java.util.HashMap;  
21 import java.util.List; 20 import java.util.List;
22 -import java.util.Map;  
23 21
24 /** 22 /**
25 * @Description: gateway路由管理 23 * @Description: gateway路由管理
@@ -37,7 +35,7 @@ public class SysGatewayRouteServiceImpl extends ServiceImpl<SysGatewayRouteMappe @@ -37,7 +35,7 @@ public class SysGatewayRouteServiceImpl extends ServiceImpl<SysGatewayRouteMappe
37 35
38 @Override 36 @Override
39 public void addRoute2Redis(String key) { 37 public void addRoute2Redis(String key) {
40 - List<SysGatewayRoute> ls = this.list(new LambdaQueryWrapper<SysGatewayRoute>().eq(SysGatewayRoute::getStatus, 1)); 38 + List<SysGatewayRoute> ls = this.list(new LambdaQueryWrapper<SysGatewayRoute>());
41 redisTemplate.opsForValue().set(key, JSON.toJSONString(ls)); 39 redisTemplate.opsForValue().set(key, JSON.toJSONString(ls));
42 } 40 }
43 41
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 { @@ -128,7 +128,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
128 * @return 128 * @return
129 */ 129 */
130 private void loadRoutesByRedis() { 130 private void loadRoutesByRedis() {
131 - List<RouteDefinition> routes = Lists.newArrayList(); 131 + List<MyRouteDefinition> routes = Lists.newArrayList();
132 configService = createConfigService(); 132 configService = createConfigService();
133 if (configService == null) { 133 if (configService == null) {
134 log.warn("initConfigService fail"); 134 log.warn("initConfigService fail");
@@ -143,9 +143,14 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware { @@ -143,9 +143,14 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
143 e.printStackTrace(); 143 e.printStackTrace();
144 } 144 }
145 } 145 }
146 - for (RouteDefinition definition : routes) { 146 + for (MyRouteDefinition definition : routes) {
147 log.info("update route : {}", definition.toString()); 147 log.info("update route : {}", definition.toString());
148 - dynamicRouteService.add(definition); 148 + Integer status=definition.getStatus();
  149 + if(status.equals(0)){
  150 + dynamicRouteService.delete(definition.getId());
  151 + }else{
  152 + dynamicRouteService.add(definition);
  153 + }
149 } 154 }
150 this.publisher.publishEvent(new RefreshRoutesEvent(this)); 155 this.publisher.publishEvent(new RefreshRoutesEvent(this));
151 } 156 }
@@ -161,12 +166,13 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware { @@ -161,12 +166,13 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
161 * @return 166 * @return
162 */ 167 */
163 168
164 - public static List<RouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {  
165 - List<RouteDefinition> ls = new ArrayList<>(); 169 + public static List<MyRouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
  170 + List<MyRouteDefinition> ls = new ArrayList<>();
166 for (int i = 0; i < array.size(); i++) { 171 for (int i = 0; i < array.size(); i++) {
167 JSONObject obj = array.getJSONObject(i); 172 JSONObject obj = array.getJSONObject(i);
168 - RouteDefinition route = new RouteDefinition(); 173 + MyRouteDefinition route = new MyRouteDefinition();
169 route.setId(obj.getString("routerId")); 174 route.setId(obj.getString("routerId"));
  175 + route.setStatus(obj.getInteger("status"));
170 Object uri = obj.get("uri"); 176 Object uri = obj.get("uri");
171 if (uri == null) { 177 if (uri == null) {
172 route.setUri(new URI("lb://" + obj.getString("name"))); 178 route.setUri(new URI("lb://" + obj.getString("name")));
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 { @@ -47,14 +47,13 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
47 * @param id 47 * @param id
48 * @return 48 * @return
49 */ 49 */
50 - public synchronized Mono<ResponseEntity<Object>> delete(String id) {  
51 - return this.repository.delete(Mono.just(id)).then(Mono.defer(() -> {  
52 - return Mono.just(ResponseEntity.ok().build());  
53 - })).onErrorResume((t) -> {  
54 - return t instanceof NotFoundException;  
55 - }, (t) -> {  
56 - return Mono.just(ResponseEntity.notFound().build());  
57 - }); 50 + public synchronized void delete(String id) {
  51 + try {
  52 + repository.delete(Mono.just(id)).subscribe();
  53 + this.publisher.publishEvent(new RefreshRoutesEvent(this));
  54 + }catch (Exception e){
  55 + e.printStackTrace();
  56 + }
58 } 57 }
59 58
60 /** 59 /**
jeecg-boot/jeecg-cloud-module/jeecg-cloud-gateway/src/main/java/org/jeecg/loader/MyRouteDefinition.java 0 → 100644
  1 +package org.jeecg.loader;
  2 +
  3 +import org.springframework.cloud.gateway.route.RouteDefinition;
  4 +
  5 +/**
  6 + * 自定义RouteDefinition
  7 + * @author zyf
  8 + */
  9 +public class MyRouteDefinition extends RouteDefinition {
  10 + /**
  11 + * 路由状态
  12 + */
  13 + private Integer status;
  14 +
  15 + public Integer getStatus() {
  16 + return status;
  17 + }
  18 +
  19 + public void setStatus(Integer status) {
  20 + this.status = status;
  21 + }
  22 +}