从零开始搭建SpringCloud(三)断路器Hystrix Turbine

一、Hystrix简介

   在微服务架构中,我们把每个业务都拆成了单个服务模块,然后当有业务需求时,服务间可互相调用,但是,由于网络原因或者其他一些因素,有可能出现服务不可用的情况,当某个服务出现问题时,其他服务如果继续调用这个服务,就有可能出现线程阻塞,但如果同时有大量的请求,就会造成线程资源被用完,这样就可能会导致服务瘫痪,由于服务间会相互调用,很容易造成蝴蝶效应导致整个系统宕掉。因此,就有人提出来断路器来解决这一问题。而更多简介可以自行百度。

二、断路器搭建

    本次搭建直接将每个模块的数据进行整合,就要用到Hystrix Turbine,而每个单独服务的断路器状态可以使用Hystrix Dashboard来管理。下面先搭建单个服务断路器监控。
    首先添加POM文件依赖:

    <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>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>

    接着在Application程序上加上@EnableHystrix注解开启断路器,并且需要在程序中声明断路点HystrixCommand;加上@EnableHystrixDashboard注解,开启HystrixDashboard,我们这里是指负载均衡模块中使用的

 @HystrixCommand(fallbackMethod = "hiError")
 public String hiService(String name) {
    return restTemplate.getForObject("http://SERVER-HI/hi?name="+name,String.class);
 }
 public String hiError(String name) {
    return "hi,"+name+",sorry,error!";
 }

   接下来可以使用http://localhost:8764/hystrix.stream可以看到以下显示:

data: {"type":"HystrixCommand","name":"hiService","group":"HelloService","currentTime":1526784048258,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountBadRequests":0,"rollingCountCollapsedRequests":0,"rollingCountEmit":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackEmit":0,"rollingCountFallbackFailure":0,"rollingCountFallbackMissing":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"rollingMaxConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1,"threadPool":"HelloService"}

>   访问http://localhost:8764/hystrix可以看到界面如下:

这里写图片描述

    现在只能 看到一个服务的数据,接下来我们配置一个Hystrix Turbine,需要新建一个项目,pom文件如下:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-turbine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-netflix-turbine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    接下来在Application上加上注解@EnableTurbine,开启turbine,@EnableTurbine注解包含了@EnableDiscoveryClient注解

    接下来在yml中配置

turbine:
  aggregator:
  clusterConfig: default   # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
 appConfig: service-ribbon  # 配置Eureka中的serviceId列表,表明监控哪些服务
 clusterNameExpression: new String("default")

    因为我们这里使用配置中心来统一管理的,所有可以直接在github上添加以上配置。

   接下来再访问http://localhost:8764/hystrix,输入http://localhost:8769/turbine.stream,就可以看到就可以看到聚合了多个服务的hystrix dashbord数据,这里我只配置了一个服务,可以自己加上多个服务。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013305783/article/details/80316727