服务降级
什么是服务降级?当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作。
一、微服务提供者服务降级【更适合做客户端降级】
- 引入 Hystrix 依赖
<!-- Hystrix 服务降级 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 修改 Controller 代码,使用 HystrixCommand 进行服务降级
@HystrixProperty 规定三秒以内的逻辑为正常逻辑。
providerTimeOutHandler 方法,当调用的方法出错时,调用该方法。
我们让测试程序睡眠 5 秒,测试是否会进行降级
@RestController
public class PHystarixController {
@Value("${server.port}")
private String port;
@GetMapping(value = "/provider/hystrix/timeout/{name}")
@HystrixCommand(fallbackMethod = "providerTimeOutHandler",commandProperties = {
//规定三秒以内的逻辑为正常逻辑
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public String providerTimeOut(@PathVariable("name") String name){
//int a=10/0; 错误也可导致调用 providerTimeOutHandler 方法
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
return name+" Print From Provider[TimeOut] "+port;
}
/**
* fallback 方法 当方法出现异常时调用该方法
* @param name
* @return
*/
public String providerTimeOutHandler(@PathVariable("name") String name){
return name+" Print From providerTimeOutHandler 系统繁忙,请稍后再试!"+port;
}
}
- 启动类开启注解 @EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class ProviderApp8001 {
public static void main(String[] args) {
SpringApplication.run(ProviderApp8001.class);
}
}
- 测试服务提供者代码,是否进行了降级
发现程序做了降级处理

二、 微服务消费者服务降级 【更适合做客户端降级】
- 引入 Hystrix 依赖
<!-- Hystrix 服务降级 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 修改 yml,新增开启 hystrix
feign:
hystrix:
enabled: true
- 修改 Controller 代码,使用 HystrixCommand 进行服务降级
@RestController
public class CHystrixController {
@Resource
private OpenFeginService service;
@GetMapping(value = "/consumer/hystrix/ok/{name}")
public String consumerOk(@PathVariable("name")String name){
return service.providerOK(name)+" And Consumer[OK]";
}
@GetMapping(value = "/consumer/hystrix/timeout/{name}")
@HystrixCommand(fallbackMethod = "consumerTimeOutHandler",commandProperties = {
//规定三秒以内的逻辑为正常逻辑
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
})
public String consumerTimeOut(@PathVariable("name")String name){
return service.providerTimeOut(name)+" And Consumer[TimeOut]";
}
/**
* fallback 方法 当方法出现异常时调用该方法
* @param name
* @return
*/
public String consumerTimeOutHandler(@PathVariable("name") String name){
return name+" Print From consumerTimeOutHandler 系统繁忙,请稍后再试!";
}
}
- 启动类开启注解 @EnableCircuitBreaker
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients //开启使用 OpenFegin
@EnableHystrix //@EnableHystrix继承了@EnableCricuitBreaker
//@RibbonClient(value = "cloud-provider",configuration = MySelfRule.class) 开启自定义复杂均衡策略
public class ConsumerApp81 {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp81.class);
}
}
- 测试服务提供者代码,是否进行了降级
发现程序做了降级处理
测试结果.png
网友评论