SpringCloud Hystrix服务应用熔断服务降级@HystrixCommand fallbackMethod
个人博客网:www.lfuping.cn (你想要这里多有)
Hystrix服务熔断服务降级@HystrixCommand fallbackMethod
熔断机制是应对雪崩效应的一种微服务链路保护机制。
当某个服务不可用或者响应时间超时,会进行服务降级,进而熔断该节点的服务调用,快速返回自定义的错误影响页面信息。
一、修改服务生产者项目springcloud-provider-1,springcloud-provider-2(两个项目修改内容都是一样的):
1.pom.xml加下 hystrix支持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2.在项目的启动类中加**解支持 @EnableCircuitBreaker;
3.在Controller类中新增方法getInfo:
package com.li.spingcloud.provider.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.li.spingcloud.provider.service.StudentService;
import com.li.springcloud.common.model.Student;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class ProviderController {
@Autowired
private StudentService studentService;
/**
* 查询
*
* @return
*/
@RequestMapping(value = "/provider/list", produces = "application/json; charset=utf-8", method = {
RequestMethod.POST, RequestMethod.GET })
public List<Student> list() {
System.out.println("ProviderController----我是生产者1");
return studentService.list();
}
/**
* 查询 Hystrix的方式
*
* @return
* @throws InterruptedException
*/
// 加上@HystrixCommand注解 以及 fallbackMethod
// 表明这个方法再没有异常以及没有超时(hystrix默认1秒算超时)的情况,才返回正常的业务数据;
@HystrixCommand(fallbackMethod = "getInfoFallback")
@RequestMapping(value = "/provider/listHystrix", produces = "application/json; charset=utf-8", method = {
RequestMethod.POST, RequestMethod.GET })
public List<Student> listHystrix() throws InterruptedException {
Thread.sleep(2000);
System.out.println("ProviderController----我是生产者1");
// 模拟调用服务超时
// 然后采取 Hystrix进行服务降级,进而熔断该节点的服务调用,快速返回自定义的错误影响页面信息。
return studentService.list();
}
/**
* 组成返回错误信息的map
*
* @return
* @throws InterruptedException
*/
public List<Student> getInfoFallback() throws InterruptedException {
List<Student> list = new ArrayList<Student>();
Student stu=new Student();
stu.setName("系统繁忙,请稍后重试....");
stu.setId(500);
list.add(stu);
return list;
}
}
正常访问返回的是查询到的业务数据;
Thread.sleep(2000) 模拟超时;
加上@HystrixCommand注解 以及 fallbackMethod
表明这个方法再没有异常以及没有超时(hystrix默认1秒算超时)的情况,才返回正常的业务数据;
否则,进入我们fallback指定的本地方法,500是表示系统出错,稍后重试,有效的解决雪崩效应,以及返回给用户界面很好的报错提示信息;
4、在springcloud-consumer-1消费者项目修改Controller类的调用地址:
package com.li.spingcloud.consumer.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.li.springcloud.common.model.Student;
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
/**
* 查询数据库
*
* @return
*/
@RequestMapping(value = "/consumer/list", produces = "application/json; charset=utf-8", method = {
RequestMethod.POST, RequestMethod.GET })
public List<Student> list() {
return restTemplate.getForObject("http://PROVIDER/provider/listHystrix",
List.class);
}
}
5.测试先启动Erueka集群或者单机,在启动服务生产者项目集群或者单机,在启动pringcloud-consumer-1消费者项目;最后访问页面地址结果如下:
因为 Hystrix默认1秒算超时,所有 sleep了2秒 所以进入自定义fallback方法,防止服务雪崩;返回了错误信息;
6.Hystrix默认1秒算超时这个时间也是可以进行配置的:
在生产者项目application.yml配置文件下:
#hystrix超时时间配置 (如果不配置的话默认是1000毫秒超时)
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1000
当配置的超时间小于,调用服务的延迟时间就不会触发熔断机制。
附上源码:https://pan.baidu.com/s/1flBSgjBq_Rua6B77jphYPw
提取码: r2ua
欢迎关注我的微信公众号:平川大叔