(1)Hystrix缺点:
(2)作用:
(3)特性
(1)下载:https://github.com/alibaba/Sentinel/releases
(2)接下来步骤查看:https://blog.csdn.net/weixin_45176509/article/details/123583281
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>0.9.0.RELEASEversion>
dependency>
<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>cloud2022artifactId>
<groupId>org.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>org.consumer8083groupId>
<artifactId>cloud-alibaba-consumer8083artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>0.9.0.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.2.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>0.9.0.RELEASEversion>
dependency>
<dependency>
<groupId>com.commonsgroupId>
<artifactId>commonsartifactId>
<version>1.0-SNAPSHOTversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
dependency>
dependencies>
project>
server:
port: 8083
spring:
application:
name: consumer-nacos
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: localhost:8848
file-extension: yaml
sentinel: #配置
transport:
dashboard: localhost:8080
port: 8719 #默认为8719,如果占用会从8719+1开始扫描寻找未占用端口
management: #暴露监控
endpoints:
web:
exposure.include: '*'
package com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerDemoApplication.class, args);
}
}
package com.consumer.Controller;
import com.commons.Entity.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("nacos/")
@RefreshScope //动态刷新
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@Value("${spring.datasource.username}")
private String configInfo;
@Value("${service-url.nacos-user-service}")
private String url;
@GetMapping("/{id}")
public Result saveEntity(@PathVariable("id") Integer id) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
@GetMapping("/getInfo")
public String getInfo() {
return configInfo;
}
}
直接:默认模式;
链路:多个请求操作同一个微服务
快速失败:默认处理;
WarmUp(预热):长期处于低访问,在某一时刻高访问,需要通过限流慢慢启动,预热时长之后才会慢慢达到单价阈值;
Sentinel断路器非开即断,没有半开状态;
RT(平均响应时间):当1秒内进入5个请求,平均响应时间均超过阈值,那么接下来的时间窗口之内,调用这个方法就会自动熔断,时间窗口期结束,关闭熔断。默认为4900ms,若要设置上线需要配置;
异常比例:当统计时间内,请求次数达到设置数,异常次数达到异常数,则发生熔断,超过熔断时长,关闭熔断;
异常数:当统计时间内,请求次数达到设置数,异常比列到达设置比例,则发生熔断,超过熔断时长,关闭熔断;
实现:
@SentinelResource(value = "nacosId",blockHandler = "dealHandler")
该注解执行的是配置的热点规则,如果出现java异常,不会执行限流方法;
package com.consumer.Controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
import com.sun.deploy.security.BlockedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("nacos/")
@RefreshScope //动态刷新
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@Value("${spring.datasource.username}")
private String configInfo;
@Value("${service-url.nacos-user-service}")
private String url;
@GetMapping("/{id}")
@SentinelResource(value = "nacosId",blockHandler = "dealHandler")
public Result saveEntity(@PathVariable("id") Integer id,
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
public Result dealHandler(Integer id, String p1, String p2, BlockException blockException){
Result result = new Result(404,"/","");
return result;
}
}
踩坑:com.alibaba.csp.sentinel.slots.block.flow.FlowException
参数例外项
维度 | 描述 |
---|---|
Load自适应 | 系统最高负载,建议取值 CPU cores * 2.5 |
并发线程数 | 单机应用的最大线程并发数 |
入口QPS | 单机应用维度入口QPS |
平均RT | 单机应用所有请求的平均RT |
CPU使用率 | CPU使用率,取值范围[0, 1] |
不支持private方法;
@RestController
public class RateLimitController {
@GetMapping("/getRate")
@SentinelResource(value = "getRate" ,blockHandler = "error")
public Result get(){
return new Result(200,"正常访问","200");
}
public Result error(BlockException blockException){
return new Result(404,blockException.getClass().getName()+"异常访问","404");
}
}
存在2种配置:
如果有配置blockHandler 方法,则使用该方法,没有则使用系统默认方法;
以上配置导致业务代码和处理代码混合,每个方法都需要配置,导致代码膨胀,改造如下:
package com.consumer.Handler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
public class BlockHandler {
public static Result error1(BlockException blockException){
return new Result(404,blockException.getClass().getName()+"异常访问1","404");
}
public static Result error2(BlockException blockException){
return new Result(404,blockException.getClass().getName()+"异常访问2","404");
}
}
@SentinelResource(value = "getRate" ,blockHandlerClass = BlockException.class,blockHandler = "error1")
package com.consumer.Controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RateLimitController {
@GetMapping("/getRate")
@SentinelResource(value = "getRate" ,blockHandlerClass = BlockException.class,blockHandler = "error1")
public Result get(){
return new Result(200,"正常访问","200");
}
}
Sentinel核心API:Sphu(定义资源)、Tracer(定义统计)、ContextUtil(定义上下文)
pom.xml
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>0.9.0.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.2.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>0.9.0.RELEASEversion>
dependency>
application.yml
spring:
application:
name: consumer-nacos
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: localhost:8848
file-extension: yaml
sentinel: #配置******************************
transport:
dashboard: localhost:8080
port: 8719 #默认为8719,如果占用会从8719+1开始扫描寻找未占用端口
management: #暴露监控
endpoints:
web:
exposure.include: '*'
主启动
package com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerDemoApplication.class, args);
}
}
业务Controller
package com.consumer.Controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
import com.sun.deploy.security.BlockedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("nacos/")
@RefreshScope //动态刷新
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@Value("${spring.datasource.username}")
private String configInfo;
@Value("${service-url.nacos-user-service}")
private String url;
@GetMapping("/{id}")
@SentinelResource(value = "nacosId")
public Result saveEntity(@PathVariable("id") Integer id,
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
}
RestTemplate配置
package com.consumer.Config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
只改造业务Controller,其他相同
package com.consumer.Controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
import com.sun.deploy.security.BlockedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("nacos/")
@RefreshScope //动态刷新
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@Value("${spring.datasource.username}")
private String configInfo;
@Value("${service-url.nacos-user-service}")
private String url;
@GetMapping("/{id}")
//fallback处理运行时异常,blockHandler处理配置违规
@SentinelResource(value = "nacosId",fallback = "dealHandler1" ,blockHandler = "dealHandler2")
public Result saveEntity(@PathVariable("id") Integer id,
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
public Result dealHandler1(Integer id, String p1, String p2, BlockException blockException){
Result result = new Result(404,"/","1");
return result;
}
public Result dealHandler2(Integer id, String p1, String p2, BlockException blockException){
Result result = new Result(404,"/","2");
return result;
}
}
当二者同时出现方法调用时,blockHandler 优先级高于fallback ;
异常忽略 exceptionsToIgnore
@GetMapping("/{id}")
@SentinelResource(value = "nacosId",fallback = "dealHandler1" ,blockHandler = "dealHandler2",
exceptionsToIgnore = {IllegalArgumentException.class})
public Result saveEntity(@PathVariable("id") Integer id,
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
配置该属性之后,如果发生IllegalArgumentException异常不会执行fallback 指定方法;
断路方式对比
Sentinel | Hystrix |
---|---|
信号量隔离 | 线程池、信号量隔离 |
基于响应时间、异常比率、异常数 | 基于异常比率 |
基于QPS、调用关系限流 | 有限的限流支持 |
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
dependency>
<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>cloud2022artifactId>
<groupId>org.examplegroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<groupId>org.consumer8083groupId>
<artifactId>cloud-alibaba-consumer8083artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
<version>0.9.0.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.2.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>0.9.0.RELEASEversion>
dependency>
<dependency>
<groupId>com.commonsgroupId>
<artifactId>commonsartifactId>
<version>1.0-SNAPSHOTversion>
<scope>compilescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.10version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
dependency>
dependencies>
project>
#数据数据源
datasource:
ds1: #数据源1
nacos:
server-addr: localhost:8848 #nacos地址
dataId: cloudalibaba-sentinel-service #dataId名称
groupId: DEFAULT_GROUP #分组名称
data-type: json #文件类型
rule-type: flow
server:
port: 8083
spring:
application:
name: consumer-nacos
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: localhost:8848
file-extension: yaml
sentinel: #配置
transport:
dashboard: localhost:8080
port: 8719 #默认为8719,如果占用会从8719+1开始扫描寻找未占用端口
#数据数据源
datasource:
ds1: #数据源1
nacos:
server-addr: localhost:8848 #nacos地址
dataId: cloudalibaba-sentinel-service #dataId名称
groupId: DEFAULT_GROUP #分组名称
data-type: json #文件类型
rule-type: flow
management: #暴露监控
endpoints:
web:
exposure.include: '*'
#激活feign对sentinel支持
feign:
sentinel:
enabled: true
package com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerDemoApplication.class, args);
}
}
resourcenacosId:资源名称
limitAppdefalut: 来源应用
grade:阈值类型:0-线程数 1-QPS
count:单机阈值
strategy: 流控模式 0-直接 1-关联 2-链路
controlBehavior:流控效果:0-快速失败 2-WarmUp 3-表示排队等待
clusterModefalse :是否集群