From ee4d802f8c8bad3e972887f687a5b9e0e847368b Mon Sep 17 00:00:00 2001 From: xiejiashuai <707094980@qq.com> Date: Sat, 8 Sep 2018 13:07:30 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9B=B4=E5=A4=9ASentine?= =?UTF-8?q?l=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/sentinel/SentinelProperties.java | 287 ++++++++++++++---- .../SentinelWebAutoConfiguration.java | 96 ++++-- .../custom/SentinelAutoConfiguration.java | 70 +++-- .../SentinelAutoConfigurationTests.java | 2 +- 4 files changed, 337 insertions(+), 118 deletions(-) diff --git a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java index eb1f2be77..8e51c8553 100644 --- a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java +++ b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java @@ -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; + + /** + * Enable sentinel auto configure, the default value is true + */ + private boolean enabled = true; + + /** + * 字符编码集 + */ + private String charset = "UTF-8"; + + /** + * 通信相关配置 + */ + @NestedConfigurationProperty + private Transport transport = new Transport(); + + /** + * 监控数据相关配置 + */ + @NestedConfigurationProperty + private Metric metric = new Metric(); + + /** + * web 相关配置 + */ + @NestedConfigurationProperty + private Servlet servlet = new Servlet(); + + /** + * 限流相关 + */ + @NestedConfigurationProperty + private Filter filter = new Filter(); + + @NestedConfigurationProperty + 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() { + 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 = ""; + /** + * Sentinel dashboard address, won't try to connect dashboard when address is empty + */ + private String dashboard = ""; - @NestedConfigurationProperty - private Filter filter; + /** + * 客户端和DashBord心跳发送时间 + */ + private String heartbeatIntervalMs; - public boolean isEnabled() { - return enabled; - } + public String getHeartbeatIntervalMs() { + return heartbeatIntervalMs; + } - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } + public void setHeartbeatIntervalMs(String heartbeatIntervalMs) { + this.heartbeatIntervalMs = heartbeatIntervalMs; + } - public String getPort() { - return port; - } + public String getPort() { + return port; + } - public void setPort(String port) { - this.port = port; - } + public void setPort(String port) { + this.port = port; + } - public String getDashboard() { - return dashboard; - } + public String getDashboard() { + return dashboard; + } - public void setDashboard(String dashboard) { - this.dashboard = dashboard; - } + public void setDashboard(String 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. + */ + private int order = Ordered.HIGHEST_PRECEDENCE; - /** - * Sentinel filter chain order. - */ - private int order = Ordered.HIGHEST_PRECEDENCE; + /** + * URL pattern for sentinel filter,default is /* + */ + private List urlPatterns; - /** - * URL pattern for sentinel filter,default is /* - */ - private List urlPatterns; + public int getOrder() { + return this.order; + } - public int getOrder() { - return this.order; - } + public void setOrder(int order) { + this.order = order; + } - public void setOrder(int order) { - this.order = order; - } + public List getUrlPatterns() { + return urlPatterns; + } - public List getUrlPatterns() { - return urlPatterns; - } + public void setUrlPatterns(List urlPatterns) { + this.urlPatterns = urlPatterns; + } + } - public void setUrlPatterns(List urlPatterns) { - this.urlPatterns = urlPatterns; - } - } } diff --git a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java index f982e8ceb..967819add 100644 --- a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java +++ b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java @@ -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 + @Qualifier + private Optional urlBlockHandlerOptional; + + @Autowired + @Qualifier + private Optional urlCleanerOptional; + + @PostConstruct + public void init() { + + if (urlBlockHandlerOptional.isPresent()) { + WebCallbackManager.setUrlBlockHandler(urlBlockHandlerOptional.get()); + } + + if (urlCleanerOptional.isPresent()) { + WebCallbackManager.setUrlCleaner(urlCleanerOptional.get()); + } + + if (StringUtils.hasText(properties.getServlet().getBlockPage())) { + WebServletConfig.setBlockPage(properties.getServlet().getBlockPage()); + } + + } - @Autowired - private SentinelProperties properties; + @Bean + public FilterRegistrationBean servletRequestListener() { + FilterRegistrationBean registration = new FilterRegistrationBean<>(); - @Bean - public FilterRegistrationBean servletRequestListener() { - FilterRegistrationBean registration = new FilterRegistrationBean<>(); + SentinelProperties.Filter filterConfig = properties.getFilter(); - SentinelProperties.Filter filterConfig = properties.getFilter(); + if (null == filterConfig) { + filterConfig = new SentinelProperties.Filter(); + properties.setFilter(filterConfig); + } - if (null == filterConfig) { - filterConfig = new SentinelProperties.Filter(); - properties.setFilter(filterConfig); - } + if (filterConfig.getUrlPatterns() == null + || filterConfig.getUrlPatterns().isEmpty()) { + List defaultPatterns = new ArrayList<>(); + defaultPatterns.add("/*"); + filterConfig.setUrlPatterns(defaultPatterns); + } - if (filterConfig.getUrlPatterns() == null - || filterConfig.getUrlPatterns().isEmpty()) { - List 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; - 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; + } - } } \ No newline at end of file diff --git a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java index 623422926..98b3a6ddd 100644 --- a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java +++ b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java @@ -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)) && 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(TransportConfig.CONSOLE_SERVER))) { - System.setProperty(TransportConfig.CONSOLE_SERVER, properties.getDashboard()); + 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(); + } } diff --git a/spring-cloud-alibaba-sentinel-autoconfigure/src/test/java/org/springframework/cloud/alibaba/sentinel/SentinelAutoConfigurationTests.java b/spring-cloud-alibaba-sentinel-autoconfigure/src/test/java/org/springframework/cloud/alibaba/sentinel/SentinelAutoConfigurationTests.java index 29fbc8ee2..521e7d7c9 100644 --- a/spring-cloud-alibaba-sentinel-autoconfigure/src/test/java/org/springframework/cloud/alibaba/sentinel/SentinelAutoConfigurationTests.java +++ b/spring-cloud-alibaba-sentinel-autoconfigure/src/test/java/org/springframework/cloud/alibaba/sentinel/SentinelAutoConfigurationTests.java @@ -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)) From 9e5de2eabb9b7007c5f2e49ce81a12e7bb6c7b4f Mon Sep 17 00:00:00 2001 From: xiejiashuai <707094980@qq.com> Date: Fri, 21 Sep 2018 21:24:15 +0800 Subject: [PATCH 2/5] add author --- .../cloud/alibaba/sentinel/SentinelProperties.java | 1 + .../cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java | 1 + .../cloud/alibaba/sentinel/custom/SentinelAutoConfiguration.java | 1 + 3 files changed, 3 insertions(+) diff --git a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java index 8e51c8553..0d367843f 100644 --- a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java +++ b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelProperties.java @@ -25,6 +25,7 @@ import java.util.List; /** * @author xiaojing * @author hengyunabc + * @author jiashuai.xie */ @ConfigurationProperties(prefix = SentinelConstants.PROPERTY_PREFIX) public class SentinelProperties { diff --git a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java index 967819add..f16a96ceb 100644 --- a/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java +++ b/spring-cloud-alibaba-sentinel-autoconfigure/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelWebAutoConfiguration.java @@ -41,6 +41,7 @@ import java.util.Optional; /** * @author xiaojing + * @author jiashuai.xie Date: Sat, 22 Sep 2018 10:30:15 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E5=A4=9A=E4=BD=99?= =?UTF-8?q?=E7=9A=84=E5=AF=BC=E5=85=A5=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sentinel/SentinelWebAutoConfiguration.java | 8 -------- .../custom/SentinelAutoConfiguration.java | 18 +++++++++--------- 2 files changed, 9 insertions(+), 17 deletions(-) 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 2b51b36fb..c19630e8e 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 @@ -17,27 +17,19 @@ package org.springframework.cloud.alibaba.sentinel; 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 javax.annotation.PostConstruct; import javax.servlet.Filter; import java.util.ArrayList; import java.util.List; -import java.util.Optional; /** * @author xiaojing 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 0ed816600..c9e77f35c 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 @@ -17,15 +17,14 @@ package org.springframework.cloud.alibaba.sentinel.custom; import com.alibaba.csp.sentinel.Env; -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.node.NodeBuilder; -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.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; import org.springframework.beans.factory.annotation.Autowired; @@ -41,6 +40,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.util.StringUtils; import javax.annotation.PostConstruct; +import java.util.Optional; /** * @author xiaojing @@ -51,11 +51,11 @@ import javax.annotation.PostConstruct; @EnableConfigurationProperties(SentinelProperties.class) public class SentinelAutoConfiguration { - @Value("${project.name:${spring.application.name:}}") - private String projectName; + @Value("${project.name:${spring.application.name:}}") + private String projectName; - @Autowired - private SentinelProperties properties; + @Autowired + private SentinelProperties properties; @Autowired private Optional urlCleanerOptional; From 03a64885d4b7d94df9d4622bb891f874af929957 Mon Sep 17 00:00:00 2001 From: xiejiashuai <707094980@qq.com> Date: Thu, 27 Sep 2018 10:08:23 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC=E4=B8=BAeclipse=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/sentinel/SentinelProperties.java | 428 +++++++++--------- .../SentinelWebAutoConfiguration.java | 62 +-- .../custom/SentinelAutoConfiguration.java | 191 ++++---- 3 files changed, 350 insertions(+), 331 deletions(-) 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 0d367843f..3f91a3b0b 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 @@ -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 @@ -30,245 +30,245 @@ import java.util.List; @ConfigurationProperties(prefix = SentinelConstants.PROPERTY_PREFIX) public class SentinelProperties { + /** + * 是否提前初始化心跳连接 + */ + private boolean eager = false; + + /** + * Enable sentinel auto configure, the default value is true + */ + private boolean enabled = true; + + /** + * 字符编码集 + */ + private String charset = "UTF-8"; + + /** + * 通信相关配置 + */ + @NestedConfigurationProperty + private Transport transport = new Transport(); + + /** + * 监控数据相关配置 + */ + @NestedConfigurationProperty + private Metric metric = new Metric(); + + /** + * web 相关配置 + */ + @NestedConfigurationProperty + private Servlet servlet = new Servlet(); + + /** + * 限流相关 + */ + @NestedConfigurationProperty + private Filter filter = new Filter(); + + @NestedConfigurationProperty + private Flow flow = new Flow(); - /** - * 是否提前初始化心跳连接 - */ - private boolean eager = false; - - /** - * Enable sentinel auto configure, the default value is true - */ - private boolean enabled = true; - - /** - * 字符编码集 - */ - private String charset = "UTF-8"; - - /** - * 通信相关配置 - */ - @NestedConfigurationProperty - private Transport transport = new Transport(); - - /** - * 监控数据相关配置 - */ - @NestedConfigurationProperty - private Metric metric = new Metric(); - - /** - * web 相关配置 - */ - @NestedConfigurationProperty - private Servlet servlet = new Servlet(); - - /** - * 限流相关 - */ - @NestedConfigurationProperty - private Filter filter = new Filter(); - - @NestedConfigurationProperty - 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() { - 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 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 static class Servlet { + 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() { + 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; + } - /** - * url 限流后的处理页面 - */ - private String blockPage; + } + + public static class Servlet { - public String getBlockPage() { - return blockPage; - } + /** + * url 限流后的处理页面 + */ + private String blockPage; - public void setBlockPage(String blockPage) { - this.blockPage = blockPage; - } - } + public String getBlockPage() { + return blockPage; + } - public static class Metric { + public void setBlockPage(String blockPage) { + this.blockPage = blockPage; + } + } - /** - * 监控数据写磁盘时单个文件的大小 - */ - private String fileSingleSize; + public static class Metric { - /** - * 监控数据在磁盘上的总数量 - */ - private String fileTotalCount; + /** + * 监控数据写磁盘时单个文件的大小 + */ + private String fileSingleSize; - public String getFileSingleSize() { - return fileSingleSize; - } + /** + * 监控数据在磁盘上的总数量 + */ + private String fileTotalCount; - public void setFileSingleSize(String fileSingleSize) { - this.fileSingleSize = fileSingleSize; - } + public String getFileSingleSize() { + return fileSingleSize; + } - public String getFileTotalCount() { - return fileTotalCount; - } + public void setFileSingleSize(String fileSingleSize) { + this.fileSingleSize = fileSingleSize; + } - public void setFileTotalCount(String fileTotalCount) { - this.fileTotalCount = fileTotalCount; - } - } + public String getFileTotalCount() { + return fileTotalCount; + } - public static class Transport { + public void setFileTotalCount(String fileTotalCount) { + this.fileTotalCount = fileTotalCount; + } + } - /** - * sentinel api port,default value is 8721 - */ - private String port = "8721"; + public static class Transport { - /** - * Sentinel dashboard address, won't try to connect dashboard when address is empty - */ - private String dashboard = ""; + /** + * sentinel api port,default value is 8721 + */ + private String port = "8721"; - /** - * 客户端和DashBord心跳发送时间 - */ - private String heartbeatIntervalMs; + /** + * Sentinel dashboard address, won't try to connect dashboard when address is + * empty + */ + private String dashboard = ""; - public String getHeartbeatIntervalMs() { - return heartbeatIntervalMs; - } + /** + * 客户端和DashBord心跳发送时间 + */ + private String heartbeatIntervalMs; - public void setHeartbeatIntervalMs(String heartbeatIntervalMs) { - this.heartbeatIntervalMs = heartbeatIntervalMs; - } + public String getHeartbeatIntervalMs() { + return heartbeatIntervalMs; + } - public String getPort() { - return port; - } + public void setHeartbeatIntervalMs(String heartbeatIntervalMs) { + this.heartbeatIntervalMs = heartbeatIntervalMs; + } - public void setPort(String port) { - this.port = port; - } + public String getPort() { + return port; + } - public String getDashboard() { - return dashboard; - } + public void setPort(String port) { + this.port = port; + } - public void setDashboard(String dashboard) { - this.dashboard = dashboard; - } + 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; + public static class Filter { - /** - * URL pattern for sentinel filter,default is /* - */ - private List urlPatterns; + /** + * Sentinel filter chain order. + */ + private int order = Ordered.HIGHEST_PRECEDENCE; - public int getOrder() { - return this.order; - } - - public void setOrder(int order) { - this.order = order; - } - - public List getUrlPatterns() { - return urlPatterns; - } - - public void setUrlPatterns(List urlPatterns) { - this.urlPatterns = urlPatterns; - } - } + /** + * URL pattern for sentinel filter,default is /* + */ + private List urlPatterns; + + public int getOrder() { + return this.order; + } + + public void setOrder(int order) { + this.order = order; + } + + public List getUrlPatterns() { + return urlPatterns; + } + + public void setUrlPatterns(List urlPatterns) { + 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 c19630e8e..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 @@ -16,7 +16,11 @@ package org.springframework.cloud.alibaba.sentinel; -import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.Filter; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -27,9 +31,7 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import javax.servlet.Filter; -import java.util.ArrayList; -import java.util.List; +import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter; /** * @author xiaojing @@ -40,38 +42,38 @@ import java.util.List; @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 registration = new FilterRegistrationBean<>(); + @Bean + public FilterRegistrationBean servletRequestListener() { + FilterRegistrationBean registration = new FilterRegistrationBean<>(); - SentinelProperties.Filter filterConfig = properties.getFilter(); + SentinelProperties.Filter filterConfig = properties.getFilter(); - if (null == filterConfig) { - filterConfig = new SentinelProperties.Filter(); - properties.setFilter(filterConfig); - } + if (null == filterConfig) { + filterConfig = new SentinelProperties.Filter(); + properties.setFilter(filterConfig); + } - if (filterConfig.getUrlPatterns() == null - || filterConfig.getUrlPatterns().isEmpty()) { - List defaultPatterns = new ArrayList<>(); - defaultPatterns.add("/*"); - filterConfig.setUrlPatterns(defaultPatterns); - } + if (filterConfig.getUrlPatterns() == null + || filterConfig.getUrlPatterns().isEmpty()) { + List 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; + 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; - } + } } \ 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 c9e77f35c..ce0e7a910 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 @@ -16,17 +16,10 @@ package org.springframework.cloud.alibaba.sentinel.custom; -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; -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; +import java.util.Optional; + +import javax.annotation.PostConstruct; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -39,8 +32,17 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.StringUtils; -import javax.annotation.PostConstruct; -import java.util.Optional; +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; +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; /** * @author xiaojing @@ -51,79 +53,94 @@ import java.util.Optional; @EnableConfigurationProperties(SentinelProperties.class) public class SentinelAutoConfiguration { - @Value("${project.name:${spring.application.name:}}") - private String projectName; - - @Autowired - private SentinelProperties properties; - - @Autowired - private Optional urlCleanerOptional; - - @Autowired - private Optional urlBlockHandlerOptional; - - @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)) && 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.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()) { - NodeBuilder nodeBuilder = Env.nodeBuilder; - } - - } - - @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 - public SentinelDataSourcePostProcessor sentinelDataSourcePostProcessor() { - return new SentinelDataSourcePostProcessor(); - } + @Value("${project.name:${spring.application.name:}}") + private String projectName; + + @Autowired + private SentinelProperties properties; + + @Autowired + private Optional urlCleanerOptional; + + @Autowired + private Optional urlBlockHandlerOptional; + + @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)) + && 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.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()) { + NodeBuilder nodeBuilder = Env.nodeBuilder; + } + + } + + @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 + public SentinelDataSourcePostProcessor sentinelDataSourcePostProcessor() { + return new SentinelDataSourcePostProcessor(); + } } From 6794c350efd7256b8770b001f6dd475a4bec20a3 Mon Sep 17 00:00:00 2001 From: xiejiashuai <707094980@qq.com> Date: Thu, 27 Sep 2018 13:33:29 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alibaba/sentinel/SentinelProperties.java | 42 ++++++++++++------- .../custom/SentinelAutoConfiguration.java | 9 ++-- .../SentinelAutoConfigurationTests.java | 3 +- 3 files changed, 31 insertions(+), 23 deletions(-) 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 3f91a3b0b..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,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 when the + * transport dependency is on classpath ,the configuration is effective */ 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 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(); @@ -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; 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 ce0e7a910..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 @@ -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(); } } 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 521e7d7c9..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");