(四)Hystrix容错保护

Feign默认是整合了Ribbon和Hystrix这两个框架,因此代码咱们在上一篇的基础上进行修改,启动Eureka,service-hello,Feignhtml

 

 

所谓的熔断机制和平常生活中见到电路保险丝是很是类似的,当出现了问题以后,保险丝会自动烧断,以保护咱们的电器, 那么若是换到了程序之中呢?java

当如今服务的提供方出现了问题以后整个的程序将出现错误的信息显示,而这个时候若是不想出现这样的错误信息,而但愿替换为一个错误时的内容。git

一个服务挂了后续的服务跟着不能用了,这就是雪崩效应github

 对于熔断技术的实现须要考虑如下几种状况:web

 · 出现错误以后能够 fallback 错误的处理信息;spring

 · 若是要结合 Feign 一块儿使用的时候还须要在 Feign(客户端)进行熔断的配置。springboot

 

在上文的feign项目中,修改启动类

@EnableCircuitBreaker	// 开启Hystrix容错
@EnableDiscoveryClient
@EnableFeignClients      //开启Feign的功能:
@SpringBootApplication
public class SpringCloundEurekaFeginExampleApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloundEurekaFeginExampleApplication.class, args);
	}
}

  

properties文件添加配置,打开hystrix

feign.hystrix.enabled=true

  

实现IFeignService的FallBack

添加FeignServiceFallbackapp

@Component
public class FeignServiceFallback implements IFeginService {
    @Override
    public String index() {
        return "错误了是吗???";
    }
}

  

修改IFeignService框架

//表明改接口用费"service-hello"的服务 提供
@FeignClient(value = "service-hello", fallback = FeignServiceFallback.class)
public interface IFeginService {

    @RequestMapping(value = "/index")
    public String index();
}

  这个仅仅是在@FeignClient注解中增长了fallback的配置,并设置其值为咱们刚刚新建的类:FeignServiceFallback。ide

 

接下来停掉SERVICE-HELLO或者在服务方法直接抛错

能够看到FallBack已经启做用,当所有SERVICE-HELLO不起做用时,SERVICE-FEIGN中的FeignServiceFallback进入了回退处理。

 

在不使用Feign时如何使用Hystrix

其实Hystrix提供了两个对象来支持回退处理:HystrixCommandHystrixObservableCommand,其中后者是用在依赖的服务返回多个操做结果的时候,这里咱们只演示一下HystrixCommand的使用,对于后者可自行尝试,或者查看官方文档

基于Ribbon

添加pom:spring-cloud-starter-netflix-hystrix

启动类开启:@EnableCircuitBreaker

@Service
public class HelloServiceImpl implements IHelloService {

@Autowired
RestTemplate restTemplate;

@HystrixCommand(fallbackMethod = "serviceFallback")
@Override
public String index() {
return restTemplate.getForObject("http://SERVICE-HELLO-2/index", String.class);
}

public String serviceFallback() {
return "错误了吗?";
}
}

 

 

Hystrix监控

Hystrix除了实现服务容错以外,还提供了对服务请求的监控:每秒执行的请求数、成功数等。开启Hystrix的监控很是简单,
一个是添加 spring-cloud-starter-hystrix(spring-cloud-starter-netflix-hystrix
二是添加 spring-boot-starter-actuator,可以让 /hystrix-stream端点能够获取到Hystrix的监控数据。
针对 上文的feign项目
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

springboot2的查看/actuator 是没办法查看全部监控接口的,须要在application.properties添加

management.endpoints.web.exposure.include=*

 

 

能够看到页面会重复输出一些统计数据(注: 你要先尝试访问一下所提供的服务才会有这些数据输出)。至于这些数据究竟是什么我在这里就不一一解析了,幸亏Hystrix还为咱们提供了一个可视化界面来查看这些数据。

Hystrix Dashboard

添加依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

入口文件添加 @EnableHystrixDashboard 注解

打开http://localhost:8886/hystrix,能够看到以下界面:

 

说明Dashboard已经启动成功。而后在界面中输入以前的地址: http://localhost:8886/actuator/hystrix.stream,而后点击[Monitor Stream]就能够看到统计报表页面:

相关文章
相关标签/搜索