Search in sources :

Example 6 with BizSystemModule

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));
        }
    }));
}
Also used : RewriteBodyServerHttpResponse(com.mendmix.gateway.filter.post.RewriteBodyServerHttpResponse) BizSystemModule(com.mendmix.gateway.model.BizSystemModule)

Example 7 with BizSystemModule

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();
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) GlobalRuntimeContext(com.mendmix.common.GlobalRuntimeContext) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) Map(java.util.Map) AppMetadataHolder(com.mendmix.springweb.exporter.AppMetadataHolder) GatewayProperties(org.springframework.cloud.gateway.config.GatewayProperties) Properties(java.util.Properties) Logger(org.slf4j.Logger) HttpRequestEntity(com.mendmix.common.http.HttpRequestEntity) InstanceFactory(com.mendmix.spring.InstanceFactory) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) Collection(java.util.Collection) FilterDefinition(org.springframework.cloud.gateway.filter.FilterDefinition) AppMetadata(com.mendmix.springweb.model.AppMetadata) Set(java.util.Set) PredicateDefinition(org.springframework.cloud.gateway.handler.predicate.PredicateDefinition) SystemMgtApi(com.mendmix.gateway.api.SystemMgtApi) List(java.util.List) ApiInfo(com.mendmix.common.model.ApiInfo) BizSystemPortal(com.mendmix.gateway.model.BizSystemPortal) Entry(java.util.Map.Entry) ResourceUtils(com.mendmix.common.util.ResourceUtils) Collections(java.util.Collections) RouteDefinition(org.springframework.cloud.gateway.route.RouteDefinition) HashMap(java.util.HashMap) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException)

Example 8 with BizSystemModule

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;
}
Also used : BizSystemModule(com.mendmix.gateway.model.BizSystemModule)

Example 9 with BizSystemModule

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());
}
Also used : RouteDefinition(org.springframework.cloud.gateway.route.RouteDefinition) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) EnableBodyCachingEvent(org.springframework.cloud.gateway.event.EnableBodyCachingEvent)

Example 10 with BizSystemModule

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);
    }
}
Also used : ApiInfo(com.mendmix.common.model.ApiInfo) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) ActionLog(com.mendmix.logging.integrate.ActionLog)

Aggregations

BizSystemModule (com.mendmix.gateway.model.BizSystemModule)12 ApiInfo (com.mendmix.common.model.ApiInfo)4 JeesuiteBaseException (com.mendmix.common.JeesuiteBaseException)3 HashMap (java.util.HashMap)3 RouteDefinition (org.springframework.cloud.gateway.route.RouteDefinition)3 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)3 ArrayList (java.util.ArrayList)2 Entry (java.util.Map.Entry)2 Properties (java.util.Properties)2 GatewayProperties (org.springframework.cloud.gateway.config.GatewayProperties)2 GlobalRuntimeContext (com.mendmix.common.GlobalRuntimeContext)1 HttpRequestEntity (com.mendmix.common.http.HttpRequestEntity)1 ResourceUtils (com.mendmix.common.util.ResourceUtils)1 SystemMgtApi (com.mendmix.gateway.api.SystemMgtApi)1 RewriteBodyServerHttpResponse (com.mendmix.gateway.filter.post.RewriteBodyServerHttpResponse)1 BizSystemPortal (com.mendmix.gateway.model.BizSystemPortal)1 ActionLog (com.mendmix.logging.integrate.ActionLog)1 ApiPermission (com.mendmix.security.model.ApiPermission)1 InstanceFactory (com.mendmix.spring.InstanceFactory)1 AppMetadataHolder (com.mendmix.springweb.exporter.AppMetadataHolder)1