Spring Cloud实战系列(四) - 熔断器Hystrix

栏目: Java · 发布时间: 6年前

内容简介:在微服务框架为了保证服务的服务与服务之间具有

在微服务框架 Spring Cloud 中,可以用 RestTemplate 配合 RibbonFeign 实现 服务与服务 之间的 相互调用

为了保证服务的 高可用单个服务 通常会采用 集群部署 。由于 网络原因 ,服务并不能保证 100%可用性 ,如果 单个服务 出现问题,调用这个服务就会出现 线程阻塞 ,此时若有 大量的请求 涌入, Servlet 容器的 线程资源 会被耗尽,导致 服务瘫痪

服务与服务之间具有 依赖性 ,故障会传播,导致整个微服务系统发生 雪崩

正文

Netflix 开源了 Hystrix 组件,实现了 熔断器模式Spring Cloud 对这个组件进行了整合。在微服务架构中,一个请求需要调用 多个服务 是非常常见的,如下图所示:

Spring Cloud实战系列(四) - 熔断器Hystrix

下层的服务如果出现故障,会导致 故障级联效应 。当对特定的服务的调用的 失败次数 达到一个 阀值Hystrix520 次), 断路器 将会被打开。

Spring Cloud实战系列(四) - 熔断器Hystrix

断路器打开后 底层服务 将会隔断,可用避免 故障级联 问题, 上层服务 调用 fallback 方法直接返回一个 固定值

1. 在Ribbon上使用熔断器

pom.xml 文件中引入 hystrix起步依赖 spring-cloud-starter-hystrix

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
复制代码

在应用的启动类上使用 @EnableHystrix 开启 hystrix 的功能。

@EnableHystrix
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
复制代码

使用注解 @HystrixCommand 标记调用失败时需要熔断的方法, fallbackMethod 属性指定 降级方法方法名fallback

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public String requestForHiService(String name){
        return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
    }

    public String fallback(String name){
        return "Fallback method invoked, name is " + name;
    }
}
复制代码

2. 在Feign上使用熔断器

Feign 是自带 断路器 的,不过需要在 配置文件 中开启 hystrix 的配置。

feign:
  hystrix:
    enabled: true
复制代码

Hystrix 支持 降级回退 操作,当 发生熔断出现错误 时,调用会执行 默认代码路径

@FeignClient(value = "service-hi", fallback = HelloServiceFallback.class)
public interface HelloService {
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    String sayHi(@RequestParam(value = "name") String name);
}
复制代码

通过设置 fallback 属性为实现 降级回退 ,来启用 @FeignClient失败降级

@Service
public class HelloServiceFallback implements HelloService {

    @Override
    public String sayHi(String name) {
        return "Fallback method invoked, name is " + name;
    }
}
复制代码

如果需要获取导致 回退触发 的原因,可以指定 @FeignClient 注解内部的 fallbackFactory 属性, fallbackFactory 属性和 fallback 属性不能一起使用。

@FeignClient(value = "service-hi", fallbackFactory = HelloServiceFallbackFactory.class)
public interface HelloService {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHi(@RequestParam(value = "name") String name);
}
复制代码

然后提供一个 FallbackFactory实现类 ,实现类指定 泛型参数HelloService

@Component
public class HelloServiceFallbackFactory implements FallbackFactory<HelloService> {

    @Override
    public HelloService create(Throwable throwable) {
        return new HelloService() {
            @Override
            public String sayHi(String name) {
                return "Fallback method invoked, name is "
                         + name + ", message is " + throwable.getMessage();
            }
        };
    }
}
复制代码

3. Hystrix Dashboard监控熔断器的状态

Hystrix Dashboard 是一个 监控熔断器 状况的组件,提供了 数据监控图形界面

3.1. 在Ribbon中使用Hystrix Dashboard

在加入 spring-cloud-starter-hystrix 依赖的基础上,加入下面的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
复制代码

在应用程序 启动类 已经加上 @EnableHystrix 的基础上,加入 @EnableHystrixDashboard 注解,代码如下:

@EnableHystrix
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication {
}
复制代码

启动应用程序,访问 http://localhost:8765/hystrix.stream ,可以查看 熔断器数据指标 。访问 http://localhost:8765/hystrix ,查看 Hystrix Dashboard 的界面。在界面上填写 数据指标URL 地址,点击 Monitor Stream 进入页面,如图所示:

Spring Cloud实战系列(四) - 熔断器Hystrix

3.2. 在Feign中使用Hystrix Dashboard

在加入 spring-cloud-starter-hystrix 依赖的基础上,加入下面的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
复制代码

在启动程序上加上 @EnableHystrixDashboard 注解 开启 Hystrix Dashboard ,完整代码如下:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableHystrix
public class ServiceFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }
}
复制代码

完成上面两步配置,即可开启 FeignHystrix Dashboard 功能。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Linux Device Drivers

Linux Device Drivers

Jonathan Corbet、Alessandro Rubini、Greg Kroah-Hartman / O'Reilly Media / 2005-2-17 / USD 39.95

Device drivers literally drive everything you're interested in--disks, monitors, keyboards, modems--everything outside the computer chip and memory. And writing device drivers is one of the few areas ......一起来看看 《Linux Device Drivers》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换