路由网关禁用Demo配置后,系统仍可以通过网关路由到Demo服务。issues/I49457
This commit is contained in:
parent
55ebea88af
commit
4da1948cb0
|
@ -17,9 +17,7 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Description: gateway路由管理
|
* @Description: gateway路由管理
|
||||||
|
@ -37,7 +35,7 @@ public class SysGatewayRouteServiceImpl extends ServiceImpl<SysGatewayRouteMappe
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addRoute2Redis(String key) {
|
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));
|
redisTemplate.opsForValue().set(key, JSON.toJSONString(ls));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -128,7 +128,7 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private void loadRoutesByRedis() {
|
private void loadRoutesByRedis() {
|
||||||
List<RouteDefinition> routes = Lists.newArrayList();
|
List<MyRouteDefinition> routes = Lists.newArrayList();
|
||||||
configService = createConfigService();
|
configService = createConfigService();
|
||||||
if (configService == null) {
|
if (configService == null) {
|
||||||
log.warn("initConfigService fail");
|
log.warn("initConfigService fail");
|
||||||
|
@ -143,9 +143,14 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (RouteDefinition definition : routes) {
|
for (MyRouteDefinition definition : routes) {
|
||||||
log.info("update route : {}", definition.toString());
|
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));
|
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||||
}
|
}
|
||||||
|
@ -161,12 +166,13 @@ public class DynamicRouteLoader implements ApplicationEventPublisherAware {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public static List<RouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
|
public static List<MyRouteDefinition> getRoutesByJson(JSONArray array) throws URISyntaxException {
|
||||||
List<RouteDefinition> ls = new ArrayList<>();
|
List<MyRouteDefinition> ls = new ArrayList<>();
|
||||||
for (int i = 0; i < array.size(); i++) {
|
for (int i = 0; i < array.size(); i++) {
|
||||||
JSONObject obj = array.getJSONObject(i);
|
JSONObject obj = array.getJSONObject(i);
|
||||||
RouteDefinition route = new RouteDefinition();
|
MyRouteDefinition route = new MyRouteDefinition();
|
||||||
route.setId(obj.getString("routerId"));
|
route.setId(obj.getString("routerId"));
|
||||||
|
route.setStatus(obj.getInteger("status"));
|
||||||
Object uri = obj.get("uri");
|
Object uri = obj.get("uri");
|
||||||
if (uri == null) {
|
if (uri == null) {
|
||||||
route.setUri(new URI("lb://" + obj.getString("name")));
|
route.setUri(new URI("lb://" + obj.getString("name")));
|
||||||
|
|
|
@ -47,14 +47,13 @@ public class DynamicRouteService implements ApplicationEventPublisherAware {
|
||||||
* @param id
|
* @param id
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public synchronized Mono<ResponseEntity<Object>> delete(String id) {
|
public synchronized void delete(String id) {
|
||||||
return this.repository.delete(Mono.just(id)).then(Mono.defer(() -> {
|
try {
|
||||||
return Mono.just(ResponseEntity.ok().build());
|
repository.delete(Mono.just(id)).subscribe();
|
||||||
})).onErrorResume((t) -> {
|
this.publisher.publishEvent(new RefreshRoutesEvent(this));
|
||||||
return t instanceof NotFoundException;
|
}catch (Exception e){
|
||||||
}, (t) -> {
|
e.printStackTrace();
|
||||||
return Mono.just(ResponseEntity.notFound().build());
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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…
Reference in New Issue