Spring Cloud 之 Hystrix

在微服务架构中,根据业务来拆分红一个个的服务,服务与服务之间能够相互调用(RPC),在Spring Cloud能够用RestTemplate+RibbonFeign来调用。为了保证其高可用,单个服务一般会集群部署。因为网络缘由或者自身的缘由,服务并不能保证100%可用,若是单个服务出现问题,调用这个服务就会出现线程阻塞,此时如有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,致使服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统形成灾难性的严重后果,这就是服务故障的“雪崩”效应。java

为了解决这个问题,业界提出了断路器模型。git

断路器简介

Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.spring

Netflix建立了一个名为Hystrix的库,该库实现了断路器模式。在微服务架构中,一般有多个服务调用层。浏览器

较底层的服务若是出现故障,会致使连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。网络

断路打开后,可用避免连锁故障,fallback方法能够直接返回一个固定值。架构

准备工做

在以前工程的基础上, 启动eureka-server,端口为9090;启动eureka-client, 端口为8040。app

在Ribbon中使用断路器

改造rebbon-service工程的代码,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依赖:ide

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

在项目启动类上注解@EnableHystrix, 开启断路器能力:微服务

@EnableEurekaClient
@SpringBootApplication
@EnableHystrix
public class RibbonServiceApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

改造HelloController类, 在hello方法加上@HystrixCommand注解。 该注解给方法低通了熔断器的能力, 指定fallbackMethod熔断方法, 当远程服务调用时候后执行熔断方法:ui

@RestController
public class HelloController {

    private final RestTemplate restTemplate;

    @Autowired
    public HelloController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @HystrixCommand(fallbackMethod = "helloError")
    @GetMapping("/hello")
    public String hello(@RequestParam("name") String name) {
        return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class);
    }

    public String helloError(String name) {
        return String.format("Hello, %s! Access remote service fail!", name);
    }
}

启动ribbon-service项目,在浏览器访问 http://localhost:8050/hello?name=Mars:

Hello, My name is Mars, I'm from port: 8040

这时候咱们关闭eureka-service项目, 再次访问 http://localhost:8050/hello?name=Mars:

Hello, Mars! Access remote service fail!

这就说明eureka-service服务不可达时, ribbon-service调用接口会快速失败, 直接调用熔断方法, 而不是等待响应超时, 这很好的控制了容器的线程阻塞。

Feign中使用断路器

Feign已经集成了断路器, 基于feign-service项目进行改造, 只须要在@FeignClient注解中加上fallback的指定类就行:

@FeignClient(value = "hello-eureka-client", fallback = FeignServiceHystrix.class)
public interface FeignService {

    @GetMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);
}

FeignServiceHystrix须要实现FeignService接口,并注入到Ioc容器中:

@Component
public class FeignServiceHystrix implements FeignService {
    @Override
    public String hello(String name) {
        return String.format("Hello, %s! Access remote service fail!", name);
    }
}

先启动eureka-client和eureka-server项目, 而后再启动feign-service项目,在浏览器访问 http://localhost:8080/hello?name=Mars:

Hello, My name is Mars, I'm from port: 8040

这时候咱们关闭eureka-service项目, 再次访问 http://localhost:8080/hello?name=Mars:

Hello, Mars! Access remote service fail!

这证实断路器起到做用了。

关注公众号:JAVA九点半课堂,回复【资料】获取1T最新技术资料!

我的博客:http://blog.hxpgxt.cn

点击进入源码仓库

相关文章
相关标签/搜索