mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
import changes from master
reorganizing modules
This commit is contained in:
@@ -30,6 +30,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>com.alibaba.csp</groupId>-->
|
||||
|
@@ -184,7 +184,7 @@ Spring Boot 应用支持通过 Endpoint 来暴露相关信息,Sentinel Starter
|
||||
* Spring Boot 1.x 中添加配置 `management.security.enabled=false`
|
||||
* Spring Boot 2.x 中添加配置 `management.endpoints.web.exposure.include=*`
|
||||
|
||||
Spring Boot 1.x 可以通过访问 http://127.0.0.1:18083/sentinel 来查看 Sentinel Endpoint 的信息。Spring Boot 2.x 可以通过访问 http://127.0.0.1:18083/acutator/sentinel 来访问。
|
||||
Spring Boot 1.x 可以通过访问 http://127.0.0.1:18083/sentinel 来查看 Sentinel Endpoint 的信息。Spring Boot 2.x 可以通过访问 http://127.0.0.1:18083/actuator/sentinel 来访问。
|
||||
|
||||
<p align="center"><img src="https://cdn.yuque.com/lark/0/2018/png/54319/1532084199224-1a41591d-7a06-4680-be8a-5de319ac635d.png" width="480" heigh='360' ></p>
|
||||
|
||||
|
@@ -193,15 +193,27 @@ Now ReadableDataSource type support 5 categories: `file`, `nacos`, `zk`, `apollo
|
||||
|
||||
If you want to use `nacos`, `zk`, `apollo` or `redis` ReadableDataSource, you could add `sentinel-datasource-nacos`, `sentinel-datasource-zookeeper`,`sentinel-datasource-apollo` or `sentinel-datasource-redis` dependency.
|
||||
|
||||
|
||||
|
||||
When ReadableDataSource load rule data successfully, console will print some logs:
|
||||
|
||||
```
|
||||
[Sentinel Starter] DataSource ds1-sentinel-file-datasource load 3 DegradeRule
|
||||
[Sentinel Starter] DataSource ds2-sentinel-nacos-datasource load 2 FlowRule
|
||||
```
|
||||
## Warning
|
||||
You should use `file` ReadableDataSource in a fatjar carefully or you may get error like this below
|
||||
|
||||
```
|
||||
java.lang.RuntimeException: [Sentinel Starter] DataSource ds1 handle file [classpath: flowrule.json] error: class path resource [flowrule.json] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:xxx/xxx.jar!/BOOT-INF/classes!/flowrule.jso
|
||||
```
|
||||
|
||||
You could use absolute path when you use File datasource & fat jar.
|
||||
It is recommended to use Nacos/Apollo/Zookeeper/Redis datasource to store rules.
|
||||
|
||||
https://github.com/alibaba/spring-cloud-alibaba/issues/428
|
||||
|
||||
## More
|
||||
For more information about Sentinel, see [Sentinel Project](https://github.com/alibaba/Sentinel).
|
||||
|
||||
If you have any ideas or suggestions for Spring Cloud Sentinel starter, please don't hesitate to tell us by submitting github issues.
|
||||
|
||||
|
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples;
|
||||
|
||||
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
|
||||
@@ -9,7 +25,11 @@ import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
/**
|
||||
* @author fangjian
|
||||
*/
|
||||
public class ExceptionUtil {
|
||||
public final class ExceptionUtil {
|
||||
|
||||
private ExceptionUtil() {
|
||||
|
||||
}
|
||||
|
||||
public static SentinelClientHttpResponse handleException(HttpRequest request,
|
||||
byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
|
||||
|
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples;
|
||||
|
||||
import java.util.List;
|
||||
@@ -11,9 +27,11 @@ import com.alibaba.fastjson.TypeReference;
|
||||
* @author fangjian
|
||||
*/
|
||||
public class JsonFlowRuleListConverter implements Converter<String, List<FlowRule>> {
|
||||
|
||||
@Override
|
||||
public List<FlowRule> convert(String source) {
|
||||
return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,10 +1,33 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import com.alibaba.cloud.circuitbreaker.sentinel.SentinelCircuitBreakerFactory;
|
||||
import com.alibaba.cloud.circuitbreaker.sentinel.SentinelConfigBuilder;
|
||||
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
|
||||
import com.alibaba.csp.sentinel.datasource.Converter;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.circuitbreaker.Customizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@@ -15,7 +38,8 @@ import org.springframework.web.client.RestTemplate;
|
||||
public class ServiceApplication {
|
||||
|
||||
@Bean
|
||||
@SentinelRestTemplate(blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)
|
||||
@SentinelRestTemplate(blockHandler = "handleException",
|
||||
blockHandlerClass = ExceptionUtil.class)
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
@@ -30,6 +54,18 @@ public class ServiceApplication {
|
||||
return new JsonFlowRuleListConverter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Customizer<SentinelCircuitBreakerFactory> defaultConfig() {
|
||||
return factory -> {
|
||||
factory.configureDefault(
|
||||
id -> new SentinelConfigBuilder().resourceName(id)
|
||||
.rules(Collections.singletonList(new DegradeRule(id)
|
||||
.setGrade(RuleConstant.DEGRADE_GRADE_RT).setCount(100)
|
||||
.setTimeWindow(10)))
|
||||
.build());
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ServiceApplication.class, args);
|
||||
}
|
||||
|
@@ -1,10 +1,26 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples;
|
||||
|
||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@@ -17,26 +33,42 @@ public class TestController {
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@RequestMapping(value = "/hello", method = RequestMethod.GET)
|
||||
@Autowired
|
||||
private CircuitBreakerFactory circuitBreakerFactory;
|
||||
|
||||
@GetMapping("/hello")
|
||||
@SentinelResource("resource")
|
||||
public String hello() {
|
||||
return "Hello";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/aa", method = RequestMethod.GET)
|
||||
@GetMapping("/aa")
|
||||
@SentinelResource("aa")
|
||||
public String aa(int b, int a) {
|
||||
return "Hello test";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/test", method = RequestMethod.GET)
|
||||
@GetMapping("/test")
|
||||
public String test1() {
|
||||
return "Hello test";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/template", method = RequestMethod.GET)
|
||||
@GetMapping("/template")
|
||||
public String client() {
|
||||
return restTemplate.getForObject("http://www.taobao.com/test", String.class);
|
||||
}
|
||||
|
||||
@GetMapping("/slow")
|
||||
public String slow() {
|
||||
return circuitBreakerFactory.create("slow").run(() -> {
|
||||
try {
|
||||
Thread.sleep(500L);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "slow";
|
||||
}, throwable -> "fallback");
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.cloud.examples;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class WebMvcConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/errorPage").setViewName("errorPage");
|
||||
}
|
||||
|
||||
}
|
@@ -1,8 +1,17 @@
|
||||
spring.application.name=sentinel-example
|
||||
server.port=18083
|
||||
management.endpoints.web.exposure.include=*
|
||||
management.endpoint.health.show-details=always
|
||||
|
||||
# we can disable health check, default is enable
|
||||
management.health.diskspace.enabled=false
|
||||
# management.health.sentinel.enabled=false
|
||||
|
||||
spring.cloud.sentinel.transport.dashboard=localhost:8080
|
||||
spring.cloud.sentinel.eager=true
|
||||
|
||||
#spring.cloud.sentinel.block-page=/errorPage
|
||||
#spring.cloud.sentinel.filter.enabled=false
|
||||
#spring.cloud.sentinel.http-method-specify=false
|
||||
|
||||
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
|
||||
@@ -20,7 +29,4 @@ spring.cloud.sentinel.datasource.ds4.file.file=classpath: system.json
|
||||
spring.cloud.sentinel.datasource.ds4.file.rule-type=system
|
||||
|
||||
spring.cloud.sentinel.datasource.ds5.file.file=classpath: param-flow.json
|
||||
spring.cloud.sentinel.datasource.ds5.file.rule-type=param-flow
|
||||
|
||||
|
||||
management.endpoint.health.show-details=always
|
||||
spring.cloud.sentinel.datasource.ds5.file.rule-type=param_flow
|
||||
|
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
This is error page.
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user