1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Polish alibaba/spring-cloud-alibaba#1739 : Sync the Sentinel from master

This commit is contained in:
mercyblitz 2020-09-18 11:18:32 +08:00
parent 03f0a44037
commit bafe58a65a
10 changed files with 52 additions and 28 deletions

View File

@ -103,6 +103,11 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-circuitbreaker-sentinel</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba.csp</groupId> <groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-apache-dubbo-adapter</artifactId> <artifactId>sentinel-apache-dubbo-adapter</artifactId>

View File

@ -102,6 +102,20 @@ public class SentinelProperties {
*/ */
private Boolean httpMethodSpecify = false; private Boolean httpMethodSpecify = false;
/**
* Specify whether unify web context(i.e. use the default context name), and is true
* by default.
*/
private Boolean webContextUnify = true;
public Boolean getWebContextUnify() {
return webContextUnify;
}
public void setWebContextUnify(Boolean webContextUnify) {
this.webContextUnify = webContextUnify;
}
public boolean isEager() { public boolean isEager() {
return eager; return eager;
} }

View File

@ -94,6 +94,7 @@ public class SentinelWebAutoConfiguration implements WebMvcConfigurer {
public SentinelWebMvcConfig sentinelWebMvcConfig() { public SentinelWebMvcConfig sentinelWebMvcConfig() {
SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig(); SentinelWebMvcConfig sentinelWebMvcConfig = new SentinelWebMvcConfig();
sentinelWebMvcConfig.setHttpMethodSpecify(properties.getHttpMethodSpecify()); sentinelWebMvcConfig.setHttpMethodSpecify(properties.getHttpMethodSpecify());
sentinelWebMvcConfig.setWebContextUnify(properties.getWebContextUnify());
if (blockExceptionHandlerOptional.isPresent()) { if (blockExceptionHandlerOptional.isPresent()) {
blockExceptionHandlerOptional blockExceptionHandlerOptional

View File

@ -79,9 +79,9 @@ public class SentinelAutoConfiguration {
System.setProperty(LogBase.LOG_NAME_USE_PID, System.setProperty(LogBase.LOG_NAME_USE_PID,
String.valueOf(properties.getLog().isSwitchPid())); String.valueOf(properties.getLog().isSwitchPid()));
} }
if (StringUtils.isEmpty(System.getProperty(AppNameUtil.APP_NAME)) if (StringUtils.isEmpty(System.getProperty(SentinelConfig.APP_NAME_PROP_KEY))
&& StringUtils.hasText(projectName)) { && StringUtils.hasText(projectName)) {
System.setProperty(AppNameUtil.APP_NAME, projectName); System.setProperty(SentinelConfig.APP_NAME_PROP_KEY, projectName);
} }
if (StringUtils.isEmpty(System.getProperty(TransportConfig.SERVER_PORT)) if (StringUtils.isEmpty(System.getProperty(TransportConfig.SERVER_PORT))
&& StringUtils.hasText(properties.getTransport().getPort())) { && StringUtils.hasText(properties.getTransport().getPort())) {

View File

@ -61,7 +61,7 @@ public class SentinelEndpoint {
result.put("metricsFileSize", SentinelConfig.singleMetricFileSize()); result.put("metricsFileSize", SentinelConfig.singleMetricFileSize());
result.put("metricsFileCharset", SentinelConfig.charset()); result.put("metricsFileCharset", SentinelConfig.charset());
result.put("totalMetricsFileCount", SentinelConfig.totalMetricFileCount()); result.put("totalMetricsFileCount", SentinelConfig.totalMetricFileCount());
result.put("consoleServer", TransportConfig.getConsoleServer()); result.put("consoleServer", TransportConfig.getConsoleServerList());
result.put("clientIp", TransportConfig.getHeartbeatClientIp()); result.put("clientIp", TransportConfig.getHeartbeatClientIp());
result.put("heartbeatIntervalMs", TransportConfig.getHeartbeatIntervalMs()); result.put("heartbeatIntervalMs", TransportConfig.getHeartbeatIntervalMs());
result.put("clientPort", TransportConfig.getPort()); result.put("clientPort", TransportConfig.getPort());

View File

@ -17,6 +17,7 @@
package com.alibaba.cloud.sentinel.endpoint; package com.alibaba.cloud.sentinel.endpoint;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.cloud.sentinel.SentinelProperties; import com.alibaba.cloud.sentinel.SentinelProperties;
@ -24,13 +25,14 @@ import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider; import com.alibaba.csp.sentinel.heartbeat.HeartbeatSenderProvider;
import com.alibaba.csp.sentinel.transport.HeartbeatSender; import com.alibaba.csp.sentinel.transport.HeartbeatSender;
import com.alibaba.csp.sentinel.transport.config.TransportConfig; import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.alibaba.csp.sentinel.util.function.Tuple2;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status; import org.springframework.boot.actuate.health.Status;
import org.springframework.util.StringUtils; import org.springframework.util.CollectionUtils;
/** /**
* A {@link HealthIndicator} for Sentinel, which checks the status of Sentinel Dashboard * A {@link HealthIndicator} for Sentinel, which checks the status of Sentinel Dashboard
@ -74,7 +76,7 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
// detail // detail
if (!sentinelProperties.isEnabled()) { if (!sentinelProperties.isEnabled()) {
detailMap.put("enabled", false); detailMap.put("enabled", false);
withDetails(builder.up(), detailMap); builder.up().withDetails(detailMap);
return; return;
} }
@ -82,8 +84,9 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
// Check health of Dashboard // Check health of Dashboard
boolean dashboardUp = true; boolean dashboardUp = true;
String consoleServer = TransportConfig.getConsoleServer(); List<Tuple2<String, Integer>> consoleServerList = TransportConfig
if (StringUtils.isEmpty(consoleServer)) { .getConsoleServerList();
if (CollectionUtils.isEmpty(consoleServerList)) {
// If Dashboard isn't configured, it's OK and mark the status of Dashboard // If Dashboard isn't configured, it's OK and mark the status of Dashboard
// with UNKNOWN. // with UNKNOWN.
detailMap.put("dashboard", detailMap.put("dashboard",
@ -101,8 +104,10 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
else { else {
// If failed to send heartbeat message, means that the Dashboard is DOWN // If failed to send heartbeat message, means that the Dashboard is DOWN
dashboardUp = false; dashboardUp = false;
detailMap.put("dashboard", new Status(Status.DOWN.getCode(), detailMap.put("dashboard",
consoleServer + " can't be connected")); new Status(Status.UNKNOWN.getCode(), String.format(
"the dashboard servers [%s] one of them can't be connected",
consoleServerList)));
} }
} }
@ -133,22 +138,16 @@ public class SentinelHealthIndicator extends AbstractHealthIndicator {
// DOWN // DOWN
dataSourceUp = false; dataSourceUp = false;
dataSourceDetailMap.put(dataSourceBeanName, dataSourceDetailMap.put(dataSourceBeanName,
new Status(Status.DOWN.getCode(), e.getMessage())); new Status(Status.UNKNOWN.getCode(), e.getMessage()));
} }
} }
// If Dashboard and DataSource are both OK, the health status is UP // If Dashboard and DataSource are both OK, the health status is UP
if (dashboardUp && dataSourceUp) { if (dashboardUp && dataSourceUp) {
withDetails(builder.up(), detailMap); builder.up().withDetails(detailMap);
} }
else { else {
withDetails(builder.down(), detailMap); builder.unknown().withDetails(detailMap);
}
}
private void withDetails(Health.Builder builder, Map<String, Object> detailMap) {
for (String key : detailMap.keySet()) {
builder.withDetail(key, detailMap.get(key));
} }
} }
} }

View File

@ -19,7 +19,12 @@
"description": "earlier initialize heart-beat when the spring container starts when the transport dependency is on classpath, the configuration is effective." "description": "earlier initialize heart-beat when the spring container starts when the transport dependency is on classpath, the configuration is effective."
}, },
{ {
"name": "spring.cloud.sentinel.transport.port", "name": "spring.cloud.sentinel.web-context-unify",
"type": "java.lang.Boolean",
"defaultValue": true,
"description": "Specify whether unify web context(i.e. use the default context name), and is true by default."
},
{ "name": "spring.cloud.sentinel.transport.port",
"type": "java.lang.String", "type": "java.lang.String",
"defaultValue": "8719", "defaultValue": "8719",
"description": "sentinel api port." "description": "sentinel api port."

View File

@ -74,7 +74,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
"spring.cloud.sentinel.flow.coldFactor=3", "spring.cloud.sentinel.flow.coldFactor=3",
"spring.cloud.sentinel.eager=true", "spring.cloud.sentinel.eager=true",
"spring.cloud.sentinel.log.switchPid=true", "spring.cloud.sentinel.log.switchPid=true",
"spring.cloud.sentinel.transport.dashboard=http://localhost:8080", "spring.cloud.sentinel.transport.dashboard=http://localhost:8080,http://localhost:8081",
"spring.cloud.sentinel.transport.port=9999", "spring.cloud.sentinel.transport.port=9999",
"spring.cloud.sentinel.transport.clientIp=1.1.1.1", "spring.cloud.sentinel.transport.clientIp=1.1.1.1",
"spring.cloud.sentinel.transport.heartbeatIntervalMs=20000" }, webEnvironment = RANDOM_PORT) "spring.cloud.sentinel.transport.heartbeatIntervalMs=20000" }, webEnvironment = RANDOM_PORT)

View File

@ -41,8 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest(classes = { SentinelBeanAutowiredTests.TestConfig.class }, properties = { @SpringBootTest(classes = { SentinelBeanAutowiredTests.TestConfig.class },
"spring.cloud.sentinel.filter.order=111" }) properties = { "spring.cloud.sentinel.filter.order=111" })
public class SentinelBeanAutowiredTests { public class SentinelBeanAutowiredTests {
@Autowired @Autowired
@ -118,4 +118,4 @@ public class SentinelBeanAutowiredTests {
} }
} }

View File

@ -109,9 +109,9 @@ public class SentinelHealthIndicatorTests {
Health health = sentinelHealthIndicator.health(); Health health = sentinelHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN); assertThat(health.getStatus()).isEqualTo(Status.UNKNOWN);
assertThat(health.getDetails().get("dashboard")).isEqualTo( assertThat(health.getDetails().get("dashboard")).isEqualTo(new Status(
new Status(Status.DOWN.getCode(), "localhost:8080 can't be connected")); Status.UNKNOWN.getCode(), "localhost:8080 can't be connected"));
} }
@Test @Test
@ -163,13 +163,13 @@ public class SentinelHealthIndicatorTests {
Health health = sentinelHealthIndicator.health(); Health health = sentinelHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN); assertThat(health.getStatus()).isEqualTo(Status.UNKNOWN);
Map<String, Status> dataSourceDetailMap = (Map<String, Status>) health Map<String, Status> dataSourceDetailMap = (Map<String, Status>) health
.getDetails().get("dataSource"); .getDetails().get("dataSource");
assertThat(dataSourceDetailMap.get("ds1-sentinel-file-datasource")) assertThat(dataSourceDetailMap.get("ds1-sentinel-file-datasource"))
.isEqualTo(Status.UP); .isEqualTo(Status.UP);
assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource")) assertThat(dataSourceDetailMap.get("ds2-sentinel-file-datasource"))
.isEqualTo(new Status(Status.DOWN.getCode(), "fileDataSource2 error")); .isEqualTo(new Status(Status.UNKNOWN.getCode(), "fileDataSource2 error"));
} }
} }