From 4829157c09ada4aed3db095eebc4c88002b70019 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 16 Oct 2018 16:39:47 +0800 Subject: [PATCH] update comments and docs --- .../sentinel-core-example/readme-zh.md | 6 +-- .../sentinel-core-example/readme.md | 6 +-- .../annotation/SentinelDataSource.java | 22 +++++--- .../SentinelDataSourceRegistry.java | 50 ++++++++++--------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md index a4b4d87ac..47b4a51dc 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md @@ -204,7 +204,7 @@ Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置 spring.cloud.sentinel.datasource.recommendRefreshMs=2000 spring.cloud.sentinel.datasource.bufSize=2048 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 然后使用`@SentinelDataSource` 注解修饰 DataSource 即可注入: @@ -220,7 +220,7 @@ Sentinel starter 整合了目前存在的几类 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。 @@ -260,7 +260,7 @@ type目前支持file, nacos, zk, apollo。 spring.cloud.sentinel.datasource.fieldA = valueA spring.cloud.sentinel.datasource.fieldB = valueB - 注意:由于目前Sentinel的AbstractDataSource需要有个ConfigParser作为构造函数中的参数,并且它的子类的构造都是通过多个参数的构造函数构造的。 + 注意:由于目前Sentinel的AbstractDataSource需要有个Converter作为构造函数中的参数,并且它的子类的构造都是通过多个参数的构造函数构造的。 所以目前所有的Sentinel starter中的DataSource都是基于FactoryBean并且通过设置属性构造的。如果有这方面的需求,需要再多加一个registerFactoryBean过程。 SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class); diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md index daaa3e8fb..8d838ebcd 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md @@ -177,7 +177,7 @@ If you want to define FileRefreshableDataSource: spring.cloud.sentinel.datasource.recommendRefreshMs=2000 spring.cloud.sentinel.datasource.bufSize=2048 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 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.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. @@ -230,7 +230,7 @@ User-defined DataSource need 2 steps. spring.cloud.sentinel.datasource.fieldA = valueA 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. SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class); diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java index ef09eb0ef..93ef1bdb9 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java @@ -16,28 +16,34 @@ 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; /** - * 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 * specific prefix. * * @author fangjian - * @see com.alibaba.csp.sentinel.datasource.DataSource + * @see ReadableDataSource */ -@Target({ ElementType.FIELD }) +@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface SentinelDataSource { - @AliasFor("prefix") - String value() default ""; + @AliasFor("prefix") + String value() default ""; - @AliasFor("value") - String prefix() default ""; + @AliasFor("value") + String prefix() default ""; String name() default ""; // spring bean name diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java index 9bd92eee5..ceb90ee1f 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java @@ -18,6 +18,8 @@ package org.springframework.cloud.alibaba.sentinel.datasource; import java.util.concurrent.ConcurrentHashMap; +import com.alibaba.csp.sentinel.datasource.ReadableDataSource; + import org.springframework.beans.factory.FactoryBean; import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean; 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 * * @author fangjian - * @see com.alibaba.csp.sentinel.datasource.DataSource + * @see ReadableDataSource * @see FileRefreshableDataSourceFactoryBean * @see ZookeeperDataSourceFactoryBean * @see NacosDataSourceFactoryBean @@ -36,32 +38,32 @@ import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.Zookeep */ public class SentinelDataSourceRegistry { - private static ConcurrentHashMap> cache = new ConcurrentHashMap<>( - 32); + private static ConcurrentHashMap> cache = new ConcurrentHashMap<>( + 32); - static { - SentinelDataSourceRegistry.registerFactoryBean("file", - FileRefreshableDataSourceFactoryBean.class); - SentinelDataSourceRegistry.registerFactoryBean("zk", - ZookeeperDataSourceFactoryBean.class); - SentinelDataSourceRegistry.registerFactoryBean("nacos", - NacosDataSourceFactoryBean.class); - SentinelDataSourceRegistry.registerFactoryBean("apollo", - ApolloDataSourceFactoryBean.class); - } + static { + SentinelDataSourceRegistry.registerFactoryBean("file", + FileRefreshableDataSourceFactoryBean.class); + SentinelDataSourceRegistry.registerFactoryBean("zk", + ZookeeperDataSourceFactoryBean.class); + SentinelDataSourceRegistry.registerFactoryBean("nacos", + NacosDataSourceFactoryBean.class); + SentinelDataSourceRegistry.registerFactoryBean("apollo", + ApolloDataSourceFactoryBean.class); + } - public static synchronized void registerFactoryBean(String alias, - Class clazz) { - cache.putIfAbsent(alias, clazz); - cache.put(alias, clazz); - } + public static synchronized void registerFactoryBean(String alias, + Class clazz) { + cache.putIfAbsent(alias, clazz); + cache.put(alias, clazz); + } - public static Class getFactoryBean(String alias) { - return cache.get(alias); - } + public static Class getFactoryBean(String alias) { + return cache.get(alias); + } - public static boolean checkFactoryBean(String alias) { - return cache.containsKey(alias); - } + public static boolean checkFactoryBean(String alias) { + return cache.containsKey(alias); + } }