Hystrix断路器
是一种保护机制。Hystrix也是Netflix公司的一款组件。
Hystrix的几个重要概念
(1)服务降级:
服务器忙碌或者网络拥堵时,不让客户端等待并立刻返回一个友好提示,
相当于当程序运行异常时 给一个友善的回应 给程序降一级
(2)服务熔断
相当于一层保险 ,当服务访问量到最大时 相当于一个最后底线
(3)服务限流
到了一定程度 线程有序排队 限制进入
Hystrix案例
导入pom
<dependencies>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</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>
<dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.jiahui</groupId>
<artifactId>jiahui-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
编写配置文件老一套
server:
port: 8001
spring:
application:
name: cloud-payment-service #服务名称
eureka:
client:
# 注册进 Eureka 的服务中心
register-with-eureka: true
# 检索 服务中心 的其它服务
fetch-registry: true
service-url:
# 设置与 Eureka Server 交互的地址
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
启动类
@SpringBootApplication
@DiscoveryClient
public class HystrixPaymentApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixPaymentApplication.class,args);
}
}
编写业务代码
服务降级
导入pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
我导入的时候一直报一个找不到依赖包 的错 后来我加入了版本号才好了
版本不一样可能就会报错
当你写完测试并发时 会报一个错
这个时候就用到了服务降级
/**
超时访问的方法
*/
@HystrixCommand(fallbackMethod = "timeoutHandler",commandProperties = {
//设置峰值,超过 3 秒,就会调用兜底方法,这个时间也可以由feign控制
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public String paymentInfo_Timeout(Integer id){
// int interTime = 5;
int i = 10/0;
// try {
// TimeUnit.SECONDS.sleep(interTime);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// return "线程池"+Thread.currentThread().getName()+ " ,paymentInfo_Timeout,id:" + id + "耗时" + interTime + "秒钟";
return "hello";
}
// 降级的逻辑
public String timeoutHandler(Integer id){
return "服务异常,请重试......";
}
为了保险 启动类加上熔断
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启feign客户端
@EnableCircuitBreaker//开启服务熔断
public class HystrixOrderApplication80 {
public static void main(String[] args) {
SpringApplication.run(HystrixOrderApplication80.class,args);
}
}
启动测试
我们发现咱们项目有内部错误 或者是服务不可用了 都可以使用降级来处理
看着友善