mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
update comments and docs
This commit is contained in:
parent
7fdd8fa3e2
commit
4829157c09
@ -204,7 +204,7 @@ Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置
|
|||||||
spring.cloud.sentinel.datasource.recommendRefreshMs=2000
|
spring.cloud.sentinel.datasource.recommendRefreshMs=2000
|
||||||
spring.cloud.sentinel.datasource.bufSize=2048
|
spring.cloud.sentinel.datasource.bufSize=2048
|
||||||
spring.cloud.sentinel.datasource.charset=utf-8
|
spring.cloud.sentinel.datasource.charset=utf-8
|
||||||
spring.cloud.sentinel.datasource.configParser=myParser
|
spring.cloud.sentinel.datasource.converter=myParser
|
||||||
spring.cloud.sentinel.datasource.file=/Users/you/rule.json
|
spring.cloud.sentinel.datasource.file=/Users/you/rule.json
|
||||||
|
|
||||||
然后使用`@SentinelDataSource` 注解修饰 DataSource 即可注入:
|
然后使用`@SentinelDataSource` 注解修饰 DataSource 即可注入:
|
||||||
@ -220,7 +220,7 @@ Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置
|
|||||||
|
|
||||||
`spring.cloud.sentinel.datasource.recommendRefreshMs` 里的 `recommendRefreshMs` 对应相关 DataSource 的属性。
|
`spring.cloud.sentinel.datasource.recommendRefreshMs` 里的 `recommendRefreshMs` 对应相关 DataSource 的属性。
|
||||||
|
|
||||||
`spring.cloud.sentinel.datasource.configParser`代表 `ConfigParser` 在 Spring 容器里的 name。如果没找到,会抛出异常。
|
`spring.cloud.sentinel.datasource.converter`代表 `Converter` 在 Spring 容器里的 name。如果没找到,会抛出异常。
|
||||||
|
|
||||||
type目前支持file, nacos, zk, apollo。
|
type目前支持file, nacos, zk, apollo。
|
||||||
|
|
||||||
@ -260,7 +260,7 @@ type目前支持file, nacos, zk, apollo。
|
|||||||
spring.cloud.sentinel.datasource.fieldA = valueA
|
spring.cloud.sentinel.datasource.fieldA = valueA
|
||||||
spring.cloud.sentinel.datasource.fieldB = valueB
|
spring.cloud.sentinel.datasource.fieldB = valueB
|
||||||
|
|
||||||
注意:由于目前Sentinel的AbstractDataSource需要有个ConfigParser作为构造函数中的参数,并且它的子类的构造都是通过多个参数的构造函数构造的。
|
注意:由于目前Sentinel的AbstractDataSource需要有个Converter作为构造函数中的参数,并且它的子类的构造都是通过多个参数的构造函数构造的。
|
||||||
所以目前所有的Sentinel starter中的DataSource都是基于FactoryBean并且通过设置属性构造的。如果有这方面的需求,需要再多加一个registerFactoryBean过程。
|
所以目前所有的Sentinel starter中的DataSource都是基于FactoryBean并且通过设置属性构造的。如果有这方面的需求,需要再多加一个registerFactoryBean过程。
|
||||||
|
|
||||||
SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class);
|
SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class);
|
||||||
|
@ -177,7 +177,7 @@ If you want to define FileRefreshableDataSource:
|
|||||||
spring.cloud.sentinel.datasource.recommendRefreshMs=2000
|
spring.cloud.sentinel.datasource.recommendRefreshMs=2000
|
||||||
spring.cloud.sentinel.datasource.bufSize=2048
|
spring.cloud.sentinel.datasource.bufSize=2048
|
||||||
spring.cloud.sentinel.datasource.charset=utf-8
|
spring.cloud.sentinel.datasource.charset=utf-8
|
||||||
spring.cloud.sentinel.datasource.configParser=myParser
|
spring.cloud.sentinel.datasource.converter=myParser
|
||||||
spring.cloud.sentinel.datasource.file=/Users/you/rule.json
|
spring.cloud.sentinel.datasource.file=/Users/you/rule.json
|
||||||
|
|
||||||
then use `@SentinelDataSource` to annotate DataSource:
|
then use `@SentinelDataSource` to annotate DataSource:
|
||||||
@ -191,7 +191,7 @@ spring.cloud.sentinel.datasource.type means the type of DataSource.
|
|||||||
|
|
||||||
spring.cloud.sentinel.datasource.recommendRefreshMs means the recommendRefreshMs property of specified DataSource.
|
spring.cloud.sentinel.datasource.recommendRefreshMs means the recommendRefreshMs property of specified DataSource.
|
||||||
|
|
||||||
spring.cloud.sentinel.datasource.configParser means the name of spring bean that type is ConfigParser. If the bean is not exists, will throw exception.
|
spring.cloud.sentinel.datasource.converter means the name of spring bean that type is Converter. If the bean is not exists, will throw exception.
|
||||||
|
|
||||||
Now datasource type support 4 categories: file, nacos, zk, apollo.
|
Now datasource type support 4 categories: file, nacos, zk, apollo.
|
||||||
|
|
||||||
@ -230,7 +230,7 @@ User-defined DataSource need 2 steps.
|
|||||||
spring.cloud.sentinel.datasource.fieldA = valueA
|
spring.cloud.sentinel.datasource.fieldA = valueA
|
||||||
spring.cloud.sentinel.datasource.fieldB = valueB
|
spring.cloud.sentinel.datasource.fieldB = valueB
|
||||||
|
|
||||||
Note: The AbstractDataSource of Sentinel need a ConfigParser as a constructor param and the subclass of AbstractDataSource was construct by multi-param constructor.
|
Note: The AbstractDataSource of Sentinel need a Converter as a constructor param and the subclass of AbstractDataSource was construct by multi-param constructor.
|
||||||
Now All DataSources in starter was construct by FactoryBean. If you want to do it in this way, you should register FactoryBean by SentinelDataSourceRegistry.
|
Now All DataSources in starter was construct by FactoryBean. If you want to do it in this way, you should register FactoryBean by SentinelDataSourceRegistry.
|
||||||
|
|
||||||
SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class);
|
SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class);
|
||||||
|
@ -16,28 +16,34 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.sentinel.annotation;
|
package org.springframework.cloud.alibaba.sentinel.annotation;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
||||||
|
|
||||||
import org.springframework.core.annotation.AliasFor;
|
import org.springframework.core.annotation.AliasFor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An annotation to inject {@link com.alibaba.csp.sentinel.datasource.DataSource} instance
|
* An annotation to inject {@link ReadableDataSource} instance
|
||||||
* into a Spring Bean. The Properties of DataSource bean get from config files with
|
* into a Spring Bean. The Properties of DataSource bean get from config files with
|
||||||
* specific prefix.
|
* specific prefix.
|
||||||
*
|
*
|
||||||
* @author fangjian
|
* @author fangjian
|
||||||
* @see com.alibaba.csp.sentinel.datasource.DataSource
|
* @see ReadableDataSource
|
||||||
*/
|
*/
|
||||||
@Target({ ElementType.FIELD })
|
@Target({ElementType.FIELD})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
public @interface SentinelDataSource {
|
public @interface SentinelDataSource {
|
||||||
|
|
||||||
@AliasFor("prefix")
|
@AliasFor("prefix")
|
||||||
String value() default "";
|
String value() default "";
|
||||||
|
|
||||||
@AliasFor("value")
|
@AliasFor("value")
|
||||||
String prefix() default "";
|
String prefix() default "";
|
||||||
|
|
||||||
String name() default ""; // spring bean name
|
String name() default ""; // spring bean name
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@ package org.springframework.cloud.alibaba.sentinel.datasource;
|
|||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
|
||||||
|
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean;
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean;
|
||||||
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean;
|
import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean;
|
||||||
@ -28,7 +30,7 @@ import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.Zookeep
|
|||||||
* Registry to save DataSource FactoryBean
|
* Registry to save DataSource FactoryBean
|
||||||
*
|
*
|
||||||
* @author fangjian
|
* @author fangjian
|
||||||
* @see com.alibaba.csp.sentinel.datasource.DataSource
|
* @see ReadableDataSource
|
||||||
* @see FileRefreshableDataSourceFactoryBean
|
* @see FileRefreshableDataSourceFactoryBean
|
||||||
* @see ZookeeperDataSourceFactoryBean
|
* @see ZookeeperDataSourceFactoryBean
|
||||||
* @see NacosDataSourceFactoryBean
|
* @see NacosDataSourceFactoryBean
|
||||||
@ -36,32 +38,32 @@ import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.Zookeep
|
|||||||
*/
|
*/
|
||||||
public class SentinelDataSourceRegistry {
|
public class SentinelDataSourceRegistry {
|
||||||
|
|
||||||
private static ConcurrentHashMap<String, Class<? extends FactoryBean>> cache = new ConcurrentHashMap<>(
|
private static ConcurrentHashMap<String, Class<? extends FactoryBean>> cache = new ConcurrentHashMap<>(
|
||||||
32);
|
32);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
SentinelDataSourceRegistry.registerFactoryBean("file",
|
SentinelDataSourceRegistry.registerFactoryBean("file",
|
||||||
FileRefreshableDataSourceFactoryBean.class);
|
FileRefreshableDataSourceFactoryBean.class);
|
||||||
SentinelDataSourceRegistry.registerFactoryBean("zk",
|
SentinelDataSourceRegistry.registerFactoryBean("zk",
|
||||||
ZookeeperDataSourceFactoryBean.class);
|
ZookeeperDataSourceFactoryBean.class);
|
||||||
SentinelDataSourceRegistry.registerFactoryBean("nacos",
|
SentinelDataSourceRegistry.registerFactoryBean("nacos",
|
||||||
NacosDataSourceFactoryBean.class);
|
NacosDataSourceFactoryBean.class);
|
||||||
SentinelDataSourceRegistry.registerFactoryBean("apollo",
|
SentinelDataSourceRegistry.registerFactoryBean("apollo",
|
||||||
ApolloDataSourceFactoryBean.class);
|
ApolloDataSourceFactoryBean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void registerFactoryBean(String alias,
|
public static synchronized void registerFactoryBean(String alias,
|
||||||
Class<? extends FactoryBean> clazz) {
|
Class<? extends FactoryBean> clazz) {
|
||||||
cache.putIfAbsent(alias, clazz);
|
cache.putIfAbsent(alias, clazz);
|
||||||
cache.put(alias, clazz);
|
cache.put(alias, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<? extends FactoryBean> getFactoryBean(String alias) {
|
public static Class<? extends FactoryBean> getFactoryBean(String alias) {
|
||||||
return cache.get(alias);
|
return cache.get(alias);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean checkFactoryBean(String alias) {
|
public static boolean checkFactoryBean(String alias) {
|
||||||
return cache.containsKey(alias);
|
return cache.containsKey(alias);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user