use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.
the class AbstracResponseFilter method filter.
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
if (exchange.getAttribute(GatewayConstants.CONTEXT_IGNORE_FILTER) != null) {
return chain.filter(exchange);
}
if (RuequestHelper.isWebSocketRequest(exchange.getRequest())) {
return chain.filter(exchange);
}
//
BizSystemModule module = RuequestHelper.getCurrentModule(exchange);
RewriteBodyServerHttpResponse newResponse = new RewriteBodyServerHttpResponse(exchange, module);
return chain.filter(exchange.mutate().response(newResponse).build()).then(Mono.fromRunnable(() -> {
Long start = exchange.getAttribute(GatewayConstants.CONTEXT_REQUEST_START_TIME);
if (logger.isDebugEnabled() && start != null) {
logger.debug(">request_time_trace -> uri:{},useTime:{} ms", exchange.getRequest().getPath().value(), (System.currentTimeMillis() - start));
}
}));
}
use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.
the class CurrentSystemHolder method load.
public static synchronized void load() {
//
loadLocalRouteModules();
List<BizSystemModule> modules;
try {
modules = InstanceFactory.getInstance(SystemMgtApi.class).getSystemModules();
if (!localModules.isEmpty()) {
modules.addAll(localModules);
}
} catch (Exception e) {
modules = localModules;
}
// 网关本身
if (!modules.stream().anyMatch(o -> GlobalRuntimeContext.APPID.equalsIgnoreCase(o.getServiceId()))) {
BizSystemModule module = new BizSystemModule();
module.setServiceId(GlobalRuntimeContext.APPID);
module.setRouteName(GlobalRuntimeContext.APPID);
module.setStripPrefix(0);
modules.add(module);
localModules.add(module);
}
Map<String, BizSystemModule> _modules = new HashMap<>(modules.size());
for (BizSystemModule module : modules) {
boolean isGateway = GlobalRuntimeContext.APPID.equalsIgnoreCase(module.getServiceId());
if (!isGateway && StringUtils.isBlank(module.getRouteName())) {
continue;
}
module.finalCorrect();
//
if (moduleApiInfos.containsKey(module.getServiceId())) {
module.setApiInfos(moduleApiInfos.get(module.getServiceId()));
}
_modules.put(module.getRouteName(), module);
}
routeModuleMappings.set(_modules);
// 查询api信息
if (fetchApiMetaRound == 0) {
new Thread(() -> {
while (_modules.size() > moduleApiInfos.size() && fetchApiMetaRound < 360) {
for (BizSystemModule module : _modules.values()) {
if (moduleApiInfos.containsKey(module.getServiceId()))
continue;
initModuleApiInfos(module);
}
fetchApiMetaRound++;
try {
Thread.sleep(10000);
} catch (Exception e) {
}
}
}).start();
}
}
use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.
the class RuequestHelper method getCurrentModule.
public static BizSystemModule getCurrentModule(ServerWebExchange exchange) {
BizSystemModule module = exchange.getAttribute(GatewayConstants.CONTEXT_ROUTE_SERVICE);
if (module != null)
return module;
String routeName = resolveRouteName(exchange.getRequest().getPath().value());
module = CurrentSystemHolder.getModule(routeName);
if (module != null) {
exchange.getAttributes().put(GatewayConstants.CONTEXT_ROUTE_SERVICE, module);
}
return module;
}
use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.
the class CustomRouteDefinitionRepository method getRouteDefinitions.
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
if (routeHub.get() == null) {
routeHub.set(new HashMap<>());
}
if (routeHub.get().isEmpty()) {
// 本地路由
List<RouteDefinition> routes = gatewayProperties.getRoutes();
for (RouteDefinition routeDef : routes) {
routeHub.get().put(routeDef.getId().toLowerCase(), routeDef);
}
//
Map<String, BizSystemModule> modules = CurrentSystemHolder.getRouteModuleMappings();
for (BizSystemModule module : modules.values()) {
// 网关本身
if (GlobalRuntimeContext.APPID.equals(module.getServiceId())) {
continue;
}
// 本地已加载
if (routeHub.get().containsKey(module.getServiceId())) {
continue;
}
//
loadDynamicRouteDefinition(module);
}
for (RouteDefinition route : routeHub.get().values()) {
EnableBodyCachingEvent enableBodyCachingEvent = new EnableBodyCachingEvent(new Object(), route.getId());
adaptCachedBodyGlobalFilter.onApplicationEvent(enableBodyCachingEvent);
}
StringBuilder logBuilder = new StringBuilder("\n=============routes map Begin==============\n");
for (BizSystemModule module : modules.values()) {
if (GlobalRuntimeContext.APPID.equals(module.getServiceId()))
continue;
logBuilder.append(module.toString()).append("\n");
}
logBuilder.append("=============routes map End==============\n");
logger.info(logBuilder.toString());
}
return Flux.fromIterable(routeHub.get().values());
}
use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.
the class GatewayReactiveCustomAuthnHandler method afterAuthentication.
@Override
public void afterAuthentication(ServerWebExchange exchange, UserSession userSession) {
if (!GatewayConfigs.actionLogEnabled)
return;
ServerHttpRequest request = exchange.getRequest();
BizSystemModule module = CurrentSystemHolder.getModule(RuequestHelper.resolveRouteName(request.getPath().value()));
ApiInfo apiInfo = module.getApiInfo(request.getMethodValue(), request.getPath().value());
boolean logging = apiInfo != null ? apiInfo.isActionLog() : true;
if (logging) {
logging = !GatewayConfigs.actionLogGetMethodIngore || !request.getMethod().equals(HttpMethod.GET);
}
if (logging) {
String clientIp = RuequestHelper.getIpAddr(request);
ActionLog actionLog = ActionLogCollector.onRequestStart(request.getMethodValue(), request.getPath().value(), clientIp).apiMeta(apiInfo);
exchange.getAttributes().put(ActionLogCollector.CURRENT_LOG_CONTEXT_NAME, actionLog);
}
}
Aggregations