org.zodiac.autoconfigure.sentinel.web.SentinelReactiveAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.sentinel.web;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnSpringCloudBootstrapDisabled;
import org.zodiac.autoconfigure.sentinel.condition.ConditionalOnSentinelConfigEnabled;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
@SpringBootConfiguration
@ConditionalOnSentinelConfigEnabled
@ConditionalOnSpringCloudBootstrapDisabled
@ConditionalOnWebApplication(type = Type.REACTIVE)
@ConditionalOnClass(value = {com.alibaba.csp.sentinel.Env.class, com.alibaba.csp.sentinel.annotation.aspectj.AbstractSentinelAspectSupport.class, com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer.class, org.zodiac.sentinel.base.model.SentinelRule.class})
public class SentinelReactiveAutoConfiguration implements InitializingBean{
private static final Logger log = SmartSlf4jLoggerFactory.getLogger(SentinelReactiveAutoConfiguration.class);
private final List viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
private Optional blockRequestHandlerOptional;
public SentinelReactiveAutoConfiguration(
ObjectProvider> viewResolvers,
ServerCodecConfigurer serverCodecConfigurer,
Optional blockRequestHandlerOptional) {
this.viewResolvers = viewResolvers.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
this.blockRequestHandlerOptional = blockRequestHandlerOptional;
}
@Override
public void afterPropertiesSet() throws Exception {
blockRequestHandlerOptional.ifPresent(com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager::setBlockHandler);
}
@Bean
@ConditionalOnMissingBean
protected org.zodiac.sentinel.base.config.web.ReactiveSentinelConfigurer reactiveSentinelConfigurer() {
return new org.zodiac.sentinel.base.config.web.ReactiveSentinelConfigurer(viewResolvers, serverCodecConfigurer);
}
@Bean
@ConditionalOnMissingBean
protected com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler blockRequestHandler(org.zodiac.sentinel.base.config.web.ReactiveSentinelConfigurer reactiveSentinelConfigurer) {
return reactiveSentinelConfigurer.blockRequestHandler();
}
@Bean
@Order(-2)
@ConditionalOnProperty(name = org.zodiac.sentinel.base.constants.SentinelBaseSystemPropertiesConstants.Zodiac.SENTINEL_HTTP_FILTER_ENABLED, havingValue = "true", matchIfMissing = true)
protected com.alibaba.csp.sentinel.adapter.spring.webflux.exception.SentinelBlockExceptionHandler sentinelBlockExceptionHandler(org.zodiac.sentinel.base.config.web.ReactiveSentinelConfigurer reactiveSentinelConfigurer) {
return reactiveSentinelConfigurer.sentinelBlockExceptionHandler();
}
@Bean
@Order(-1)
@ConditionalOnProperty(name = org.zodiac.sentinel.base.constants.SentinelBaseSystemPropertiesConstants.Zodiac.SENTINEL_HTTP_FILTER_ENABLED, havingValue = "true", matchIfMissing = true)
protected com.alibaba.csp.sentinel.adapter.spring.webflux.SentinelWebFluxFilter sentinelWebFluxFilter(org.zodiac.sentinel.base.config.web.ReactiveSentinelConfigurer reactiveSentinelConfigurer) {
log.info("[Sentinel Starter] register Sentinel SentinelWebFluxFilter");
return reactiveSentinelConfigurer.sentinelWebFluxFilter();
}
}