Search in sources :

Example 1 with RequestOriginParser

use of com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser in project blade-tool by chillzhuang.

the class BladeSentinelFilterConfiguration method sentinelWebMvcConfig.

@Bean
public SentinelWebMvcConfig sentinelWebMvcConfig(SentinelProperties properties, Optional<UrlCleaner> urlCleanerOptional, Optional<BlockExceptionHandler> blockExceptionHandlerOptional, Optional<RequestOriginParser> requestOriginParserOptional) {
    SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig();
    sentinelWebMvcConfig.setHttpMethodSpecify(properties.getHttpMethodSpecify());
    sentinelWebMvcConfig.setWebContextUnify(properties.getWebContextUnify());
    if (blockExceptionHandlerOptional.isPresent()) {
        blockExceptionHandlerOptional.ifPresent(sentinelWebMvcConfig::setBlockExceptionHandler);
    } else {
        if (StringUtils.hasText(properties.getBlockPage())) {
            sentinelWebMvcConfig.setBlockExceptionHandler(((request, response, e) -> response.sendRedirect(properties.getBlockPage())));
        } else {
            sentinelWebMvcConfig.setBlockExceptionHandler(new DefaultBlockExceptionHandler());
        }
    }
    urlCleanerOptional.ifPresent(sentinelWebMvcConfig::setUrlCleaner);
    requestOriginParserOptional.ifPresent(sentinelWebMvcConfig::setOriginParser);
    return sentinelWebMvcConfig;
}
Also used : DefaultBlockExceptionHandler(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler) SentinelWebMvcConfig(com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig) SentinelWebInterceptor(com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor) UrlCleaner(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner) RequiredArgsConstructor(lombok.RequiredArgsConstructor) SentinelProperties(com.alibaba.cloud.sentinel.SentinelProperties) Optional(java.util.Optional) RequestOriginParser(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser) Bean(org.springframework.context.annotation.Bean) BlockExceptionHandler(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler) ConditionalOnWebApplication(org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication) StringUtils(org.springframework.util.StringUtils) DefaultBlockExceptionHandler(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler) SentinelWebMvcConfig(com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig) Bean(org.springframework.context.annotation.Bean)

Example 2 with RequestOriginParser

use of com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser in project Sentinel by alibaba.

the class InterceptorConfig method addSpringMvcInterceptor.

private void addSpringMvcInterceptor(InterceptorRegistry registry) {
    // Config
    SentinelWebMvcConfig config = new SentinelWebMvcConfig();
    config.setBlockExceptionHandler(new BlockExceptionHandler() {

        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
            String resourceName = e.getRule().getResource();
            // Depending on your situation, you can choose to process or throw
            if ("/hello".equals(resourceName)) {
                // Do something ......
                // Write string or json string;
                response.getWriter().write("/Blocked by sentinel");
            } else {
                // Handle in global exception handling
                throw e;
            }
        }
    });
    // Custom configuration if necessary
    config.setHttpMethodSpecify(false);
    config.setWebContextUnify(true);
    config.setOriginParser(new RequestOriginParser() {

        @Override
        public String parseOrigin(HttpServletRequest request) {
            return request.getHeader("S-user");
        }
    });
    // Add sentinel interceptor
    registry.addInterceptor(new SentinelWebInterceptor(config)).addPathPatterns("/**");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) BlockExceptionHandler(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler) RequestOriginParser(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser) HttpServletResponse(javax.servlet.http.HttpServletResponse) SentinelWebInterceptor(com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException)

Example 3 with RequestOriginParser

use of com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser in project springboot-learning by lyb-geek.

the class CircuitBreakerAspect method invokeResourceWithSentinel.

@Around("@annotation(circuitBreakerMapping)")
public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp, CircuitBreakerMapping circuitBreakerMapping) throws Throwable {
    Method originMethod = resolveMethod(pjp);
    CircuitBreakerMapping controllerCircuitBreakerMapping = AnnotationUtils.findAnnotation(pjp.getTarget().getClass(), CircuitBreakerMapping.class);
    String baseResouceName = "lybgeek:";
    if (circuitBreakerMapping != null) {
        baseResouceName = baseResouceName + controllerCircuitBreakerMapping.value()[0];
    }
    baseResouceName = baseResouceName + circuitBreakerMapping.value()[0];
    String resourceName = getResourceName(baseResouceName, originMethod);
    EntryType entryType = circuitBreakerMapping.entryType();
    int resourceType = circuitBreakerMapping.resourceType();
    Entry entry = null;
    try {
        String contextName = "lybgeek_circuitbreaker_context";
        RequestOriginParser parser = SpringUtil.getBean(RequestOriginParser.class);
        ContextUtil.enter(contextName, parser.parseOrigin(getRequest()));
        entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());
        Object result = pjp.proceed();
        return result;
    } catch (BlockException ex) {
        return handleBlockException(pjp, circuitBreakerMapping, ex);
    } catch (Throwable ex) {
        Class<? extends Throwable>[] exceptionsToIgnore = circuitBreakerMapping.exceptionsToIgnore();
        // The ignore list will be checked first.
        if (exceptionsToIgnore.length > 0 && exceptionBelongsTo(ex, exceptionsToIgnore)) {
            throw ex;
        }
        if (exceptionBelongsTo(ex, circuitBreakerMapping.exceptionsToTrace())) {
            traceException(ex, circuitBreakerMapping);
            return handleFallback(pjp, circuitBreakerMapping, ex);
        }
        // No fallback function can handle the exception, so throw it out.
        throw ex;
    } finally {
        if (entry != null) {
            entry.exit(1, pjp.getArgs());
        }
        ContextUtil.exit();
    }
}
Also used : Entry(com.alibaba.csp.sentinel.Entry) BlockException(com.alibaba.csp.sentinel.slots.block.BlockException) EntryType(com.alibaba.csp.sentinel.EntryType) RequestOriginParser(com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser) Method(java.lang.reflect.Method) CircuitBreakerMapping(com.github.lybgeek.circuitbreaker.framework.annotation.CircuitBreakerMapping) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Around(org.aspectj.lang.annotation.Around)

Aggregations

RequestOriginParser (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser)3 SentinelWebInterceptor (com.alibaba.csp.sentinel.adapter.spring.webmvc.SentinelWebInterceptor)2 BlockExceptionHandler (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler)2 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)2 SentinelProperties (com.alibaba.cloud.sentinel.SentinelProperties)1 Entry (com.alibaba.csp.sentinel.Entry)1 EntryType (com.alibaba.csp.sentinel.EntryType)1 DefaultBlockExceptionHandler (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.DefaultBlockExceptionHandler)1 UrlCleaner (com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.UrlCleaner)1 SentinelWebMvcConfig (com.alibaba.csp.sentinel.adapter.spring.webmvc.config.SentinelWebMvcConfig)1 CircuitBreakerMapping (com.github.lybgeek.circuitbreaker.framework.annotation.CircuitBreakerMapping)1 Method (java.lang.reflect.Method)1 Optional (java.util.Optional)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1 ConditionalOnWebApplication (org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication)1 Bean (org.springframework.context.annotation.Bean)1