diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java
index eb1f2be77..bd7120957 100644
--- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java
+++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java
@@ -22,53 +22,119 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.core.Ordered;
+import com.alibaba.csp.sentinel.config.SentinelConfig;
+import com.alibaba.csp.sentinel.transport.config.TransportConfig;
+
/**
* @author xiaojing
* @author hengyunabc
+ * @author jiashuai.xie
*/
@ConfigurationProperties(prefix = SentinelConstants.PROPERTY_PREFIX)
public class SentinelProperties {
/**
- * Enable sentinel auto configure, the default value is true
+ * earlier initialize heart-beat when the spring container starts when the
+ * transport dependency is on classpath ,the configuration is effective
+ */
+ private boolean eager = false;
+
+ /**
+ * enable sentinel auto configure, the default value is true
*/
private boolean enabled = true;
/**
- * sentinel api port,default value is 8721
+ * charset when sentinel write or search metric file {@link SentinelConfig#CHARSET}
*/
- private String port = "8721";
+ private String charset = "UTF-8";
/**
- * Sentinel dashboard address, won't try to connect dashboard when address is empty
+ * transport configuration about dashboard and client
*/
- private String dashboard = "";
+ @NestedConfigurationProperty
+ private Transport transport = new Transport();
+ /**
+ * metric configuration about resource
+ */
@NestedConfigurationProperty
- private Filter filter;
+ private Metric metric = new Metric();
- public boolean isEnabled() {
- return enabled;
+ /**
+ * web servlet configuration when the application is web ,the configuration is
+ * effective
+ */
+ @NestedConfigurationProperty
+ private Servlet servlet = new Servlet();
+
+ /**
+ * sentinel filter when the application is web ,the configuration is effective
+ *
+ */
+ @NestedConfigurationProperty
+ private Filter filter = new Filter();
+
+ /**
+ * flow configuration
+ */
+ @NestedConfigurationProperty
+ private Flow flow = new Flow();
+
+ public boolean isEager() {
+ return eager;
}
- public void setEnabled(boolean enabled) {
- this.enabled = enabled;
+ public void setEager(boolean eager) {
+ this.eager = eager;
+ }
+
+ public Flow getFlow() {
+ return flow;
+ }
+
+ public void setFlow(Flow flow) {
+ this.flow = flow;
+ }
+
+ public String getCharset() {
+ return charset;
+ }
+
+ public void setCharset(String charset) {
+ this.charset = charset;
+ }
+
+ public Transport getTransport() {
+ return transport;
+ }
+
+ public void setTransport(Transport transport) {
+ this.transport = transport;
+ }
+
+ public Metric getMetric() {
+ return metric;
+ }
+
+ public void setMetric(Metric metric) {
+ this.metric = metric;
}
- public String getPort() {
- return port;
+ public Servlet getServlet() {
+ return servlet;
}
- public void setPort(String port) {
- this.port = port;
+ public void setServlet(Servlet servlet) {
+ this.servlet = servlet;
}
- public String getDashboard() {
- return dashboard;
+ public boolean isEnabled() {
+ return enabled;
}
- public void setDashboard(String dashboard) {
- this.dashboard = dashboard;
+ public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
}
public Filter getFilter() {
@@ -79,10 +145,117 @@ public class SentinelProperties {
this.filter = filter;
}
+ public static class Flow {
+
+ /**
+ * the cold factor {@link SentinelConfig#COLD_FACTOR}
+ */
+ private String coldFactor = "3";
+
+ public String getColdFactor() {
+ return coldFactor;
+ }
+
+ public void setColdFactor(String coldFactor) {
+ this.coldFactor = coldFactor;
+ }
+
+ }
+
+ public static class Servlet {
+
+ /**
+ * The process page when the flow control is triggered
+ */
+ private String blockPage;
+
+ public String getBlockPage() {
+ return blockPage;
+ }
+
+ public void setBlockPage(String blockPage) {
+ this.blockPage = blockPage;
+ }
+ }
+
+ public static class Metric {
+
+ /**
+ * the metric file size {@link SentinelConfig#SINGLE_METRIC_FILE_SIZE}
+ */
+ private String fileSingleSize;
+
+ /**
+ * the total metric file count {@link SentinelConfig#TOTAL_METRIC_FILE_COUNT}
+ */
+ private String fileTotalCount;
+
+ public String getFileSingleSize() {
+ return fileSingleSize;
+ }
+
+ public void setFileSingleSize(String fileSingleSize) {
+ this.fileSingleSize = fileSingleSize;
+ }
+
+ public String getFileTotalCount() {
+ return fileTotalCount;
+ }
+
+ public void setFileTotalCount(String fileTotalCount) {
+ this.fileTotalCount = fileTotalCount;
+ }
+ }
+
+ public static class Transport {
+
+ /**
+ * sentinel api port,default value is 8721 {@link TransportConfig#SERVER_PORT}
+ */
+ private String port = "8721";
+
+ /**
+ * sentinel dashboard address, won't try to connect dashboard when address is
+ * empty {@link TransportConfig#CONSOLE_SERVER}
+ */
+ private String dashboard = "";
+
+ /**
+ * send heartbeat interval millisecond
+ * {@link TransportConfig#HEARTBEAT_INTERVAL_MS}
+ */
+ private String heartbeatIntervalMs;
+
+ public String getHeartbeatIntervalMs() {
+ return heartbeatIntervalMs;
+ }
+
+ public void setHeartbeatIntervalMs(String heartbeatIntervalMs) {
+ this.heartbeatIntervalMs = heartbeatIntervalMs;
+ }
+
+ public String getPort() {
+ return port;
+ }
+
+ public void setPort(String port) {
+ this.port = port;
+ }
+
+ public String getDashboard() {
+ return dashboard;
+ }
+
+ public void setDashboard(String dashboard) {
+ this.dashboard = dashboard;
+ }
+
+ }
+
public static class Filter {
/**
- * Sentinel filter chain order.
+ * sentinel filter chain order.
*/
private int order = Ordered.HIGHEST_PRECEDENCE;
@@ -107,4 +280,5 @@ public class SentinelProperties {
this.urlPatterns = urlPatterns;
}
}
+
}
diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java
index f982e8ceb..8428632f8 100644
--- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java
+++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java
@@ -75,4 +75,5 @@ public class SentinelWebAutoConfiguration {
return registration;
}
+
}
\ No newline at end of file
diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java
index d8ae30d9a..2f0ef6084 100644
--- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java
+++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java
@@ -18,14 +18,9 @@ package org.springframework.cloud.alibaba.sentinel.custom;
import java.util.Optional;
-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.WebCallbackManager;
-import com.alibaba.csp.sentinel.init.InitExecutor;
-import com.alibaba.csp.sentinel.transport.config.TransportConfig;
-import com.alibaba.csp.sentinel.util.AppNameUtil;
+import javax.annotation.PostConstruct;
+
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -36,14 +31,20 @@ import org.springframework.cloud.alibaba.sentinel.datasource.SentinelDataSourceP
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
-import org.springframework.web.client.RestTemplate;
+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.WebCallbackManager;
+import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
-
-import javax.annotation.PostConstruct;
+import com.alibaba.csp.sentinel.config.SentinelConfig;
+import com.alibaba.csp.sentinel.init.InitExecutor;
+import com.alibaba.csp.sentinel.transport.config.TransportConfig;
+import com.alibaba.csp.sentinel.util.AppNameUtil;
/**
* @author xiaojing
+ * @author jiashuai.xie
*/
@Configuration
@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
@@ -57,27 +58,67 @@ public class SentinelAutoConfiguration {
private SentinelProperties properties;
@Autowired
- private Optional urlBlockHandlerOptional;
+ private Optional urlCleanerOptional;
@Autowired
- private Optional urlCleanerOptional;
+ private Optional urlBlockHandlerOptional;
@PostConstruct
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);
}
- if (StringUtils.isEmpty(System.getProperty(TransportConfig.SERVER_PORT))) {
- System.setProperty(TransportConfig.SERVER_PORT, properties.getPort());
+ if (StringUtils.isEmpty(System.getProperty(TransportConfig.SERVER_PORT))
+ && StringUtils.hasText(properties.getTransport().getPort())) {
+ System.setProperty(TransportConfig.SERVER_PORT,
+ properties.getTransport().getPort());
+ }
+ if (StringUtils.isEmpty(System.getProperty(TransportConfig.CONSOLE_SERVER))
+ && StringUtils.hasText(properties.getTransport().getDashboard())) {
+ System.setProperty(TransportConfig.CONSOLE_SERVER,
+ properties.getTransport().getDashboard());
}
- if (StringUtils.isEmpty(System.getProperty(TransportConfig.CONSOLE_SERVER))) {
- System.setProperty(TransportConfig.CONSOLE_SERVER, properties.getDashboard());
+ if (StringUtils.isEmpty(System.getProperty(TransportConfig.HEARTBEAT_INTERVAL_MS))
+ && StringUtils
+ .hasText(properties.getTransport().getHeartbeatIntervalMs())) {
+ System.setProperty(TransportConfig.HEARTBEAT_INTERVAL_MS,
+ properties.getTransport().getHeartbeatIntervalMs());
+ }
+ if (StringUtils.isEmpty(System.getProperty(SentinelConfig.CHARSET))
+ && StringUtils.hasText(properties.getCharset())) {
+ System.setProperty(SentinelConfig.CHARSET, properties.getCharset());
+ }
+ if (StringUtils
+ .isEmpty(System.getProperty(SentinelConfig.SINGLE_METRIC_FILE_SIZE))
+ && StringUtils.hasText(properties.getMetric().getFileSingleSize())) {
+ System.setProperty(SentinelConfig.SINGLE_METRIC_FILE_SIZE,
+ properties.getMetric().getFileSingleSize());
+ }
+ if (StringUtils
+ .isEmpty(System.getProperty(SentinelConfig.TOTAL_METRIC_FILE_COUNT))
+ && StringUtils.hasText(properties.getMetric().getFileTotalCount())) {
+ System.setProperty(SentinelConfig.TOTAL_METRIC_FILE_COUNT,
+ properties.getMetric().getFileTotalCount());
+ }
+ if (StringUtils.isEmpty(System.getProperty(SentinelConfig.COLD_FACTOR))
+ && StringUtils.hasText(properties.getFlow().getColdFactor())) {
+ System.setProperty(SentinelConfig.COLD_FACTOR,
+ properties.getFlow().getColdFactor());
}
+ if (StringUtils.hasText(properties.getServlet().getBlockPage())) {
+ WebServletConfig.setBlockPage(properties.getServlet().getBlockPage());
+ }
urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler);
urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner);
- InitExecutor.doInit();
+ // earlier initialize
+ if (properties.isEager()) {
+ InitExecutor.doInit();
+ }
+
}
@Bean
diff --git a/spring-cloud-alibaba-sentinel/src/test/java/org/springframework/cloud/alibaba/sentinel/SentinelAutoConfigurationTests.java b/spring-cloud-alibaba-sentinel/src/test/java/org/springframework/cloud/alibaba/sentinel/SentinelAutoConfigurationTests.java
index 29fbc8ee2..647fde936 100644
--- a/spring-cloud-alibaba-sentinel/src/test/java/org/springframework/cloud/alibaba/sentinel/SentinelAutoConfigurationTests.java
+++ b/spring-cloud-alibaba-sentinel/src/test/java/org/springframework/cloud/alibaba/sentinel/SentinelAutoConfigurationTests.java
@@ -34,13 +34,14 @@ import com.alibaba.csp.sentinel.slots.block.BlockException;
/**
* @author fangjian
+ * @author jiashuai.xie
*/
public class SentinelAutoConfigurationTests {
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SentinelAutoConfiguration.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.urlPatterns=/*,/test");
@@ -65,7 +66,7 @@ public class SentinelAutoConfigurationTests {
this.contextRunner.run(context -> {
SentinelProperties sentinelProperties = context
.getBean(SentinelProperties.class);
- assertThat(sentinelProperties.getPort()).isEqualTo("8888");
+ assertThat(sentinelProperties.getTransport().getPort()).isEqualTo("8888");
assertThat(sentinelProperties.getFilter().getUrlPatterns().size())
.isEqualTo(2);
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))