一、下载Sentinel服务端
1)、github找到Sentinel
https://github.com/alibaba/Sentinel
2)、启动
java -jar sentinel-dashboard-1.7.0.jar
访问Sentinel控制台
http://localhost:8080/
二、SpringCloud项目整合案例
1)、相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.zhq.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-sentinel-service8401</artifactId>
<dependencies>
<!--SpringCloud alibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--SpringCloud alibaba sentinel sentinel-datasource-nacos 持久化相关 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud alibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.zhq.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2)、相关配置
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会依次+1扫描,直到找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
3)、启动类和测试代码
/**
* @author Mr.Zheng
* @Program: cloud2020
* @Description: 项目启动类
* @date 2020-10-29 14:52
*/
@SpringBootApplication
@EnableDiscoveryClient
public class MainApp8401 {
public static void main(String[] args) {
SpringApplication.run(MainApp8401.class,args);
}
}
@Slf4j
@RestController
public class FlowLimitController {
@GetMapping("/testA")
public String testA() {
return "testA";
}
@GetMapping("/testB")
public String testB() {
try {
TimeUnit.MILLISECONDS.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "testB";
}
@GetMapping("/testC")
public String testC() {
log.info("testC");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "testC";
}
}
4)、配置限流熔断服务降级
5)、自定义资源和错误提示
默认资源为接口访问路径,错误提示为Sentinel自带的,如果要自定义,请使用@SentinelResource,案例如下:表示资源名为testHotKey,规则不满足走errorTestHotKey方法
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey",blockHandler = "errorTestHotKey")
public String testHotKey(
@RequestParam(value = "p1", required = false) String p1,
@RequestParam(value = "p2", required = false) String p2) {
return "testHotKey";
}
public String errorTestHotKey(String p1, String p2, BlockException b){
return "errorTestHotKey";
}
如果要全局指定可以
@GetMapping("/testHandleException")
@SentinelResource(value = "testHandleException",blockHandler = "handlerException",blockHandlerClass = CustomerBlockHandler.class)
public CommonResult testHandleException(){
r
public class CustomerBlockHandler {
public static CommonResult handlerException(BlockException e){
return new CommonResult(500,"自定义全局提示,服务忙,请稍后再试!");
}
public static CommonResult handlerException1(BlockException e){
return new CommonResult(500,"自定义全局提示1,服务忙,请稍后再试!");
}
}
6)、服务降级、熔断一起配置复杂案例
fallback只管java异常,blockHandler 只管sentinel规则异常,如果两个同时触发,最终返回blockHandler ,exceptionsToIgnore 是排除某些异常,不参与
@RestController
@Slf4j
public class OrderNacosController {
@Value("${service-url.nacos-payment-service}")
private String serverURL;
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id){
return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class);
}
@GetMapping("/consumer/testFallback/{id}")
@SentinelResource(value = "testFallback",fallback = "fallback",blockHandler = "handleException",exceptionsToIgnore = {NullPointerException.class})
public CommonResult<Payment> testFallback(@PathVariable("id") Long id){
if(id==4){
throw new IllegalArgumentException("非法参数");
}
CommonResult<Payment> result= restTemplate.getForObject(serverURL+"/payment/nacos/"+id,CommonResult.class,id);
if(result.getData()==null){
throw new NullPointerException("id没有记录");
}
return result;
}
public CommonResult fallback(@PathVariable("id") Long id,Throwable e){
return new CommonResult(500,e.getClass().getCanonicalName()+"fallback管java异常,服务不可用");
}
public CommonResult handleException(@PathVariable("id") Long id,BlockException e){
return new CommonResult(500,e.getClass().getCanonicalName()+"handleException管Sentinel规则异常,服务不可用");
}
}