1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

sync sentinel zuul refactor in edgware

This commit is contained in:
fangjian0423
2019-05-08 20:28:25 +08:00
parent 6671dd0d4d
commit 6aa80645bf
25 changed files with 755 additions and 215 deletions

View File

@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-alibaba</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>0.1.3.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
<name>Spring Cloud Alibaba Sentinel Gateway</name>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-zuul-adapter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,117 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.alibaba.sentinel.zuul;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.alibaba.sentinel.zuul.handler.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.gateway.zuul.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.callback.ZuulGatewayCallbackManager;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulErrorFilter;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPostFilter;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.filters.SentinelZuulPreFilter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.http.ZuulServlet;
/**
* Sentinel Spring Cloud Zuul AutoConfiguration
*
* @author tiger
*/
@Configuration
@ConditionalOnClass(ZuulServlet.class)
@ConditionalOnProperty(prefix = SentinelZuulAutoConfiguration.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class SentinelZuulAutoConfiguration {
private static final Logger logger = LoggerFactory
.getLogger(SentinelZuulAutoConfiguration.class);
public static final String PREFIX = "spring.cloud.sentinel.zuul";
@Autowired
private Environment environment;
@Autowired(required = false)
private RequestOriginParser requestOriginParser;
@PostConstruct
private void init() {
if (requestOriginParser != null) {
ZuulGatewayCallbackManager.setOriginParser(requestOriginParser);
}
}
@Bean
public ZuulFilter sentinelZuulPreFilter() {
String preOrderStr = environment.getProperty(PREFIX + "." + "order.pre");
int order = 10000;
try {
order = Integer.parseInt(preOrderStr);
}
catch (NumberFormatException e) {
// ignore
}
logger.info("[Sentinel Zuul] register SentinelZuulPreFilter {}", order);
return new SentinelZuulPreFilter(order);
}
@Bean
public ZuulFilter sentinelZuulPostFilter() {
String postOrderStr = environment.getProperty(PREFIX + "." + "order.post");
int order = 1000;
try {
order = Integer.parseInt(postOrderStr);
}
catch (NumberFormatException e) {
// ignore
}
logger.info("[Sentinel Zuul] register SentinelZuulPostFilter {}", order);
return new SentinelZuulPostFilter(order);
}
@Bean
public ZuulFilter sentinelZuulErrorFilter() {
String errorOrderStr = environment.getProperty(PREFIX + "." + "order.error");
int order = -1;
try {
order = Integer.parseInt(errorOrderStr);
}
catch (NumberFormatException e) {
// ignore
}
logger.info("[Sentinel Zuul] register SentinelZuulErrorFilter {}", order);
return new SentinelZuulErrorFilter(order);
}
@Bean
public FallBackProviderHandler fallBackProviderHandler(
DefaultListableBeanFactory beanFactory) {
return new FallBackProviderHandler(beanFactory);
}
}

View File

@@ -0,0 +1,45 @@
package org.springframework.cloud.alibaba.sentinel.zuul.handler;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.util.CollectionUtils;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.DefaultBlockFallbackProvider;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager;
import com.alibaba.csp.sentinel.adapter.gateway.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 (!CollectionUtils.isEmpty(providerMap)) {
for (String k : providerMap.keySet()) {
logger.info("[Sentinel Zuul] Register provider name:{}, instance: {}", k,
providerMap.get(k));
ZuulBlockFallbackManager.registerProvider(providerMap.get(k));
}
}
else {
logger.info("[Sentinel Zuul] Register default fallback provider. ");
ZuulBlockFallbackManager.registerProvider(new DefaultBlockFallbackProvider());
}
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.alibaba.sentinel.zuul.SentinelZuulAutoConfiguration