(四)Hystrix容错保护

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

 

 

所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢?

当现在服务的提供方出现了问题之后整个的程序将出现错误的信息显示,而这个时候如果不想出现这样的错误信息,而希望替换为一个错误时的内容。

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

 对于熔断技术的实现需要考虑以下几种情况:

 · 出现错误之后可以 fallback 错误的处理信息;

 · 如果要结合 Feign 一起使用的时候还需要在 Feign(客户端)进行熔断的配置。

 

在上文的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

添加FeignServiceFallback

@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。

 

接下来停掉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]就可以看到统计报表页面:

 

posted @ 2018-11-23 15:49  sky丶尕少  阅读(169)  评论(0编辑  收藏  举报