优化代码

pull/29/head
xiejiashuai 6 years ago
parent 03a64885d4
commit 6794c350ef

@ -22,6 +22,9 @@ 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
@ -31,44 +34,50 @@ import org.springframework.core.Ordered;
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;
/**
* Enable sentinel auto configure, the default value is true
* enable sentinel auto configure, the default value is true
*/
private boolean enabled = true;
/**
*
* charset when sentinel write or search metric file {@link SentinelConfig#CHARSET}
*/
private String charset = "UTF-8";
/**
*
* transport configuration about dashboard and client
*/
@NestedConfigurationProperty
private Transport transport = new Transport();
/**
*
* metric configuration about resource
*/
@NestedConfigurationProperty
private Metric metric = new Metric();
/**
* web
* web servlet configuration <note> when the application is web ,the configuration is
* effective </note>
*/
@NestedConfigurationProperty
private Servlet servlet = new Servlet();
/**
*
* sentinel filter <note> when the application is web ,the configuration is effective
* </note>
*/
@NestedConfigurationProperty
private Filter filter = new Filter();
/**
* flow configuration
*/
@NestedConfigurationProperty
private Flow flow = new Flow();
@ -139,7 +148,7 @@ public class SentinelProperties {
public static class Flow {
/**
*
* the cold factor {@link SentinelConfig#COLD_FACTOR}
*/
private String coldFactor = "3";
@ -156,7 +165,7 @@ public class SentinelProperties {
public static class Servlet {
/**
* url
* The process page when the flow control is triggered
*/
private String blockPage;
@ -172,12 +181,12 @@ public class SentinelProperties {
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;
@ -201,18 +210,19 @@ public class SentinelProperties {
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";
/**
* Sentinel dashboard address, won't try to connect dashboard when address is
* empty
* sentinel dashboard address, won't try to connect dashboard when address is
* empty {@link TransportConfig#CONSOLE_SERVER}
*/
private String dashboard = "";
/**
* DashBord
* send heartbeat interval millisecond
* {@link TransportConfig#HEARTBEAT_INTERVAL_MS}
*/
private String heartbeatIntervalMs;
@ -245,7 +255,7 @@ public class SentinelProperties {
public static class Filter {
/**
* Sentinel filter chain order.
* sentinel filter chain order.
*/
private int order = Ordered.HIGHEST_PRECEDENCE;

@ -32,7 +32,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.UrlCleaner;
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.config.SentinelConfig;
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.util.AppNameUtil;
@ -68,7 +66,8 @@ public class SentinelAutoConfiguration {
@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))
@ -115,11 +114,9 @@ public class SentinelAutoConfiguration {
urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler);
urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner);
InitExecutor.doInit();
// earlier initialize
if (properties.isEager()) {
NodeBuilder nodeBuilder = Env.nodeBuilder;
InitExecutor.doInit();
}
}

@ -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");

Loading…
Cancel
Save