熔断与降级
熔断:下游 服务挂了,开启熔断
降级:访问 熔断的服务 不能直接抛异常,使用 降级处理返回假数据
引入依赖
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.15</version>
</dependency>
<!--httpClient 的依赖 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
<!--ribbon 的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<!--hystrix依赖,主要是用 @HystrixCommand -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<!--服务注册-->
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--服务调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<!-- mysql 驱动的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!-- mybatis-plus 的依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
配置文件yaml
因为一个接口,理论的最佳响应速度应该在 200ms 以内,或者慢点的接口就几百毫秒
spring:
# 配置数据源信息
datasource:
druid:
# 高并发场景下,万一遇到网络问题,可能会导致你跟数据库的Socket连接异常无法通信
connect-timeout: 1200 # 建立TCP连接的超时时间
socket-timeout: 3000 # 发送请求后 等待响应的超时时间
max-wait: 800 # 0.8s 以上, 大量线程获取不到连接,0.8s 左右快速就失败
maxActive: 20 # 确实有高并发场景,可以适当增加到3~5倍,不超过 100
initial-size: 5 # 初始化大小
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.111.101:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: 123456
mybatis-plus:
configuration:
# 数据库字段:user_id===>实体属性:userId
map-underscore-to-camel-case: true
# 打印sql日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: ASSIGN_ID # 采用 分布式ID策略
feign:
client:
config:
default: # default全局的配置
loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息
httpclient:
enabled: true # 开启 feign对HttpClient的支持
max-connections: 200 # 最大的连接数
max-connections-per-route: 50 # 每个路径的最大连接数
hystrix:
enabled: true #开启熔断机制
compression:
request:
enabled: true # 配置请求GZIP压缩
mime-types: [ "text/xml","application/xml","application/json" ] # 配置压缩支持的MIME TYPE
min-request-size: 2048 # 配置压缩数据大小的下限
response:
enabled: true # 配置 响应 GZIP压缩
hystrix:
command:
default:
execution:
isolation:
strategy: THREAD # THREAD|SEMAPHORE
thread:
timeoutInMilliseconds: 300 # 设置熔断超时时间 default 1000
timeout:
enabled: true # 打开超时熔断功能 default true
ribbon:
ConnectTimeout: 10000 # 设置连接超时时间 default 2000
ReadTimeout: 2000 # 设置读取超时时间 default 5000
OkToRetryOnAllOperations: true # 对所有操作请求都进行重试 default false
MaxAutoRetriesNextServer: 2 # 切换实例的重试次数 default 1
MaxAutoRetries: 2 # 对当前实例的重试次数 default 0
Feign接口
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
@FeignClient(
name = "service-vod", // 调用接口的 服务名
fallback = VodFileDegradeFeignClient.class // 降级类
)
@Component
public interface VodClient {
@DeleteMapping(value = "/eduvod/vod/{videoId}")
public String removeVideo(@PathVariable("videoId") String videoId);
@DeleteMapping(value = "/eduvod/vod/delete-batch")
public String removeVideoList(@RequestParam("videoIdList") List videoIdList);
}
熔断降级类
import org.springframework.stereotype.Component;
import java.util.List;
@Component
// 必须要 实现VodClient,重写的方法为 降级的方法!!!
// 比如 服务不可用,就会调用 降级类中的方法~
public class VodFileDegradeFeignClient implements VodClient {
@Override
public String removeVideo(String videoId) {
return "time out";
}
@Override
public String removeVideoList(List videoIdList) {
return "time out";
}
}