笔记:Spring Cloud Hystrix 服务容错保护

由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身问题出现调用故障或延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加,最后就会因等待出现故障的依赖方响应形成任务积压,最终导致自身服务的瘫痪。

在微服务架构中,存在着大量的服务单元,若一个单元出现故障,就很容易因依赖关系而引发故障的蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构更加不稳定,为了解决这样的问题,产生了断路器等一系列的服务保护机制。

在分布式架构中,断路器模式的作用也是类似的,当某个服务单元发生故障之后,通过断路器的故障监控,向调用方返回一个错误响应,而不是长时间的等待,这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。

针对上述问题,Spring Cloud Hystrix 实现了断路器、线程隔离等一系列服务保护功能,下面展示通过断路器实现服务的保护,需要启动我们之前创建的服务注册中心、hello-service 服务和consumer-helloservice 工程,在没有加入断路器之前,关闭 hello-service 服务,在 consumer-helloservice 进行访问 hello-service 时,会出现如下的输出:

Type Exception Report

Message java.lang.IllegalStateException: No instances available for ORG.DRSOFT.WEBSERVICE.HELLONAMESERVICE

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: java.lang.IllegalStateException: No instances available for ORG.DRSOFT.WEBSERVICE.HELLONAMESERVICE

org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489)

org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)

org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)

org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)

。。。。。。

下面我们来增加断路器对服务进行保护:

  • 在 consumer-helloservice 工程的 pom.xml 中增加 spring-cloud-starter-hystrix 的依赖:

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-hystrix</artifactId>

    </dependency>

  • 在 consumer-helloservice 工程的主类 ConsumerHelloserviceApplication 中使用 @EnableCircuitBreaker 注解开启断路器功能:

    @EnableCircuitBreaker

    @EnableDiscoveryClient

    @SpringBootApplication

    public class ConsumerHelloserviceApplication {

       

            public static void main(String[] args) {

                    SpringApplication.run(ConsumerHelloserviceApplication.class, args);

            }

       

            @Bean

            @LoadBalanced

            public RestTemplate restTemplate() {

                  return new RestTemplate();

            }

    }

  • 改造服务消费方式,在服务调用类增加 @Service 注解,在服务调用的方法中增加 @HystrixCommand 注解,并增加参数 fallbackMethod 设置服务调用失败的方法名称(需要和调用服务方法的参数一致)

    @HystrixCommand (fallbackMethod = "helloFallback")

                public String hello(){

                    return restTemplate.getForEntity("http://hello-service/hello/get",String.class).getBody();

                }

       

    public String helloFallback(){

                return"error";

    }

  • 关闭 hello-service 服务,在 consumer-helloservice 进行访问 hello-service,就会出现 "error "的返回
posted @ 2017-09-10 16:37  立3807  阅读(750)  评论(0编辑  收藏  举报