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

pull/3100/head
zhangdaiscott 3 years ago
parent 55ebea88af
commit 4da1948cb0

@ -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<SysGatewayRouteMappe
@Override
public void addRoute2Redis(String key) {
List<SysGatewayRoute> ls = this.list(new LambdaQueryWrapper<SysGatewayRoute>().eq(SysGatewayRoute::getStatus, 1));
List<SysGatewayRoute> ls = this.list(new LambdaQueryWrapper<SysGatewayRoute>());
redisTemplate.opsForValue().set(key, JSON.toJSONString(ls));
}

@ -128,7 +128,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
* @return
*/
private void loadRoutesByRedis() {
List<RouteDefinition> routes = Lists.newArrayList();
List<MyRouteDefinition> 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<RouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
List<RouteDefinition> ls = new ArrayList<>();
public static List<MyRouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
List<MyRouteDefinition> 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")));

@ -47,14 +47,13 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
* @param id
* @return
*/
public synchronized Mono<ResponseEntity<Object>> 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();
}
}
/**

@ -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;
}
}
Loading…
Cancel
Save