mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
支持更多Sentinel配置
This commit is contained in:
parent
31864b34d4
commit
ee4d802f8c
@ -16,12 +16,12 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author hengyunabc
|
||||
@ -29,82 +29,245 @@ import org.springframework.core.Ordered;
|
||||
@ConfigurationProperties(prefix = SentinelConstants.PROPERTY_PREFIX)
|
||||
public class SentinelProperties {
|
||||
|
||||
/**
|
||||
* Enable sentinel auto configure, the default value is true
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* sentinel api port,default value is 8721
|
||||
*/
|
||||
private String port = "8721";
|
||||
/**
|
||||
* 是否提前初始化心跳连接
|
||||
*/
|
||||
private boolean eager = false;
|
||||
|
||||
/**
|
||||
* Sentinel dashboard address, won't try to connect dashboard when address is empty
|
||||
*/
|
||||
private String dashboard = "";
|
||||
/**
|
||||
* Enable sentinel auto configure, the default value is true
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
@NestedConfigurationProperty
|
||||
private Filter filter;
|
||||
/**
|
||||
* 字符编码集
|
||||
*/
|
||||
private String charset = "UTF-8";
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
/**
|
||||
* 通信相关配置
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private Transport transport = new Transport();
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
/**
|
||||
* 监控数据相关配置
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private Metric metric = new Metric();
|
||||
|
||||
public String getPort() {
|
||||
return port;
|
||||
}
|
||||
/**
|
||||
* web 相关配置
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private Servlet servlet = new Servlet();
|
||||
|
||||
public void setPort(String port) {
|
||||
this.port = port;
|
||||
}
|
||||
/**
|
||||
* 限流相关
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private Filter filter = new Filter();
|
||||
|
||||
public String getDashboard() {
|
||||
return dashboard;
|
||||
}
|
||||
@NestedConfigurationProperty
|
||||
private Flow flow = new Flow();
|
||||
|
||||
public void setDashboard(String dashboard) {
|
||||
this.dashboard = dashboard;
|
||||
}
|
||||
public boolean isEager() {
|
||||
return eager;
|
||||
}
|
||||
|
||||
public Filter getFilter() {
|
||||
return filter;
|
||||
}
|
||||
public void setEager(boolean eager) {
|
||||
this.eager = eager;
|
||||
}
|
||||
|
||||
public void setFilter(Filter filter) {
|
||||
this.filter = filter;
|
||||
}
|
||||
public Flow getFlow() {
|
||||
return flow;
|
||||
}
|
||||
|
||||
public static class Filter {
|
||||
public void setFlow(Flow flow) {
|
||||
this.flow = flow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sentinel filter chain order.
|
||||
*/
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE;
|
||||
public String getCharset() {
|
||||
return charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* URL pattern for sentinel filter,default is /*
|
||||
*/
|
||||
private List<String> urlPatterns;
|
||||
public void setCharset(String charset) {
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return this.order;
|
||||
}
|
||||
public Transport getTransport() {
|
||||
return transport;
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
public void setTransport(Transport transport) {
|
||||
this.transport = transport;
|
||||
}
|
||||
|
||||
public List<String> getUrlPatterns() {
|
||||
return urlPatterns;
|
||||
}
|
||||
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() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public Filter getFilter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public void setFilter(Filter filter) {
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
public static class Flow {
|
||||
|
||||
/**
|
||||
* 限流冷启动因子
|
||||
*/
|
||||
private String coldFactor = "3";
|
||||
|
||||
public String getColdFactor() {
|
||||
return coldFactor;
|
||||
}
|
||||
|
||||
public void setColdFactor(String coldFactor) {
|
||||
this.coldFactor = coldFactor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Servlet {
|
||||
|
||||
/**
|
||||
* url 限流后的处理页面
|
||||
*/
|
||||
private String blockPage;
|
||||
|
||||
public String getBlockPage() {
|
||||
return blockPage;
|
||||
}
|
||||
|
||||
public void setBlockPage(String blockPage) {
|
||||
this.blockPage = blockPage;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Metric {
|
||||
|
||||
/**
|
||||
* 监控数据写磁盘时单个文件的大小
|
||||
*/
|
||||
private String fileSingleSize;
|
||||
|
||||
/**
|
||||
* 监控数据在磁盘上的总数量
|
||||
*/
|
||||
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
|
||||
*/
|
||||
private String port = "8721";
|
||||
|
||||
/**
|
||||
* Sentinel dashboard address, won't try to connect dashboard when address is empty
|
||||
*/
|
||||
private String dashboard = "";
|
||||
|
||||
/**
|
||||
* 客户端和DashBord心跳发送时间
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
private int order = Ordered.HIGHEST_PRECEDENCE;
|
||||
|
||||
/**
|
||||
* URL pattern for sentinel filter,default is /*
|
||||
*/
|
||||
private List<String> urlPatterns;
|
||||
|
||||
public int getOrder() {
|
||||
return this.order;
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public List<String> getUrlPatterns() {
|
||||
return urlPatterns;
|
||||
}
|
||||
|
||||
public void setUrlPatterns(List<String> urlPatterns) {
|
||||
this.urlPatterns = urlPatterns;
|
||||
}
|
||||
}
|
||||
|
||||
public void setUrlPatterns(List<String> urlPatterns) {
|
||||
this.urlPatterns = urlPatterns;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,22 +16,28 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
|
||||
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 org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.Filter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
@ -42,37 +48,63 @@ import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
|
||||
@EnableConfigurationProperties(SentinelProperties.class)
|
||||
public class SentinelWebAutoConfiguration {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(SentinelWebAutoConfiguration.class);
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(SentinelWebAutoConfiguration.class);
|
||||
|
||||
@Autowired
|
||||
private SentinelProperties properties;
|
||||
@Autowired
|
||||
private SentinelProperties properties;
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean servletRequestListener() {
|
||||
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
|
||||
@Autowired
|
||||
@Qualifier
|
||||
private Optional<UrlBlockHandler> urlBlockHandlerOptional;
|
||||
|
||||
SentinelProperties.Filter filterConfig = properties.getFilter();
|
||||
@Autowired
|
||||
@Qualifier
|
||||
private Optional<UrlCleaner> urlCleanerOptional;
|
||||
|
||||
if (null == filterConfig) {
|
||||
filterConfig = new SentinelProperties.Filter();
|
||||
properties.setFilter(filterConfig);
|
||||
}
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
|
||||
if (filterConfig.getUrlPatterns() == null
|
||||
|| filterConfig.getUrlPatterns().isEmpty()) {
|
||||
List<String> defaultPatterns = new ArrayList<>();
|
||||
defaultPatterns.add("/*");
|
||||
filterConfig.setUrlPatterns(defaultPatterns);
|
||||
}
|
||||
if (urlBlockHandlerOptional.isPresent()) {
|
||||
WebCallbackManager.setUrlBlockHandler(urlBlockHandlerOptional.get());
|
||||
}
|
||||
|
||||
registration.addUrlPatterns(filterConfig.getUrlPatterns().toArray(new String[0]));
|
||||
Filter filter = new CommonFilter();
|
||||
registration.setFilter(filter);
|
||||
registration.setOrder(filterConfig.getOrder());
|
||||
logger.info("[Sentinel Starter] register Sentinel with urlPatterns: {}.",
|
||||
filterConfig.getUrlPatterns());
|
||||
return registration;
|
||||
if (urlCleanerOptional.isPresent()) {
|
||||
WebCallbackManager.setUrlCleaner(urlCleanerOptional.get());
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(properties.getServlet().getBlockPage())) {
|
||||
WebServletConfig.setBlockPage(properties.getServlet().getBlockPage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean servletRequestListener() {
|
||||
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
|
||||
|
||||
SentinelProperties.Filter filterConfig = properties.getFilter();
|
||||
|
||||
if (null == filterConfig) {
|
||||
filterConfig = new SentinelProperties.Filter();
|
||||
properties.setFilter(filterConfig);
|
||||
}
|
||||
|
||||
if (filterConfig.getUrlPatterns() == null
|
||||
|| filterConfig.getUrlPatterns().isEmpty()) {
|
||||
List<String> defaultPatterns = new ArrayList<>();
|
||||
defaultPatterns.add("/*");
|
||||
filterConfig.setUrlPatterns(defaultPatterns);
|
||||
}
|
||||
|
||||
registration.addUrlPatterns(filterConfig.getUrlPatterns().toArray(new String[0]));
|
||||
Filter filter = new CommonFilter();
|
||||
registration.setFilter(filter);
|
||||
registration.setOrder(filterConfig.getOrder());
|
||||
logger.info("[Sentinel Starter] register Sentinel with urlPatterns: {}.",
|
||||
filterConfig.getUrlPatterns());
|
||||
return registration;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.sentinel.custom;
|
||||
|
||||
import com.alibaba.csp.sentinel.Env;
|
||||
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
|
||||
import com.alibaba.csp.sentinel.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.node.NodeBuilder;
|
||||
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
|
||||
import com.alibaba.csp.sentinel.util.AppNameUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -29,9 +33,6 @@ 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.annotation.aspectj.SentinelResourceAspect;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@ -51,34 +52,57 @@ public class SentinelAutoConfiguration {
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
|
||||
if (StringUtils.isEmpty(System.getProperty(AppNameUtil.APP_NAME))) {
|
||||
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))) {
|
||||
System.setProperty(TransportConfig.CONSOLE_SERVER, properties.getDashboard());
|
||||
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.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 (properties.isEager()) {
|
||||
// 加载Env
|
||||
NodeBuilder nodeBuilder = Env.nodeBuilder;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SentinelResourceAspect sentinelResourceAspect() {
|
||||
return new SentinelResourceAspect();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SentinelResourceAspect sentinelResourceAspect() {
|
||||
return new SentinelResourceAspect();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
|
||||
public SentinelBeanPostProcessor sentinelBeanPostProcessor() {
|
||||
return new SentinelBeanPostProcessor();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnClass(name = "org.springframework.web.client.RestTemplate")
|
||||
public SentinelBeanPostProcessor sentinelBeanPostProcessor() {
|
||||
return new SentinelBeanPostProcessor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SentinelDataSourcePostProcessor sentinelDataSourcePostProcessor() {
|
||||
return new SentinelDataSourcePostProcessor();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SentinelDataSourcePostProcessor sentinelDataSourcePostProcessor() {
|
||||
return new SentinelDataSourcePostProcessor();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -65,7 +65,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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user