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

edgware sync sentinel -> upgrade to 1.6.2 and integrate gateway

This commit is contained in:
fangjian0423 2019-06-25 19:16:47 +08:00
parent cf88032925
commit 865095df66
11 changed files with 223 additions and 110 deletions

View File

@ -16,7 +16,7 @@
<description>Spring Cloud Alibaba Dependencies</description>
<properties>
<sentinel.version>1.6.1</sentinel.version>
<sentinel.version>1.6.2</sentinel.version>
<oss.version>3.1.0</oss.version>
<seata.version>0.5.1</seata.version>
<nacos.version>1.0.1</nacos.version>

View File

@ -23,6 +23,12 @@ public class TestController {
return "Hello";
}
@RequestMapping(value = "/aa", method = RequestMethod.GET)
@SentinelResource("aa")
public String aa(int b, int a) {
return "Hello test";
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test1() {
return "Hello test";

View File

@ -1,6 +1,6 @@
[
{
"resource": "resource",
"resource": "/hello",
"controlBehavior": 0,
"count": 1,
"grade": 1,
@ -8,9 +8,9 @@
"strategy": 0
},
{
"resource": "p",
"resource": "/test",
"controlBehavior": 0,
"count": 1,
"count": 0,
"grade": 1,
"limitApp": "default",
"strategy": 0

View File

@ -1,6 +1,6 @@
[
{
"resource": "hotResource",
"resource": "aa",
"count": 0,
"grade": 1,
"limitApp": "default",

View File

@ -19,8 +19,6 @@ package org.springframework.cloud.alibaba.sentinel.datasource;
import org.springframework.cloud.alibaba.sentinel.datasource.config.AbstractDataSourceProperties;
import org.springframework.util.StringUtils;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.slots.block.AbstractRule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
@ -59,11 +57,13 @@ public enum RuleType {
/**
* gateway flow
*/
GW_FLOW("gw-flow", GatewayFlowRule.class),
GW_FLOW("gw-flow",
"com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule"),
/**
* api
*/
GW_API_GROUP("gw-api-group", ApiDefinition.class);
GW_API_GROUP("gw-api-group",
"com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition");
/**
* alias for {@link AbstractRule}
@ -73,19 +73,39 @@ public enum RuleType {
/**
* concrete {@link AbstractRule} class
*/
private final Class clazz;
private Class clazz;
/**
* concrete {@link AbstractRule} class name
*/
private String clazzName;
RuleType(String name, Class clazz) {
this.name = name;
this.clazz = clazz;
}
RuleType(String name, String clazzName) {
this.name = name;
this.clazzName = clazzName;
}
public String getName() {
return name;
}
public Class getClazz() {
return clazz;
if (clazz != null) {
return clazz;
}
else {
try {
return Class.forName(clazzName);
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
public static RuleType getByName(String name) {

View File

@ -27,8 +27,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.cloud.alibaba.sentinel.datasource.RuleType;
import org.springframework.util.StringUtils;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
@ -72,11 +70,13 @@ public abstract class SentinelConverter<T extends Object>
Collection<Object> ruleCollection;
// hard code
if (ruleClass == GatewayFlowRule.class || ruleClass == ApiDefinition.class) {
ruleCollection = new HashSet<>();
if (ruleClass == FlowRule.class || ruleClass == DegradeRule.class
|| ruleClass == SystemRule.class || ruleClass == AuthorityRule.class
|| ruleClass == ParamFlowRule.class) {
ruleCollection = new ArrayList<>();
}
else {
ruleCollection = new ArrayList<>();
ruleCollection = new HashSet<>();
}
if (StringUtils.isEmpty(source)) {

View File

@ -19,10 +19,27 @@
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-parameter-flow-control</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-zuul-adapter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

View File

@ -0,0 +1,161 @@
/*
* Copyright (C) 2018 the original author or authors.
*
* 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
*
* https://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 java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.alibaba.sentinel.datasource.converter.JsonConverter;
import org.springframework.cloud.alibaba.sentinel.datasource.converter.XmlConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateGroupItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
/**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/
@Configuration
@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
public class SentinelGatewayAutoConfiguration {
@ConditionalOnClass(ObjectMapper.class)
@Configuration
protected static class SentinelConverterConfiguration {
static class ApiPredicateItemDeserializer
extends StdDeserializer<ApiPredicateItem> {
private Map<String, Class<? extends ApiPredicateItem>> registry = new HashMap<String, Class<? extends ApiPredicateItem>>();
ApiPredicateItemDeserializer() {
super(ApiPredicateItem.class);
}
void registerApiPredicateItem(String uniqueAttribute,
Class<? extends ApiPredicateItem> apiPredicateItemClass) {
registry.put(uniqueAttribute, apiPredicateItemClass);
}
@Override
public ApiPredicateItem deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode root = mapper.readTree(jp);
Class<? extends ApiPredicateItem> apiPredicateItemClass = null;
Iterator<Entry<String, JsonNode>> elementsIterator = root.fields();
while (elementsIterator.hasNext()) {
Entry<String, JsonNode> element = elementsIterator.next();
String name = element.getKey();
if (registry.containsKey(name)) {
apiPredicateItemClass = registry.get(name);
break;
}
}
if (apiPredicateItemClass == null) {
return null;
}
return mapper.readValue(root.toString(), apiPredicateItemClass);
}
}
@Configuration
protected static class SentinelJsonConfiguration {
private ObjectMapper objectMapper = new ObjectMapper();
public SentinelJsonConfiguration() {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer();
deserializer.registerApiPredicateItem("pattern",
ApiPathPredicateItem.class);
deserializer.registerApiPredicateItem("items",
ApiPredicateGroupItem.class);
SimpleModule module = new SimpleModule(
"PolymorphicApiPredicateItemDeserializerModule",
new Version(1, 0, 0, null));
module.addDeserializer(ApiPredicateItem.class, deserializer);
objectMapper.registerModule(module);
}
@Bean("sentinel-json-gw-flow-converter")
public JsonConverter jsonGatewayFlowConverter() {
return new JsonConverter(objectMapper, GatewayFlowRule.class);
}
@Bean("sentinel-json-gw-api-group-converter")
public JsonConverter jsonApiConverter() {
return new JsonConverter(objectMapper, ApiDefinition.class);
}
}
@ConditionalOnClass(XmlMapper.class)
@Configuration
protected static class SentinelXmlConfiguration {
private XmlMapper xmlMapper = new XmlMapper();
public SentinelXmlConfiguration() {
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer();
deserializer.registerApiPredicateItem("pattern",
ApiPathPredicateItem.class);
deserializer.registerApiPredicateItem("items",
ApiPredicateGroupItem.class);
SimpleModule module = new SimpleModule(
"PolymorphicGatewayDeserializerModule",
new Version(1, 0, 0, null));
module.addDeserializer(ApiPredicateItem.class, deserializer);
xmlMapper.registerModule(module);
}
@Bean("sentinel-xml-gw-flow-converter")
public XmlConverter xmlGatewayFlowConverter() {
return new XmlConverter(xmlMapper, GatewayFlowRule.class);
}
@Bean("sentinel-xml-gw-api-group-converter")
public XmlConverter xmlApiConverter() {
return new XmlConverter(xmlMapper, ApiDefinition.class);
}
}
}
}

View File

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

View File

@ -84,6 +84,7 @@
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
<optional>true</optional>
</dependency>
<!--spring boot-->

View File

@ -16,12 +16,6 @@
package org.springframework.cloud.alibaba.sentinel.custom;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
@ -40,11 +34,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateGroupItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
@ -62,15 +51,8 @@ import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.util.AppNameUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
/**
@ -203,41 +185,6 @@ public class SentinelAutoConfiguration {
@Configuration
protected static class SentinelConverterConfiguration {
static class ApiPredicateItemDeserializer
extends StdDeserializer<ApiPredicateItem> {
private Map<String, Class<? extends ApiPredicateItem>> registry = new HashMap<String, Class<? extends ApiPredicateItem>>();
ApiPredicateItemDeserializer() {
super(ApiPredicateItem.class);
}
void registerApiPredicateItem(String uniqueAttribute,
Class<? extends ApiPredicateItem> apiPredicateItemClass) {
registry.put(uniqueAttribute, apiPredicateItemClass);
}
@Override
public ApiPredicateItem deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode root = mapper.readTree(jp);
Class<? extends ApiPredicateItem> apiPredicateItemClass = null;
Iterator<Entry<String, JsonNode>> elementsIterator = root.fields();
while (elementsIterator.hasNext()) {
Entry<String, JsonNode> element = elementsIterator.next();
String name = element.getKey();
if (registry.containsKey(name)) {
apiPredicateItemClass = registry.get(name);
break;
}
}
if (apiPredicateItemClass == null) {
return null;
}
return mapper.readValue(root.toString(), apiPredicateItemClass);
}
}
@Configuration
protected static class SentinelJsonConfiguration {
@ -246,17 +193,6 @@ public class SentinelAutoConfiguration {
public SentinelJsonConfiguration() {
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer();
deserializer.registerApiPredicateItem("pattern",
ApiPathPredicateItem.class);
deserializer.registerApiPredicateItem("items",
ApiPredicateGroupItem.class);
SimpleModule module = new SimpleModule(
"PolymorphicApiPredicateItemDeserializerModule",
new Version(1, 0, 0, null));
module.addDeserializer(ApiPredicateItem.class, deserializer);
objectMapper.registerModule(module);
}
@Bean("sentinel-json-flow-converter")
@ -284,15 +220,6 @@ public class SentinelAutoConfiguration {
return new JsonConverter(objectMapper, ParamFlowRule.class);
}
@Bean("sentinel-json-gw-flow-converter")
public JsonConverter jsonGatewayFlowConverter() {
return new JsonConverter(objectMapper, GatewayFlowRule.class);
}
@Bean("sentinel-json-gw-api-group-converter")
public JsonConverter jsonApiConverter() {
return new JsonConverter(objectMapper, ApiDefinition.class);
}
}
@ConditionalOnClass(XmlMapper.class)
@ -304,16 +231,6 @@ public class SentinelAutoConfiguration {
public SentinelXmlConfiguration() {
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
false);
ApiPredicateItemDeserializer deserializer = new ApiPredicateItemDeserializer();
deserializer.registerApiPredicateItem("pattern",
ApiPathPredicateItem.class);
deserializer.registerApiPredicateItem("items",
ApiPredicateGroupItem.class);
SimpleModule module = new SimpleModule(
"PolymorphicGatewayDeserializerModule",
new Version(1, 0, 0, null));
module.addDeserializer(ApiPredicateItem.class, deserializer);
xmlMapper.registerModule(module);
}
@Bean("sentinel-xml-flow-converter")
@ -341,16 +258,6 @@ public class SentinelAutoConfiguration {
return new XmlConverter(xmlMapper, ParamFlowRule.class);
}
@Bean("sentinel-xml-gw-flow-converter")
public XmlConverter xmlGatewayFlowConverter() {
return new XmlConverter(xmlMapper, GatewayFlowRule.class);
}
@Bean("sentinel-xml-gw-api-group-converter")
public XmlConverter xmlApiConverter() {
return new XmlConverter(xmlMapper, ApiDefinition.class);
}
}
}