mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Fixes #43 add rules automation
This commit is contained in:
parent
4829157c09
commit
568da97adf
@ -192,9 +192,9 @@ Sentinel 控制台支持实时监控查看,您可以通过 Sentinel 控制台
|
|||||||
|
|
||||||
<p align="center"><img src="https://cdn.nlark.com/lark/0/2018/png/54319/1532313595369-8428cd7d-9eb7-4786-a149-acf0da4a2daf.png" width="480" heigh='180' ></p>
|
<p align="center"><img src="https://cdn.nlark.com/lark/0/2018/png/54319/1532313595369-8428cd7d-9eb7-4786-a149-acf0da4a2daf.png" width="480" heigh='180' ></p>
|
||||||
|
|
||||||
## DataSource 支持
|
## ReadableDataSource 支持
|
||||||
|
|
||||||
Sentinel 内部提供了[动态规则的扩展实现 DataSource](https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95#datasource-%E6%89%A9%E5%B1%95)。
|
Sentinel 内部提供了[动态规则的扩展实现 ReadableDataSource](https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95#datasource-%E6%89%A9%E5%B1%95)。
|
||||||
|
|
||||||
Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置文件中进行相关配置,即可在 Spring 容器中自动注册 DataSource。
|
Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置文件中进行相关配置,即可在 Spring 容器中自动注册 DataSource。
|
||||||
|
|
||||||
@ -210,7 +210,7 @@ Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置
|
|||||||
然后使用`@SentinelDataSource` 注解修饰 DataSource 即可注入:
|
然后使用`@SentinelDataSource` 注解修饰 DataSource 即可注入:
|
||||||
|
|
||||||
@SentinelDataSource("spring.cloud.sentinel.datasource")
|
@SentinelDataSource("spring.cloud.sentinel.datasource")
|
||||||
private DataSource dataSource;
|
private ReadableDataSource dataSource;
|
||||||
|
|
||||||
`@SentinelDataSource` 注解的 value 属性可以不填。默认值就是 `spring.cloud.sentinel.datasource`。
|
`@SentinelDataSource` 注解的 value 属性可以不填。默认值就是 `spring.cloud.sentinel.datasource`。
|
||||||
|
|
||||||
@ -230,7 +230,7 @@ type目前支持file, nacos, zk, apollo。
|
|||||||
|
|
||||||
1. 定义DataSource
|
1. 定义DataSource
|
||||||
|
|
||||||
public class CustomDataSource implements DataSource {
|
public class CustomDataSource implements ReadableDataSource {
|
||||||
private String fieldA;
|
private String fieldA;
|
||||||
private String fieldB;
|
private String fieldB;
|
||||||
...
|
...
|
||||||
|
@ -165,9 +165,9 @@ To see the metrics, click **实时监控(Real-time Monitoring)** in the left-sid
|
|||||||
|
|
||||||
<p align="center"><img src="https://cdn.nlark.com/lark/0/2018/png/54319/1532313595369-8428cd7d-9eb7-4786-a149-acf0da4a2daf.png" width="480" heigh='180'></p>
|
<p align="center"><img src="https://cdn.nlark.com/lark/0/2018/png/54319/1532313595369-8428cd7d-9eb7-4786-a149-acf0da4a2daf.png" width="480" heigh='180'></p>
|
||||||
|
|
||||||
## DataSource
|
## ReadableDataSource
|
||||||
|
|
||||||
Sentinel provide [DataSource](https://github.com/alibaba/Sentinel/blob/master/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/DataSource.java) to manage dynamic rules.
|
Sentinel provide [ReadableDataSource](https://github.com/alibaba/Sentinel/blob/master/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/ReadableDataSource.java) to manage dynamic rules.
|
||||||
|
|
||||||
Sentinel starter integrated 4 DataSources provided by Sentinel. It will be register into Spring Context if you write some configs in `application.properties`.
|
Sentinel starter integrated 4 DataSources provided by Sentinel. It will be register into Spring Context if you write some configs in `application.properties`.
|
||||||
|
|
||||||
@ -183,7 +183,7 @@ If you want to define FileRefreshableDataSource:
|
|||||||
then use `@SentinelDataSource` to annotate DataSource:
|
then use `@SentinelDataSource` to annotate DataSource:
|
||||||
|
|
||||||
@SentinelDataSource("spring.cloud.sentinel.datasource")
|
@SentinelDataSource("spring.cloud.sentinel.datasource")
|
||||||
private DataSource dataSource;
|
private ReadableDataSource dataSource;
|
||||||
|
|
||||||
The value() of `@SentinelDataSource` is not required, it means the prefix of configuration. Default value is `spring.cloud.sentinel.datasource`.
|
The value() of `@SentinelDataSource` is not required, it means the prefix of configuration. Default value is `spring.cloud.sentinel.datasource`.
|
||||||
|
|
||||||
@ -201,7 +201,7 @@ User-defined DataSource need 2 steps.
|
|||||||
|
|
||||||
1. Define DataSource
|
1. Define DataSource
|
||||||
|
|
||||||
public class CustomDataSource implements DataSource {
|
public class CustomDataSource implements ReadableDataSource {
|
||||||
private String fieldA;
|
private String fieldA;
|
||||||
private String fieldB;
|
private String fieldB;
|
||||||
...
|
...
|
||||||
|
@ -36,15 +36,27 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
|||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
|
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||||
import org.springframework.cloud.alibaba.sentinel.SentinelConstants;
|
import org.springframework.cloud.alibaba.sentinel.SentinelConstants;
|
||||||
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelDataSource;
|
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelDataSource;
|
||||||
import org.springframework.cloud.alibaba.sentinel.util.PropertySourcesUtils;
|
import org.springframework.cloud.alibaba.sentinel.util.PropertySourcesUtils;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.event.EventListener;
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||||
|
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
||||||
|
import com.alibaba.csp.sentinel.property.SentinelProperty;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
|
||||||
|
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
|
||||||
|
import com.alibaba.csp.sentinel.slots.system.SystemRule;
|
||||||
|
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link SentinelDataSource @SentinelDataSource} Post Processor
|
* {@link SentinelDataSource @SentinelDataSource} Post Processor
|
||||||
@ -187,6 +199,41 @@ public class SentinelDataSourcePostProcessor
|
|||||||
beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition());
|
beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@EventListener(classes = ApplicationStartedEvent.class)
|
||||||
|
public void appStartedListener(ApplicationStartedEvent event) throws Exception {
|
||||||
|
Map<String, ReadableDataSource> dataSourceMap = event.getApplicationContext().getBeansOfType(ReadableDataSource.class);
|
||||||
|
if(dataSourceMap.size() == 1) {
|
||||||
|
ReadableDataSource dataSource = dataSourceMap.values().iterator().next();
|
||||||
|
Object ruleConfig = dataSource.loadConfig();
|
||||||
|
SentinelProperty sentinelProperty = dataSource.getProperty();
|
||||||
|
if(checkRuleType(ruleConfig, FlowRule.class)) {
|
||||||
|
FlowRuleManager.register2Property(sentinelProperty);
|
||||||
|
}
|
||||||
|
if(checkRuleType(ruleConfig, DegradeRule.class)) {
|
||||||
|
DegradeRuleManager.register2Property(sentinelProperty);
|
||||||
|
}
|
||||||
|
if(checkRuleType(ruleConfig, SystemRule.class)) {
|
||||||
|
SystemRuleManager.register2Property(sentinelProperty);
|
||||||
|
}
|
||||||
|
if(checkRuleType(ruleConfig, AuthorityRule.class)) {
|
||||||
|
AuthorityRuleManager.register2Property(sentinelProperty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkRuleType(Object ruleConfig, Class type) {
|
||||||
|
if(ruleConfig.getClass() == type) {
|
||||||
|
return true;
|
||||||
|
} else if(ruleConfig instanceof List) {
|
||||||
|
List ruleList = (List)ruleConfig;
|
||||||
|
if(ruleList.stream().filter(rule -> rule.getClass() == type).toArray().length == ruleList.size()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
class SentinelDataSourceField {
|
class SentinelDataSourceField {
|
||||||
private SentinelDataSource sentinelDataSource;
|
private SentinelDataSource sentinelDataSource;
|
||||||
private Field field;
|
private Field field;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user