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

优化代码

This commit is contained in:
xiejiashuai 2018-09-27 13:33:29 +08:00
parent 03a64885d4
commit 6794c350ef
3 changed files with 31 additions and 23 deletions

View File

@ -22,6 +22,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
/** /**
* @author xiaojing * @author xiaojing
* @author hengyunabc * @author hengyunabc
@ -31,44 +34,50 @@ import org.springframework.core.Ordered;
public class SentinelProperties { public class SentinelProperties {
/** /**
* 是否提前初始化心跳连接 * earlier initialize heart-beat when the spring container starts <note> when the
* transport dependency is on classpath ,the configuration is effective </note>
*/ */
private boolean eager = false; private boolean eager = false;
/** /**
* Enable sentinel auto configure, the default value is true * enable sentinel auto configure, the default value is true
*/ */
private boolean enabled = true; private boolean enabled = true;
/** /**
* 字符编码集 * charset when sentinel write or search metric file {@link SentinelConfig#CHARSET}
*/ */
private String charset = "UTF-8"; private String charset = "UTF-8";
/** /**
* 通信相关配置 * transport configuration about dashboard and client
*/ */
@NestedConfigurationProperty @NestedConfigurationProperty
private Transport transport = new Transport(); private Transport transport = new Transport();
/** /**
* 监控数据相关配置 * metric configuration about resource
*/ */
@NestedConfigurationProperty @NestedConfigurationProperty
private Metric metric = new Metric(); private Metric metric = new Metric();
/** /**
* web 相关配置 * web servlet configuration <note> when the application is web ,the configuration is
* effective </note>
*/ */
@NestedConfigurationProperty @NestedConfigurationProperty
private Servlet servlet = new Servlet(); private Servlet servlet = new Servlet();
/** /**
* 限流相关 * sentinel filter <note> when the application is web ,the configuration is effective
* </note>
*/ */
@NestedConfigurationProperty @NestedConfigurationProperty
private Filter filter = new Filter(); private Filter filter = new Filter();
/**
* flow configuration
*/
@NestedConfigurationProperty @NestedConfigurationProperty
private Flow flow = new Flow(); private Flow flow = new Flow();
@ -139,7 +148,7 @@ public class SentinelProperties {
public static class Flow { public static class Flow {
/** /**
* 限流冷启动因子 * the cold factor {@link SentinelConfig#COLD_FACTOR}
*/ */
private String coldFactor = "3"; private String coldFactor = "3";
@ -156,7 +165,7 @@ public class SentinelProperties {
public static class Servlet { public static class Servlet {
/** /**
* url 限流后的处理页面 * The process page when the flow control is triggered
*/ */
private String blockPage; private String blockPage;
@ -172,12 +181,12 @@ public class SentinelProperties {
public static class Metric { public static class Metric {
/** /**
* 监控数据写磁盘时单个文件的大小 * the metric file size {@link SentinelConfig#SINGLE_METRIC_FILE_SIZE}
*/ */
private String fileSingleSize; private String fileSingleSize;
/** /**
* 监控数据在磁盘上的总数量 * the total metric file count {@link SentinelConfig#TOTAL_METRIC_FILE_COUNT}
*/ */
private String fileTotalCount; private String fileTotalCount;
@ -201,18 +210,19 @@ public class SentinelProperties {
public static class Transport { public static class Transport {
/** /**
* sentinel api port,default value is 8721 * sentinel api port,default value is 8721 {@link TransportConfig#SERVER_PORT}
*/ */
private String port = "8721"; private String port = "8721";
/** /**
* Sentinel dashboard address, won't try to connect dashboard when address is * sentinel dashboard address, won't try to connect dashboard when address is
* empty * empty {@link TransportConfig#CONSOLE_SERVER}
*/ */
private String dashboard = ""; private String dashboard = "";
/** /**
* 客户端和DashBord心跳发送时间 * send heartbeat interval millisecond
* {@link TransportConfig#HEARTBEAT_INTERVAL_MS}
*/ */
private String heartbeatIntervalMs; private String heartbeatIntervalMs;
@ -245,7 +255,7 @@ public class SentinelProperties {
public static class Filter { public static class Filter {
/** /**
* Sentinel filter chain order. * sentinel filter chain order.
*/ */
private int order = Ordered.HIGHEST_PRECEDENCE; private int order = Ordered.HIGHEST_PRECEDENCE;

View File

@ -32,7 +32,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.alibaba.csp.sentinel.Env;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner; import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager; import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
@ -40,7 +39,6 @@ import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect; import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.config.SentinelConfig; import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.init.InitExecutor; import com.alibaba.csp.sentinel.init.InitExecutor;
import com.alibaba.csp.sentinel.node.NodeBuilder;
import com.alibaba.csp.sentinel.transport.config.TransportConfig; import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.util.AppNameUtil; import com.alibaba.csp.sentinel.util.AppNameUtil;
@ -68,7 +66,8 @@ public class SentinelAutoConfiguration {
@PostConstruct @PostConstruct
private void init() { private void init() {
if (StringUtils.isEmpty(System.getProperty(AppNameUtil.APP_NAME))) { if (StringUtils.isEmpty(System.getProperty(AppNameUtil.APP_NAME))
&& StringUtils.hasText(projectName)) {
System.setProperty(AppNameUtil.APP_NAME, projectName); System.setProperty(AppNameUtil.APP_NAME, projectName);
} }
if (StringUtils.isEmpty(System.getProperty(TransportConfig.SERVER_PORT)) if (StringUtils.isEmpty(System.getProperty(TransportConfig.SERVER_PORT))
@ -115,11 +114,9 @@ public class SentinelAutoConfiguration {
urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler); urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler);
urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner); urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner);
InitExecutor.doInit();
// earlier initialize // earlier initialize
if (properties.isEager()) { if (properties.isEager()) {
NodeBuilder nodeBuilder = Env.nodeBuilder; InitExecutor.doInit();
} }
} }

View File

@ -34,13 +34,14 @@ import com.alibaba.csp.sentinel.slots.block.BlockException;
/** /**
* @author fangjian * @author fangjian
* @author jiashuai.xie
*/ */
public class SentinelAutoConfigurationTests { public class SentinelAutoConfigurationTests {
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SentinelAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(SentinelAutoConfiguration.class,
SentinelWebAutoConfiguration.class, SentinelTestConfiguration.class)) SentinelWebAutoConfiguration.class, SentinelTestConfiguration.class))
.withPropertyValues("spring.cloud.sentinel.port=8888") .withPropertyValues("spring.cloud.sentinel.transport.port=8888")
.withPropertyValues("spring.cloud.sentinel.filter.order=123") .withPropertyValues("spring.cloud.sentinel.filter.order=123")
.withPropertyValues("spring.cloud.sentinel.filter.urlPatterns=/*,/test"); .withPropertyValues("spring.cloud.sentinel.filter.urlPatterns=/*,/test");