Merge pull request #29 from xiejiashuai/featrue/support_more_configuration

Featrue/support more sentinel configuration
pull/1534/head
format 6 years ago committed by GitHub
commit 117f4f70c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,30 +22,112 @@ 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
* @author jiashuai.xie
*/ */
@ConfigurationProperties(prefix = SentinelConstants.PROPERTY_PREFIX) @ConfigurationProperties(prefix = SentinelConstants.PROPERTY_PREFIX)
public class SentinelProperties { public class SentinelProperties {
/** /**
* Enable sentinel auto configure, the default value is true * 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
*/ */
private boolean enabled = 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 Metric metric = new Metric();
/**
* 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 @NestedConfigurationProperty
private Filter filter; private Flow flow = new Flow();
public boolean isEager() {
return eager;
}
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 Servlet getServlet() {
return servlet;
}
public void setServlet(Servlet servlet) {
this.servlet = servlet;
}
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
@ -55,6 +137,103 @@ public class SentinelProperties {
this.enabled = enabled; this.enabled = enabled;
} }
public Filter getFilter() {
return filter;
}
public void setFilter(Filter filter) {
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() { public String getPort() {
return port; return port;
} }
@ -71,18 +250,12 @@ public class SentinelProperties {
this.dashboard = dashboard; this.dashboard = dashboard;
} }
public Filter getFilter() {
return filter;
}
public void setFilter(Filter filter) {
this.filter = filter;
} }
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;
@ -107,4 +280,5 @@ public class SentinelProperties {
this.urlPatterns = urlPatterns; this.urlPatterns = urlPatterns;
} }
} }
} }

@ -75,4 +75,5 @@ public class SentinelWebAutoConfiguration {
return registration; return registration;
} }
} }

@ -18,14 +18,9 @@ package org.springframework.cloud.alibaba.sentinel.custom;
import java.util.Optional; import java.util.Optional;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; import javax.annotation.PostConstruct;
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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils; 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 com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import javax.annotation.PostConstruct; 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 xiaojing
* @author jiashuai.xie
*/ */
@Configuration @Configuration
@ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true) @ConditionalOnProperty(name = "spring.cloud.sentinel.enabled", matchIfMissing = true)
@ -57,29 +58,69 @@ public class SentinelAutoConfiguration {
private SentinelProperties properties; private SentinelProperties properties;
@Autowired @Autowired
private Optional<UrlBlockHandler> urlBlockHandlerOptional; private Optional<UrlCleaner> urlCleanerOptional;
@Autowired @Autowired
private Optional<UrlCleaner> urlCleanerOptional; private Optional<UrlBlockHandler> urlBlockHandlerOptional;
@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))
System.setProperty(TransportConfig.SERVER_PORT, properties.getPort()); && 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))) { if (StringUtils.isEmpty(System.getProperty(TransportConfig.HEARTBEAT_INTERVAL_MS))
System.setProperty(TransportConfig.CONSOLE_SERVER, properties.getDashboard()); && 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); urlBlockHandlerOptional.ifPresent(WebCallbackManager::setUrlBlockHandler);
urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner); urlCleanerOptional.ifPresent(WebCallbackManager::setUrlCleaner);
// earlier initialize
if (properties.isEager()) {
InitExecutor.doInit(); InitExecutor.doInit();
} }
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public SentinelResourceAspect sentinelResourceAspect() { public SentinelResourceAspect sentinelResourceAspect() {

@ -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");
@ -65,7 +66,7 @@ public class SentinelAutoConfigurationTests {
this.contextRunner.run(context -> { this.contextRunner.run(context -> {
SentinelProperties sentinelProperties = context SentinelProperties sentinelProperties = context
.getBean(SentinelProperties.class); .getBean(SentinelProperties.class);
assertThat(sentinelProperties.getPort()).isEqualTo("8888"); assertThat(sentinelProperties.getTransport().getPort()).isEqualTo("8888");
assertThat(sentinelProperties.getFilter().getUrlPatterns().size()) assertThat(sentinelProperties.getFilter().getUrlPatterns().size())
.isEqualTo(2); .isEqualTo(2);
assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0)) assertThat(sentinelProperties.getFilter().getUrlPatterns().get(0))

Loading…
Cancel
Save