1)背景
在微服务中,服务与服务之间,不可避免地存在一些相互调用。Springcloud可以用RestTemplate+Ribbon或Feign,但是由于网络或者自身的问题,服务无法做到100%可用;如:服务死掉,又或者某个服务性能出现问题,导致调用链响应时间过长,导致响应失败等等,可能导致服务“雪崩”。因此,提出了熔断器这种保障机制。
2)简介
简单的说,就是服务预先设置一个固定值,若是服务调用失败时,即返回该固定值给调用者。
3)依赖组件:注册中心与服务发现,服务提供者(eureka client注册到注册中心中)
4)搭建Hystrix
4.1)在Ribbon中使用熔断器(在rest+ribbon基础上改造)
4.1.1)添加pom.xml配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
4.1.2)配置类(调整两个地方,启动类和调用处)
启动类:添加@EnableHystrix,启动熔断器
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
service服务调用处:添加@HystrixCommand注解,启动熔断功能,并指定了fallbackMethod熔断方法
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class UserService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "findAllFallBack")
public Map findAll() {
return restTemplate.getForObject("http://PANDA-FRAME/user/findAll",Map.class);
}
public Map findAllFallBack() {
Map<String,Object> map = new HashMap<String,Object>();
map.put("rtn", false);
map.put("info", "获取接口数据失败!");
return map;
}
}
4.1.3)测试
正常调用时:
当PANDA-FRAME服务挂掉后,触发熔断器
4.2)在Feign中使用熔断器(在Feign基础上改造)
4.2.1)不需要添加pom.xml配置:因为Feign默认集成了熔断器Hystrix,不过默认没有启动而已
4.2.2)配置YML,启动熔断器
feign:
hystrix:
enabled: true #启动feign自带的熔断器
4.2.3)配置类
service服务调用处,调整:添加@FeignClient注解添加fallback参数,指定熔断时,响应的处理类,该类需要实现当前UserService类
import java.util.Map;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value="PANDA-FRAME", fallback = UserServiceHystrix.class)
public interface UserService {
@RequestMapping("/user/findAll")
Map findAll();
}
熔断指定的处理类:这里需要添加@Component,让其被spring扫入ioc容器中
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
@Component
public class UserServiceHystrix implements UserService{
@Override
public Map findAll() {
Map<String,Object> map = new HashMap<String,Object>();
map.put("rtn", false);
map.put("info", "获取接口数据失败!");
return map;
}
}
4.2.4)测试:
正常时,访问接口
当PANDA-FRAME服务挂掉后,触发熔断器
github代码:
riddon方式:https://github.com/April-D-phil/panda-riddon
feign方式:https://github.com/April-D-phil/panda-feign