mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
sentinel zuul refactor
This commit is contained in:
parent
0e4de15632
commit
80d27a1824
@ -16,6 +16,17 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel.zuul;
|
||||
|
||||
import static org.springframework.cloud.alibaba.sentinel.zuul.SentinelZuulAutoConfiguration.PREFIX;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.alibaba.sentinel.zuul.listener.FallBackProviderHandler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.DefaultRequestOriginParser;
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.DefaultUrlCleaner;
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.RequestOriginParser;
|
||||
@ -25,17 +36,8 @@ import com.alibaba.csp.sentinel.adapter.zuul.filters.SentinelPostFilter;
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.filters.SentinelPreFilter;
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.alibaba.sentinel.zuul.listener.FallBackProviderListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.springframework.cloud.alibaba.sentinel.zuul.SentinelZuulAutoConfiguration.PREFIX;
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
|
||||
/**
|
||||
* Sentinel Spring Cloud Zuul AutoConfiguration
|
||||
@ -46,65 +48,67 @@ import static org.springframework.cloud.alibaba.sentinel.zuul.SentinelZuulAutoCo
|
||||
@ConditionalOnProperty(prefix = PREFIX, name = "enabled", havingValue = "true")
|
||||
public class SentinelZuulAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
public static final String PREFIX = "spring.cloud.alibaba.sentinel.zuul";
|
||||
public static final String PREFIX = "spring.cloud.alibaba.sentinel.zuul";
|
||||
|
||||
@Bean
|
||||
public SentinelZuulProperties sentinelZuulProperties() {
|
||||
SentinelZuulProperties properties = new SentinelZuulProperties();
|
||||
String enabledStr = environment.getProperty(PREFIX + "." + "enabled");
|
||||
String preOrderStr = environment.getProperty(PREFIX + "." + "order.pre");
|
||||
String postOrderStr = environment.getProperty(PREFIX + "." + "order.post");
|
||||
String errorOrderStr = environment.getProperty(PREFIX + "." + "order.error");
|
||||
if (StringUtil.isNotEmpty(enabledStr)) {
|
||||
Boolean enabled = Boolean.valueOf(enabledStr);
|
||||
properties.setEnabled(enabled);
|
||||
}
|
||||
if (StringUtil.isNotEmpty(preOrderStr)) {
|
||||
properties.getOrder().setPre(Integer.parseInt(preOrderStr));
|
||||
}
|
||||
if (StringUtil.isNotEmpty(postOrderStr)) {
|
||||
properties.getOrder().setPost(Integer.parseInt(postOrderStr));
|
||||
}
|
||||
if (StringUtil.isNotEmpty(errorOrderStr)) {
|
||||
properties.getOrder().setError(Integer.parseInt(errorOrderStr));
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
@Bean
|
||||
public SentinelZuulProperties sentinelZuulProperties() {
|
||||
SentinelZuulProperties properties = new SentinelZuulProperties();
|
||||
String enabledStr = environment.getProperty(PREFIX + "." + "enabled");
|
||||
String preOrderStr = environment.getProperty(PREFIX + "." + "order.pre");
|
||||
String postOrderStr = environment.getProperty(PREFIX + "." + "order.post");
|
||||
String errorOrderStr = environment.getProperty(PREFIX + "." + "order.error");
|
||||
if (StringUtil.isNotEmpty(enabledStr)) {
|
||||
Boolean enabled = Boolean.valueOf(enabledStr);
|
||||
properties.setEnabled(enabled);
|
||||
}
|
||||
if (StringUtil.isNotEmpty(preOrderStr)) {
|
||||
properties.getOrder().setPre(Integer.parseInt(preOrderStr));
|
||||
}
|
||||
if (StringUtil.isNotEmpty(postOrderStr)) {
|
||||
properties.getOrder().setPost(Integer.parseInt(postOrderStr));
|
||||
}
|
||||
if (StringUtil.isNotEmpty(errorOrderStr)) {
|
||||
properties.getOrder().setError(Integer.parseInt(errorOrderStr));
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(UrlCleaner.class)
|
||||
public UrlCleaner urlCleaner(){
|
||||
return new DefaultUrlCleaner();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(UrlCleaner.class)
|
||||
public UrlCleaner urlCleaner() {
|
||||
return new DefaultUrlCleaner();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(RequestOriginParser.class)
|
||||
public RequestOriginParser requestOriginParser(){
|
||||
return new DefaultRequestOriginParser();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(RequestOriginParser.class)
|
||||
public RequestOriginParser requestOriginParser() {
|
||||
return new DefaultRequestOriginParser();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ZuulFilter preFilter(SentinelZuulProperties sentinelZuulProperties,UrlCleaner urlCleaner,
|
||||
RequestOriginParser requestOriginParser) {
|
||||
return new SentinelPreFilter(sentinelZuulProperties,urlCleaner,requestOriginParser);
|
||||
}
|
||||
@Bean
|
||||
public ZuulFilter preFilter(SentinelZuulProperties sentinelZuulProperties,
|
||||
UrlCleaner urlCleaner, RequestOriginParser requestOriginParser) {
|
||||
return new SentinelPreFilter(sentinelZuulProperties, urlCleaner,
|
||||
requestOriginParser);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ZuulFilter postFilter(SentinelZuulProperties sentinelZuulProperties) {
|
||||
return new SentinelPostFilter(sentinelZuulProperties);
|
||||
}
|
||||
@Bean
|
||||
public ZuulFilter postFilter(SentinelZuulProperties sentinelZuulProperties) {
|
||||
return new SentinelPostFilter(sentinelZuulProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ZuulFilter errorFilter(SentinelZuulProperties sentinelZuulProperties) {
|
||||
return new SentinelErrorFilter(sentinelZuulProperties);
|
||||
}
|
||||
@Bean
|
||||
public ZuulFilter errorFilter(SentinelZuulProperties sentinelZuulProperties) {
|
||||
return new SentinelErrorFilter(sentinelZuulProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FallBackProviderListener fallBackProviderListener(DefaultListableBeanFactory beanFactory) {
|
||||
return new FallBackProviderListener(beanFactory);
|
||||
}
|
||||
@Bean
|
||||
public FallBackProviderHandler fallBackProviderListener(
|
||||
DefaultListableBeanFactory beanFactory) {
|
||||
return new FallBackProviderHandler(beanFactory);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,45 @@
|
||||
package org.springframework.cloud.alibaba.sentinel.zuul.listener;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.DefaultBlockFallbackProvider;
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.ZuulBlockFallbackManager;
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.ZuulBlockFallbackProvider;
|
||||
|
||||
/**
|
||||
* @author tiger
|
||||
*/
|
||||
public class FallBackProviderHandler implements SmartInitializingSingleton {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(FallBackProviderHandler.class);
|
||||
|
||||
private final DefaultListableBeanFactory beanFactory;
|
||||
|
||||
public FallBackProviderHandler(DefaultListableBeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
Map<String, ZuulBlockFallbackProvider> providerMap = beanFactory
|
||||
.getBeansOfType(ZuulBlockFallbackProvider.class);
|
||||
if (MapUtils.isNotEmpty(providerMap)) {
|
||||
providerMap.forEach((k, v) -> {
|
||||
logger.info("[Sentinel Zuul] Register provider name:{}, instance: {}", k,
|
||||
v);
|
||||
ZuulBlockFallbackManager.registerProvider(v);
|
||||
});
|
||||
}
|
||||
else {
|
||||
logger.info("[Sentinel Zuul] Register default fallback provider. ");
|
||||
ZuulBlockFallbackManager.registerProvider(new DefaultBlockFallbackProvider());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package org.springframework.cloud.alibaba.sentinel.zuul.listener;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.DefaultBlockFallbackProvider;
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.ZuulBlockFallbackManager;
|
||||
import com.alibaba.csp.sentinel.adapter.zuul.fallback.ZuulBlockFallbackProvider;
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author tiger
|
||||
*/
|
||||
public class FallBackProviderListener implements SmartInitializingSingleton {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(FallBackProviderListener.class);
|
||||
|
||||
private final DefaultListableBeanFactory beanFactory;
|
||||
|
||||
public FallBackProviderListener(DefaultListableBeanFactory beanFactory) {
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
Map<String, ZuulBlockFallbackProvider> providerMap = beanFactory.getBeansOfType(ZuulBlockFallbackProvider.class);
|
||||
if (MapUtils.isNotEmpty(providerMap)) {
|
||||
providerMap.forEach((k, v) -> {
|
||||
logger.info("[Sentinel] Register provider name:{}, instance: {}", k, v);
|
||||
ZuulBlockFallbackManager.registerProvider(v);
|
||||
});
|
||||
} else {
|
||||
logger.info("[Sentinel] Register default fallback provider. ");
|
||||
ZuulBlockFallbackManager.registerProvider(new DefaultBlockFallbackProvider());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user