Search in sources :

Example 1 with BizSystemModule

use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.

the class RuequestHelper method getCurrentApi.

public static ApiInfo getCurrentApi(ServerWebExchange exchange) {
    ApiInfo api = exchange.getAttribute(GatewayConstants.CONTEXT_CURRENT_API);
    if (api != null)
        return api;
    BizSystemModule module = getCurrentModule(exchange);
    ServerHttpRequest request = exchange.getRequest();
    ApiInfo apiInfo = module.getApiInfo(request.getMethodValue(), request.getPath().value());
    if (apiInfo != null) {
        exchange.getAttributes().put(GatewayConstants.CONTEXT_CURRENT_API, apiInfo);
    }
    return apiInfo;
}
Also used : ApiInfo(com.mendmix.common.model.ApiInfo) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest)

Example 2 with BizSystemModule

use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.

the class CurrentSystemHolder method loadLocalRouteModules.

private static void loadLocalRouteModules() {
    if (localModules != null)
        return;
    localModules = new ArrayList<>();
    List<RouteDefinition> defaultRouteDefs = InstanceFactory.getInstance(GatewayProperties.class).getRoutes();
    Properties properties = ResourceUtils.getAllProperties("spring.cloud.gateway.routes");
    Set<Entry<Object, Object>> entrySet = properties.entrySet();
    BizSystemModule module;
    String prefix;
    for (Entry<Object, Object> entry : entrySet) {
        if (entry.getKey().toString().endsWith(".id")) {
            prefix = entry.getKey().toString().replace(".id", "");
            module = new BizSystemModule();
            module.setDefaultRoute(true);
            module.setServiceId(entry.getValue().toString());
            module.setProxyUri(properties.getProperty(prefix + ".uri"));
            module.setAnonymousUris(properties.getProperty(prefix + ".anonymousUris"));
            // 
            updateModuleRouteInfos(module, defaultRouteDefs);
            localModules.add(module);
        }
    }
}
Also used : Entry(java.util.Map.Entry) RouteDefinition(org.springframework.cloud.gateway.route.RouteDefinition) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) GatewayProperties(org.springframework.cloud.gateway.config.GatewayProperties) GatewayProperties(org.springframework.cloud.gateway.config.GatewayProperties) Properties(java.util.Properties)

Example 3 with BizSystemModule

use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.

the class AbstracRequestFilter method filter.

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    exchange.getAttributes().put(GatewayConstants.CONTEXT_REQUEST_START_TIME, System.currentTimeMillis());
    String requestUri = exchange.getRequest().getPath().value();
    if (ignoreUris.stream().anyMatch(o -> requestUri.endsWith(o))) {
        exchange.getAttributes().put(GatewayConstants.CONTEXT_IGNORE_FILTER, Boolean.TRUE);
        return chain.filter(exchange);
    }
    BizSystemModule module = RuequestHelper.getCurrentModule(exchange);
    try {
        Builder requestBuilder = exchange.getRequest().mutate();
        for (PreFilterHandler handler : handlers) {
            requestBuilder = handler.process(exchange, module, requestBuilder);
        }
        exchange = exchange.mutate().request(requestBuilder.build()).build();
    } catch (Exception e) {
        exchange.getAttributes().clear();
        if (e instanceof JeesuiteBaseException == false) {
            logger.error("requestFilter_error", e);
        }
        ServerHttpResponse response = exchange.getResponse();
        byte[] bytes = JsonUtils.toJson(WrapperResponse.fail(e)).getBytes(StandardCharsets.UTF_8);
        return response.writeWith(Mono.just(response.bufferFactory().wrap(bytes)));
    }
    return chain.filter(exchange);
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) Builder(org.springframework.http.server.reactive.ServerHttpRequest.Builder) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Example 4 with BizSystemModule

use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.

the class ActuatorController method health.

@GetMapping("/health")
public Map<String, Object> health(@RequestParam(value = "details", required = false) boolean details) {
    Map<String, Object> result = new HashMap<>(1);
    result.put("status", "UP");
    result.put("startTime", GlobalRuntimeContext.STARTUP_TIME);
    if (details) {
        Collection<BizSystemModule> modules = CurrentSystemHolder.getModules();
        Map<String, Object> moduleStatus = new HashMap<>(modules.size());
        for (BizSystemModule module : modules) {
            try {
                String status = HttpRequestEntity.get(module.getHealthUri()).execute().toValue("status");
                moduleStatus.put(module.getServiceId(), status);
            } catch (JeesuiteBaseException e) {
                if (e.getCode() == 404 || e.getCode() == 401 || e.getCode() == 403) {
                    result.put(module.getServiceId(), "UP");
                } else {
                    result.put(module.getServiceId(), "UNKNOW");
                }
            } catch (Exception e) {
                result.put(module.getServiceId(), "UNKNOW");
            }
        }
        result.put("modules", moduleStatus);
    }
    return result;
}
Also used : JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) HashMap(java.util.HashMap) BizSystemModule(com.mendmix.gateway.model.BizSystemModule) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with BizSystemModule

use of com.mendmix.gateway.model.BizSystemModule in project jeesuite-libs by vakinge.

the class RouteErrorWebExceptionHandler method getErrorAttributes.

@Override
protected Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
    Throwable error = super.getError(request);
    logger.warn("routeError{},errorType:{},errorMsg:{}", LogMessageFormat.buildLogTail(request.path()), error.getClass().getName(), error.getMessage());
    String msg = error.getMessage();
    Integer code = HttpStatus.INTERNAL_SERVER_ERROR.value();
    if (msg.contains("Connection refused") || msg.contains("connection timed out")) {
        msg = "Service Unavailable";
        code = 503;
    }
    Map<String, Object> errorAttributes = new HashMap<>(3);
    errorAttributes.put(GlobalConstants.PARAM_MSG, msg);
    errorAttributes.put(GlobalConstants.PARAM_CODE, code);
    Map<String, String> data = new HashMap<>(2);
    String routeName = RuequestHelper.resolveRouteName(request.path());
    BizSystemModule module = CurrentSystemHolder.getModule(routeName);
    data.put("serviceId", module.getServiceId());
    data.put("path", request.path());
    errorAttributes.put(GlobalConstants.PARAM_DATA, data);
    return errorAttributes;
}
Also used : HashMap(java.util.HashMap) BizSystemModule(com.mendmix.gateway.model.BizSystemModule)

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