SpringCloud学习笔记(四)- SpringCloud Hystrix

Hystrix 服务容错保护

SpringCloud 在远程服务调用时, 可能因为网络原因或是依赖自身服务问题出现调用故障或延迟,这些故障直接导致调用方对外服务也出现延迟,若此时调用方服务不断增加,这样就会因为等待或延迟出现人员积压,最终导致服务崩溃。
针对上述问题, SpringCloud Hystrix 实现了断路器、线程隔离等一系列服务保护功能,从而对延迟和故障提供了强大的容错能力。
Hystrix 具备了服务降级、服务熔断、线程和信号隔离、请求缓存、请求合并以及服务监控等强大功能。


依赖包

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

Camden.SR6

1、 在ribbon 基础上配置简单hystrix

1、 在启动类中添加

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class Hystrix01Application {

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

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

2、 controller 层中

@RestController
public class ShowController {
    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/show/{id}")
    @HystrixCommand(fallbackMethod = "showForErr")
    public Object show(@PathVariable String id){
       Object obj = restTemplate.getForEntity("http://EUREKA-CLIENT/show/"+id,Object.class);
       return obj;
    }

    public Object  showForErr(String id){
        return "出错了:"+id;
    }

3、 通过以下配置,使用与此方法同一线程调用

@HystrixCommand(fallbackMethod = "showForErr",
    commandProperties = {
      @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
    }
)

注意:在feign 中使用hystrix 要在yml 文件中添加 feign.hystrix.enabled=true

2、 Hystrix HystrixDashboard

ystrix的主要优点之一是它收集关于每个HystrixCommand的一套指标。Hystrix仪表板以有效的方式显示每个断路器的运行状况。他的配置只需要在主类中加上 @EnableHystrixDashboard 注解
1、例如:

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableHystrixDashboard
public class Hystrix01Application {

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

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

2、通过地址 http://localhost:8088/hystrix 进入还有Hystrix dashboard 搜索界面
这里写图片描述

3、 在搜索款内输入http://localhost:8088/hystrix.stream,跳转到监控图形界面
这里写图片描述

hystrix 参数配置

// 设置超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 6000

Turbine 集群监控

在分布式系统中,往往有很多实例需要维护和监控,上述只介绍了对单实例的监控, 这里我们利用turbine 和 hystrix dashboard 配合对集群进行监控
1、 pom 引用:

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

2、配置yml 配置文件

spring:
  application:
    name: turbine
server:
  port: 9093
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/

turbine:
  aggregator:
    clusterConfig: HYSTRIX
  appConfig: hystrix   
  clusterNameExpression: "'default'"
turbine.instanceUrlSuffix.HYSTRIX: /panlei/hystrix.stream

logging:
  level:
  root : INFO
  com.netflix.turbine.monitor: INFO

turbine.aggregator.clusterConfig 指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://…/turbine.stream?cluster={clusterConfig之一}访问
turbine.appconfig 指定需要收集信息的服务名
turbine.clusterNameExpression 指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称

3、 java 启动类

@EnableTurbine
@SpringBootApplication
public class TurbineApplication {
    public static void main(String[] args) {
        SpringApplication.run(TurbineApplication.class, args);
    }
}

猜你喜欢

转载自blog.csdn.net/panleiaiying/article/details/80063918
今日推荐