springcloud--part 4 : 断路器 (Hystrix)
摘自 方志朋的博客--http://blog.csdn.net/forezp/article/details/69934399
前言
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
一 断路器简介
Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的,如果较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystrix 是5秒20次) 断路器将会被打开。
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。
二 准备工作
启动 eureka-server,端口 8761 ;
启动 eureka-client ,端口8762 ;
三 在 Ribbon 中使用断路器
3.1 导入 Hystrix 依赖包
<!--导入熔断器Hystrix的依赖包--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
3.2 在主程序入口类上加上注解 @EnableHystrix 表示开启Hystrix
@SpringBootApplication @EnableDiscoveryClient @EnableHystrix //开启 Hystrix public class ServiceRibbonApplication { @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ServiceRibbonApplication.class, args); } }
3.3 在Service 中的方法上加上@HystrixCommand(fallbackMethod=" 方法名 "),表示如果调用此服务出现错误时,给出的错误提示页面(需要重新写一个方法)。
该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串,字符串为”hi,”+name+”,sorry, it is error!”,
/**
* 在应用ribbon + restTemplate 调用服务的方式中增加hystrix的步骤
* 1.导包 spring-cloud-starter-hystrix
* 2.在主程序入口类上增加注解 @EnableHystrix
* 3.在Service 的getClient方法上增加 @HystrixCommand( fallback= " hiError ")
* 4.在service中增加方法 hiError ,给出熔断之后返回的错误提示页面。
* 5.测试运行
* 保证service-hi 服务连接,访问 http://localhost:8765/hello?name=zdj -->this port is : 8762 , zdj
* 断开 service-hi ,再次访问,--> hi , zdj , sorry,it is error! , zdj
*/
@Service public class RestTemplateService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiError") public String getClient(String name){ return restTemplate.getForObject("http://SERVICE-HI/hi?name=?"+name,String.class); } public String hiError(String name){ return "hi , "+name+" , sorry,it is error!"; } }
***********************************************
注意:hiError( )方法,此处如果是不带参数(String name)或者参数类型与getClient( String name) 不一致的,在运行ribbon这个服务的时候直接报错,报 fallback method wasn't found: hiError([class java.lang.String])
该方法必须和getClient(String name)完全匹配,参数类型不同都不可以;
***********************************************
3.4 运行测试 (注意 此时service-hi 服务时正常开启的)
启动 ribbon,访问 http://localhost:8765/hello?name=*** --> this port is : 8762
断开service-hi 服务,再次访问,--> hi , *** , sorry, it is error, ***
这就说明当 service-hi 工程不可用的时候,service-ribbon调用 service-hi的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。
四 Feign 中使用断路器 Hystrix
因为 feign 中已经支持了 Hystrix ,所以在 Feign 中使用 Hystrix 时,不需要导包,也不需要在入口类上面增加额外的注解;
4.1 Feign 虽然支持了 Hystrix ,但是默认情况下是关闭的,需要在 .yaml 配置文件配置
server:
port: 8764
spring:
application:
name: feign-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
# 将feign集成的断路器设置成有效状态
feign:
hystrix:
enabled: true
# 启动时 总是发现第一个进入的是fallback的方法,因为hystrix 默认超时的时间是1s,如果1秒内没有响应,就会被认为服务有问题;此处将默认时间设置成为5秒,避免首次进去的就是fallback方法
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
( 在设置这个属性的时候,yaml文件中没有提示,但是继续写上去,是支持的,因为是feign自身支持的,所以没有给提示 )
4.2 修改 FeignService 接口 , 只需要在接口的注解上加上 fallback=***.class 属性即可
在@FeignClient (value=" service-hi " , fallback = hystrixMethod.class ) , 表明如果调用该服务时,这个服务是 宕掉的 ,那么 选择断路器跳转的方法去;
@Service
@FeignClient(value = "service-hi",fallback = hystrixMethod.class)
public interface FeignService {
@RequestMapping("/hi")
String getClintOne(String name);
}
4.3 hystrixMethod 需要实现FeignService 接口,并需要注入到Ioc容器中,
@Component public class hystrixMethod implements FeignService { @Override public String getClintOne(String name) { return "sorry , it is error , "+ name; } }
4.4 此时 启动服务,访问 Http://localhost:8765/hello?name=***
在service - hi 服务是正常启动的时候 --> this port is 8762 ;
在service - hi 服务挂掉的时候访问 --> sorry , it is error , zdj ;
说明:Hystrix 断路器起到作用了。
五 Hystrix Dashboard (Hystrix 仪表盘)
restTemplate + ribbon:
/**
* Hystrix Dashboard 步骤
* 1. 导包 spring-cloud-starter-actuator(spring boot提供的对应用系统的自省和监控的集成功能);
* spring-cloud-starter-hystrix-dashboard
* 2. 在主程序入口类上加上注解 @EnableDashboard 注解 ,开启仪表盘
* 3. 访问网址 http://localhost:8765/hystrix
* 在出现的界面中输入网址http://localhost:8765/hystrix.stream title: haha
* 点击 Monitor Sream
*/
feign :
/**
* 给用 Feign 调用服务的方式上加dashboard :
* 1. 导包 spring-cloud-starter-hystrix-dashboard + spring-boot-starter-actuator;
* 2. 在主程序入口类上加上注解 : @EnableHystrixDashboard
* 3. 访问 http://localhost:8765/hystrix
* 在出现的界面地址栏输入 http://localhost:8765/hystrix.stream , title : hihi
* ---Unable to connect Comm and Metric Stream
* 4. 导包 spring-cloud-starter-hystrix ; 在 主程序入口类上加上注解 @EnableCircuitBreaker 运行即可
*/